May 21, 2019, 10:53:51 PM

Author Topic: Problem with PostFX function depth render [SOLVED]  (Read 270 times)

Offline markcwm

  • Sr. Member
  • ****
  • Posts: 379
Problem with PostFX function depth render [SOLVED]
« on: March 31, 2019, 02:40:31 AM »
Hi, just in case Angros still visits here (he hasn't for about a year now) I could use some help.

I have just uploaded examples of how to use the PostFX functions in openb3dmax.docs but I'm really stuck on how to use the PostFX depth buffer, I'm not sure if I'm just not setting it up right or if there's a problem in postfx.cpp, see below examples for how I think it's supposed to work in FreeBasic and Blitzmax. I think it's supposed to happen in a single pass, and there is 1 color buffer and 1 depth buffer, both generated and not supplied.

depthoffield.frag.glsl
Code: [Select]
// Depth of Field by klepto2
// from Minib3dPFXNewton0.40.tar.gz

varying vec4 texCoord0;
varying vec4 texCoord1;

uniform sampler2D colortex; // tex0
uniform sampler2D depthtex;

uniform float blursize; // 0.002

void main(void)
{
vec4 blurFactor = texture2D(depthtex, texCoord1.st) * blursize;
float bf = blurFactor.r;
vec4 blurSample = vec4(0.0, 0.0, 0.0, 0.0);

int lo = 2; // 2
int hi = 3; // 3
for(int tx =-lo; tx<hi; tx++){
for(int ty =-lo; ty<hi; ty++){
vec2 uv = texCoord0.st;
uv.x = uv.x + float(tx) * bf;
uv.y = uv.y + float(ty) * bf;
blurSample = blurSample + texture2D(colortex, uv);
}
}
float darken = 4.0 * float(hi * lo); // 23
blurSample = blurSample / darken;

gl_FragColor = vec4(vec3(blurSample), 1.0);
}

depthoffield.vert.glsl
Code: [Select]
varying vec4 texCoord0;
varying vec4 texCoord1;

void main()
{
texCoord0 = gl_TextureMatrix[0] * gl_MultiTexCoord0;
texCoord1 = gl_TextureMatrix[1] * gl_MultiTexCoord1;

gl_Position = ftransform();
}

Freebasic:
Code: [Select]
#include once "openb3d.bi"

screen 18,  32, , &h10002

   
Graphics3d 640,480,32,1,1

var camera=CreateCamera()
CameraRange camera,0.5,1000.0
CameraClsColor camera,150,200,250

var light=CreateLight()
TurnEntity light,45,45,0

var shader=LoadShader("","depthoffield.vert.glsl","depthoffield.frag.glsl")
SetInteger(shader,"colortex",0) ' 0 is render texture
SetInteger(shader,"depthtex",1) ' 1 is depth texture
dim as single blursize=0.002
UseFloat(shader,"blursize",blursize)

var PostFx=createpostfx(camera,1)
hideentity camera

PositionEntity camera,0,3,0
MoveEntity camera,0,0,-25

var pass_no=0, numColBufs=1, depth=1
var source_pass=0, index=1, slot=0
AddRenderTarget PostFx,pass_no,numColBufs,depth
PostFXBuffer PostFx,pass_no,source_pass,index,slot
PostFXShader PostFx,pass_no,shader

For r as integer=0 To 6
For t as integer=-2 To 2
var sphere=CreateSphere()
PositionEntity sphere,(t*4),1,(t*4)+((r-2)*8)
If r Mod 3=1 Then EntityColor sphere,0,0,255
If r Mod 3=2 Then EntityColor sphere,0,255,255
If r Mod 3=0 Then EntityColor sphere,0,255,0
Next
Next

var cube=CreateCube()
ScaleMesh cube,2,2,2
PositionEntity cube,0,8,40
EntityColor cube,255,0,0

var ground=CreatePlane(128)

dim key as string
do
key=inkey
if key=chr(255)+"=" And blursize<0.004 Then blursize=blursize+0.0001
if key=chr(255)+"-" And blursize>0.0004 Then blursize=blursize-0.0001

TurnEntity cube,0.1,0.2,0.3

updateworld 1
renderworld

sleep 1
flip
loop until key=chr(27)

Blitzmax:
Code: [Select]
' depthoffield.bmx

Strict

Framework Openb3dmax.B3dglgraphics

Local width%=DesktopWidth(),height%=DesktopHeight()

Graphics3D width,height

ClearTextureFilters ' remove mipmap flag for postfx texture

Global camera:TCamera=CreateCamera()
CameraRange camera,0.5,1000.0 ' near must be closer than screen sprite to prevent clipping
CameraClsColor camera,150,200,250

Local light:TLight=CreateLight()
TurnEntity light,45,45,0

Local shader:TShader=LoadShader("","../glsl/depthoffield.vert.glsl","../glsl/depthoffield.frag.glsl")
SetInteger(shader,"colortex",0) ' 0 is render texture
SetInteger(shader,"depthtex",1) ' 1 is depth texture
Local blursize#=0.002
UseFloat(shader,"blursize",blursize)

Local PostFx:TPostFX=CreatePostFX(Camera,1)
HideEntity Camera ' note: this boosts framerate as it prevents extra camera render

PositionEntity camera,0,3,0 ' move camera now sprite is parented to it
MoveEntity camera,0,0,-25

Local pass_no%=0, numColBufs%=1, depth%=1
Local source_pass%=0, index%=1, slot%=0
AddRenderTarget PostFx,pass_no,numColBufs,depth
PostFXBuffer PostFx,pass_no,source_pass,index,slot
PostFXShader PostFx,pass_no,shader

Local sphere:TMesh
For Local r:Int=0 To 6
For Local t:Int=-2 To 2
sphere=CreateSphere()
PositionEntity sphere,(t*4),1,(t*4)+((r-2)*8)
If r Mod 3=1 EntityColor sphere,0,0,255
If r Mod 3=2 EntityColor sphere,0,255,255
If r Mod 3=0 EntityColor sphere,0,255,0
Next
Next

Local cube:TMesh=CreateCube()
ScaleMesh cube,2,2,2
PositionEntity cube,0,8,40
EntityColor cube,255,0,0

Local ground:TMesh=CreatePlane(128)

' main loop
Local postprocess%=0
While Not KeyDown(KEY_ESCAPE)

If KeyHit(KEY_SPACE)
postprocess=Not postprocess
If postprocess Then PostFXShader PostFx,0,Null
If Not postprocess Then PostFXShader PostFx,0,shader
EndIf

If KeyDown(KEY_EQUALS) And blursize<0.004 Then blursize:+0.0001
If KeyDown(KEY_MINUS) And blursize>0.0004 Then blursize:-0.0001

' control camera
MoveEntity camera,KeyDown(KEY_D)-KeyDown(KEY_A),0,KeyDown(KEY_W)-KeyDown(KEY_S)
TurnEntity camera,KeyDown(KEY_DOWN)-KeyDown(KEY_UP),KeyDown(KEY_LEFT)-KeyDown(KEY_RIGHT),0

TurnEntity cube,0.1,0.2,0.3

UpdateWorld
RenderWorld

Flip

Wend

End

Offline markcwm

  • Sr. Member
  • ****
  • Posts: 379
Re: Problem with PostFX function depth render
« Reply #1 on: March 31, 2019, 02:52:46 PM »
Oh I forgot to mention, you'll need to use my fixed version of postfx.cpp to get UseFloat to work.

Oh if you're wondering whether there is a speed increase using the postfx render instead of screen sprites and CameraToTex, there is a significant boost of say 10% but this is only when you hide the camera which will skip the standard render which is not needed when using the postfx render.

Offline angros47

  • Jr. Member
  • **
  • Posts: 14
Re: Problem with PostFX function depth render
« Reply #2 on: April 24, 2019, 06:55:37 PM »
After the line:
Code: [Select]
PostFXBuffer PostFx,pass_no,source_pass,index,slot

You must add:
Code: [Select]
PostFXBuffer PostFx,pass_no,source_pass,0,1
you created the buffers in the right way with AddRenderTarget (specifying that you needed one color buffer, and one depth buffer as output of the first pass). But the command PostFXBuffer is used to specify which buffer is used in the next pass: you specified that you wanted to use the buffer number 1 (the first color buffer) created in the pass 0, and you wanted to put it in the slot 0. With SetInteger you then told the shader to use the slot 0 as the render texture.
But you also told the shader to use the slot 1 as a depth texture, although you forgot to tell OpenB3D to put the depth buffer in slot 1.
The command I added uses the buffer 0 (the depth buffer) in the slot 1.

I know, it's not easy to use. That's why I made the (experimental) OB3DPlus, that provides some pre-done shaders, to be used almost as easily as the other OpenB3D features

Offline markcwm

  • Sr. Member
  • ****
  • Posts: 379
Re: Problem with PostFX function depth render [SOLVED]
« Reply #3 on: April 25, 2019, 04:07:15 AM »
Awesome, thank you Angelo!

I'm so glad it works properly as I can't understand the code much, I think you did a great job on it by the way.

Thanks for the explanation, it's not that hard to use once you understand how it works.