Skip to main content
added 758 characters in body
Source Link
user13213
user13213

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.

Then you canAn 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(..) to draw the geometry in, and your vertex buffer once withvertices will be drawn numinstances times, each of the positions in thetime 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

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.

With this approach you can move your model to different positions by calculating a new world matrix and updating the constant buffer, 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.

Then you can call ID3D11DeviceContext::DrawInstanced(..) or DrawInstancedIndexed(..) to draw the geometry in your vertex buffer once with each of the positions in the instance buffer.

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

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

Source Link
user13213
user13213

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.

With this approach you can move your model to different positions by calculating a new world matrix and updating the constant buffer, 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.

Then you can call ID3D11DeviceContext::DrawInstanced(..) or DrawInstancedIndexed(..) to draw the geometry in your vertex buffer once with each of the positions in the instance buffer.

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