• Advertisement
Sign in to follow this  

.obj Importer for D3D9

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, I got a D3D9 question.
Im wrote a file parser to get all the data I need out of a .obj file. Now when creating the vertex and index buffer im getting confused. I know the the index of the .obj file starts at 1, so I allready subtracted 1. My problem is I have no clue what to put in the index buffer when using vertecies AND normals. because I need to created almost every vertex more than once with each vertex on it. And I have no idea what to feed the index buffer now.

It all worked fine when I put the vertices alone into the vertex buffer and then the face indexes into the index buffer, but I cant do this anymore with Normals.

Heres my code (scroll to bottom for the D3D code: http://pastebin.com/EEES2Xvb
And this is the obj file: http://pastebin.com/ACp7T8vb

Share this post

Link to post
Share on other sites
The SDK ships with a sample .obj importer. You should probably look into that.

What they do is re-create the vertices and indices entirely from the .obj file.

For example, assume you have 2 triangles, the exporter will supply 4 vertices and 6 normals (assuming non-smoothed normals).
You will need 6 vertices (2 of them duplicating the position) in order to render the triangles.

So now you created two extra vertices in order to match the number of positions and normals. That means you need to re-calculate the indices.

The SDK does all of this by comparing vertices:

vector vertexCont // This will contain the vertices
vector indexCont // This will contain the NEW face indices

for each face,
for each index,
create a vertex with all the vertex data, (position, normals, uv-coords) // Every face index is has an index for each of those in the format: iV/ iUV / iN
compare this vertex with the other created vertices in vertexCont

if the same vertex has been created,
look up its index in vertexCont and add it to indexCont
continue on to the next interation to avoid adding a duplicate vertex

add the vertex to vertexCont and its position to indexCont

And that's pretty much it. However, comparing a vertex to all the vertices in vertexCont is going to be INSANELY slow for large models. Instead, you should use a hashtable like the SDK sample.

Also, may i ask why you're using this format? Because frankly it sucks. it's pretty much useless for anything other than static meshes.. and even with those you will have to go through all of the above for every model at run-time.. meaning unnecessary wait for the user.. So my advice is to use a different format.. Why not use .x?

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement