MDL7 skinpoints

Posted By: sheefo

MDL7 skinpoints - 08/10/09 18:55

I am using the MDL7 library with VC++ and I have managed to load a model and render it in DX9. The only thing left to do is handle the textures.

I have notices that some skinpoints are mapped to the same vertex so I would need to duplicate the appropriate vertices and apply the skinpoints to the uv values. I have tried all day to figure out a method of doing this and I have searched for samples with no luck. I have been testing a cube model with 8 vertices and 12 faces. When I load the model in 3DGS the D3DXMESH objects has 12 vertices and 12 faces.

Is there a DirectX mesh function you use to optimise the mesh or is it done manually in the vertex buffer? All I need is a push in the right direction.

Thanks!
Posted By: amy

Re: MDL7 skinpoints - 08/10/09 19:13

I would iterate through all triangles and generate a list of needed vertices by using a STL container like set or map or something like that.
Posted By: sheefo

Re: MDL7 skinpoints - 08/10/09 21:30

If I try and add new vertices it screws with the index buffer. I think I need a small sample of the source code or a detailed explanation.
Posted By: amy

Re: MDL7 skinpoints - 08/10/09 22:27

Code:
// pseudo code
vertices = set()
foreach triangle:
    foreach vertex of triangle:
        vertices.add(vertex.x, vertex.y, vertex.z, vertex.nx, vertex.ny, vertex.nz, vertex.u, vertex.v)



The set or map or some other hashed data structure will make sure that you will end up with exactly the amount of vertices you need for your vertex buffer. Afterwards you can loop through the triangles again to build your index buffer.
Posted By: jcl

Re: MDL7 skinpoints - 08/11/09 08:19

The duplication of 8 to 12 vertices is done in the MDL7 library, transparent to the user. There is no DirectX function for this - it has to be done with a code such as posted by amy.
Posted By: sheefo

Re: MDL7 skinpoints - 08/11/09 13:07

I think I am too stupid to figure it out. I used an std::map<int, std::set<int>> object to store the vertices and their corrisponding skinpoints and it gave the correct number of vertices needed but I cannot seem to figure out how to store the vertex pos and normals or fill the index buffer with the new vertex values.

Since the verts and tris need to be read from seperate loops I need to store everything for access in a single loop. I got that done but storing unique vertices is proving tricky (for me).

EDIT: I have managed to create a list of all needed vertices but for some reason the list is not in the same order as the 3DGS vertex buffer. I made sure no sorting is done on the list by not using std::set.
Posted By: amy

Re: MDL7 skinpoints - 08/11/09 18:28

It doesn't matter that the order isn't the same. You have to use different indices anyway since the number of vertices very likely won't be the same as in the mdl7 file anymore.

Once you have the vertex buffer you have to build your index buffer. Loop through the mdl7 triangles again and do a look-up in your map to figure out the new indices.
Posted By: sheefo

Re: MDL7 skinpoints - 08/12/09 17:50

I have re-written the vertex code to work just like the example above. The vertex buffer seems to be correct because I rendered the points in world space and they form a box (and other model shapes I have tested). The only thing left is the index buffer.
I don't understand how to calculate the new indices. In the index buffer I loop through each triangle but then I don't know how to look up the value of the new index. The code below is my setup, although it uses old indices.

Code:
for (unsigned i = 0; i < dwNumTris; i++) {
	MD7_TRIANGLE tr;
	memcpy(&tr, &pTriangles[i], sizeof(MD7_TRIANGLE));

	// triangle
	pIndexBuffer[i*3+0] = tr.v_index[0]; // old index
	pIndexBuffer[i*3+1] = tr.v_index[1]; // old index
	pIndexBuffer[i*3+2] = tr.v_index[2]; // old index
}


Posted By: amy

Re: MDL7 skinpoints - 08/12/09 19:40

Your second loop will look almost like the first pseudo code loop I posted above but instead of adding to the hashed data structure you do an index look-up. The details depend on what data structure you use but here is some pseudo code again:
Code:
// first loop
vertices = set()
foreach triangle:
    foreach vertex of triangle:
        vertices.add(vertex.x, vertex.y, vertex.z, vertex.nx, vertex.ny, vertex.nz, vertex.u, vertex.v)

// second loop
indices = array()
foreach triangle:
    foreach vertex of triangle:
        indices.add(vertices.indexof(vertex.x, vertex.y, vertex.z, vertex.nx, vertex.ny, vertex.nz, vertex.u, vertex.v))

I hope you get the idea now?

I think it also would be possible to do the whole job in one loop but then it gets a bit more complicated and I don't think it would have much performance advantage.
Posted By: sheefo

Re: MDL7 skinpoints - 08/12/09 20:13

IT WORKED! Thank You very much. My nightmare is over laugh
© 2024 lite-C Forums