PDA

View Full Version : Easy question.Why is this glDrawArrays failing?

Stuart McDonald
10-13-2008, 11:08 PM
I was drawing a HUD and I noticed the following code with glDrawArrays doesn't work. If I use 3D points it does work.

GLfloat x = 100;
GLfloat y = 100;
GLfloat w = 50;
GLfloat h = 50;
GLfloat box[2*3][2] = {
{ x, y },
{ x, y-h },
{ x+w, y },

{ x+w, y },
{ x, y-h },
{ x+w, y-h }
};
glColor4f(1,1,1,1);
#if 0
glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(2, GL_FLOAT, 0, &amp;box[0][0]);
glDrawArrays(GL_TRIANGLES, 0, 2*3);
glDisableClientState(GL_VERTEX_ARRAY);
#else
glBegin(GL_TRIANGLES);
glVertex2f(x, y);
glVertex2f(x, y-h);
glVertex2f(x+w, y);

glVertex2f(x+w, y);
glVertex2f(x, y-h);
glVertex2f(x+w, y-h);
glEnd();
#endif
With my basic understanding I thought the two bits of code were equivalent. Can someone explain the difference. Thanks!

_NK47
10-14-2008, 02:09 AM
what is <box[2*3][2]> ?

GLfloat box[2*3] =
{
{ x, y },
{ x, y-h },
{ x+w, y },

{ x+w, y },
{ x, y-h },
{ x+w, y-h }
};
glVertexPointer(2, GL_FLOAT, 0, &box[0]);

Stuart McDonald
10-14-2008, 03:03 AM
what is <box[2*3][2]> ?
It's a C++ 2D array of GLfloats, 6 rows, 2 columns; 6 rows for 3 points per triangle * 2 triangles and 2 columns for 2 points per vertex. You can see all that from the code though, so I don't understand why you are asking.

The only reason I define it that way instead of box[12] is I consider it more readable to be able to do box[0][0/1] to access the x,y of the vertex.

As I mentioned if I use 3D points (so box[2*3][3], include a Z in the initialization pass 3 to glVertexPointer) it works.

_NK47
10-14-2008, 08:45 AM
i knew what it is, just strage because you dont address your glBegin primitves like that. just try to remove it and see what happens.
if other client states are enabled make sure to disable those.
GL_NORMAL_ARRAY, GL_COLOR_ARRAY, GL_TEXCOORD_ARRAY ...
or try to specify the actuall stride instead of 0.

Stuart McDonald
10-14-2008, 01:32 PM
if other client states are enabled make sure to disable those.
Ahhh, I had a bug just a few days ago of a missing disable causing a crash....Found another case, fixed it and it all now works (and I now have debug checks to ensure I dont do it again).

Thanks!

_NK47
10-15-2008, 07:28 AM
yes, if not disabled OpenGL expects them to be bound and reads from them. crash can occur if previous draw used a buffer which is smaller then the one you specified now and this buffer is not disabled.