# glInterleavedArray with Struct Arrays?

I currently have a struct array which holds all of the data required for a point of a triangle.

struct TriangleSurfaceArray{
float u, v, nx, ny, nz, x, y, n
};

After putting the data into the struct array. I cnn’t figure out how to make glInterleavedArray accept the struct as an array and parse the data correctly.

Using a direct rendering approach this code works fine:

``````		glBegin(GL_TRIANGLES);
for(int i=0; i &lt; TriSurfaceCount*3; i++) //*3
{
//glColor3f(TriangleSurfaceStructUV[i].x,TriangleSurfaceStructUV[i].y,TriangleSurfaceStructUV[i].z);
glTexCoord2f(TriangleSurfaceStructUV[i].u,TriangleSurfaceStructUV[i].v);
glNormal3f(TriangleSurfaceStructUV[i].nx,TriangleSurfaceStructUV[i].ny,TriangleSurfaceStructUV[i].nz);
glVertex3f(TriangleSurfaceStructUV[i].x,TriangleSurfaceStructUV[i].y,TriangleSurfaceStructUV[i].z);
}
glEnd();
``````

But as for the interleaved array version i have no idea how to make it work correctly =(

Any help would be greatly appreciated.

typo on the struct format btw
struct TriangleSurfaceArray{
float u, v, nx, ny, nz, x, y, z
};

n->z

Does glInterleavedArrays(GL_T2F_N3F_V3F, sizeof(TriangleSurfaceArray) - 8 * sizeof(float), pointer); not work?

haha that made it render atleast now, but in actuality i have 2 structs, one for triangles and one for quads, both setup the same way

``````		glInterleavedArrays(GL_T2F_N3F_V3F, sizeof(InterleavedTriangleArrayUV)-8 * sizeof(float), TriangleSurfaceStructUV);
glDrawArrays(GL_TRIANGLES, 0, TriSurfaceCount);
//glInterleavedArrays(GL_T2F_N3F_V3F, sizeof(InterleavedTriangleArrayUV), TriangleSurfaceStructUV);
``````

appears that only one of the arrays is rendering via that code

for a better example heres what the correct rendering looks like via the direct render method in the first post.
Direct Render
and heres one using the interleaved array code above. interleaved struct

I think you should try
glInterleavedArrays(GL_T2F_N3F_V3F, 0, TriangleSurfaceStructUV);
because the second parameter (stride) is the size of unuseful data between array elements… At least I use the function this way in my program…

I think you should scrap the glInterleavedArrays and move over to glVertexPointer/glNormalPointer/glTexCoordPointer etc.

In here:

glInterleavedArrays(GL_T2F_N3F_V3F, sizeof(TriangleSurfaceArray) - 8 * sizeof(float), pointer);

What is the “- 8 * sizeof(float)” for? Surely you just want a stride of sizeof(TriangleSurfaceArray).

Remember that the stride is the offset between the BEGINNING of two successive elements. Stride=0 is a special case, which means that the arrays are tightly packed. All other values give the distance between the beginnings of vertices.

Heh, whoops. Should be sizeof(TriangleSurfaceArray).

Originally posted by bakery2k:
Remember that the stride is the offset between the BEGINNING of two successive elements. Stride=0 is a special case, which means that the arrays are tightly packed. All other values give the distance between the beginnings of vertices.

Yes, so in this case stride=0 should work…

glInterleavedArrays(GL_T2F_N3F_V3F, 0, TriangleSurfaceStructUV);
glDrawArrays(GL_TRIANGLES, 0, TriSurfaceCount);
//glInterleavedArrays(GL_T2F_N3F_V3F, sizeof(InterleavedTriangleArrayUV), TriangleSurfaceStructUV);
``````		glInterleavedArrays(GL_T2F_N3F_V3F, 0, TriangleSurfaceStructUV);