View Full Version : Newbie problem with glDrawElements/Arrays

Stuart McDonald
07-09-2006, 11:45 AM
The following code works and I get a triangle

static GLfloat vtx[] =
-10.0f, 0.0f, -50.0f,
10.0f, 0.0f, -50.0f,
0.0f, 10.0f, -50.0f

glVertex3f(vtx[0], vtx[1], vtx[2]);
glVertex3f(vtx[3], vtx[4], vtx[5]);
glVertex3f(vtx[6], vtx[7], vtx[8]);
However when I try to use glDrawElements

static GLuint idx[] = { 0, 1, 2 };

glVertexPointer(3, GL_FLOAT, 0, &vtx[0]);
glDrawElements(GL_TRIANGLES, 3, GL_UNSIGNED_INT, &idx[0]);
Or replcae the glDrawElements with glDrawArrays

glDrawArrays(GL_TRIANGLES, 0, 3);
I get nothing....Given that the first code segment works what is it I'm doing wrong? (or even just suggestions).

I've tried clockwise/aniti-clockwise indices, but since the first code segment works I didn't think that could be the problem.

Thing is I've got another part of the code all working using glDrawArrays and glDrawElements using VBOs so I'm at a loss as to why this simpler code doesn't work.



07-09-2006, 03:08 PM
When a VBO is bound, the pointer parameter of the gl*Pointer functions acts as an offset into the buffer object. If you pass a pointer to a vertex array while a VBO is bound it will use the pointer's value as the offset.

Stuart McDonald
07-09-2006, 05:19 PM
Excellent! An addition of

glBindBuffer(GL_ARRAY_BUFFER, 0);
and everything works.


07-10-2006, 07:05 AM
If you add : glBindBuffer(GL_ARRAY_BUFFER, 0);
you won't use VBOs...maybe it is what you want...

If you want to use VBO you have to create it with glBufferData and then doing:

glBindBuffer(GL_ARRAY_BUFFER, yourVBObuffer);
glVertexPointer(3, GL_FLOAT, 0, NULL);But it is not worth to do this if you want to draw only a triangle.

Stuart McDonald
07-10-2006, 11:03 AM
I am using both (or maybe). The triangle was just an example because I couldn't get it to work. I already had glBindBuffer to my VBO buffer IDs when using them. What I didn't know was to bind to ID 0 when using non-VBOs (obvious now of course).