Vertex attributes and buffers.

Started by
2 comments, last by menohack 11 years, 1 month ago

Hello,

I'm new here and learning Direct3D. Usually I don't ask questions since Internet is already filled with answers, but failed to find a satisfying answer to the question I'm about to ask. Was thinking and thinking and couldn't find an appropriate solution... could be there's none. However, the problem.

Lets say I have a cube. In a file I could store vertex data in a compact way - 8 different vertex positions, 6 different vertex normals, 14 different texture coordinates, and finally, face definitions via respected indices. That should be the case of Wavefront OBJ file if I'm not mistaken. When reading documentation for IASetVertexBuffers I thought I was in luck and in the program can split the data in same manner in different vertex buffers, but apparently only one index buffer apply to all of them??

When loading OBJ files, then if vertex normal is different, then vertex should be duplicated. So, in memory I would have 24 vertices instead of 8, and when adding the texture coordinate attribute, I'd have them again 24 instead of 14. Bumping aside from these numbers for a moment, I managed to get to a decision that it's mesh designer's responsibility that sharp edges should have duplicated vertices / different vertex normals. But returning to the cube, is that waste considered okay since in reality I'm dealing with more complex objects than cubes and with smooth edges... unless I want to build a brick wall with individual bricks in it, lol. If so, I wouldn't even need to have cube texture unwrapped like the cross on a Bible, but just 6 sperate panes will do.

So, is that how it's meant to be or is there some better approach?

Advertisement
I've never heard of multiple index buffers being used. So you're assumption is correct as far as I know - vertices need to be duplicated for sharp/discontinuous edges.

yeah when blasting geometry at the GPU you generally format it so you have fat vertices each with their attributes (like position, normal, texture coordinate, color, etc) all packed together one after the other. so you have one buffer of pos0, normal0, texcoord0, color0, pos1, normal1, texcoord1, color1, pos2, normal2.... you get the idea. then another buffer that are indices for your triangles so like 0, 1, 2, 0, 2, 3, ... each index here then grabs the contiguous blob of data for the vertex at that index.

you'll find a lot of software like max and maya and formats like OBJ don't specify meshes this way. they'll specify them as you describe where each face or triangle has separate indices for each vertex component. so positions 0,1,2 and normals 6,1,3 and texcoords 4,3,1.

when you load data like this you do indeed have to duplicate vertices when the same vertex is referenced but with different attributes. while this does mean potentially having many duplicate copies of attributes (like the texture coordinate 0,0 used by many vertices) it keeps things a bit simpler and avoids the GPU running all around memory gathering up the attributes from many different locations and also keeps the APIs a bit simpler as well. in the general case of a big complex mesh created by an artist there's unlikely to be much savings in indexing attributes separately.

Use CreateInputLayout to describe each vertex then IASetInputLayout. It requires something like


D3D11_INPUT_ELEMENT_DESC layoutPosColor[] =
{
{ "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 },
{ "COLOR", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 12, D3D11_INPUT_PER_VERTEX_DATA, 0 },
};

which describes each vertex. Here we have a 12 byte (4 float) position followed by a 12 byte vertex color. Then you SetVertexBuffer and either call Draw() to just draw them in the order you specified (requiring duplicate vertices) or SetIndexBuffer followed by DrawIndexed(). The second method allows you to only specify a vertex once and use it multiple times. The problem with this is that it will only have one set of texture coordinates an one normal. So if a vertex has two faces like the corner of a box each with different normals (the box is flat, not rounded at the corners) you have a problem.

To answer your question: yes, you need to specify extraneous data to do what you want. The box with 6 faces each with a different normal requires 24 vertices, each with a normal and texture coordinates.

This topic is closed to new replies.

Advertisement