Author Topic: Motion Blur Technique  (Read 2746 times)

0 Members and 1 Guest are viewing this topic.

Offline blackhole

  • Still not over the rainbow
  • 29
  • Destiny can suck it
    • Black Sphere Studios
I have implemented a method of doing motion blur that is fast and effective in both 2D and 3D. The implementation on my own 2D graphics engine results in the following comparison:



There is only one parameter, and that is the blur amount. Its a simple float value that should probably be set around 0.5f, possibly lower considering how fast those fighters can zip by your cockpit. The code that was used to implement this is as follows:

Code: [Select]
    UINT iPassCount;
   
    if(!effect)
      return;

    //Set variables
    effect->SetTechnique("comboblur");
    if(FAILED(effect->SetFloat(effect->GetParameterByName(NULL, "fblur"), 0.7f))) return;

    //Run the shader
    effect->Begin(&iPassCount,0);

      _pengine->getDriver()->getDevice()->SetRenderTarget(0, prev);
      _pengine->getDriver()->getDevice()->SetTexture(0, HDRtex);
      _pengine->getDriver()->getDevice()->SetTexture(1, tex1);
      effect->BeginPass(0); //screen pass
      _pengine->getDriver()->DrawFullScreenQuad(0.0f,0.0f,1.0f,1.0f);
      effect->EndPass();
     
    effect->End();

    //copy the result to the target
    _pengine->getDriver()->getDevice()->SetTexture(0, tex1);
    _pengine->getDriver()->getDevice()->SetRenderTarget(0, target);
    _pengine->getDriver()->DrawFullScreenQuad(0.0f,0.0f,1.0f,1.0f);

    _pengine->getDriver()->getDevice()->SetTexture(0, NULL); //reset textures
    _pengine->getDriver()->getDevice()->SetTexture(1, NULL);

The effect file that is referenced contains a single pixel shader that is shown below:

Code: [Select]
// Global variables
float fblur;

// Samplers
sampler s0 : register(s0);
sampler s1 : register(s1);

// Combo pass
float4 ps_main( float2 TexCoord : TEXCOORD0 ) : COLOR0
{   
   return lerp(tex2D(s0,TexCoord), tex2D(s1,TexCoord), fblur);
}

// Technique Section
technique comboblur
{
   pass combination
   {
      PixelShader = compile ps_2_0 ps_main();
   }
}

This is all done in directX, but its implementation is very simple. The contents of the rendered scene are stuck on a texture, which can be done either by grabbing the backbuffer or by setting the rendertarget prior to rendering, then that target is fed to a simple interpolation shader that, as you can see, only has one line of code in it. The interpolation shader takes the previous frame and blends it into the current one. The result of this is stored in the previous frame texture, and then the previous frame texture is copied into the backbuffer or whatever is being used for postprocessing.

The first hurdle in converting this to OGL and implementing it in FSO is the postprocessing. The best method would be to create a texture, set it as the rendertarget, go through all the normal rendering routines, then pass that texture and its surface to the motion blur shader, which would then have its own texture and corresponding surface for the previous frame. A GLSL version of the shader would then be called after setting the two textures (plus setting the rendertarget to the previous frame surface), and then the information on the previous frame texture would need to get copied into the original rendertarget that was used to gather the scene render. The one method that would be a sticking point is the full screen quad - a method would need to be created for that. Once the data is copied back into the rendertarget, its a simple matter of rendering the GUI to that rendertarget, then resetting the rendertarget back to the backbuffer and stuffing all the data back into it.

I think most of the stuff here has a direct equivelent in OpenGL, but I'm not sure. Taylor and anyone else whose working on the shader implementation, what do you think?

 

Offline DaBrain

  • Screensniper
  • 212
    • Shadows of Lylat board
 :eek2: I would love to see that in FS2.
--------------------------------------------------
SoL is looking for a sound effect artist
Please PM me in case you want to apply
---------------------------------
Shadows of Lylat - A Freespace 2 total conversion
(hosted by Game-Warden)
----------------------------------

 

Offline Colonol Dekker

  • HLP is my mistress
  • 213
  • Aken Tigh Dekker- you've probably heard me
    • My old squad sub-domain
All that code was about 19 miles over my head. Lol but i'm extremely happy with the results and it get's my vote for SCP's next release.. . . . . . . . Eventally :)
Campaigns I've added my distinctiveness to-
- Blue Planet: Battle Captains
-Battle of Neptune
-Between the Ashes 2
-Blue planet: Age of Aquarius
-FOTG?
-Inferno R1
-Ribos: The aftermath / -Retreat from Deneb
-Sol: A History
-TBP EACW teaser
-Earth Brakiri war
-TBP Fortune Hunters (I think?)
-TBP Relic
-Trancsend (Possibly?)
-Uncharted Territory
-Vassagos Dirge
-War Machine
(Others lost to the mists of time and no discernible audit trail)

Your friendly Orestes tactical controller.

Secret bomb God.
That one time I got permabanned and got to read who was being bitxhy about me :p....
GO GO DEKKER RANGERSSSS!!!!!!!!!!!!!!!!!
President of the Scooby Doo Model Appreciation Society
The only good Zod is a dead Zod
NEWGROUNDS COMEDY GOLD, UPDATED DAILY
http://badges.steamprofile.com/profile/default/steam/76561198011784807.png

 

Offline Wanderer

  • Wiki Warrior
  • 211
  • Mostly harmless
IIRC wouldn't the said postprocessing would also enable a whole lot of other stuff too that could be made with shaders.. like bloom etc.
Do not meddle in the affairs of coders for they are soggy and hard to light

 

Offline taylor

  • Super SCP/Linux Guru
  • Moderator
  • 212
    • http://www.icculus.org/~taylor
Post-processing is great and all, but it ain't going to happen until 3.7.  There is a lot of code upgrades that need to be done before things like post-processing can be effectively handled by the game.

 

Offline blackhole

  • Still not over the rainbow
  • 29
  • Destiny can suck it
    • Black Sphere Studios
openGL version of that line of code:

Code: [Select]
gl_FragColor = mix(texture2D(s0, TexCoord), texture2D(s1, TexCoord), vec4( fblur, fblur, fblur, fblur ));
I looked through the graphics code and after some failed attempts at locating the beginning of it, I've concluded that FS2's rendering code is the most <censored> piece of <censored> I've ever seen in my life. Good luck with that :P

 

Offline taylor

  • Super SCP/Linux Guru
  • Moderator
  • 212
    • http://www.icculus.org/~taylor
I looked through the graphics code and after some failed attempts at locating the beginning of it, I've concluded that FS2's rendering code is the most <censored> piece of <censored> I've ever seen in my life. Good luck with that :P
Exactly!  :lol:

It's a huge mess, and until we get all of that crap straightened out there really isn't any hope of getting pre/post processing effects working.  It's going to need a few months of dedicated effort to redo all of that code.  I don't really have that much time to dedicate to just one thing, so it just gets done little by little, which takes a lot of time.  I'd love to just get it over and done with, but we are talking about a complete rewrite of a massive section of the engine (roughly 35% by my estimate) in order to handle effects like this and still keep the game remotely playable.  :(

 

Offline blackhole

  • Still not over the rainbow
  • 29
  • Destiny can suck it
    • Black Sphere Studios
The whole engine should be recoded from scratch!

 :nervous:

*gets lynched*

 

Offline Kaine

  • 27
Hopefully the C classes I'm taking can get me up to speed enough to be useful in the next 6-12 months  ;)

  

Offline Dark RevenantX

  • 29
  • anonymity —> animosity
By that time the code would have been rewritten (with any luck).