Jump to content

  • Log In with Google      Sign In   
  • Create Account

Instanced rotation?


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
2 replies to this topic

#1 mrheisenberg   Members   -  Reputation: 356

Like
0Likes
Like

Posted 09 September 2012 - 07:38 PM

Hello,everyone,I am having some confusion about instsancing.I learned it from this example:
////////////////////////////////////////////////////////////////////////////////
// Filename: texture.vs
////////////////////////////////////////////////////////////////////////////////

/////////////
// GLOBALS //
/////////////
cbuffer MatrixBuffer
{
matrix worldMatrix;
matrix viewMatrix;
matrix projectionMatrix;
};

//////////////
// TYPEDEFS //
//////////////
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 dont get it..like,why does he change the input structure with the instance data instead of just editing the output one with the instance data like this:
output.position.x += input.instancePosition.x;
    output.position.y += input.instancePosition.y;
    output.position.z += input.instancePosition.z;
?
Also,if this IS the proper way to do it,then would rotation be done in the same way?(by changing the input structure).Then again,I'm not sure about how rotation would be implemented,maybe create a rotation matrix for each instance for each vertex,using a float3 instanceRotation variable and trig functions?

Sponsor:

#2 phil_t   Crossbones+   -  Reputation: 3915

Like
0Likes
Like

Posted 09 September 2012 - 09:14 PM

There's no difference between the two ways. Changing the input structure doesn't actually change the vertices, it just changes them from the point of view of that single shader function.

#3 CC Ricers   Members   -  Reputation: 623

Like
0Likes
Like

Posted 10 September 2012 - 10:12 AM

Also,if this IS the proper way to do it,then would rotation be done in the same way?(by changing the input structure).Then again,I'm not sure about how rotation would be implemented,maybe create a rotation matrix for each instance for each vertex,using a float3 instanceRotation variable and trig functions?


You can either pass rotational values (euler or quaternion) as a shader variable input and create the matrix on the GPU, or use these values on the CPU to produce the final world matrix, and pass the matrix as a float4x4 to the shader.
My development blog: Electronic Meteor




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS