Hi,
I have made a set of functions to manipulate (fake) cameras, to create post-rendering. You can exclude objects rendering, disable rendering, resize rendering, it is very simple to use, the explanations are in the source code. I took the mirror effect for this demo, with 3 cameras.
remstart
==============================================================
= Title : Reflect Test
= Author : Silverman
= Date : 25/10/2012
= Update :
= Version: 2
==============================================================
Comments
* Bitmap 31 reserved
* How it work ?
-need two pass;
1: take_snapshot() capture image. When it is done, sync_snapshot() lock functiion take_snapshot().
2: take_snapshot() do nothing, sync_snapshot() display image and unlock functiion take_snapshot().
3: take_snapshot() capture image. When ...
4: ...
* Fps
Ex., at 60 fps, if you make 2 snapshots, you have(image refresh):
- main screen = 30 fps
- snapshot 1 = 15 fps
- snapshot 2 = 15 fps
So, for 3 snaphots, you have 30 / 10 / 10 / 10 fps.
* Initialization
1 array and 1 functiion:
- array snapbuff#(16+1024) : buffer for global variables, 1024 is nb of objects that
can be hidden in snapshot. Change this value if you need more.
- functiion dim_snapshot(sizeX,sizeY) : maximum size of our buffer snapshot. SizeX or sizeY must
not exceed screen size
* Set of functiion
-functiion:
1) dim_snapshot(sizeX,sizeY)
2) redim_snapshot(sizeX,sizeY)
3) delete_snapshot()
4) take_snapshot(snapshot_nb, image_nb, poscamX#, poscamY#, poscamZ#, pointcamX#, pointcamY#, pointcamZ#)
5) sync_snapshot()
6) return_value = snapshot_in_progress()
7) exclude_object_snapshot(obj)
8) desactivate_snapshot(snapshot_nb, value)
9) return_value = snapshot_activate(snapshot_nb)
-parameters:
1) sizeX,sizeY : see 'Initialization'
2) sizeX,sizeY : new size of snapshot. New size can not exceed snapshot buffer, see 'Initialization'
3) free memory
4) snapshot_nb : snapshot value must be between 1 and 32 / image_nb : image of snapshot /
data camera : positionX,Y,Z and pointX,Y,Z camera(look at)
5) once per game loop, same sync command
6) return_value : 1(true) or 0(false). Check if snapshot are in progress
7) obj : object to exclude from snapshot. sync_snapshot() clears the list of excluded items, so
you must recall this function for each snapshot
8) snapshot_nb : snapshot value must be between 1 and 32 / value : 1(true) or 0(false)
9) return_value : 1(true) or 0(false) / snapshot_nb : snapshot value must be between 1 and 32. This
functiion check if 'snapshot_nb' are active
-note
.take_snapshot(snapshot_nb, image_nb,... : you can re-use same snapshot_nb, but image_nb
must be different
.the set of functiion use 'set camera view' command, so if you use 'set camera view' in your code, it
may not work properly
.the set of functiion use global variable for synchronizing, so if you use more 'sync' in your
game loop, the display may be altered
==============================================================
remend
rem =============================================================
rem = SET UP DISPLAY
rem =============================================================
autocam off
set display mode 640,480,32
sync on
sync rate 0
rem =============================================================
rem = MAIN
rem =============================================================
_main:
gosub _init
gosub _make_scene
repeat
rem move a cone around independent of the reflection
gosub _move_cone
rem Rotate our camera(red sphere) slowly
if timer()-tim > 30
rem
position object 1,-25,0,0
yrotate object 1,wrapvalue(object angle y(1)+1)
move object 1,-50
tim=timer()
endif
rem exclude red sphere from all snapshots
exclude_object_snapshot(1)
rem make multiple snapshot
rem if snapshot are in progress ---> sip = NOT(snapshot in progress)
sip = 1-snapshot_in_progress() : inc snapshot,sip
if snapshot>3 then snapshot=1
`so snapshot = 1 1 2 2 3 3 1 1 ...
`need 2 pass; first: capture snapshot, second: display snapshot
select snapshot
case 1
rem exclude green cone from this snapshot...
rem view from red sphere
`each snapshot can be redim if we want
`redim_snapshot(128,128)
exclude_object_snapshot(4)
image2=take_snapshot(2, 100, object position x(1), object position y(1), object position z(1), -25.0, 0.0, 0.0)
texture object 2,image2
endcase
case 2
rem ... and this snapshot
rem view from top of little cube
`redim_snapshot(32,32)
exclude_object_snapshot(4)
image3=take_snapshot(3, 101, object position x(3),130+object position y(3), object position z(3), -25.0, 0.0, 0.0)
texture object 3,image3
endcase
case 3
rem view from big cube(big cube look at little cube)
`redim_snapshot(256,256)
image4=take_snapshot(4, 102, object position x(2), object position y(2), object position z(2), -25.0, 0.0, 0.0)
texture object 5,image4
endcase
endselect
rem control keyboard
gosub _keyboard
rem display text after make snapshot, else text is captured
gosub _display_text
rem update screen
sync_snapshot()
sync
until exit_loop<>0
_end_reflect:
delete_snapshot()
`not need, only for 'sync rate VALUE' test
sync rate 0 : sync
end
rem =============================================================
rem = SUBROUTINES - PROCEDURES
rem =============================================================
_init:
rem initialize buffer for global variables(1024 objects can be hidden) and initialise 2nd buffer
rem for image. Square size = max speed !
dim snapbuff#(16+1024)
dim_snapshot(256,256)
rem do not see max quality at start
size=128 : redim_snapshot(size,size)
return
`================================================================
_make_scene:
rem make matrix for 0,0,0 reference
make matrix 1,1000,1000,25,25
position matrix 1,-500,-30,-500
rem make a red sphere that represents the camera
make object sphere 1,5
color object 1,rgb(255,0,0)
rem make a object to display the reflection on
make object cube 2,50
position object 2,50,0,0
rem make sure the object is fully bright
set object emissive 2,rgb(255,255,255)
rem make a white cube to put in the scene
make object cube 3,25
position object 3,-25,0,0
rem a green cone that will move independently of the reflection code
make object cone 4,20
color object 4,rgb(0,255,0)
rem make a 2nd object to display the reflection on
make object plain 5,50,50
position object 5,10,40,80
point object 5,0,0,-100
rem make sure the object is fully bright
set object emissive 5,rgb(255,255,255)
rem set the initial camera position
position camera 0,0,-100
return
`================================================================
_move_cone:
position object 4,-25,0,0
turn object left 4,1
move object 4,40
return
`================================================================
_display_text:
text 0,0,"Fps = "+str$(screen fps())
text 0,20,"1/2/3 = desactivate/activate reflect / Upkey/Downkey = quality reflect / Other key = exit"
text 0,40,"The green cone is excluded from two snapshots, whereas the red sphere is excluded from all !"
return
`================================================================
_keyboard:
rem 1 make pause
oldkey1=key1
key1=keystate(2)
if key1>oldkey1
snapshot2=(snapshot2!1)
endif
desactivate_snapshot(2,snapshot2)
rem 2 make pause
oldkey2=key2
key2=keystate(3)
if key2>oldkey2
snapshot3=(snapshot3!1)
endif
desactivate_snapshot(3,snapshot3)
rem 3 make pause
oldkey3=key3
key3=keystate(4)
if key3>oldkey3
snapshot4=(snapshot4!1)
endif
desactivate_snapshot(4,snapshot4)
rem Up / Down resize
oldupk=upk
upk=upkey()
if upk>oldupk
size=size*2
if size>256 then size=256
redim_snapshot(size,size)
endif
rem Up / Down resize
olddownk=downk
downk=downkey()
if downk>olddownk
size=size/2
if size<2 then size=2
redim_snapshot(size,size)
endif
rem other key = exit loop
exit_loop=scancode()
if exit_loop=57 or exit_loop=200 or exit_loop=208 or exit_loop=2 or exit_loop=3 or exit_loop=4 then exit_loop=0
return
`================================================================
Rem set of functions for snapshot
function dim_snapshot(sizeX,sizeY)
if bitmap exist(31)
delete bitmap 31
endif
create bitmap 31,sizeX,sizeY
set current bitmap 0
`activate snapshot ability
snapbuff#(3)=0
`
snapbuff#(7)=screen width()
snapbuff#(8)=screen height()
snapbuff#(9)=sizeX-1
snapbuff#(10)=sizeY-1
endfunction
function redim_snapshot(sizeX,sizeY)
`snapshot can be resizing on the fly
if (sizeX<>oldsizeX) or (sizeY<>oldsizeY)
snapbuff#(4)=1
snapbuff#(5)=sizeX
snapbuff#(6)=sizeY
endif
oldsizeX=sizeX : oldsizeY=sizeY
endfunction
function delete_snapshot()
`clean memory
sync_snapshot()
delete bitmap 31
` for i=0 to 10
` snapbuff#(i)=0
` next i
undim snapbuff#(0)
endfunction
function take_snapshot(snapshot_nb, image_nb, poscamX#, poscamY#, poscamZ#, pointcamX#, pointcamY#, pointcamZ#)
if (snapshot_nb&31)=0
unlock cli
unlock editor
break "snapshot value must be between 1 and 32 !"
end
endif
tmp=snapbuff#(3)
`if no desactivate
if (tmp&(2^(snapshot_nb-1)))=0
flipflop=snapbuff#(0)
if flipflop=0
snapbuff#(1)=1
rem *** CORE FONCTION HOME ***
`save main camera position
angx#=camera angle x()
angy#=camera angle y()
angz#=camera angle z()
posx#=camera position x()
posy#=camera position y()
posz#=camera position z()
snapbuff#(11)=angx#
snapbuff#(12)=angy#
snapbuff#(13)=angz#
snapbuff#(14)=posx#
snapbuff#(15)=posy#
snapbuff#(16)=posz#
`set second camera
set camera view 0,0,snapbuff#(9)+1,snapbuff#(10)+1
`position of second camera for snapshot
position camera poscamX#, poscamY#, poscamZ#
point camera pointcamX#, pointcamY#, pointcamZ#
`update screen
get image image_nb,0,0,snapbuff#(9)+1,snapbuff#(10)+1
`copy bitmap 31,0
copy bitmap 31,0,0,snapbuff#(9),snapbuff#(10),0,0,0,snapbuff#(9),snapbuff#(10)
rem *** CORE FONCTION END ***
endif
endif
endfunction image_nb
function sync_snapshot()
if snapbuff#(1)<>0
flipflop=snapbuff#(0)
if flipflop=1
rem shows the objects that were hidden from the snapshot
endlist=snapbuff#(2)+16
for i=17 to endlist
obj=snapbuff#(i)
show object obj
next i
snapbuff#(2)=0
rem *** CORE FONCTION HOME ***
snapbuff#(1)=0
angx#=snapbuff#(11)
angy#=snapbuff#(12)
angz#=snapbuff#(13)
posx#=snapbuff#(14)
posy#=snapbuff#(15)
posz#=snapbuff#(16)
`restore main camera position
rotate camera angx#,angy#,angz#
position camera posx#,posy#,posz#
`update screen
set camera view 0,0,snapbuff#(7),snapbuff#(8)
`if resize on the fly:
if snapbuff#(4)=1
snapbuff#(4)=0
snapbuff#(9)=snapbuff#(5)-1
snapbuff#(10)=snapbuff#(6)-1
endif
copy bitmap 0,0,0,snapbuff#(9),snapbuff#(10),31,0,0,snapbuff#(9),snapbuff#(10)
rem *** CORE FONCTION END ***
else
rem hide object in snapshot
endlist=snapbuff#(2)+16
for i=17 to endlist
obj=snapbuff#(i)
hide object obj
next i
endif
flipflop=flipflop!1 : snapbuff#(0)=flipflop
else
snapbuff#(2)=0
endif
endfunction
function snapshot_in_progress()
result=snapbuff#(1)
endfunction result
function exclude_object_snapshot(obj)
if snapbuff#(0)=0
tmp=snapbuff#(2)
inc tmp
snapbuff#(2)=tmp
snapbuff#(16+tmp)=obj
endif
endfunction
function desactivate_snapshot(snapshot_nb, value)
if (snapshot_nb&31)=0
unlock cli
unlock editor
break "snapshot value must be between 1 and 32 !"
end
endif
tmp=snapbuff#(3)
pos=2^(snapshot_nb-1)
if value=1
tmp=tmp|pos
else
tmp=tmp&(-1-pos)
endif
snapbuff#(3)=tmp
endfunction
function snapshot_activate(snapshot_nb)
if (snapshot_nb&31)=0
unlock cli
unlock editor
break "snapshot value must be between 1 and 32 !"
end
endif
tmp=snapbuff#(3)
result=(tmp&(2^(snapshot_nb-1)))!0
endfunction result
remstart
buffer info:
snapbuff#(0) flipflop for switch sync
snapbuff#(1) =1 if snapshot are captured, else 0
snapbuff#(2) nb of objects excluded
snapbuff#(3) start / pause (boolean)
snapbuff#(4) =1 if user resize snapshot on the fly, else 0
snapbuff#(5) newsize X
snapbuff#(6) newsize Y
snapbuff#(7) screen width
snapbuff#(8) screen height
snapbuff#(9) bitmap width
snapbuff#(10) bitmap height
snapbuff#(11) camera angle X#
snapbuff#(12) camera angle Y#
snapbuff#(13) camera angle Z#
snapbuff#(14) camera position X#
snapbuff#(15) camera position Y#
snapbuff#(16) camera position Z#
snapbuff#(17) home of the list of objects excluded
.
.
.
snapbuff#(16+1024) end of the list of objects excluded
remend
enjoy!
@+
DirectX 9.0c (February 2010)/ DBClassic v1.20