# OpenGL My first attempt with vertex pointers.

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

## Recommended Posts

Hello, I'm trying to figure out how to use vertex pointers with no luck... I have verified that all the loaded vertex data, vertices, colors, normals, uvs, and indices are correctly sent to this code:
/////////////// drawTrianglePointer /////////////
void pss::CGraphics_OpenGL::drawTrianglePointer(const SVertex* vertices, const unsigned short* indices, const int triangleCount)
{
if(! vertices || ! indices)
return;

glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glEnableClientState(GL_NORMAL_ARRAY);
glEnableClientState(GL_COLOR_ARRAY);
glEnableClientState(GL_INDEX_ARRAY);

glVertexPointer(3,GL_FLOAT,sizeof(SVertex),&vertices[0].pos);
glNormalPointer(GL_FLOAT, sizeof(SVertex),&vertices[0].normals);
glTexCoordPointer(2,GL_FLOAT,sizeof(SVertex),&vertices[0].uv);
glColorPointer(4,GL_UNSIGNED_BYTE,sizeof(SVertex),&vertices[0].color);
glIndexPointer(GL_UNSIGNED_SHORT,0,indices);

glDrawElements(GL_TRIANGLES,triangleCount*3,GL_UNSIGNED_SHORT,indices);
glFlush();
}


However nothing is drawn to the screen. When I draw my test triangle using glBegin and glEnd however, something is drawn. I have checked the size of the model and adjusted the camera view so that the data should be in view, culling is turned off. So why doesn't it draw anything? According to my time logger I've been debugging this for over three hours, and I'm getting pretty upset. Thanks~ John DiSanti

##### Share on other sites
GL_INDEX_ARRAY and glIndexPointer are for color-indexed rendering (as in for 8-bit palette displays), not indicies into the vertex array. I agree -- the names are misleading :)

First try taking those two lines out and see what happens. Odds are since both the GL_COLOR_ARRAY and GL_INDEX_ARRAY are enabled, there's probably an error being thrown by glDrawElements.

##### Share on other sites
Shouldn't it be especially if pos, normals, uv and color are actually arrays. Notice the non-usage of reference.

glVertexPointer(3,GL_FLOAT,sizeof(SVertex),vertices[0].pos);glNormalPointer(GL_FLOAT, sizeof(SVertex),vertices[0].normals);glTexCoordPointer(2,GL_FLOAT,sizeof(SVertex),vertices[0].uv);glColorPointer(4,GL_UNSIGNED_BYTE, sizeof(SVertex),vertices[0].color);

And you shouldn't be needing to use the glIndexPointer.

##### Share on other sites
i think u need the reference cause u are giving the function the beginning address to start with it .
try to use glVertexPointer w/o the rest as beginning and tell us what happens.

##### Share on other sites
glVertexPointer(3,GL_FLOAT,sizeof(SVertex),vertices[0].pos);

make it

glVertexPointer(3,GL_FLOAT,sizeof(SVertex), &vertices[0].pos);

##### Share on other sites
Thank you all for the replies!

Quote:
 GL_INDEX_ARRAY and glIndexPointer are for color-indexed rendering (as in for 8-bit palette displays), not indicies into the vertex array. I agree -- the names are misleading :)First try taking those two lines out and see what happens. Odds are since both the GL_COLOR_ARRAY and GL_INDEX_ARRAY are enabled, there's probably an error being thrown by glDrawElements.

OK, so I removed the indexed color stuff, and it still isn't drawing anything. So then I tried commenting everything but the stuff for vertices and the draw elements function. Still no success.

This is my SVertex structure for those wondering:
	typedef struct	{		struct {			float x,y,z;		}pos;		struct {			float x,y,z;		}normals;		struct {			float u,v;		}uv;		struct {			unsigned char r,g,b,a;		}color;	}SVertex;

Thanks! Rating++

##### Share on other sites
Hrm, the code looks right... I can't think of any other reason why what you have shouldn't work.

The other thing you could try is using glArrayElement between a glBegin/glEnd pair, instead of the glDrawElements call, like so:

glBegin(GL_TRIANGLES);for (int t = 0; t < triangleCount * 3; t++)    glArrayElement(indices[t]);glEnd();

Trying this will at least determine if your problem is setting up the pointers to the data properly, or the indices being passed to glDrawElements. Another possibility is an error is being thrown somewhere. Stick in a few calls to glGetError() within the code there and make sure it's returning GL_NO_ERROR.

##### Share on other sites
IMO you should redo the struct and not nest them.

Do something like this
struct Vertex{float vx;float vy;float vz;float nx;float ny;float nz;float s;float t;unsigned char r,g,b,a;};

Keep it simple due to their is no need to nest struct inside of structs.

This way you can do
vertex.vx; ect... and you will be able to interleave them easier IMO... HTH

##### Share on other sites
OK, I'll try that! Thanks!
Rating++

##### Share on other sites
OK, I've tried that, and I even tried writing a class that uses Direct3D. Neither draw the model so I'm getting convinced that something really isn't right here.

Edit: I figured I should do one last debug run tonight, and in the corner of my eye I say an error with the "DirectX debug output" that said "invalid number of vertices", so I debugged a little further and somehow I forgot to put the correct number of vertices into the mesh class I have. It draws in OpenGL now, however not in DirectX.

Thank you all for the help! My problem here is solved.

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 18
• 12
• 12
• 11
• 9
• ### Forum Statistics

• Total Topics
634753
• Total Posts
3019146
×