Ali1S232
Ali1S232

Reputation: 3411

opengl glDrawArray throws exception

I'm new to openGL and I need to directly use it for debuging perpose can anyone please tell me why in this code glDrawArray should throw exception (trying to access memory at 0x00000000)

glEnableClientState(GL_VERTEX_ARRAY);
float data[8];
data[0] = 10;
data[1] = 10; 
data[2] = 10;
data[3] = 20; 
data[4] = 20;
data[5] = 20; 
data[6] = 20;
data[7] = 10; 
glVertexPointer(2,GL_FLOAT,0,data);
glDrawArrays(GL_LINE_LOOP,0,4);
glDisableClientState(GL_VERTEX_ARRAY);

Upvotes: 1

Views: 2103

Answers (3)

Christian Rau
Christian Rau

Reputation: 45968

Perhaps you have enabled another array with glEnableClientState at another point in your program and didn't disable it. So glDrawArrays tries to read from another array that causes the problem.

EDIT:: It could also be, that the code you showed us is not the real code and you call glVertexPointer at a completely different place than glDrawArrays. When giving data as vertex pointer, you only tell it to take the vertices from data, when glDrawArrays is called. The vertex data is not copied, so data has to still exist when glDrawArrays is called (at the moment it's a local variable, but with the code snippet you gave us it should work).

EDIT: I also suppose you are not using any buffer objects, as that could also be a problem, if a vertex buffer is bound when you call glVertexPointer.

Upvotes: 2

ralphtheninja
ralphtheninja

Reputation: 133138

Try:

glDrawArrays(GL_LINE_LOOP,0,4);

In your call to glVertexPointer() you are saying that each vertex has two floats. But in the call to glDrawArrays() you don't specify the number of elements in the array, you specify the number of coordinates, in this case they are four.

Upvotes: 0

tibur
tibur

Reputation: 11636

Your glDrawArrays call is equivalent to:

glBegin(GL_LINE_LOOP);
glVertex2fv(data +  0);
glVertex2fv(data +  2);
glVertex2fv(data +  4);
glVertex2fv(data +  6);
glVertex2fv(data +  8);
glVertex2fv(data + 10);
glVertex2fv(data + 12);
glVertex2fv(data + 14);
glEnd();

So you are definitely trying to access some memory outside of data.

Upvotes: 4

Related Questions