   Creating a mesh
  (C++, DX 9)HelloI have a problem with understanding, how to create mesh from data, i have. I am importing FBX file, but i need to know, how to create a mesh right in DX.For simplicity, i used a simple cube.I have 8 verticles + 36 indices. that's okI have 36 normals, but how can i save them to the mesh ? because, i have only 8 verticles data :) + i have 24x UV and indices. do i have to make another buffer for normals ? It looks like, i have to create a buffer with all verticles without indices .. but that's not good :)what about materials ? i have material indices, what mat every polygon has. How to optimalize it for best performace, how OptimizeinPlace command do in DX.Could you please show me a right way, how can i manage it ? :) how to create a custom mesh right with place for tangents and binormals? (i can't see that option in FVF format) thank you very much   
Quote:Original post by wh1sp3rikI have 8 verticles + 36 indices. that's okI have 36 normals, but how can i save them to the mesh ? because, i have only 8 verticles data :) + i have 24x UV and indices. do i have to make another buffer for normals ? It looks like, i have to create a buffer with all verticles without indices .. but that's not good :)In most modelling packages, a vertex is just a position - in DirectX, a vertex is position, normal, UV, and any other information there might be. So you need to duplicate the vertices, so for a cube you'll usually have 24 - 4 per face - each with a unique combination of position, UV and normal.Quote:Original post by wh1sp3rikwhat about materials ? i have material indices, what mat every polygon has. How to optimalize it for best performace, how OptimizeinPlace command do in DX.You should draw all the triangles that use a particular material in one DrawIndexedPrimitive() call. ID3DXMesh::OptimizeInplace() presumably re-orders the vertex buffer for the most efficient use of the GPU's vertex cache, by making sure that vertices used are as close as possible in the buffer.Quote:Original post by wh1sp3rikCould you please show me a right way, how can i manage it ? :) how to create a custom mesh right with place for tangents and binormals? (i can't see that option in FVF format)For anything but the most basic vertex format you should be using vertex declarations. FVF codes don't support tangents and binormals.;  Thank you for answer :)so, i will duplicate vertex positions and UVs.What about indices ?if i use PIX for debugging, i see, DrawSubset is using DrawIndexedPrimitive, how they can do this then ?i understand, i can use one normal per object vertex, because i can average all normals on cube vertex, so i can use indices for normals too, but what about UV's ? they are different for every vertex  ... great :DSo, is it good way to use own format or use DX mesh ?   ;Quote:Original post by wh1sp3rikThank you for answer :)so, i will duplicate vertex positions and UVs.What about indices ?I'm unfamiliar with the FBX file format, but if you're changing the vertices you'll have to change the indices to match.Quote:Original post by wh1sp3rikif i use PIX for debugging, i see, DrawSubset is using DrawIndexedPrimitive, how they can do this then ?I don't follow - ID3DXMesh manages a vertex and index buffer internally, so it uses DrawIndexedPrimitive to do its rendering.Quote:Original post by wh1sp3riki understand, i can use one normal per object vertex, because i can average all normals on cube vertex, so i can use indices for normals too, but what about UV's ? they are different for every vertex  ... great :DIf you have one position (E.g. one corner of the cube), but different possible UVs (E.g. one for each face connecting to that corner), then you need to duplicate the vertex so that the UV data is unique.Quote:Original post by wh1sp3rikSo, is it good way to use own format or use DX mesh ?I'd recommend goign with ID3DXMesh unless you're doing this for the learning experience. In any case, you can always go with ID3DXMesh and then change to your own class later on.   ;  ok, why I am asking .. i think, it's a very fast to use indices and when i use custom mesh, not ID3DXMesh, it will be terribly slow.How to optimalize Mesh well ?should i sort verticles by material ? i will render material one and all vertices assigned to this material , second material and all verticles assigned to second material etc ?Are there other optimalization techniques ?   ;Quote:Original post by wh1sp3rikok, why I am asking .. i think, it's a very fast to use indices and when i use custom mesh, not ID3DXMesh, it will be terribly slow.You should be using an index buffer if your mesh benefits from it - I.e. you have vertices that are shared between faces (Which will be the case with most meshes). By "Vertex", I mean the D3D version of a vertex - a unique instance of a struct containing position, normal, UV, etc.Quote:Original post by wh1sp3rikHow to optimalize Mesh well ?should i sort verticles by material ? i will render material one and all vertices assigned to this material , second material and all verticles assigned to second material etc ?Are there other optimalization techniques ?First, you want to make sure that you make as few DrawIndexedPrimitive() calls as possible. If your mesh has 4 different materials, there's absolutely no reason you should make more than 4 DrawIndexedPrimitive() calls to render that mesh.Second, you can optimise the vertex or index buffer so that vertices are re-used as quickly as possible. That'll mean they're more likely to fit in the GPU post-transform cache, which means the GPU will need to do less work in transforming them.By that, I mean if you have this index data:(0, 1, 2) (4, 5, 6) (1, 2, 3) (5, 6, 7)it can be re-ordered into the following:(0, 1, 2) (1, 2, 3) (4, 5, 6) (5, 6, 7)Becase then vertices 1 & 2 and 5 & 6 are re-used much sooner, so they're more likely to still be in the GPU cache.   ;  i understand."You should be using an index buffer if your mesh benefits from it - I.e. you have vertices that are shared between faces (Which will be the case with most meshes). "for example plain .. there are lots of shared vertices and every vertex has a normal, so, should i average them ? because i want to use indices :)So, looks like, Cube is not a good example, because it haven't usable shared vertices. Plain is good example, because they have shared verticles, which share UV too.so, I am starting understand, how it works :) heheAnother thing .. what about tangents and binormals ? if i average normals, looks it looks like, i have to recalculate them, am i right ?   ;  Well, a cube has 24 vertices, but your model has 36 normals. Each face of a cube has 4 vertices and 6 indices (Because it's made from 2 triangles), which implies that your file has a normal per index (I don't see how that would work, but never mind...), so to have one normal per vertex instead, you'd need to average the normals.I don't know anything about binormals or tangents I'm afraid, but it stands to reason that if you modify the normal, the binormal and tangent will also need modified.   ;  oki, last question i hope :)how can i create a mesh with binormals and tangents, because no FVF exists ?;Quote:Original post by wh1sp3rikoki, last question i hope :)how can i create a mesh with binormals and tangents, because no FVF exists ?You need to use Vertex Declarations instead of FVF codes.   