# glDrawElements not working

This topic is 4225 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

When i compile, everything workes great, no warnings or anything. When i run, i get a runtime error telling me that im trying to access memory from 0x000000000. This makes me think that the error is something to do with the glVertexPointer. I also get the same error if i try using glArrayElement which also makes me feel like its something going wrong with glVertexPointer.
static GLfloat verts[] = { -8.0f, 2.0f, -15.0f,
-6.0f, 0.0f, -15.0f,
-4.0f, 2.0f, -15.0f };

static GLshort indices[] = { 0, 1, 2 };

glEnableClientState( GL_VERTEX_ARRAY );
glVertexPointer( 3, GL_FLOAT, 0, verts );

// If i comment out this, it runs but is just a blank screen unless i draw something using glBegin, which does work
// If it isnt commented i get an access violation, reading 0x0000000 from memory.
// This makes me thing its glVertexPointer that is going wrong, but i see nothing wrong with it.
glDrawElements( GL_TRIANGLES, 3, GL_UNSIGNED_SHORT, indices );



##### Share on other sites
It's been a while, but I think the glDrawElements call is incorrect. The last argument passed into it is supposed to be a pointer to your vertice array. So instead you get:
glDrawElements(GL_TRIANGLES, 9, GL_UNSIGNED_SHORT, verts);

Also, if I remember correctly you should use glDrawArrays instead when using glVertexPointer. glDrawElements is a seperate function which doesn't require it.

##### Share on other sites
Im getting my example straight from OpenGL Superbible.

glDrawElements is for drawing indexed arrays.
So, if i have 4 verticies and an index that has 8 ints i could reuse the veticies setup by glVertexPointer.

##### Share on other sites
Cribs as far as i can tell your code is correct. The only thing i can think of is, that you may have another array enabled and you didn't set the pointer. e.g. Do you enable a texcoord array somewhere else? Is this the only piece of code that deals with vertex arrays in your code? Also when using glArrayElement(), does the program crash from the first index?

HellRaiZer

##### Share on other sites
Yes, it does crash on the first index, and yes this is the only place im using arrays. Im not sure(because the book doesnt tell me) but i think i need to do something like,

GLint vertices;glGenBuffersARB( 1, &vertices ); // make this called in the initilization processglBindBufferARB(GL_ARRAY_BUFFER_ARB, vertices);glBufferDataARB(GL_ARRAY_BUFFER_ARB, 3*3*sizeof(GLfloat), verts, GL_STATIC_DRAW_ARB); // verts being my array of points

or something of this sort. In the book it never tells me to generate a buffer for it, but i feel like i should. I dunno, im fairly new to OpenGL

##### Share on other sites
Quote:
 Original post by CribsGLint vertices;glGenBuffersARB( 1, &vertices ); // make this called in the initilization processglBindBufferARB(GL_ARRAY_BUFFER_ARB, vertices);glBufferDataARB(GL_ARRAY_BUFFER_ARB, 3*3*sizeof(GLfloat), verts, GL_STATIC_DRAW_ARB); // verts being my array of points

You have created a vertex buffer VBO, where is the index buffer? Are you creating one? You must create an index buffer (GL_ELEMENT_ARRAY_BUFFER_ARB) and load the index data into it just as you did with the vertex data.
GLint nIBOID;glGenBuffersARB( 1, &nIBOID);glBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, nIBOID);glBufferDataARB(GL_ELEMENT_ARRAY_BUFFER_ARB, 3*sizeof(GLshort), indices, GL_STATIC_DRAW_ARB);

Then just before drawing bind both buffers

glBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, nIBOID);glBindBufferARB(GL_ARRAY_BUFFER_ARB, vertices);// Draw range elements here...

Also remember one more thing, while using VBOs the last parameters to gl*Pointer functions are taken as offsets w.r.t the VBO data. Similarly for glDrawElements, glDrawRangeElements

[Edited by - _neutrin0_ on March 23, 2007 3:46:29 AM]

1. 1
2. 2
3. 3
4. 4
5. 5
Rutin
15

• 14
• 9
• 9
• 9
• 10
• ### Forum Statistics

• Total Topics
632912
• Total Posts
3009183
• ### Who's Online (See full list)

There are no registered users currently online

×