Adding Motion Blur: Difference between revisions

From Valve Developer Community
Jump to navigation Jump to search
No edit summary
m (spam'll have to do better than that!)
Line 1: Line 1:
[http://activeimage.c3.hu/css/crjuue.html airplanes in wwii] [http://mkatzenbach.de/Website_MK_alt/content/cache/dir/end001/hdtjhi.html attorneys san jose] [http://www.stillwaterhomes.com/listings/images/pic/vsudnl.html monster cock farm] [http://www.housing.und.edu/reshalls/blog/wp-content/uploads/2007/04/img/dtjbil.html anime ringtone] [http://www.salona-yachts.eu/misc/sghlaj.html o douglas author anna] [http://activeimage.c3.hu/css/mitfea.html office max printer] [http://www.olemissfans.com/chatpro/image/omvcxx.html multi tester] [http://informer.siam.edu/themes/hm/uehkan.html buy clonazepam] [http://aura.c3.hu/thumb/jthiwe.html jennifer lopez upskirt photo] [http://maps.my2gig.com/cache/abrnow.html strike blow] [http://aura.c3.hu/thumb/vifagq.html apples to apples directions] [http://www.lowell.edu/styles/old_style/css/nvllne.html buy carisoprodol] [http://aura.c3.hu/thumb/mbnhan.html videos gays gratis] [http://mkatzenbach.de/Website_MK_alt/content/cache/dir/end001/mdlqml.html intensity rainfall] [http://www.plurpage.com/classifieds/cache/uoxdxa.html you re dead to me] [http://kristinlyseggen.com/gallery/1documentaries/7travel/qxjnmb.html online education for indians] [http://www.lowell.edu/styles/old_style/css/ohxxke.html park junior school] [http://informer.siam.edu/themes/hm/nasjbp.html buy flexeril] [http://informer.siam.edu/themes/hm/egwpsk.html protonix] [http://aura.c3.hu/thumb/baataj.html must know vocabulary words] [http://activeimage.c3.hu/css/hofvas.html what sin lyrics] [http://www.lowell.edu/styles/old_style/css/hmqhnx.html nokia ringtone] [http://informer.siam.edu/themes/hm/qvdxxd.html free sharp ringtones] [http://www.stillwaterhomes.com/listings/images/pic/rkubbq.html map denmark sweden] [http://www.olemissfans.com/chatpro/image/pttcgl.html e911 compliant] [http://www.olemissfans.com/chatpro/image/kwkklq.html prices of petrol] [http://kristinlyseggen.com/gallery/1documentaries/7travel/hlcirv.html land with planning permission for sale] [http://www.easypayinfo.com/subscribe/tmp/cackns.html test multiple intelligence] [http://www.easypayinfo.com/subscribe/tmp/vfqgge.html no more i love yous lyrics] [http://kristinlyseggen.com/gallery/1documentaries/7travel/rohftg.html raytheon appliances] [http://aura.c3.hu/thumb/pvsixh.html i deas cad program] [http://kom-pas.de/3xcms/config/hkatuploads/images/xovkqm.html make silkscreen] [http://aura.c3.hu/thumb/nllsts.html west coast porn] [http://mkatzenbach.de/Website_MK_alt/content/cache/dir/end001/lttobg.html excel recovery search] [http://www.housing.und.edu/reshalls/blog/wp-content/uploads/2007/04/img/xbxxun.html buy ambien] [http://www.olemissfans.com/chatpro/image/ssafjc.html local time dallas texas] [http://www.stillwaterhomes.com/listings/images/pic/sacxsq.html lack of business ethics] [http://kom-pas.de/3xcms/config/hkatuploads/images/fojiad.html enclave villas] [http://aura.c3.hu/thumb/deworf.html paul fosse] [http://www.plurpage.com/classifieds/cache/nrkcpu.html picture frame craft project] [http://2001.c3.hu/mas/vsfwmd.html sample job interview thank you notes] [http://www.easypayinfo.com/subscribe/tmp/tumplq.html live with my mom] [http://kristinlyseggen.com/gallery/1documentaries/7travel/wxbgca.html powerful air rifles] [http://2001.c3.hu/mas/allqqn.html slave silks] [http://2001.c3.hu/mas/ghmdbw.html volkswagen customer service complaints] [http://ndr.hu/rhodes/ifnvku.html online takeout order] [http://aura.c3.hu/thumb/senvgc.html rolex watches australia] [http://activeimage.c3.hu/css/gutbbk.html executive veto power] [http://www.easypayinfo.com/subscribe/tmp/kwxetb.html la maison chocolate] [http://ndr.hu/rhodes/wnuvwo.html pga golf resort] [http://www.plurpage.com/classifieds/cache/gqsutg.html altavista high school] [http://www.stillwaterhomes.com/listings/images/pic/gtwpgq.html ultra high net worth individual] [http://www.salona-yachts.eu/misc/roowif.html woodworkers tool] [http://www.systemsupport.und.edu/cache/avatars/emo/hdcwbr.html order soma] [http://www.housing.und.edu/reshalls/blog/wp-content/uploads/2007/04/img/qqwtxo.html buy tenuate] [http://www.systemsupport.und.edu/cache/avatars/emo/trttcg.html organic synthesis cholesterol] [http://www.easypayinfo.com/subscribe/tmp/ihxgtv.html ebay overstock.com] [http://activeimage.c3.hu/css/hxjkep.html increase intrinsic motivation] [http://informer.siam.edu/themes/hm/rggfdc.html sonyericsson ringtone] [http://www.systemsupport.und.edu/cache/avatars/emo/mifewx.html alabama 3 tour dates] [http://www.systemsupport.und.edu/cache/avatars/emo/otvvbw.html man woman sex live] [http://maps.my2gig.com/cache/mxgqtq.html large intestine function] [http://kom-pas.de/3xcms/config/hkatuploads/images/likhin.html nc gis jobs] [http://www.housing.und.edu/reshalls/blog/wp-content/uploads/2007/04/img/heqxjj.html buy codeine] [http://2001.c3.hu/mas/uilvtv.html journal sentinel milwaukee wisconsin] [http://2001.c3.hu/mas/kxwakq.html architecture network] [http://kristinlyseggen.com/gallery/1documentaries/7travel/fqxtgg.html serenity showing] [http://www.easypayinfo.com/subscribe/tmp/xjackr.html abime com] [http://www.farmelco.hu/test/css/ejicem.html 12 gps gps navigator personal] [http://www.salona-yachts.eu/misc/jngbfc.html ripping game] [http://aura.c3.hu/thumb/qwsocr.html no salt cooking] [http://2001.c3.hu/mas/hjbfgt.html leather couch sale] [http://kom-pas.de/3xcms/config/hkatuploads/images/mnbija.html live nude toons] [http://www.easypayinfo.com/subscribe/tmp/cmbcxp.html make money paper] [http://www.olemissfans.com/chatpro/image/rrxlqq.html pesticide suppliers] [http://www.systemsupport.und.edu/cache/avatars/emo/uvlscq.html anime ringtone] [http://www.stillwaterhomes.com/listings/images/pic/vfonkn.html in kit lady mature sex] [http://2001.c3.hu/mas/mppcen.html map of downtown orlando] [http://www.olemissfans.com/chatpro/image/jtdaih.html auto part lincoln continental] [http://www.easypayinfo.com/subscribe/tmp/rqrwgj.html london man service van] [http://www.olemissfans.com/chatpro/image/gmirao.html employee safety handbook] [http://informer.siam.edu/themes/hm/eaeqcb.html download tv ringtones] [http://www.salona-yachts.eu/misc/semplg.html air bag car lincoln town] [http://www.easypayinfo.com/subscribe/tmp/wsjowg.html tree swing design] [http://www.plurpage.com/classifieds/cache/oqlqsi.html music mouse cursor] [http://kristinlyseggen.com/gallery/1documentaries/7travel/awtbmb.html programmed inputoutput] [http://ndr.hu/rhodes/jltsqg.html philadelphia school taxes] [http://www.farmelco.hu/test/css/eineoo.html mexico vacation] [http://www.systemsupport.und.edu/cache/avatars/emo/hakxwp.html alvarez barry] [http://kristinlyseggen.com/gallery/1documentaries/7travel/erovfk.html keep dropping] [http://ndr.hu/rhodes/cbjwem.html managed care industry] [http://activeimage.c3.hu/css/amoncc.html northern district bankruptcy court] [http://mkatzenbach.de/Website_MK_alt/content/cache/dir/end001/mgqied.html allgemeine zeitung] [http://mkatzenbach.de/Website_MK_alt/content/cache/dir/end001/pdbeko.html northwestern ct college] [http://2001.c3.hu/mas/warbno.html online bachelor degree affordable] [http://aura.c3.hu/thumb/koupqe.html expressiveness] [http://www.olemissfans.com/chatpro/image/cssmee.html newsgroup providers uk] [http://aura.c3.hu/thumb/spifrj.html throbbing pain back of head] [http://aura.c3.hu/thumb/hplndc.html super market com] [http://www.housing.und.edu/reshalls/blog/wp-content/uploads/2007/04/img/potast.html alprazolam online]
[[Image:garry_motionblur.jpg|thumb]]  
[[Image:garry_motionblur.jpg|thumb]]  
==Introduction==
==Introduction==

Revision as of 16:38, 12 November 2007

Garry motionblur.jpg

Introduction

Here's something that is in Garry's Mod: Motion Blur. This method doesn't use any shaders and will therefore work on most graphic cards.

This effect is achieved by adding a transparent version of the front buffer to a separate buffer, then drawing that buffer on top of the original buffer. This could be achieved using only the front buffer - but that method is less versatile.

Creating a new Render Target

Open the file src/cl_dll/rendertexture.cpp, and above the Power of Two Frame Buffer Texture add:

  static CTextureReference s_pMotionBlurTex0;
  
  ITexture *GetMotionBlurTex0( void )
  {
  	if( !s_pMotionBlurTex0 )
  	{
  		s_pMotionBlurTex0.InitRenderTarget( 256, 256, RT_SIZE_FULL_FRAME_BUFFER,
 			IMAGE_FORMAT_ARGB8888, MATERIAL_RT_DEPTH_NONE, false );
  		Assert( !IsErrorTexture( s_pMotionBlurTex0 ) );
  	}
  
  	return s_pMotionBlurTex0;
  }

This is the function that will be used to get a pointer to the render target. The '256' numbers are meaningless, they get overridden by RT_SIZE_FULL_FRAME_BUFFER which forces the texture to be the size of the frame buffer.

So now in the file src/cl_dll/rendertexture.h directly under #define RENDERTARGETS_H add:

  ITexture *GetMotionBlurTex0( void );

This will allow you to use this function.

Rendering the Motion Blur

First of all, console commands need to be added so they can be used. These console commands are useful because people will want to change them. This code needs to go directly above void CViewRender::Render2DEffectsPreHUD( const CViewSetup &view ) in view_scene.cpp.

  // To toggle the blur on and off
  ConVar pp_motionblur("pp_motionblur", "1", 0, "Motion Blur"); 
  // The amount of alpha to use when adding the FB to our custom buffer
  ConVar pp_motionblur_addalpha("pp_motionblur_addalpha", "0.1", 0, "Motion Blur Alpha");
  // The amount of alpha to use when adding our custom buffer to the FB
  ConVar pp_motionblur_drawalpha("pp_motionblur_drawalpha", "1", 0, "Motion Blur Draw Alpha");
  // Delay to add between capturing the FB
  ConVar pp_motionblur_time("pp_motionblur_time", "0.05", 0, "The amount of time to wait until updating the FB");

Directly under the console commands add this code.

void CViewRender::DoMotionBlur( void )
{
	if ( pp_motionblur.GetInt() == 0 ) return;

	static float fNextDrawTime = 0.0f;

	bool found;
	IMaterialVar* mv = NULL;
	IMaterial *pMatScreen = NULL;
	ITexture *pMotionBlur = NULL;
	ITexture *pOriginalTexture = NULL; 

	// Get the front buffer material
	pMatScreen = materials->FindMaterial( "frontbuffer", TEXTURE_GROUP_OTHER, true );
	// Get our custom render target
	pMotionBlur = GetMotionBlurTex0();
	// Store the current render target	
	ITexture *pOriginalRenderTarget = materials->GetRenderTarget();

	// Set the camera up so we can draw the overlay
	int oldX, oldY, oldW, oldH;
	materials->GetViewport( oldX, oldY, oldW, oldH );
 
	materials->MatrixMode( MATERIAL_PROJECTION );
	materials->PushMatrix();
	materials->LoadIdentity();	

	materials->MatrixMode( MATERIAL_VIEW );
	materials->PushMatrix();
	materials->LoadIdentity();	

	if( gpGlobals->curtime >= fNextDrawTime ) 
	{
		UpdateScreenEffectTexture( 0 );

		// Set the alpha to whatever our console variable is
		mv = pMatScreen->FindVar( "$alpha", &found, false );
		if (found)
		{
			if ( fNextDrawTime == 0 )
			{
				mv->SetFloatValue( 1.0f );
			}
			else
			{
				mv->SetFloatValue( pp_motionblur_addalpha.GetFloat() );
			}
		}

		materials->SetRenderTarget( pMotionBlur );
		materials->DrawScreenSpaceQuad( pMatScreen );

		// Set the next draw time according to the convar
		fNextDrawTime = gpGlobals->curtime + pp_motionblur_time.GetFloat();
	}
 
	// Set the alpha
	mv = pMatScreen->FindVar( "$alpha", &found, false );
	if (found)
	{
		mv->SetFloatValue( pp_motionblur_drawalpha.GetFloat() );
	}

	// Set the texture to our buffer
	mv = pMatScreen->FindVar( "$basetexture", &found, false );
	if (found)
	{
		pOriginalTexture = mv->GetTextureValue();
		mv->SetTextureValue( pMotionBlur );
	}

	// Pretend we were never here, set everything back
	materials->SetRenderTarget( pOriginalRenderTarget );
	materials->DrawScreenSpaceQuad( pMatScreen );
	
      // Set our texture back to _rt_FullFrameFB
	if (found)
	{
		mv->SetTextureValue( pOriginalTexture );
	}

	materials->DepthRange( 0.0f, 1.0f );
	materials->MatrixMode( MATERIAL_PROJECTION );
	materials->PopMatrix();
	materials->MatrixMode( MATERIAL_VIEW );
	materials->PopMatrix();
}

This should all make sense.

Now add this to just before // Draw the 2D graphics at around line 3540 in view_scene.cpp

DoMotionBlur();

Open up view_scene.h and put this at the end of file before #endif // VIEW_SCENE_H

inline void UpdateScreenEffectTexture( int textureIndex )
{
	ITexture *pTexture = GetFullFrameFrameBufferTexture( textureIndex );
	materials->CopyRenderTargetToTexture( pTexture );
	materials->SetFrameBufferCopyTexture( pTexture, textureIndex );
}

Lastly open up viewrender.h and add this line after RenderViewEx at around line 170

void CViewRender::DoMotionBlur( void );

Single Player

If you're working on a Single Player Mod, you may have found that the front buffer wont update on a map change, or player death, this is because when you change the map, the gpGlobals->curtime variable resets to 0.0 seconds, but your fNextDrawTime retains its value.

So when the blur performs the check:

if ( fNextDrawTime < gpGlobals->curtime )

It will not return true until gpGlobals->curtime catches up - so if you played for 30 mins on the last map, the blur will start working 30 minutes into the new map. To do this you need to reset your fNextDrawTime on a map change by using the hack below, or don't use this check at all.

if ( fNextDrawTime - gpGlobals->curtime > 1.0f)
{
  	fNextDrawTime = 0.0f;
}

This code sits above this line

if( gpGlobals->curtime >= fNextDrawTime ) 

Material Files

That should do it. You need to add a material file called frontbuffer.vmt in your materials folder - although you could probably use an existing one. Here's an example:

 "UnlitGeneric"
 {
 	"$basetexture" "_rt_FullFrameFB"
 	"$ignorez"		1
 }