Archived

This topic is now archived and is closed to further replies.

fdisano

Vertex Buffer Headscratcher

Recommended Posts

Ok, this is a wierd problem so bear with me. Our engine supports meshes that can have multiple textures AND materials. Upon loading a mesh, our engine groups each face in the mesh by it''s material/texture forming what we call a mesh group. Now when it comes time to render a mesh, we transform it from local to world and then we do a for loop for each group in the mesh, set the current texture and material for the group and then render it using DrawIndexedPrimitive. Like this: (pseudo code...not an actual snippet) for (i=0; iNumGroups; i++) { SetCurrentTexture(Mesh->MeshGroup(i).Texture); SetCurrentMaterial(Mesh->MeshGroup(i).Material); DrawIndexedPrimitive(D3DFVF_VERTEX,Mesh..........) } Now, the problem with this is that DrawIndexedPrimitive retransformes from world->view space the entire mesh FOR EACH GROUP when really it should be transformed once. Logical soloution? Vertex buffers. BUT, now the problem lies with the fact that if we have 1 or more materials per mesh, we have to relight the mesh for each material. This poses a problem with vertex buffers because you transform them once and then render each group seperatly. As a side note, I thought that once the vertices were transformed and lit in the vertex buffer, the lighting information would be retained when rendering the different materials. IT seems that is not the case. Or is it and I am doing something wrong? ie. I transform and light a mesh that has 1 or more materials and no textures. The scene has 3 directional lights each pointing towards the mesh. The first material gets rendered but not the second or third. So....I''m stuck. Any suggestions from you guys would be very cool. Later ~----------------------------~ Fred Di Sano System Programmer Artech Studios Ottawa.CA ~----------------------------~

Share this post


Link to post
Share on other sites
After the vertices have been lit, the light information is stored in the diffuse and specular color attributes of the vertex. So a change of material after lighting won''t affect the outcome of the rendering.

My suggestion to you is to store the material properties in the vertices themselves. You can use the diffuse and specular color attributes for that end.

You just need to tell Direct3D where it can find the material properties and that is done with:

SetRenderState(D3DRENDERSTATE_DIFFUSEMATERIALSOURCE, ?);
SetRenderState(D3DRENDERSTATE_SPECULARMATERIALSOURCE, ?);
SetRenderState(D3DRENDERSTATE_AMBIENTMATERIALSOURCE, ?);
SetRenderState(D3DRENDERSTATE_EMISSIVEMATERIALSOURCE, ?);

Of course you also need to change your vertex format to include the color attributes, but that shouldn''t be to much of a problem.




- WitchLord

Share this post


Link to post
Share on other sites
fdisano, I, too, am dividing my models into "groups" that share the same texture & material. It sounds like you are using one sorta "global" vertex list for all your mesh groups. Have you thought about giving each group its own vertex list? This would allow you to use DrawIndexedPrimitive (and hardware TnL).

This method results in a lot of duplicate vertices, along the edges of two groups. It turns out I actually need these duplicate vertices, though. Within a group, I smooth-shade all the faces, but, along the edges of two different textures/materials, I (logically) want to have "creases". These duplicate edge vertices need to have different normals in order to create the "crease".

Share this post


Link to post
Share on other sites