Hi,
i want to display a triangle using OpenGL 3.3 and QT.
I have tried an example on the web which display a triangle using VBO + VAO + GLSL
it works.
But now, i want to modify the code to use only VBO+GLSL (no VAO)(i want to make it compatible with OpenGL ES for the future). But it displays nothing.
This is the code which works unsing VAO:
void MyGLWindow::InitVAO()
{
const float vertexData[] = {
// x y z w
0.0f, 0.5f, 0.0f, 1.0f,
0.5f, -0.366f, 0.0f, 1.0f,
-0.5f, -0.366f, 0.0f, 1.0f,
// R G B A
1.0f, 0.0f, 0.0f, 1.0f,
0.0f, 1.0f, 0.0f, 1.0f,
0.0f, 0.0f, 1.0f, 1.0f,
};
// create Vertex Buffer Object
glGenBuffers(1, &VB_Position);
glBindBuffer(GL_ARRAY_BUFFER, VB_Position);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertexData), vertexData, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
// create Vertex Array Object
glGenVertexArrays(1, &VAO);
glBindVertexArray(VAO);
glBindBuffer(GL_ARRAY_BUFFER, VB_Position);
glEnableVertexAttribArray(0);
glEnableVertexAttribArray(1);
glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 0, 0);
glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, 0, (void*)48);
glBindVertexArray(0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
}
void MyGLWindow::RenderWithVAO()
{
m_context->makeCurrent(this);
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glClear(GL_COLOR_BUFFER_BIT);
glUseProgram(m_program->programId());
glBindVertexArray(VAO);
glDrawArrays(GL_TRIANGLES, 0, 3);
glBindVertexArray(0);
glUseProgram(0);
m_context->swapBuffers(this);
}
This is my code which do not use VAO, just VBO. I don't know why it does not work.
void MyGLWindow::InitVBOSeveralArrays()
{
float vertexPositionData[] = {
// x y z w
0.0f, 0.5f, 0.0f, 1.0f,
0.5f, -0.366f, 0.0f, 1.0f,
-0.5f, -0.366f, 0.0f, 1.0f
};
float vertexColorData[] = {
// R G B A
1.0f, 0.0f, 0.0f, 1.0f,
0.0f, 1.0f, 0.0f, 1.0f,
0.0f, 0.0f, 1.0f, 1.0f
};
int indexData[] = {
0,1,2
};
glGenBuffers(1, &VB_Position); // create Vertex Buffer Object
glBindBuffer(GL_ARRAY_BUFFER, VB_Position); // bind it to the GL_ARRAY_BUFFER
glBufferData(GL_ARRAY_BUFFER, 4*3*sizeof(float), vertexPositionData, GL_STATIC_DRAW); // copy data to GPU
glBindBuffer(GL_ARRAY_BUFFER, 0); // bind the GL_ARRAY_BUFFER to 0
glGenBuffers(1, &VB_Color); // create Vertex Buffer Object
glBindBuffer(GL_ARRAY_BUFFER, VB_Color); // bind it to the GL_ARRAY_BUFFER
glBufferData(GL_ARRAY_BUFFER, 4*3*sizeof(float), vertexColorData, GL_STATIC_DRAW); // copy data to GPU
glBindBuffer(GL_ARRAY_BUFFER, 0); // bind the GL_ARRAY_BUFFER to 0
glGenBuffers( 1, &VB_Index );
glBindBuffer( GL_ELEMENT_ARRAY_BUFFER_ARB, VB_Index );
glBufferData( GL_ELEMENT_ARRAY_BUFFER_ARB, 3*sizeof(int), indexData, GL_STATIC_DRAW_ARB );
glBindBuffer(GL_ARRAY_BUFFER, 0);
}
void MyGLWindow::RenderWithVBOSeveralArrays()
{
m_context->makeCurrent(this);
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glClear(GL_COLOR_BUFFER_BIT);
glUseProgram(m_program->programId());
GLuint positionIndex = glGetAttribLocation(m_program->programId(), "position");
GLuint colorIndex = glGetAttribLocation(m_program->programId(), "color");
// Vertices
glBindBuffer(GL_ARRAY_BUFFER_ARB, VB_Position);
glVertexAttribPointer(positionIndex, 4, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(positionIndex);
// Color
glBindBuffer(GL_ARRAY_BUFFER_ARB, VB_Color);
glVertexAttribPointer(colorIndex, 4, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(colorIndex);
// Indices
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER_ARB, VB_Index);
glDrawElements(GL_TRIANGLES, 3, GL_UNSIGNED_INT, nullptr); // Draw
glDisableVertexAttribArray(positionIndex);
glDisableVertexAttribArray(colorIndex);
glUseProgram(0);
m_context->swapBuffers(this);
}
Do you have some ideas ?
I have attached a very simple source code of the project.
Thanks.