#### How to use instancing and world translation together?

I’m trying to optimize my application, so I’ve started to use instancing to save myself from multiple draw calls for the same object.
But what I’m not being able to understand is how do I position each instanced object on the right position, I’ve been learning from Rastertek tutorials and this is their shader code:

``````struct VertexInputType
{
float4 position : POSITION;
float2 tex : TEXCOORD0;
float3 instancePosition : TEXCOORD1;
};

struct PixelInputType
{
float4 position : SV_POSITION;
float2 tex : TEXCOORD0;
};

////////////////////////////////////////////////////////////////////////////////
// Vertex Shader
////////////////////////////////////////////////////////////////////////////////
PixelInputType TextureVertexShader(VertexInputType input)
{
PixelInputType output;

// Change the position vector to be 4 units for proper matrix calculations.
input.position.w = 1.0f;

// Update the position of the vertices based on the data for this particular instance.
input.position.x += input.instancePosition.x;
input.position.y += input.instancePosition.y;
input.position.z += input.instancePosition.z;

// Calculate the position of the vertex against the world, view, and projection matrices.
output.position = mul(input.position, worldMatrix);
output.position = mul(output.position, viewMatrix);
output.position = mul(output.position, projectionMatrix);

// Store the texture coordinates for the pixel shader.
output.tex = input.tex;

return output;
}
``````

I’ve been using `XMMatrixTranslation` from `DirectXMath.h` on my world matrix to put each object on place, but now looks like the instanced objects have to get their coordinates directly on the shader and as I’m aware of this will be done for each vertex on each instance.

This really got me confused, what’s the purpose of the world matrix if every instance of the object is already going to be placed in the world by `instancePosition`? If there’s a way to keep using my world matrix to translate each instance of the same object to the world it would be many times better for me, but I don’t know if it is possible, if anyone knows anything which could help me on this it would be great.

Source: Windows Questions C++