views:

20

answers:

3

In Direct3D, you can create any type of Vertex you like. You can have a simple Vertex with just positional information, or you could add colour info, texture info, etc etc. When creating your input layout, you define what parts of a Vertex you've implemented:

D3D10_INPUT_ELEMENT_DESC layout[] =
{
    { "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D10_INPUT_PER_VERTEX_DATA, 0 },
    { "COLOR", 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 0, D3D10_APPEND_ALIGNED_ELEMENT, D3D10_INPUT_PER_VERTEX_DATA, 0 },
};

My question is, should I define a vertex structure with all of the input types (position, colour, texture etc etc). Or should I create several vertex structures, each with different types of input.

The downsides to using multiple classes is that you have to create and maintain several classes, and it could be confusing knowing which type of vertex to use. What are the downsides of having 1 vertex structure?

+1  A: 

You will upload unused vertex data to the 3d accelerator, and it will eat some bandwidth. It shouldn't matter much unless you are doing some top notch video game (you'll almost certainly have a bottleneck somewhere else).

n0rd
What's the "normal" way of working with vertices then? To have multiple different types of vertices, with just the specific data you need?
Mark Ingram
I'm not very into DirectX, so I can't say how exactly it is done there. Generally you'd want to abstract the exact drawing function calls, so you'll deal with some generic interface and exact implementation depends on what exactly you are drawing. Or to use some data-driven approach when you make some generic drawing routine that submits only required parts of vertex data (in OpenGL you can submit vertex data elements separately from each other, not sure if it is possible in DirectX), depending in what you are actually drawing.
n0rd
A: 

I solved it by generating a chunk of memory on the heap and only filling it with the required elements. The code is available here:

http://code.google.com/p/woof/source/browse/trunk/Libraries/WOOF3D/Direct3D10Vertices.cpp?r=24

Mark Ingram
+1  A: 

Don't do that, use specialized structures. Uploading vertex data to the GPU consumes a lot of your bandwidth and quickly becomes a bottleneck (given that the problem is trivial to solve -- wrap a Vertexbuffer in a custom class to handle the conversion to the most efficient input layout automatically).

It's also unpractical - Skinning, for example, needs a lot of per-vertex extra data. Do you really want every single vertex in your application to carry it as well, even though it is not needed at all?

I do, however, recommend to establish some kind of convention for the order in which the vertex components appear in the data structure. That'll help you at writing your shaders, because these are ultimatively coupled to vertex input layout.

Alexander Gessler
Hi Alexander, thanks for the answer. It sounds pretty similar to what I ended up doing (see my answer below).
Mark Ingram