Displaying models with vertex arrays

Started by
3 comments, last by havok4567 15 years, 5 months ago
Hi, i've just started trying to use vertex arrays as opposed to immediate mode and i'm confused about what the best way is now to display a 3D model. If I am loading a .obj file I get an array of vertices and normals and a list of triangles saying how the vertices and normals match up. With immediate mode I would have done something like iterate through the list of triangles and call glVertex for the vertices listed and glNormal for the normals. This way they would have been in the correct order and set in the .obj file. But now I just pass OpenGL the list of vertices and normals but it is missing the extra triangle information to work out how they match up so do I have to cleverly order the two arrays based on the triangle information? Or is there a bit more to it. I'm a bit confused and would appreciate some help, cheers.
Advertisement
Quote:Original post by havok4567
But now I just pass OpenGL the list of vertices and normals but it is missing the extra triangle information to work out how they match up so do I have to cleverly order the two arrays based on the triangle information? Or is there a bit more to it. I'm a bit confused and would appreciate some help, cheers.
Yes, you need to rebuild the vertex, normal and texture coordinate arrays to match the triangle information in the .obj faces list. This process is often called 'vertex welding', which ought to bring up a few hits on google.

Tristam MacDonald. Ex-BigTech Software Engineer. Future farmer. [https://trist.am]

Cheers man, yeah i've been searching for a while but without the right jargon it's not always easy :) Thanks a lot, I love how fast the responses on these forums always are!

EDIT: Okay i've got a really simple .obj loader going and its a great feeling :) I ended up loading in a list of vertices, normals and faces then iterating over the list of faces and adding each vertice into a vertex array and the normals into a normal array. This means that the normals and vertices are in the correct order and match up so the model displays and looks good but i'm not sure if it could be improved by getting rid of repeated vertices?

EDIT 2: I have been reading up more and found a function "glDrawElements" which I hadn't seen before. I'm really new to this whole vertex array things and I had found a site which taught me to use "glDrawArrays". So from what I gather with "glDrawArrays" you use it just how I have, have a long list of vertices that may repeat - easy to code but wastes on memory and probably transfers to the gpu. But i'll look into "glDrawElements" as it seems like a more elegant solution.

[Edited by - havok4567 on November 30, 2008 11:18:40 PM]
Quote:Original post by havok4567
This means that the normals and vertices are in the correct order and match up so the model displays and looks good but i'm not sure if it could be improved by getting rid of repeated vertices?

But i'll look into "glDrawElements" as it seems like a more elegant solution.
You can use glDrawElements() to solve the duplicate vertex problem - just build indices from your new vertex array, and when you hit a repeated vertex, remove the vertex and index the first copy.

Tristam MacDonald. Ex-BigTech Software Engineer. Future farmer. [https://trist.am]

Cheers, that makes a lot of sense.

This topic is closed to new replies.

Advertisement