Sign in to follow this  
YellowMaple

Normals and position data in a vertex buffer

Recommended Posts

YellowMaple    174
I'm exporting a simple cube mesh from Blender which exports the vertex coordinates and face normals, i.e. the number of positions vs normals are different. The only way I know how to do things in D3D is to create a vertex declaration and fill your vertex buffer accordingly. For example, in my code I have
	// My wrapper for vertex declarations, eVertexType_Float3 maps to
	// D3DDECLTYPE_FLOAT3, etc.
	D3D9VertexDeclaration vertexDeclaration;
	vertexDeclaration.AddVertexElement(0, 0, eVertexType_Float3, eVertexUsage_Position, 0);
	vertexDeclaration.AddVertexElement(0, 12, eVertexType_Float3, eVertexUsage_Normal, 0);
	vertexDeclaration.AddVertexElement(0, 24, eVertexType_Colour, eVertexUsage_Colour, 0);
	vertexDeclaration.Initialize();

which, afaik, means that you have to fill up your vertex buffer with the (ordered) data: "x, y, z, nx, ny, nz, c" with the first 6 entries being float values for the position and normal and c being a dword for the colour. So this required per-vertex normals and colour. How would you handle the case where you might want per-face normals for flat shaded geometry and thus don't have matching normals for each vertex?

Share this post


Link to post
Share on other sites
GregMichael    135
I render a cube as 6 triangle strips - each strip containing 2 triangles. All the vertices for one face (4 verts) share the same normals.

For example, front face of cube looking from negative Z axis towards positive Z axis (may be different depending on your winding order)

Vertex 0 (top right)
xyz 1.0f, 1.0f, -1.0f
norm 0.0f, 0.0f, -1.0f

Vertex 1 (top left)
xyz -1.0f, 1.0f, -1.0f
norm 0.0f, 0.0f, -1.0f

Vertex 2 (bottom right)
xyz 1.0f, -1.0f, -1.0f
norm 0.0f, 0.0f, -1.0f

Vertex 3 (bottom left)
xyz -1.0f, -1.0f, -1.0f
norm 0.0f, 0.0f, -1.0f

and so on for the rest of the cube...

HTH

Share this post


Link to post
Share on other sites
YellowMaple    174
Thanks for the reply! Does that mean for a cube you'd have 6 DIP calls? I suppose that is one solution. I was hoping there might be a way to do this with a single DrawIndexedPrimitive call as from what I know it's best to minimize them for performance.

Share this post


Link to post
Share on other sites
JohnnyCode    1046
normal is a per vertex information and since you have 12 triangles on the cube, you will need a vertex for every normal. You have 8*3 normals on the cube thus you need 24 verticies. Shared verticies, the problematic is called. Number of verticies is definitely not the number of positions on mesh, you need a vertex for every unique textue coordinate, normal, vxcolor ...

in your exporter you should fetch vertex for every index in indicies and if the vertex is not uniqe make the index reference a cashed vertex, and if the vertex is unique, grab it.

Share this post


Link to post
Share on other sites
GregMichael    135
You can render a cube with a single triangle strip - but you'll need to look up degenerate vertices in order for that to work.

For a single cube it's probably not worth it...but it depends on how many cubes you are rendering :)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this