Skip to main content
2 of 2
added 758 characters in body
user avatar
user avatar

You could go about this in different ways.

Usually, you have a model's geometry centered around the origin (0,0,0), and to place it somewhere in the world you would use a world matrix, which offsets the entire model to a desired new position.

This world matrix can reside in a constant buffer accessible to the vertex shader, where it would be combined with the view and projection matrices, and then used to transform your vertex data.

Constant buffers allow you to provide variables to your shader code, however they remain constant while a shader runs. An example in shader code:

cbuffer perObject 
{
    float4x4 World; //world matrix carrying position information
    float4 objColor;
};

cbuffer perScene
{
    float4x4 ViewProjection; //view & projection matrix
};

Here is a tutorial that covers constant buffers, among other things: http://www.rastertek.com/dx11tut04.html

And an article on MSDN: http://msdn.microsoft.com/en-us/library/windows/desktop/ff476898(v=vs.85).aspx#Shader_Constant_Buffer

With this approach you can move your model to different positions by calculating a new world matrix and updating the constant buffer containing the world matrix, no need to update the whole vertex buffer.

If you are drawing many instances though, you could use an instance buffer in which you store all the different positions your object should be drawn at.

An instance buffer is not much different from a vertex buffer, the elements from the instance buffer will be received as parameters in your vertex shader.

You would call ID3D11DeviceContext::DrawInstanced(numVertices, numInstances, vertOffset, instOffset) or DrawInstancedIndexed(..), and your vertices will be drawn numinstances times, each time supplying a different element from your instance buffer to the vertex shader.

Here is a tutorial that covers instancing in D3D11: http://www.rastertek.com/dx11tut37.html

user13213