Sign in to follow this  

CG Motion Blur

Recommended Posts

I'm trying to implement motion blur for the moving balls in my scene. I checked many articles and decided to implement it on the cg shader. From what I understood, firstly, I need to calculate velocity vectors for each ball in the vertex shader. Then I need to blend the framebuffer texture on warped texture coordinates (calculated by the velocity vectors). By hardcoding the velocity vectors (i.e. setting it to (1,0,0), blur trail seems fine but I cannot calculate per-object velocity vectors. Here is the vertex shader code. I really appreciate your help.

struct appin
	float3 TexCoord     : TEXCOORD0;

struct VS_OUT 
	float4 hposition   	: POSITION;
	float3 TexCoord         : TEXCOORD0;
	float3 normal      	: TEXCOORD1;
	float3 view		: TEXCOORD2;
	float3 light	   	: TEXCOORD3;
	float3 velocity : TEXCOORD4;

VS_OUT main( appin IN,
	in float4 position	  : POSITION,
	in float4 normal	  : NORMAL,
	uniform float4x4 MVP  : state.matrix.mvp,
	uniform float4x4 MV   : state.matrix.modelview,
	uniform float4x4 MVIT : state.matrix.modelview.invtrans,
	uniform float4x4 prevModelViewProj
	float3 LightPos = float3(100,200,300);
        VS_OUT OUT;
	float4 vPositionCurrent = mul(MVP, position);
	float4 vPositionLast = mul(prevModelViewProj, position);
	OUT.hposition = vPositionCurrent;

	// Convert to non-homogeneous points [-1,1] by dividing by w
	vPositionCurrent /= vPositionCurrent.w;
	vPositionLast /= vPositionLast.w;

	float2 velocity = vPositionCurrent - vPositionLast;
	// The velocity is now between (-2,2) so divide by 2 to get it to (-1,1)
        velocity /= 2.0f;

	float3 poscam = mul(MV, position).xyz;
	OUT.normal = mul(MVIT, normal).xyz;
	OUT.light = LightPos - poscam;
	OUT.view  = -poscam;

	OUT.TexCoord = IN.TexCoord;
	OUT.velocity = float3(velocity, 1.0f);
	return OUT;

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