PDA

View Full Version : confused by vertex and index arrays!



amhood
08-15-2004, 12:28 AM
hi, i am currently trying to render a mesh model, i use the float pointer *pVertex to point to the triangle vertex array, and the GLuint pointer *pIndex to point to the triangle index array, the rendering code below works quite well:

glBegin(GL_TRIANGLES);
for (int i=0; i [LESS THAN] iTriangleCount; i++)
{
GLuint index1 = pIndex[3*i];
GLuint index2 = pIndex[3*i+1];
GLuint index3 = pIndex[3*i+2];

glVertex3f(pVertex[5*index1],
pVertex[5*index1+1],
pVertex[5*index1+2]);
glVertex3f(pVertex[5*index2],
pVertex[5*index2+1],
pVertex[5*index2+2]);
glVertex3f(pVertex[5*index3],
pVertex[5*index3+1],
pVertex[5*index3+2]);
}
glEnd();

One thing to explain is that for each vertex, 2 floating numbers are stored after the 3 coordinate values, that's why there is a "5" in the code.

However, when I use the code below, rendering results turn out to be a mess:

glVertexPointer(3, GL_FLOAT, 2*sizeof(GLfloat),
pVertex);
glEnableClientState(GL_VERTEX_ARRAY);

glDrawElements(GL_TRIANGLES,
3 * iTriangleCount,
GL_UNSIGNED_INT, pIndex);

glDisableClientState(GL_VERTEX_ARRAY);

Could anybody tell me what the problem is? Thanks so much!!

Interface
08-15-2004, 01:26 AM
i guess your problem is your '5'. By default, the vertexpointer is expecting an x,y,z array, not x,y,z,s,t or something.

There is a function to tell GL what the interleave is between to vertices, but i can't really remember. Something with pack or so?

Anyone?

Interface
08-15-2004, 01:28 AM
never mind me, it's of course the '2*sizeof(GLfloat)' you have in the code...

Bob
08-15-2004, 02:17 AM
The stride parameter should be 5*sizeof(GLfloat), since you have 5 floats from the start of one vertex to the start of the next vertex.

amhood
08-15-2004, 02:42 AM
problem settled, thanks Bob!! :-)