我一直試圖在QT中使用OpenGL和着色器和一個簡單的頂點數組。我基本上希望在屏幕中間畫一個平原,但是當我運行該程序時沒有出現。我將我的代碼放在Qt的「紋理」示例中,一切看起來都一樣,但它不起作用!OpenGL + Qt 4.8並沒有繪製任何東西
這裏是我的glwidget.cpp的代碼:
#include "glwidget.h"
GLWidget::GLWidget(QWidget *parent):QGLWidget(parent)
{
timer.start(10);
//connect(&timer, SIGNAL(timeout()), this, SLOT(updateGL()));
Object aux;
QVector3D auxV;
auxV.setX(0.4); auxV.setY(0.4); auxV.setZ(1.0);
aux.vertices.append(auxV);
auxV.setX(0.4); auxV.setY(-0.4); auxV.setZ(1.0);
aux.vertices.append(auxV);
auxV.setX(-0.4); auxV.setY(-0.4); auxV.setZ(1.0);
aux.vertices.append(auxV);
auxV.setX(-0.4); auxV.setY(-0.4); auxV.setZ(1.0);
aux.vertices.append(auxV);
Objects.append(aux);
}
GLWidget::~GLWidget()
{
}
void GLWidget::initializeGL()
{
#define PROGRAM_VERTEX_ATTRIBUTE 0
printf("Objects Size: %d\nObj1 Size: %d\n", Objects.size(), Objects.at(0).vertices.size());
glEnable(GL_DEPTH_TEST);
glEnable(GL_CULL_FACE);
//printf("Version: %s\n", glGetString(GL_VERSION));
vShader= new QGLShader (QGLShader::Vertex, this);
vShader->compileSourceFile("../src/shaders/editorVshader.glsl");
fShader= new QGLShader (QGLShader::Fragment, this);
fShader->compileSourceFile("../src/shaders/editorFshader.glsl");
editor= new QGLShaderProgram (this);
editor->addShader(vShader);
editor->addShader(fShader);
editor->bindAttributeLocation("vertices", PROGRAM_VERTEX_ATTRIBUTE);
editor->link();
editor->bind();
}
void GLWidget::paintGL()
{
glClearColor(0.4765625, 0.54296875, 0.6171875, 1.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glTranslatef(0.0f, 0.0f, -10.0f);
glVertexPointer(3, GL_FLOAT, 0, Objects.at(0).vertices.constData());
glEnableClientState(GL_VERTEX_ARRAY);
QMatrix4x4 auxM;
auxM.ortho(-0.5, 0.5, 0.5, -0.5, 4.0, -15.0);
auxM.translate(0.0f, 0.0f, -10.0f);
editor->setUniformValue("modelmatrix", auxM);
editor->enableAttributeArray(PROGRAM_VERTEX_ATTRIBUTE);
//editor->enableAttributeArray(editor->attributeLocation("vertices"));
//editor->setAttributeArray(editor->attributeLocation("vertices"), Objects.at(0).vertices.constData(), 3);
editor->setAttributeArray (PROGRAM_VERTEX_ATTRIBUTE, Objects.at(0).vertices.constData());
glDrawArrays(GL_QUADS, 0, 4);
}
void GLWidget::resizeGL(int w, int h)
{
int side = qMin(w, h);
glViewport((w - side)/2, (h - side)/2, side, side);
//glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-0.5, 0.5, -0.5, 0.5, 4.0, -15.0);
glMatrixMode(GL_MODELVIEW);
updateGL();
}
這裏是我的vShader:
attribute highp vec4 vertices;
attribute highp mat4x4 modelmatrix;
void main (void)
{
gl_Position= modelmatrix*vertices;
}
而且我fShader:
void main(void)
{
gl_FragColor= vec4(0.0, 0.1, 1.0, 1.0);
}
你看到的錯誤那裏?
您的着色器完全令人困惑。 'highp'精度限定符在GLSL 1.10中不可用,同樣在1.10中不存在'mat4x4'。你至少需要包含'#version 120'。您是否嘗試將OpenGL ES着色器與桌面GL混合搭配? –
你是對的,mat4x4是錯的。我改變了這一點。但是我使用的基本上和上面例子一樣的着色器,他們甚至都沒有使用#version的東西(我在Qt的其他程序中也使用過),它們工作,我的不工作。 – BlastDV
我剛剛注意到的其他事情是,modelmatrix被作爲統一值傳遞,而不是作爲屬性傳遞。它已被更改,但仍無法使用。 – BlastDV