Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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;

}

share|improve this question
    
How about passing an array of floats 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
    
yes this is also my idea, but i have no idea how to apply a rotation in a shader, i just started with HLSL – rekotc Jun 26 '15 at 10:51
    
How about you start with simpler things that don't require advanced topics such as instantiation? – Panda Pajama Jun 26 '15 at 10:54
    
:D i know, i was just curious, also i think i understood how instantiation works, the problem is that i cannot find enough informations about the HLSL sintax and examples.. – rekotc Jun 26 '15 at 10:55
    
I think this is a great place to start – Panda Pajama Jun 26 '15 at 10:56

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.