glDrawElements help

Member
Posts: 94
Joined: 2008.08
Post: #1
Hi there,

I'll try and explain this as best I can:

I've previously only used glDrawArrays with triangles that I loaded from a .raw faces format. However, I've had limited success with properly mapping textures etc, so Im now trying to import a proper .obj file and draw it with glDrawElements.

I've written a converter in php to create a .h file that I can use. I've managed to get the glDrawElements to work with just the vertices, but I'm struggling to get it to properly map to the texture coords too.

So, I'm drawing my model as follows:

Code:
    glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);    

glVertexPointer(3 ,GL_FLOAT, 0, turret_vertices);
glTexCoordPointer(2, GL_FLOAT, 0, turret_textures);

glDrawElements(GL_TRIANGLES, num_turret_faces*3, GL_UNSIGNED_SHORT, &turret_faces);

which produces this: [Image: t.jpg]

Where the turret_faces array is packed as follows:
Point1Vertex, Point1Texture,
Point2Vertex, Point2Texture, etc

Like I say, when I ignore textures it draws the object properly, but obviously without any textures.

So, Im wondering what Im doing wrong? the main thing I can think of is that I'm packing the faces indices array wrongly.

I'd appreciate any help you can give!

Thanks! Smile
Quote this message in a reply
Moderator
Posts: 3,570
Joined: 2003.06
Post: #2
You need to set your stride to use glDrawElements. You probably need to do something like:

Code:
#define STRIDE    sizeof(GLfloat) * 5

glVertexPointer(3, GL_FLOAT, STRIDE, turret_vertices);
glTexCoordPointer(2, GL_FLOAT, STRIDE, turret_textures);
glDrawElements(GL_TRIANGLES, num_turret_faces*3, GL_UNSIGNED_SHORT, &turret_faces);


[edit] This was misleading. I meant the STRIDE value must be used with an interleaved array of geometry data. Should look something like this instead:

Code:
#define STRIDE    sizeof(GLfloat) * 5

glVertexPointer(3, GL_FLOAT, STRIDE, turret_geometry);
glTexCoordPointer(2, GL_FLOAT, STRIDE, &turret_geometry[3]);
glDrawElements(GL_TRIANGLES, num_turret_faces*3, GL_UNSIGNED_SHORT, &turret_faces);
Quote this message in a reply
Member
Posts: 94
Joined: 2008.08
Post: #3
Thanks for the reply, but I cant seem to get that to work for some reason. Using stride *5 seems to turn it into a blob of triangles, *3 seemed to make it return to how I already had it.

My turret_vertices and turret_textures arrays are tightly packed with just vertices/textures respectively if that helps at all? I've been using this code here to help me, which doesnt seem to use stride as far as I can tell.

I'm wondering if I'm putting the indices in the turret_faces correctly. Here is a sample of the .obj:
f 1/749/1 158/748/158 98/730/98

So if I do:
turret_faces = {1, 158, 98 };
the model looks correct but the textures are messed up. ie [Image: t3.jpg]
Here it is with textcordsarray disabled: [Image: t2.jpg]


But if I do (ignoring normals):
turret_faces = { 1, 749, 158, 748, 98, 730 }
it seems to mess up the triangles and the textures. ie [Image: t.jpg]
Quote this message in a reply
Member
Posts: 260
Joined: 2005.05
Post: #4
I say what I usually say to my students: narrow down the problem, try it on a really small dataset so you really see what is going on. It is a lot easier to see the problem in a tetrahedron, or even a single triangle, than a full model.

Accoding to your images, the model seems to work just fine, but the texture is all messed up. Is there transparency in the texture (that can be the source of the holes)? What does the texture really look like?
Quote this message in a reply
Member
Posts: 94
Joined: 2008.08
Post: #5
Here is what the texture really looks like: [Image: cylinder3_auv.png] (no transparency)
Quote this message in a reply
Member
Posts: 94
Joined: 2008.08
Post: #6
Managed to get there in the end Smile

I had to create an interleaved array for my vertices, textures and normals. Then as anotherJake suggested I had to use the stride value. Though I also had to offset the arrays as to where the data starts eg:

glTexCoordPointer(2, GL_FLOAT, STRIDE, &turret_vertices[3]);

Thanks Smile
Quote this message in a reply
Moderator
Posts: 3,570
Joined: 2003.06
Post: #7
Whoops, I'm totally sorry about that. Blush

Yes, using stride was for an interleaved array. Glad you managed to get it figured out anyway! Grin
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  6Mb of memory taken up by glDrawElements Madrayken 2 2,463 Oct 14, 2009 03:40 PM
Last Post: Madrayken