Sign in to follow this  
BradDaBug

Trouble rotating point sprites

Recommended Posts

I'm trying to rotate some points sprites by passing in an angle as one of the vertex parameters, then giving that to my vertex shader, and passing that on to the pixel shader, where it builds a 2x2 rotation matrix and multiplies the texture coords by the rotation matrix. But I can't get it to work. The rotation behaves really weird. The sprite sits there for a moment, then spins really fast until it suddenly stops, and then seems to spin in the opposite direction, then suddenly stop, and repeat. Here's my shader:
struct VertexShaderInput
{
    float4 Position : POSITION0;
    float Rotation : NORMAL;
};

struct VertexShaderOutput
{
    float4 Position : POSITION0;
    float Rotation : COLOR;
    float PointSize : PSIZE;
};

struct PixelShaderInput
{
#ifdef XBOX
	float2 TexCoord : SPRITETEXCOORD;
#else
	float2 TexCoord : TEXCOORD0;
#endif

	float Rotation : COLOR;
};

VertexShaderOutput VertexShaderFunction(VertexShaderInput input)
{
    VertexShaderOutput output;

    output.Position = mul(input.Position, WorldViewProjection);
    output.PointSize = 32.0f;
    output.Rotation = input.Rotation;

    return output;
}

float4 PixelShaderFunction(PixelShaderInput input) : COLOR0
{
	float sine, cosine;
	sincos((input.Rotation - 0.5) * 2.0 * PI, sine, cosine);

    float2x2 rotation;
    
    rotation._m00 = cosine;
    rotation._m01 = sine;
    rotation._m10 = -sine;
    rotation._m11 = cosine;
    
    // move the rotation origin to (0.5, 0.5) instead of (0,0)
    return tex2D(diffuse, mul(input.TexCoord - float2(0.5, 0.5), rotation) + float2(0.5, 0.5));
}

and I'm passing in the desired angle of rotation like this:
vector.X = ...
vector.Y = ...
vector.Z = ...
vector.W = MathHelper.WrapAngle(angle) / MathHelper.Pi * 0.5f + 0.5f;
I thought maybe there was some clamping going on in the shader, so that's why I'm mapping the angle to the (0,1) range. Can anyone see anything stupid I'm doing?

Share this post


Link to post
Share on other sites
I finally found the right combination of semantics that got it working. I'm passing in the angle of rotation as TEXCOORD0, then calculating the sine and cosine of that angle and mapping it to the range [0,1] and sending that to the pixel shader as COLOR0.

Is there any documentation on exactly how various semantic parameters and clamped? Based on the last few days of fighting with this it seems like POSITION and TEXCOORD are unclamped, NORMAL is clamped to [-1,1], and COLOR is clamped to [0,1].

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this