Hi
I have a problem to render with a VAO. I want to set up a VAO and render with calls of glBindVertexArray() and glDrawArrays(), but I have to call glEnableVertexAttribArray() and glVertexAttribPointer() everytime I bind the VAO.
I wanted to set up a VAO in a class named grid and bind a VBO in a function, where I calculate vertex-data, like seen below.
glGenVertexArrays(1, &VAO);
glGenBuffers(1, &VBO);
glBindVertexArray(VAO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, FLOATS_PER_VERTEX*numVertices*sizeof(GLfloat), vertices, GL_STATIC_DRAW);
glEnableVertexAttribArray(gridShader::loc_pos);
glEnableVertexAttribArray(gridShader::loc_col);
glVertexAttribPointer(gridShader::loc_pos, 3, GL_FLOAT, GL_FALSE, FLOATS_PER_VERTEX*sizeof(GLfloat), (GLvoid*)0);
glVertexAttribPointer(gridShader::loc_col, 3, GL_FLOAT, GL_FALSE, FLOATS_PER_VERTEX*sizeof(GLfloat), (GLvoid*)(3*sizeof(GLfloat)));
glBindVertexArray(0);
In paintGL() I just wan’t to call glBindVertexArray() but nothing is rendered.
viewpointMatrix.setColumn(3, -viewpoint+QVector3D(0,0,zoom));
glBindVertexArray(grid->VAO);
gridShader::shaderProg.bind();
gridShader::shaderProg.setUniformValue(gridShader::loc_projMatrix, projectionMatrix*viewpointMatrix);
glLineWidth(1);
glDrawArrays(GL_LINES, 0, grid->numVertices);
glBindVertexArray(0);
If all functions are called again in paintGL() everything works fine, but I think thats not the purpose of a VAO.
viewpointMatrix.setColumn(3, -viewpoint+QVector3D(0,0,zoom));
glBindVertexArray(grid->VAO);
gridShader::shaderProg.bind();
gridShader::shaderProg.setUniformValue(gridShader::loc_projMatrix, projectionMatrix*viewpointMatrix);
glBindBuffer(GL_ARRAY_BUFFER, grid->VBO);
glEnableVertexAttribArray(gridShader::loc_pos);
glEnableVertexAttribArray(gridShader::loc_col);
glVertexAttribPointer(gridShader::loc_pos, 3, GL_FLOAT, GL_FALSE, FLOATS_PER_VERTEX*sizeof(GLfloat), (GLvoid*)0);
glVertexAttribPointer(gridShader::loc_col, 3, GL_FLOAT, GL_FALSE, FLOATS_PER_VERTEX*sizeof(GLfloat), (GLvoid*)(3*sizeof(GLfloat)));
glLineWidth(1);
glDrawArrays(GL_LINES, 0, grid->numVertices);
glBindVertexArray(0);
Hope that somebody can tell me what I’m doing wrong.
Thanks