Skip to main content
GameDev.net gamedev.net
🔒 Locked

Help with loading custom model format

Started by ms291052 Jan 21, 2004 at 5:25 PM 3 replies 300+ views
Original Post
ms291052
ms291052
I''ve gone all-out and began using a custom (ASCII) file format for my 3D models in my app, and I''ve built a good class for it, but now I''m at the step where I must convert it into a LPD3DXMesh for use. I know how to do it (CreateMeshFVF with VB/IB and all) but my question is more how to convert my data into data that D3D will like. As of now I''m storing everything in vectors like this:
typedef struct
{
DWORD  vertcount; //Number of vertices in this face

DWORD* verts;
DWORD* texcoords;
DWORD  texture; //index of texture to use for this face

} FACE;

vector<string*>      textures; //string containing filenames to textures

vector<D3DXVECTOR3*> vertices;
vector<D3DXVECTOR3*> normals;
vector<D3DXVECTOR2*> texcoords;
vector<FACE*>        faces; //see struct above

I have all of those vectors initialized perfectly but I''m having difficulty getting the data into the VB/IB. I imagine I just have to loop through each face (each face may have any number of vertices [integer >3] so that I could have pentagonal faces and the like) and add 3*(vertcount-2) vertices to the buffer (triangle lists). I have all of the data for the vertex (pos, normal, texcoords) but I haven''t a clue about how to use that. How can I determine the triangles that I must add from the vertices of each face, and how to I organize it so that the subsets and all will be proper to use the textures with?
circlesoft
circlesoft
All that LPD3DXMesh actually is is a wrapper around a vertex buffer and an index buffer, so you probably don''t need to convert your mesh into one.

On the other hand, you should still fill a vertex and index buffer of your data, simply by locking, then copying the appropriate data over. There are some samples in the SDK that illustrate this well. Remember, your fvf is important, too.
ProtoPornoPants
ProtoPornoPants
Yeah. If you''ve gone to the trouble of defining an ascii format for your verts and their connectivity, then you should be able to fill up a vertex buffer and index buffer yourself and render directly.

So you make sure you have a position, normal and texcoord for every vertex in your face. You''re going to need to store your verts in structs rather than in seperate lists like you are. So a vert would be something like:


struct MyVertex
{
D3DXVECTOR3 Position;
D3DXVECTOR3 Normal;
D3DXVECTOR2 UV;
}


You''d need to define an FVF to describe this format too.

Ideally, you would sort your model by texture so that you can group together faces with the same texture to render at the same time. You fill your vertex buffer with all your vertices in order.

Next you need to decide how to triangulate your faces. Probably easiest to start with (assuming all your faces are convex) would be to render each face as a triangle fan. So you create your indices for your face in the correct order for a triangle fan, and put them into an index buffer.

Finally, set the texture for the current group of faces, then call DrawIndexedPrimitive for each face. You may as well put all the faces for the current texture into one big index buffer and use offsets to render the correct face.

So the data you might need for the model once you have filled your vertex and index buffers:


Model
{
int NumMeshes;
Mesh* Meshes;
}

Mesh
{
Texture* Texture;
int NumFaces;
Face* Faces;
}

Face
{
int NumIndices;
int StartIndex;
}


I know it''s a bit hefty, but I hope it helps. Using the D3DXMesh won''t make this much easier than I have described above I don''t think.

p.

ms291052
ms291052
I suppose that will work, but isn''t that many DIP calls going to be bad? I mean a level could have several hundred faces atleast...
ProtoPornoPants
ProtoPornoPants
Yes. Ideally you do want to reduce the number of DIP calls. To do that with what you have though, you''d need to merge all the faces you have that use the same texture and draw them using one large triangle strip with degenerates.

That can be a tricky problem to solve though!

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.