[D3D9] Quick Question: Do I really need to duplicate vertices to render them?

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

Recommended Posts

Hey there gamedev.net, it's quick question time!

Imagine a model of a simple cube. In 3DS MAX, that cube, if modeled by somebody who knows what they're doing, won't have duplicated vertices. It would have 8 vertices in other words. When I load such a model into my game, I currently need to go the painful route and unwrap that structure at load-time, creating multiple vertices in the same spot, just so that every triangle has its own set of vertices - from 8 to 8 * 3 = 24 in the cube example.

Is that really necessary? No elegant way around it in D3D9? Just checking! Any insight is appreciated!

Share on other sites
This is where index buffers come in ;)

model file:

vertices (positions):
-1, -1, 0
-1, 1, 0
1, 1, 0
1, -1, 0

faces:
0, 1, 2
0, 2, 3

notice how vertex 0 and 2 will be shared. You only need to load the 4 vertices and the face index list. then when you are drawing your geometry, use the method "[color=#2A2A2A] IDirect3DDevice9::DrawIndexedPrimitive".
[color=#2A2A2A]

[color=#2A2A2A]

Share on other sites
Oh yeah, right! I don't know what I was thinking... I am using index buffers right now but I was still making long lists of duplicated vertices with them. Thanks.

Share on other sites
Sorry for bumping this, I did make this thread a little prematurely and unprepared, sorry for that.

The problem actually lies with uv coordinates. A cube can be built from only 8 vertices but not if it's texture-mapped, right? Then you're back to needing 24 as the various faces that share the vertices only share their position, not their uv coordinates! The original question remains: does this mean I need to duplicate vertices? Or is there some way around this that I'm not seeing?

Share on other sites
No, there is no easy way around this. A vertex consists of not only position, it can also store texture-coords, normals, binormals, tangents, et.. Two vertices with different uv-coords but same position are NOT the same vertices, so having both of them only means storing doublicated position, not vertices. Afaik, there is no way around it.

Share on other sites
Makes sense, I was hoping there'd be something I overlooked... That means I have to rewrite my custom model exporter for 3DS Max AGAIN... This time I'll have it do all the work and write the vertex and index buffer ready to be used by D3D9 into the model file. Before, the exporter would write the raw data I get from 3DS Max (which comes in a weird way as far as uv coordinates go) into the file and build the vertex and index buffers from that raw data in the game itself at loadtime. Doesn't sound like a good idea now... Thanks for the feedback!

Share on other sites
Before you start rewriting the code, keep in mind that not only uv coords matter here, another important vertex property can be for example the normal vector. Even on a cube without textures, you won't be fine with 8 vertices, you'll need 24, because every face has a different normal.
(Well, as far as you NEED normals.)

Share on other sites
You're absolutely right of course. Currently my 3DS Max exporter just goes through each unique vertex and then calculates a normal for it taking the neighboring faces into consideration. I'll have to think about how to get the proper normal information out of 3DS Max as well.

Share on other sites

You're absolutely right of course. Currently my 3DS Max exporter just goes through each unique vertex and then calculates a normal for it taking the neighboring faces into consideration. I'll have to think about how to get the proper normal information out of 3DS Max as well.

I have almost 0 experience with maxscript and the inner mechanisms of 3ds max (I'm just a 3ds max user), but you'll probably have to deal with smoothing groups?
Smoothing groups are what affect normals of a mesh in 3ds max, but I have really no idea how is it implemented "under the hood".
Maybe it will be something similar as what you have now, just with the modification that you'll take into consideration only neighbouring faces which share the same smoothing group?

Share on other sites
I'm not sure if I need to say this, but I wanted to mention something about normals when your "optimizing" your mesh (or removing duplicate vertices). Its usually best to create your own normals if you want smooth lighting across an object. If you were to give every triangle in an object its own normal, that would result in choppy lighting, where you could actually see each triangle. You should impliment normal averaging, so that each vertex's normal is averaged with the face normals of each triangle that uses that vertex. This way, you wouldn't have to create duplicate vertices just because the normal of each face is different. (I would personally avoid exporting normals, since its easy enough to calculate them in code, and would just add extra space to the file).

And about texture coords. Most of the time i've found that it is possible to create a cube with only 8 vertices using texture coordinates. If you are able to map out the texture onto the cube well enough, then for example, the top left front corner vertex of the cube might have a tc of (0, 0), so that that corner of the 3 faces sharing that vertex all use the tc (0, 0) at that vertex. (i hope that makes any sense). And also, when loading models, you will usually have a subset for each section of the model that uses a certain texture. This way, usually none of the vertices in that subset will be duplicated because of texture coordinates. but you have to remember you can't share vertices between subsets, or at least you shouldn't because that can cause problems.

One last thing. I wrote a script for 3ds max to export my own custom format a while ago, but that honestly took me a very long time, since maxscript seems to be not very well documented. I have read a lot about exporting models and loading them into games since then, and i've found the best solution, and easiest is usually to export to a format such as collab or fxb (i think), and just write your own converter (especially since these two formats will do MUCH more than you would ever need in a video game). Just a suggestion

1. 1
2. 2
Rutin
19
3. 3
4. 4
5. 5

• 13
• 26
• 10
• 11
• 9
• Forum Statistics

• Total Topics
633736
• Total Posts
3013598
×