Fake DOF

Share your ZGE-development tips and techniques here!

Moderator: Moderators

Post Reply
User avatar
Kjell
Posts: 1881
Joined: Sat Feb 23, 2008 11:15 pm

Fake DOF

Post by Kjell »

8)

While we're waiting for FBO / off-screen rendering support, here's a dead-easy fake Depth-Of-Field solution for sprite based games ( among other things ). It's not perfect, but it is blazing fast. Requires a mipmapped texture.

Fragment shader

Code: Select all

uniform float Depth;
uniform sampler2D tex1;

void main()
{
  gl_FragColor = texture2D(tex1, gl_TexCoord[0].xy, Depth);
}
*Arrow keys Up/Down control the attached example.

K
Attachments
DOF.zip
(40.71 KiB) Downloaded 620 times
Last edited by Kjell on Mon Aug 31, 2009 9:39 pm, edited 1 time in total.
User avatar
VilleK
Site Admin
Posts: 2277
Joined: Mon Jan 15, 2007 4:50 pm
Location: Stockholm, Sweden
Contact:

Post by VilleK »

Very nice. I guess the bitmap-animation is done with a large texture containing all animation frames and then modifying texture coordinates? And I like the image, is it from some game or tv-show?
User avatar
Kjell
Posts: 1881
Joined: Sat Feb 23, 2008 11:15 pm

Post by Kjell »

Hej Ville,

The sprites are "borrowed" from the game Waku Waku 7 by Sunsoft :roll: And yes, all animation frames are in the same texture .. that's the standard way of doing such a thing afaik.

K
User avatar
jph_wacheski
Posts: 1005
Joined: Sat Feb 16, 2008 8:10 pm
Location: Canada
Contact:

Post by jph_wacheski »

is the effect very subtle? Not sure i'm seeing it,. or its working here.
Sprites do look nice in gl,. perhaps a 'sprite game-setup sample/tutorial' would help those kids what want to do that type of thing,. I will try to make one later but I am working on a bunch of stuff currently,.
iterationGAMES.com
User avatar
diki
Posts: 140
Joined: Thu Sep 11, 2008 7:53 pm
Location: GMT+1
Contact:

Post by diki »

i don't see it either, but i'm rather certain it's because my graphics card does not support gl-shaders :(
@jph: zge reports unsupported shaders in the log on startup, maybe it's the same for you?
User avatar
Kjell
Posts: 1881
Joined: Sat Feb 23, 2008 11:15 pm

Post by Kjell »

Hmm,

It should look like the attached screenshot. And here's a not-so-subtle example you can give a shot.

Code: Select all

ZZDC<?xml version="1.0" encoding="iso-8859-1" ?>
<Model Name="Model">
  <Definitions>
    <Bitmap Name="Bitmap" Filter="2">
      <Producers>
        <BitmapPixels/>
        <BitmapRect Color="1 1 1 1"/>
      </Producers>
    </Bitmap>
    <Shader Name="Shader" UpdateVarsOnEachUse="255">
      <VertexShaderSource>
<![CDATA[//

void main()
{
  gl_TexCoord[0] = gl_MultiTexCoord0;
  gl_Position = gl_ModelViewProjectionMatrix*gl_Vertex;
  
  gl_Position.x *= 4;
  gl_Position.y *= 4;
}]]>
      </VertexShaderSource>
      <FragmentShaderSource>
<![CDATA[//

uniform float Bias;
uniform sampler2D tex1;

//

void main()
{
  gl_FragColor = texture2D(tex1, gl_TexCoord[0].xy, Bias);
}]]>
      </FragmentShaderSource>
      <UniformVariables>
        <ShaderVariable Name="Bias" VariableName="Bias" Value="7.3"/>
      </UniformVariables>
    </Shader>
    <Material Name="Material" Texture="Bitmap" Shading="1" Light="0" Shader="Shader"/>
  </Definitions>
  <OnRender>
    <ZExpression>
      <Expression>
<![CDATA[//

Bias.Value += 0.1;

//

if(Bias.Value > 8)Bias.Value = 0;]]>
      </Expression>
    </ZExpression>
    <UseMaterial Material="Material"/>
    <RenderSprite/>
  </OnRender>
</Model>
K
Attachments
DOF.jpg
DOF.jpg (27.53 KiB) Viewed 12003 times
User avatar
jph_wacheski
Posts: 1005
Joined: Sat Feb 16, 2008 8:10 pm
Location: Canada
Contact:

Post by jph_wacheski »

Strange, that does not show here,. however the new sample works?
Attachments
DOF nope :(
DOF nope :(
DOF_missing.jpg (13.11 KiB) Viewed 11999 times
iterationGAMES.com
User avatar
Kjell
Posts: 1881
Joined: Sat Feb 23, 2008 11:15 pm

Post by Kjell »

:roll:

I might have screwed up somewhere in the Mauru example. Since the mip-mapping happens automatically depending on the projection distance, you need to counter that do pull of a nice smooth DOF gradation. So the shader I used there was a little more elaborate then in the source example provided in my previous post.

I didn't save the Mauru zgeproj though, so I can't check either :wink:

K

Edit: Typo
Last edited by Kjell on Thu Sep 03, 2009 2:08 pm, edited 1 time in total.
User avatar
jph_wacheski
Posts: 1005
Joined: Sat Feb 16, 2008 8:10 pm
Location: Canada
Contact:

Post by jph_wacheski »

oh well, I get the idea, and that last example can be adapted as needed thanks for it!
iterationGAMES.com
User avatar
y offs et
Posts: 418
Joined: Wed Apr 22, 2009 4:26 pm
Location: BC, Canada

Post by y offs et »

The original ran fine on my machine.
"great expectations"
Post Reply