I am trying to draw a quadrat using a Vertex Buffer Object in OpenGL with Qt. Here is my geometry:
numVertices = 4;
vertices = new float[3*numVertices];
int i = 0;
vertices[i++] = 0.0f; vertices[i++] = 0.0f; vertices[i++] = 0.0f; // (0,0,0)
vertices[i++] = 1.0f; vertices[i++] = 0.0f; vertices[i++] = 0.0f; // (1,0,0)
vertices[i++] = 1.0f; vertices[i++] = 1.0f; vertices[i++] = 0.0f; // (1,1,0)
vertices[i++] = 0.0f; vertices[i++] = 1.0f; vertices[i++] = 0.0f; // (0,1,0)
i = 0;
// spilt quad into two triangles:
numTriangles = 2;
indices = new unsigned int[numTriangles*3];
indices[i++] = 0; indices[i++] = 1; indices[i++] = 2;
indices[i++] = 0; indices[i++] = 2; indices[i++] = 3;
Next in initializeGL method:
QGLBuffer vertexBuffer;
vertexBuffer.create();
vertexBuffer.bind();
vertexBuffer.allocate(vertices, numVertices*sizeof(float));
QGLShaderProgram* shaderProgram_ = new QGLShaderProgram;
shaderProgram_->addShaderFromSourceFile(QGLShader::Vertex,"C:/src/light.vert.glsl") ) {
shaderProgram_->addShaderFromSourceFile(QGLShader::Fragment, "C:/src/light.frag.glsl");
bool ok = shaderProgram_->link();
ok = shaderProgram_->bind();
I think all the VBO part does is to copy vertices to GPU? (Why so many lines?)
The shader part worked fine with old style glBegin(GL_QUADS);
Next in my paintGL method:
shaderProgram_->setAttributeBuffer("vertex", GL_FLOAT, 0, 3, 0);
shaderProgram_->enableAttributeArray("vertex");
glDrawElements(GL_TRIANGLES, numTriangles, GL_UNSIGNED_INT, indices);
What are the two first lines doing? Maybe telling the shader that there is a vertex buffer named "vertex" of type GL_FLOAT? However I did not specify any name when creating the VBO!? How do OpenGL know that this is "vertex"?
Anyway I am not seing anything!? Are there any steps I am missing? My shaders are simple pass trough:
# version 120
varying vec4 color;
void main() {
vec4 vertex = gl_Vertex;
// pass trough:
gl_Position = gl_ProjectionMatrix * gl_ModelViewMatrix * vertex;
color = gl_Color;
}
# version 120
varying vec4 color;
void main (void)
{
// pass-trough:
gl_FragColor = color;
}