Ooops
October 28, 2020, 05:12:09 AM

Author Topic: [bb] Depth of Field by fredborg [ 1+ years ago ]  (Read 1188 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
[bb] Depth of Field by fredborg [ 1+ years ago ]
« on: June 29, 2017, 12:28:43 AM »
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
  1. ;
  2. ; Depth of field
  3. ;
  4. ; Created by Mikkel Fredborg
  5. ; Use as you please!
  6. ;
  7. Graphics3D 800,600,0,2
  8. SetBuffer BackBuffer()
  9.  
  10. HidePointer
  11.  
  12. ;
  13. ; Create a camera...
  14. camera = CreateCamera()
  15. CameraRange camera,0.1,1000.0
  16. CameraFogMode camera,True
  17. CameraFogRange camera,100,1000
  18.  
  19. ;
  20. ; create some cubes
  21. For i = 0 To 100
  22.         cube = CreateCube()
  23.         PositionEntity cube,Rnd(-100,100),Rnd(-100,20),Rnd(-100,100)
  24.         RotateEntity cube,Rnd(-180,180),Rnd(-180,180),Rnd(-180,180)
  25.         ScaleEntity cube,Rnd(1,10),Rnd(1,10),Rnd(1,10)
  26. Next
  27.                        
  28. ;
  29. ; Light
  30. light = CreateLight()
  31. RotateEntity light,90,0,0
  32.  
  33. ; Depth of Field setup
  34. Type DepthOfField
  35.         Field layers
  36.         Field layer[99]
  37.         Field texture
  38.         Field tsize
  39.         Field tbuffer
  40.         Field near#,far#
  41.         Field camera
  42. End Type
  43.  
  44. dof.DepthOfField = DOF_Create(camera,3,2.0)
  45.  
  46. Repeat
  47.         RotateEntity camera,MouseY(),-MouseX(),0
  48.         MoveEntity camera,KeyDown(205)-KeyDown(203),0,KeyDown(200)-KeyDown(208)
  49.  
  50.         DOF_Update(dof)
  51.        
  52.         RenderWorld
  53.        
  54.         Flip False
  55.  
  56. Until KeyHit(1)
  57.  
  58. End
  59.  
  60. Function DOF_Update(dof.depthoffield)
  61.  
  62.         HideEntity doflayer[0]
  63.  
  64.         CameraRange dofcamera,dof
  65. ear*0.95,1000
  66.         CameraViewport dofcamera,0,0,dof        size,dof        size
  67.         RenderWorld
  68.         CopyRect 0,0,dof        size,dof        size,0,0,BackBuffer(),dof       buffer
  69.        
  70.         ShowEntity doflayer[0]
  71.  
  72.         CameraRange dofcamera,0.1,1000 
  73.         CameraViewport dofcamera,0,0,GraphicsWidth(),GraphicsHeight()
  74.        
  75. End Function
  76.  
  77. Function DOF_Create.DepthOfField(camera,layers,spread#=0.0)
  78.  
  79.         dof.depthoffield = New depthoffield
  80.  
  81.         dofcamera = camera
  82.  
  83.         doflayers = layers
  84.  
  85.         dof     size     = 512
  86.         dof
  87. ear      = 100.0
  88.         doffar           = 300.0
  89.        
  90.         ClearTextureFilters
  91.         dof     exture = CreateTexture(dof      size,dof        size,1+256+16+32)
  92.         dof     buffer = TextureBuffer(dof      exture)
  93.        
  94.         ang# = 360.0/Float(doflayers)
  95.         For i = 0 To doflayers-1
  96.                 doflayer[i] = CreateFace(1)
  97.                        
  98.                 EntityAlpha doflayer[i],1.0/Float(doflayers)
  99.                 EntityFX        doflayer[i],1+8
  100.        
  101.                 ps# = dof
  102. ear+(i*((doffar-dof
  103. ear)/Float(doflayers)))
  104.  
  105.                 px# = Sin(i*ang)*(i/Float(doflayers))*spread
  106.                 py# = Cos(i*ang)*(i/Float(doflayers))*spread
  107.                
  108.                 PositionEntity doflayer[i],px,py,ps
  109.                 ScaleEntity doflayer[i],ps,ps,1.0              
  110.        
  111.                 EntityTexture doflayer[i],dof   exture
  112.                
  113.                 If i = 0
  114.                         EntityParent doflayer[i],dofcamera,True
  115.                 Else
  116.                         EntityParent doflayer[i],doflayer[i-1],True
  117.                 End If
  118.         Next
  119.  
  120.         Return dof
  121.  
  122. End Function
  123.  
  124. Function CreateFace(segs=1,parent=0)
  125.  
  126.         mesh=CreateMesh( parent )
  127.         surf=CreateSurface( mesh )
  128.         stx#=-1.0
  129.         sty#=stx
  130.         stp#=Float(2)/Float(segs)
  131.         y#=sty
  132.         For a=0 To segs
  133.                 x#=stx
  134.                 v#=a/Float(segs)
  135.                 For b=0 To segs
  136.                         u#=b/Float(segs)
  137.                         AddVertex(surf,x,-y,0,u,v) ; swap these for a different start orientation
  138.                         x=x+stp
  139.                 Next
  140.                 y=y+stp
  141.         Next
  142.         For a=0 To segs-1
  143.                 For b=0 To segs-1
  144.                         v0=a*(segs+1)+b:v1=v0+1
  145.                         v2=(a+1)*(segs+1)+b+1:v3=v2-1
  146.                         AddTriangle( surf,v0,v2,v1 )
  147.                         AddTriangle( surf,v0,v3,v2 )
  148.                 Next
  149.         Next
  150.        
  151.         FlipMesh mesh
  152.         UpdateNormals mesh
  153.  
  154.         Return mesh
  155.        
  156. 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:
Code: [Select]
; 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[/url]


virtualjesus(Posted 1+ years ago)

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

 

SimplePortal 2.3.6 © 2008-2014, SimplePortal