Inside the DirectX Samples(tutorial 8), they are initializing the vertices with local coords (I think, not sure the correct terminology)
// Create vertex buffer
SimpleVertex vertices[] =
{
{ XMFLOAT3( -1.0f, 1.0f, -1.0f ), XMFLOAT2( 1.0f, 0.0f ) },
{ XMFLOAT3( 1.0f, 1.0f, -1.0f ), XMFLOAT2( 0.0f, 0.0f ) },
{ XMFLOAT3( 1.0f, 1.0f, 1.0f ), XMFLOAT2( 0.0f, 1.0f ) },
{ XMFLOAT3( -1.0f, 1.0f, 1.0f ), XMFLOAT2( 1.0f, 1.0f ) },
{ XMFLOAT3( -1.0f, -1.0f, -1.0f ), XMFLOAT2( 0.0f, 0.0f ) },
{ XMFLOAT3( 1.0f, -1.0f, -1.0f ), XMFLOAT2( 1.0f, 0.0f ) },
{ XMFLOAT3( 1.0f, -1.0f, 1.0f ), XMFLOAT2( 1.0f, 1.0f ) },
{ XMFLOAT3( -1.0f, -1.0f, 1.0f ), XMFLOAT2( 0.0f, 1.0f ) },
{ XMFLOAT3( -1.0f, -1.0f, 1.0f ), XMFLOAT2( 0.0f, 1.0f ) },
{ XMFLOAT3( -1.0f, -1.0f, -1.0f ), XMFLOAT2( 1.0f, 1.0f ) },
{ XMFLOAT3( -1.0f, 1.0f, -1.0f ), XMFLOAT2( 1.0f, 0.0f ) },
{ XMFLOAT3( -1.0f, 1.0f, 1.0f ), XMFLOAT2( 0.0f, 0.0f ) },
{ XMFLOAT3( 1.0f, -1.0f, 1.0f ), XMFLOAT2( 1.0f, 1.0f ) },
{ XMFLOAT3( 1.0f, -1.0f, -1.0f ), XMFLOAT2( 0.0f, 1.0f ) },
{ XMFLOAT3( 1.0f, 1.0f, -1.0f ), XMFLOAT2( 0.0f, 0.0f ) },
{ XMFLOAT3( 1.0f, 1.0f, 1.0f ), XMFLOAT2( 1.0f, 0.0f ) },
{ XMFLOAT3( -1.0f, -1.0f, -1.0f ), XMFLOAT2( 0.0f, 1.0f ) },
{ XMFLOAT3( 1.0f, -1.0f, -1.0f ), XMFLOAT2( 1.0f, 1.0f ) },
{ XMFLOAT3( 1.0f, 1.0f, -1.0f ), XMFLOAT2( 1.0f, 0.0f ) },
{ XMFLOAT3( -1.0f, 1.0f, -1.0f ), XMFLOAT2( 0.0f, 0.0f ) },
{ XMFLOAT3( -1.0f, -1.0f, 1.0f ), XMFLOAT2( 1.0f, 1.0f ) },
{ XMFLOAT3( 1.0f, -1.0f, 1.0f ), XMFLOAT2( 0.0f, 1.0f ) },
{ XMFLOAT3( 1.0f, 1.0f, 1.0f ), XMFLOAT2( 0.0f, 0.0f ) },
{ XMFLOAT3( -1.0f, 1.0f, 1.0f ), XMFLOAT2( 1.0f, 0.0f ) },
};
But then they are translating the position via a Vertex Shader:
XMMATRIX mWorldViewProjection = g_World * g_View * g_Projection;
My question is:
1: If this was a static object(not moving/animating), would you still want to translate the World Position(Not sure the correct terminaology) or would you want to place the object in the correct 3D space/Scene when initializing the vertices, example:
{ XMFLOAT3( CorrectWorldX, CorrectWorldY, CorrectWorldZ ), XMFLOAT2( 1.0f, 0.0f ) },
A perfect example of this would be a randomly generated level/terrain.
2: What is the performance hit if you were to use the shader to position the location in the correct 3d world space rather than just doing it when initializing the vertices?
3: For Animation this is the preferred method for character animations bone transformation?
4: When building a level(placing objects) I guess you have no choice but to translate via the shader, but then you could also save the final position too.
Hope this is clear, as I am learning DirectX 11 and correct practices!
Static objects should only really be placed in the real world co-ordinates if their is no reuse. Then its just geometry, but best practice is to pass a translation matrix in to position/orientate the object from local space into world space. For instance, trees which you would use many times would each have an assigned matrix for rotation, scale and translate. Not only would you use this technique but you would use an "instance" buffer to rapidly feed the matrices to the GPU and also ensure no stalls in the GPU each time you placed an model (using constant buffers will introduce a slight stall, so using them for many models does become a hit).
The performance hit in the GPU is way less than doing it in the CPU. Alot less, matrix math in the GPU is its bread and butter. Obviously, the scale/rotate/translate matrix needs to be prepped through your code before loading into GPU. The GPU will rapidly multiply out your vertices from local to world space (in the vertex shader or domain shader if you are tessellating).
Animation has a few things involved, such as weighting and influence of each vertex on the bones. If a vertex is influenced by more than one bone point then lerping between the 2 are needed. But yes, the GPU is best placed to do this. Compute shader streaming into a instance buffer that the vertex shader uses is the more complete and fastest way to calcuate this.
If you want to calculate once, then the CPU is fine but its best to just hold onto the matrix instance for each model and let the GPU do the work around the vertex calculations. I wouldnt bother trying to save this data as the memory usage will grow quickly and you aren't saving much on performance. In fact, it could be worse as you have to load this data each time in to the GPU and you wont get the benefit of caching also.
good luck