Hi everyone. I have a problem with VBOs and glDrawElements(). DrawElements method doesn’t draw a simple triangle. The program run but I can see only a black screen
Here is my code:
bool StartMenu::initialize()
{
glEnable(GL_DEPTH_TEST);
glDisable(GL_CULL_FACE);
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
float radius = 0.5f;
m_vertices.push_back(Vertex(-radius,-radius,-radius));
m_vertices.push_back(Vertex(radius,-radius,-radius));
m_vertices.push_back(Vertex(radius,radius,-radius));
m_indices.push_back(0);
m_indices.push_back(1);
m_indices.push_back(2);
glEnableClientState(GL_VERTEX_ARRAY);
glGenBuffers(1, &vertexBuffer);
glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer); //Bind the vertex buffer
glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * 3 * m_vertices.size(), &m_vertices[0], GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
glVertexPointer(3, GL_FLOAT, 0, 0);
glGenBuffers(1, &indexBuffer);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBuffer); //Bind the vertex buffer
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(GLuint) * m_indices.size(), &m_indices[0], GL_STATIC_DRAW);
return true;
}
void StartMenu::render()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
renderTriangle();
}
void StartMenu::renderTriangle()
{
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBuffer);
glDrawElements(GL_TRIANGLES, m_indices.size(), GL_INT, 0);
}
I use GLUT library.
I’ve used in past simple OpenGL projects VBOs and glDrawElements() methods and it works fine, so I don’t know why this time it’s not working.
I’m using for the first time Xcode 4 on Lion OSX. I coded past projects on Xcode 3 on Snow Leopard.
Thank you