


local mat_MotionBlur		= Material( "pp/motionblur" )
local mat_Screen		= Material( "pp/fb" )
local tex_MotionBlur		= render.GetMoBlurTex0()

/*---------------------------------------------------------
   Register the convars that will control this effect
---------------------------------------------------------*/   
CreateClientConVar( "pp_motionblur", "0", false, false )
CreateClientConVar( "pp_motionblur_addalpha", "0.2", false, false )
CreateClientConVar( "pp_motionblur_drawalpha", "0.99", false, false )
CreateClientConVar( "pp_motionblur_delay", "0.0", false, false )

local NextDraw = 0

function DrawMotionBlur( addalpha, drawalpha, delay )


	// Copy the backbuffer to the screen effect texture
	render.UpdateScreenEffectTexture()

	// Set up out materials
	mat_MotionBlur:SetMaterialFloat( "$alpha", drawalpha )
	mat_MotionBlur:SetMaterialTexture( "$basetexture", tex_MotionBlur )
	
	if ( NextDraw < CurTime() ) then

		NextDraw = CurTime() + delay

		mat_Screen:SetMaterialFloat( "$alpha", addalpha )
		local OldRT = render.GetRenderTarget();
		render.SetRenderTarget( tex_MotionBlur )

		render.SetMaterial( mat_Screen )
		render.DrawScreenQuad()

		render.SetRenderTarget( OldRT )

	end


	render.SetMaterial( mat_MotionBlur )
	render.DrawScreenQuad()
	
end

local function DrawInternal()

	if ( GetConVarNumber( "pp_motionblur" ) == 0 ) then return end
	if ( !GAMEMODE:PostProcessPermitted( "motion blur" ) ) then return end

	DrawMotionBlur( GetConVarNumber( "pp_motionblur_addalpha" ),
			GetConVarNumber( "pp_motionblur_drawalpha" ),
			GetConVarNumber( "pp_motionblur_delay") );

end



hook.Add( "RenderScreenspaceEffects", "RenderMotionBlur", DrawInternal )