An example of postfx (gaussian blur)
Code: Select all
#include "openb3d.bi"
screen 18, 32, , &h10002
Graphics3d 640,480,32,1,1
var camera=createcamera(0)
cameraclscolor camera,0,0,255
var shader=loadshader("gauss","gaussian.vert","gaussian.frag")
setinteger shader, "RenderTex",0
setinteger shader, "Width",640
setinteger shader, "Height",480
var fx=createpostfx(camera,2)
hideentity camera
AddRenderTarget fx,0,1,1
Postfxbuffer fx,0,0,1,0
AddRenderTarget fx,1,1,0
Postfxbuffer fx,1,1,1,0
PostFXShader fx,0,shader
PostFXShaderPass fx,0,"PassNum",1
PostFXShader fx,1,shader
PostFXShaderPass fx,1,"PassNum",2
var cube=createcube()
entitycolor cube,255,0,0
var plane=createplane()
moveentity plane,0,-1.4,15
var light=createlight()
moveentity light,10,10,-3
pointentity light,cube
moveentity camera,-2,0,-5
createshadow cube
dim key as string
do
key=inkey
if key=chr(255)+"H" then turnentity cube,1,0,0,0
if key=chr(255)+"P" then turnentity cube,-1,0,0,0
if key=chr(255)+"M" then turnentity cube,0,-1,0,0
if key=chr(255)+"K" then turnentity cube,0,1,0,0
updateworld 1
renderworld
sleep 1
flip
loop until key=chr(27)
gaussian.vert
Code: Select all
#version 120
varying vec3 Position;
varying vec3 Normal;
varying vec2 TexCoord;
void main()
{
TexCoord = gl_MultiTexCoord0.st;
Normal = normalize(gl_NormalMatrix * gl_Normal);
Position = vec3(gl_ModelViewMatrix * gl_Vertex);
gl_Position = ftransform();
}
gaussian.frag
Code: Select all
#version 120
uniform int PassNum;
uniform sampler2D RenderTex;
uniform int Width;
uniform int Height;
varying vec3 Position;
varying vec3 Normal;
varying vec2 TexCoord;
float PixOffset[7] = float[] (0.0,1.5,3.0,4.5,6.0,7.5,9.0);
//float PixOffset[5] = float[] (0.0,1.5,3.0,4.5,6.0);
//float PixOffset[5] = float[] (0.0,0.1,0.2,0.3,0.4);
//float Weight[5] = float[] (0.75,1.0,1.5,1.0,0.75);
float Weight[5] = float[] (0.05,0.14,0.2,0.14,0.05);
//float Weight[5] = float[] (0.001,0.02,0.23,0.02,0.001);
//float Weight[7] = float[] (0.015625,0.09375,0.234375,0.3125,0.234375,0.09375,0.015625);
//float Weight[7] = float[] (0.01562,0.0937,0.2343,0.3125,0.2343,0.0937,0.0156);
vec4 Pass1()
{
float dy = 1.0 / float(Height);
vec4 sum = texture2D(RenderTex,TexCoord) * Weight[0];
for ( int i = 1; i < 5; i++)
{
sum += texture2D(RenderTex, TexCoord + vec2(0.0,PixOffset[i]) * dy ) * Weight[i];
sum += texture2D(RenderTex, TexCoord - vec2(0.0,PixOffset[i]) * dy ) * Weight[i];
}
return sum;
}
vec4 Pass2()
{
float dx = 1.0 / float(Width);
vec4 sum = texture2D(RenderTex, TexCoord) * Weight[0];
for ( int i = 1; i < 5; i++)
{
sum += texture2D(RenderTex, TexCoord + vec2(PixOffset[i],0.0) * dx ) * Weight[i];
sum += texture2D(RenderTex, TexCoord - vec2(PixOffset[i],0.0) * dx ) * Weight[i];
}
return sum;
}
void main()
{
if (PassNum == 1)
{
gl_FragColor = Pass1();
}
if (PassNum == 2)
{
gl_FragColor = Pass2();
}
}