ok, im experimenting a little with HLSL shaders, i have the following code for a vertex shader, it is used to render a few instances of a 3d model. Let's say i want to rotate these instances on their Y axis, and i want to do it INSIDE the shader, is it possible? I cannot find any documentation about this.
Here it is my code:
UPDATE: i modified the code so that now the shader receives a rotation parameter instanceRotation (in radians) and use the instanceRotation.y to create a matrix to rotate the object instance around the y axis. It looks like it works, i leave it here in case anybody needs it.
/////////////
// GLOBALS //
/////////////
cbuffer MatrixBuffer
{
matrix worldMatrix;
matrix viewMatrix;
matrix projectionMatrix;
};
//////////////
// TYPEDEFS //
//////////////
struct VertexInputType
{
float4 position : POSITION;
float2 tex : TEXCOORD0;
float3 normal : NORMAL;
float3 instancePosition : TEXCOORD1;
float3 instanceRotation : TEXCOORD2;
};
struct PixelInputType
{
float4 position : SV_POSITION;
float2 tex : TEXCOORD0;
float3 normal : NORMAL;
};
////////////////////////////////////////////////////////////////////////////////
// Vertex Shader
////////////////////////////////////////////////////////////////////////////////
PixelInputType LightVertexShader(VertexInputType input)
{
PixelInputType output;
// Change the position vector to be 4 units for proper matrix calculations.
input.position.w = 1.0f;
matrix <float, 4, 4> translation =
{
1.0f, 0.0f, 0.0f, 0.0f,
0.0f, 1.0f, 0.0f, 0.0f,
0.0f, 0.0f, 1.0f, 0.0f,
input.instancePosition.x, input.instancePosition.y, input.instancePosition.z, 1.0f
};
matrix <float, 4, 4> rotationAroundY = {
cos(input.instanceRotation.y), 0.0f, -sin(input.instanceRotation.y), 0.0f,
0.0f, 1.0f, 0.0f, 0.0f,
sin(input.instanceRotation.y), 0.0f, cos(input.instanceRotation.y), 0.0f,
0.0f, 0.0f, 0.0f, 1.0f
};
matrix <float, 4, 4> composition;
//i compose together the rotation around y and the translation, the order is important!
composition = mul(rotationAroundY,translation);
//i apply the transformation to the vertex input.position
output.position = mul(input.position,composition);
// Calculate the position of the vertex against the world, view, and projection matrices.
output.position = mul(output.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;
// Calculate the normal vector against the world matrix only.
output.normal = mul(input.normal, (float3x3)worldMatrix);
// Normalize the normal vector.
output.normal = normalize(output.normal);
return output;
}
float
s the same size as your instance count, and you rotate by the amount specified in the element of that array corresponding to the instance identifier? – Panda Pajama Jun 26 '15 at 10:31