[bb] Depth of Field by fredborg [ 1+ years ago ]

Started by BlitzBot, June 29, 2017, 00:28:43

Previous topic - Next topic

BlitzBot

Title : Depth of Field
Author : fredborg
Posted : 1+ years ago

Description : A simple depth of field effect.

Depth of field, you ask? It is a simulation of the loss of focus a camera (or your eyes) has at a range beyond its focal point.

Should be more or less plug-and-pray!

Have fun!


Code :
Code (blitzbasic) Select
;
; Depth of field
;
; Created by Mikkel Fredborg
; Use as you please!
;
Graphics3D 800,600,0,2
SetBuffer BackBuffer()

HidePointer

;
; Create a camera...
camera = CreateCamera()
CameraRange camera,0.1,1000.0
CameraFogMode camera,True
CameraFogRange camera,100,1000

;
; create some cubes
For i = 0 To 100
cube = CreateCube()
PositionEntity cube,Rnd(-100,100),Rnd(-100,20),Rnd(-100,100)
RotateEntity cube,Rnd(-180,180),Rnd(-180,180),Rnd(-180,180)
ScaleEntity cube,Rnd(1,10),Rnd(1,10),Rnd(1,10)
Next

;
; Light
light = CreateLight()
RotateEntity light,90,0,0

; Depth of Field setup
Type DepthOfField
Field layers
Field layer[99]
Field texture
Field tsize
Field tbuffer
Field near#,far#
Field camera
End Type

dof.DepthOfField = DOF_Create(camera,3,2.0)

Repeat
RotateEntity camera,MouseY(),-MouseX(),0
MoveEntity camera,KeyDown(205)-KeyDown(203),0,KeyDown(200)-KeyDown(208)

DOF_Update(dof)

RenderWorld

Flip False

Until KeyHit(1)

End

Function DOF_Update(dof.depthoffield)

HideEntity doflayer[0]

CameraRange dofcamera,dof
ear*0.95,1000
CameraViewport dofcamera,0,0,dof size,dof size
RenderWorld
CopyRect 0,0,dof size,dof size,0,0,BackBuffer(),dof buffer

ShowEntity doflayer[0]

CameraRange dofcamera,0.1,1000
CameraViewport dofcamera,0,0,GraphicsWidth(),GraphicsHeight()

End Function

Function DOF_Create.DepthOfField(camera,layers,spread#=0.0)

dof.depthoffield = New depthoffield

dofcamera = camera

doflayers = layers

dof size = 512
dof
ear = 100.0
doffar = 300.0

ClearTextureFilters
dof exture = CreateTexture(dof size,dof size,1+256+16+32)
dof buffer = TextureBuffer(dof exture)

ang# = 360.0/Float(doflayers)
For i = 0 To doflayers-1
doflayer[i] = CreateFace(1)

EntityAlpha doflayer[i],1.0/Float(doflayers)
EntityFX doflayer[i],1+8

ps# = dof
ear+(i*((doffar-dof
ear)/Float(doflayers)))

px# = Sin(i*ang)*(i/Float(doflayers))*spread
py# = Cos(i*ang)*(i/Float(doflayers))*spread

PositionEntity doflayer[i],px,py,ps
ScaleEntity doflayer[i],ps,ps,1.0

EntityTexture doflayer[i],dof exture

If i = 0
EntityParent doflayer[i],dofcamera,True
Else
EntityParent doflayer[i],doflayer[i-1],True
End If
Next

Return dof

End Function

Function CreateFace(segs=1,parent=0)

mesh=CreateMesh( parent )
surf=CreateSurface( mesh )
stx#=-1.0
sty#=stx
stp#=Float(2)/Float(segs)
y#=sty
For a=0 To segs
x#=stx
v#=a/Float(segs)
For b=0 To segs
u#=b/Float(segs)
AddVertex(surf,x,-y,0,u,v) ; swap these for a different start orientation
x=x+stp
Next
y=y+stp
Next
For a=0 To segs-1
For b=0 To segs-1
v0=a*(segs+1)+b:v1=v0+1
v2=(a+1)*(segs+1)+b+1:v3=v2-1
AddTriangle( surf,v0,v2,v1 )
AddTriangle( surf,v0,v3,v2 )
Next
Next

FlipMesh mesh
UpdateNormals mesh

Return mesh

End Function


Comments :


Floyd(Posted 1+ years ago)

 This is extremely slow for me.After any input, such a moving the mouse, it takes about one second before I see any movement on screen.But it all works beautifully if I switch from windowed graphics to full screen.i.e. use mode 1 in Graphics3D, and change nothing else.


fredborg(Posted 1+ years ago)

 Switching from 'Flip False' to 'Flip True' may fix it?


CyBeRGoth(Posted 1+ years ago)

 Brilliant!The effect is just as good as the one in Zelda the wind waker on the GC :)EditFloyd try putting the lineEnableDirectInput 1 this seems to help a lot, I had a similar problem.


Stevie G(Posted 1+ years ago)

 Just curious John - what does that do?  My help files are not up to date.CheersStevie


CyBeRGoth(Posted 1+ years ago)

 A while ago some people were having problems with direct input, it would work ok on their PC but on other peoples the mouse, keyboard, joysyick etc would not work correctly.That command reverts the way blitz handles input to how it was before it was changed.seems to fix a lot of problems like this tho, another way is to use windowed mode but that is not ideal for games


jfk EO-11110(Posted 1+ years ago)

 this version is trying to allow camerazooms other than 1.0:
; ID: 1030
; Author: fredborg
; Date: 2004-05-14 18:19:02
; Title: Depth of Field
; Description: Simple depth of field effect!
; Attempt to handle CameraZoom added by jfk.

;
; Depth of field
;
; Created by Mikkel Fredborg
; Use as you please!
;
Graphics3D 800,600,0,2
SetBuffer BackBuffer()

HidePointer

;
; Create a camera...
camera = CreateCamera()
CameraRange camera,0.1,1000.0
CameraFogMode camera,True
CameraFogRange camera,100,1000
Global camzoom#=0.5
CameraZoom camera,camzoom#
;
; create some cubes
For i = 0 To 100
cube = CreateCube()
PositionEntity cube,Rnd(-100,100),Rnd(-100,20),Rnd(-100,100)
RotateEntity cube,Rnd(-180,180),Rnd(-180,180),Rnd(-180,180)
ScaleEntity cube,Rnd(1,10),Rnd(1,10),Rnd(1,10)
Next

;
; Light
light = CreateLight()
RotateEntity light,90,0,0

; Depth of Field setup
Type DepthOfField
Field layers
Field layer[99]
Field texture
Field tsize
Field tbuffer
Field near#,far#
Field camera
End Type

dof.DepthOfField = DOF_Create(camera,3,4); 3,5    3,2.0

Repeat
RotateEntity camera,MouseY(),-MouseX(),0
MoveEntity camera,KeyDown(205)-KeyDown(203),0,KeyDown(200)-KeyDown(208)

DOF_Update(dof)

RenderWorld

Flip False

Until KeyHit(1)

End

Function DOF_Update(dof.depthoffield)

HideEntity doflayer[0]

CameraRange dofcamera,dof
ear*0.95*camzoom,1000
CameraViewport dofcamera,0,0,dof size,dof size
RenderWorld
CopyRect 0,0,dof size,dof size,0,0,BackBuffer(),dof buffer

ShowEntity doflayer[0]

CameraRange dofcamera,0.1*camzoom,1000
CameraViewport dofcamera,0,0,GraphicsWidth(),GraphicsHeight()

End Function

Function DOF_Create.DepthOfField(camera,layers,spread#=0.0)
spread=spread*(1.0/camzoom#)
dof.depthoffield = New depthoffield

dofcamera = camera

doflayers = layers

dof size = 512
dof
ear = 100.0
doffar = 300.0

ClearTextureFilters
dof exture = CreateTexture(dof size,dof size,1+256+16+32)
dof buffer = TextureBuffer(dof exture)

ang# = 360.0/Float(doflayers)
For i = 0 To doflayers-1
doflayer[i] = CreateFace(1)

EntityAlpha doflayer[i],1.0/Float(doflayers)
EntityFX doflayer[i],1+8

ps# = dof
ear+(i*((doffar-dof
ear)/Float(doflayers)))

px# = Sin(i*ang)*(i/Float(doflayers))*spread
py# = Cos(i*ang)*(i/Float(doflayers))*spread

PositionEntity doflayer[i],px*camzoom#,py*camzoom#,ps*camzoom#
; PositionEntity doflayer[i],px,py,ps
ScaleEntity doflayer[i],ps,ps,1.0

EntityTexture doflayer[i],dof exture

If i = 0
EntityParent doflayer[i],dofcamera,True
Else
EntityParent doflayer[i],doflayer[i-1],True
End If
Next

Return dof

End Function

Function CreateFace(segs=1,parent=0)

mesh=CreateMesh( parent )
surf=CreateSurface( mesh )
stx#=-1.0
sty#=stx
stp#=Float(2)/Float(segs)
y#=sty
For a=0 To segs
x#=stx
v#=a/Float(segs)
For b=0 To segs
u#=b/Float(segs)
AddVertex(surf,x,-y,0,u,v) ; swap these for a different start orientation
x=x+stp
Next
y=y+stp
Next
For a=0 To segs-1
For b=0 To segs-1
v0=a*(segs+1)+b:v1=v0+1
v2=(a+1)*(segs+1)+b+1:v3=v2-1
AddTriangle( surf,v0,v2,v1 )
AddTriangle( surf,v0,v3,v2 )
Next
Next

FlipMesh mesh
UpdateNormals mesh

Return mesh

End Function



Robert Cummings(Posted 1+ years ago)

 The problems people are experiencing are typical of "mouse lag" and can be fixed by locking and unlocking the backbuffer. What happens is the card buffers ahead in situations with high fill rate and potentially high framerate.


Ben(t)(Posted 1+ years ago)

 sorry if I sound stupid but what is it supposed to do?


Mustang(Posted 1+ years ago)

 <div class="quote"> what is it supposed to do?  </div>Live and learn, there are no stupid questions... and wikipedia or google is your best friend (if these forums fail which is rare):<a href="http://en.wikipedia.org/wiki/Depth_of_field" target="_blank">http://en.wikipedia.org/wiki/Depth_of_field</a>


virtualjesus(Posted 1+ years ago)

 This not working for 640x480Please help me!!! [/i]