PDA

View Full Version : glDrawElements not working



Vincent22
12-06-2011, 11:42 AM
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

BionicBytes
12-06-2011, 12:34 PM
I don't see you rebinding the array buffer in the method RenderTriangle

Vincent22
12-06-2011, 12:42 PM
I rebind the array buffer before glVertexPointer.
Even if I put the array buffer binding in renderTriangle the program doesn't work

Zyx_2000
12-06-2011, 12:53 PM
Have you made sure the depth buffer is set up correctly? You must explicitly ask GLUT for a depth buffer.

And also: what does glGetError() return?

Vincent22
12-06-2011, 04:12 PM
I don't check for glGetError, the program run without error but there's no any triangle, only black screen.

Vincent22
12-07-2011, 03:31 AM
glGetError returns "0"/GL_NO_ERROR.
I call glGetError after glDrawElements function, is it correct?

I check this way:

if(glGetError == GL_NO_ERROR)
{
std::cout << "ok"
}

and program prints "ok" on console, so there's no Gl Error

Vincent22
12-07-2011, 04:04 AM
Solved the stupid problem.
It was a gluLookAt issue, now I put:

gluLookAt(0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, -2.0f);

in my render function

thank you for your answers