Page 1 of 1

Fake DOF

Posted: Mon Aug 31, 2009 5:53 pm
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

Posted: Mon Aug 31, 2009 6:44 pm
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?

Posted: Mon Aug 31, 2009 7:04 pm
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

Posted: Tue Sep 01, 2009 7:27 pm
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,.

Posted: Thu Sep 03, 2009 1:36 pm
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?

Posted: Thu Sep 03, 2009 1:51 pm
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

Posted: Thu Sep 03, 2009 1:57 pm
by jph_wacheski
Strange, that does not show here,. however the new sample works?

Posted: Thu Sep 03, 2009 2:04 pm
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

Posted: Thu Sep 03, 2009 2:06 pm
by jph_wacheski
oh well, I get the idea, and that last example can be adapted as needed thanks for it!

Posted: Thu Sep 03, 2009 2:52 pm
by y offs et
The original ran fine on my machine.