Forgot to upload the DBP code for these!
Here's the code for GG's shader:
` Simple demo of cubic shadow mapping with two lights.
` This version uses Dark Shader commands and two cube maps.
` A useful reference: http://http.developer.nvidia.com/GPUGems/gpugems_ch12.html
` Created 25 March 2012, edited 14 April 2012.
set display mode desktop width(), desktop height(), 32
sync on : sync rate 60 : sync
shadowMapSize = 512
` setup main camera
backdrop on
autocam off
position camera -100, 20, -50
point camera 50, 20, 50
global cx#
global cy#
cx# = camera angle x()
cy# = camera angle y()
null = make vector4(10)
` load textures
load image "Media/StoneWall02S.bmp", 200
load image "Media/stripes.png", 201
load image "Media/light1.png", 202
load image "Media/light2.png", 203
` load effect
load effect "Media/CubicShadowMappingV2.fx", 1, 0
set vector4 10, 0.2, 0.2, 0.2, 0.0
set effect constant vector 1, "ambientColour", 10
set effect constant float 1, "shadowMapSize", shadowMapSize
` create point light objects
type position
x as float
y as float
z as float
w as float
endtype
dim lightPosition(2) as position
make object sphere 1, 5 ` this is the first light
texture object 1, 202
set object light 1, 0
lightX# = 50.0 : lightY# = 50.0 : lightZ# = 50.0
invLightRange# = 0.004
position object 1, lightX#, lightY#, lightZ#
lightPosition(1).x = lightX#
lightPosition(1).y = lightY#
lightPosition(1).z = lightZ#
lightPosition(1).w = invLightRange#
lightProjScale1# = 0.001 ` this value needs to be defined more clearly
make object sphere 31, 5 ` this is the second light - this will be moved around the scene
texture object 31, 203
set object light 31, 0
lightX# = 50.0 : lightY# = 50.0 : lightZ# = 50.0
position object 31, lightX#, lightY#, lightZ#
lightPosition(2).x = lightX#
lightPosition(2).y = lightY#
lightPosition(2).z = lightZ#
lightPosition(2).w = invLightRange#
lightProjScale2# = 0.001 ` this value needs to be defined more clearly
make object cylinder 11, 48
scale object 11, 5, 100, 5
position object 11, lightPosition(1).x, 24, lightPosition(1).z
texture object 11, 0, 201
set vector4 10, lightPosition(1).x, lightPosition(1).y, lightPosition(1).z, lightPosition(1).w ` 4th entry is reciprocal of light range
set effect constant vector 1, "lightPosition1", 10
set effect constant float 1, "lightProjScale1", lightProjScale1#
set vector4 10, 1.0, 0.5, 0.5, 1.0
set effect constant vector 1, "lightColour1", 10
set vector4 10, lightPosition(2).x, lightPosition(2).y, lightPosition(2).z, lightPosition(2).w
set effect constant vector 1, "lightPosition2", 10
set effect constant float 1, "lightProjScale2", lightProjScale2#
set vector4 10, 0.0, 0.5, 0.5, 1.0
set effect constant vector 1, "lightColour2", 10
set vector4 10, 0.1, 0.1, 0.1, 1.0
set effect constant vector 1, "ambientColour", 10
` test camera to check part of cube map
make camera 1
color backdrop 1, 0
set camera to image 1, 1, shadowMapSize, shadowMapSize
set camera fov 1, 90.0
set camera aspect 1, 1.0
set camera range 1, 1.0, 2001.0
position camera 1, lightPosition(1).x, lightPosition(1).y, lightPosition(1).z
rotate camera 1, 0, 270, 0
testImage = 0
` Dark Shader version - no explicit extra cameras needed
make dynamic cube map 1, shadowMapSize
make dynamic cube map 2, shadowMapSize
` create room
make object cube 2, -500.0
position object 2, 0.0, 250.0, 0.0
texture object 2, 0, 200 ` put the main texture on texture stage 0
set object effect 2, 1
apply cube map to object 2, 1, 1 ` obj id, stage id, cube map id
apply cube map to object 2, 2, 2 ` obj id, stage id, cube map id
scale object texture 2, 0, 8, 8 ` scales the stage 0 UV coordinates
` the room ceiling
make object plain 12, 500, 500
xrotate object 12, 90
position object 12, 0.0, 200.0, 0.0
texture object 12, 0, 200 ` put the main texture on texture stage 0
set object effect 12, 1
apply cube map to object 12, 1, 1 ` obj id, stage id, cube map id
apply cube map to object 12, 2, 2 ` obj id, stage id, cube map id
scale object texture 12, 0, 8, 8 ` scales the stage 0 UV coordinates
` create moving objects
for obj = 3 to 8
make object sphere obj, 8.0, 100, 100
texture object obj, 0, 200
set object effect obj, 1
apply cube map to object obj, 1, 1 ` obj id, stage id, cube map id
apply cube map to object obj, 2, 2 ` obj id, stage id, cube map id
next obj
rotate = 1
` place some fixed objects
make object cone 20, 50
position object 20, 100.0, 0.0, 100.0
texture object 20, 0, 200
set object effect 20, 1
apply cube map to object 20, 1, 1 ` obj id, stage id, cube map id
apply cube map to object 20, 2, 2 ` obj id, stage id, cube map id
make object cube 21, 40
position object 21, 0.0, 0.0, 0.0
texture object 21, 0, 200
set object effect 21, 1
apply cube map to object 21, 1, 1 ` obj id, stage id, cube map id
apply cube map to object 21, 2, 2 ` obj id, stage id, cube map id
make object sphere 22, 70
position object 22, 0.0, 0.0, 100.0
texture object 22, 0, 200
set object effect 22, 1
apply cube map to object 22, 1, 1 ` obj id, stage id, cube map id
apply cube map to object 22, 2, 2 ` obj id, stage id, cube map id
make object cube 23, 50
rotate object 23, 45, 45, 0
position object 23, 100.0, 0.0, 0.0
texture object 23, 0, 200
set object effect 23, 1
apply cube map to object 23, 1, 1 ` obj id, stage id, cube map id
apply cube map to object 23, 2, 2 ` obj id, stage id, cube map id
load object "Media/tiny.x", 25
scale object 25, 10.0, 10.0, 10.0
position object 25, -50.0, 25, -50.0
set object effect 25, 1
apply cube map to object 25, 1, 1 ` obj id, stage id, cube map id
apply cube map to object 25, 2, 2 ` obj id, stage id, cube map id
loop object 25
set object speed 25, 4000
set object effect 11, 1
apply cube map to object 11, 1, 1 ` obj id, stage id, cube map id
apply cube map to object 11, 2, 2 ` obj id, stage id, cube map id
` main program loop
move = 1
repeat
center text screen width()/2, 40, "Press 1 for sample Shadow Map, m to toggle light and sphere movement"
center text screen width()/2, 70, "FPS "+str$(screen fps())
move object 25, -0.45
if object position z(25) < -240 then yrotate object 25, 180
if object position z(25) > 240 then yrotate object 25, 0
`check input
key$ = inkey$()
select key$
case "m"
if keyNow = 0
keyNow = 1
move = 1 - move
endif
endcase
case default
keyNow = 0
endcase
endselect
if move = 1
` move objects and second light
inc angle#, 0.1
for obj = 3 to 8
position object obj, sin(angle# * obj + 30.0 * obj) * 80.0 + lightPosition(1).x, sin(angle# + 20.0 * obj) * 20.0 + lightPosition(1).y, sin(angle# * obj + 35.0 * obj) * 80.0 + lightPosition(1).z
next obj
lightPosition(2).x = sin(angle#) * 200.0
lightPosition(2).y = 60.0
lightPosition(2).z = cos(angle#) * 200.0
position object 31, lightPosition(2).x, lightPosition(2).y, lightPosition(2).z
set vector4 10, lightPosition(2).x, lightPosition(2).y, lightPosition(2).z, lightPosition(2).w
set effect constant vector 1, "lightPosition2", 10
endif
`update cameras
positionCamera()
` render shadow map for first light
set effect technique 1, "shadow1"
hide object 1 ` light camera is inside this so it shouldn't be rendered
show object 31
render dynamic cube map 1, -1, lightPosition(1).x, lightPosition(1).y, lightPosition(1).z ` cube map id, temporary camera id, camX, camY, camZ
` render shadow map for second light using another cube map
set effect technique 1, "shadow2"
show object 1
hide object 31
render dynamic cube map 2, -1, lightPosition(2).x, lightPosition(2).y, lightPosition(2).z ` cube map id, temporary camera id, camX, camY, camZ
` show test image component of cube map
if key$ = "1"
` render test image
set effect technique 1, "shadow1"
position camera 1, lightPosition(1).x, lightPosition(1).y, lightPosition(1).z
sync camera 1
paste image 1, 0, 0
testImage = 1
endif
if key$ = "s" and testImage then save image "test.png", 1 : end ` for testing only
` main scene
set effect technique 1, "scene0"
sync mask 1
show object 2
show object 1
show object 31
sync
until spacekey()
end
function positionCamera()
control camera using arrowkeys 0, 1, 0
cx# = cx# + mousemovey(): cy# = cy# + mousemovex()
if cx# < -45.0
cx# = -45.0
else
if cx# > 45.0 then cx# = 45.0
endif
rotate camera cx#, cy#, 0
endfunction
and here's the code for EVOLVED's shader:
`====================================================
` Cubic Shadow Mapping
`====================================================
` By EVOLVED
` www.evolved-software.com
`====================================================
`Setup Display
load dll "user32.dll",1
Sw=call dll(1,"GetSystemMetrics",0)
Sh=call dll(1,"GetSystemMetrics",1)
delete dll 1
set display mode Sw,Sh,32,1
sync on : sync rate 0
hide mouse
`Set Text
set text font "Verdana"
set text size 15
set text to bold
ink rgb(200,200,0),0
`Setup Camera
backdrop on
color backdrop rgb(128,128,128)
autocam off
position camera 0,0,-150
set camera fov 85
xrotate camera 30
CamDis#=-100
`Load Textures
load image "Media/rockwall.dds",1 : image=1
load image "Media/rockwall_n.dds",2
make camera 1
set camera to image 1,3,512,512
color backdrop 1,rgb(255,255,255)
Make Dynamic Cube Map 1,512
load image "Media/Floor.dds",4 : image=1
load image "Media/Floor_n.dds",5
`Create Object
load effect "FX/CubicShadowMapping.fx",1,0
load object "Media/t-pot.x",1 : object=1
scale object 1,4500,4500,4500
scale object texture 1,0,4,3
texture object 1,0,1
texture object 1,1,2
Apply Cube Map To Object 1,2,1
set object effect 1,1
`Create floor
make object box 2,500,1,500
position object 2,0,-65,0
texture object 2,0,4
texture object 2,1,5
Apply Cube Map To Object 2,2,1
scale object texture 2,0,5,5
set object effect 2,1
`Light Flare
load image "Media/Flare.JPG",20
make object plain 20,50,50
texture object 20,20
ghost object on 20
set object ambient 20,0
vec=make vector4(1)
`Start loop
do
`Control Camera
position camera 0,0,0
if mouseclick()=0
xrotate camera camera angle x()-mousemovey()*0.25
yrotate camera camera angle y()+mousemovex()*0.25
if camera angle x()>80 then xrotate camera 80
if camera angle x()<-60 then xrotate camera -60
endif
if mouseclick()>0 then CamDis#=CamDis#-mousemovey()*0.25
CamDis#=CamDis#+mousemovez()*0.25
if upkey()=1 then CamDis#=CamDis#+2.5
if downkey()=1 then CamDis#=CamDis#-2.5
if CamDis#>-20 then CamDis#=-20
if CamDis#<-300 then CamDis#=-300
move camera CamDis#
if camera position y()<-60 then position camera camera position x(),-60,camera position z()
mousemovex()=0
mousemovey()=0
`Rotate object
if KEYSTATE(19)=1 and rotate=1 and nopress=0 then rotate=0:nopress=1
if KEYSTATE(19)=1 and rotate=0 and nopress=0 then rotate=1:nopress=1
if rotate=0
rotate object 1,0,object angle y(1)+0.5,0
endif
`Move Light
if KEYSTATE(38)=1 and MoveLight=1 and nopress=0 then MoveLight=0:nopress=1
if KEYSTATE(38)=1 and MoveLight=0 and nopress=0 then MoveLight=1:nopress=1
if MoveLight=0
ang#=wrapvalue(ang#+0.25)
position object 20,cos(ang#)*90,80+cos(ang#)*25,sin(ang#)*90
set vector4 1,object position x(20),object position y(20),object position z(20),0
set effect constant vector 1,"LightPosition",1
endif
point object 20,camera position x(),camera position y(),camera position z()
`Change Texture
if KEYSTATE(20)=1 and nopress=0
delete image 1:delete image 2
inc image
if image>4 then image=1
if image=1 then load image "Media/rockwall.dds",1 :load image "Media/rockwall_n.dds",2
if image=2 then load image "Media/wall1.dds",1:load image "Media/wall1_n.dds",2
if image=3 then load image "Media/oldwall3.dds",1:load image "Media/oldwall3_n.dds",2
if image=4 then load image "Media/rock4_1.dds",1 :load image "Media/rock4_1_n.dds",2
texture object 1,0,1
texture object 1,1,2
nopress=1
endif
`Change Object
if Spacekey()=1 and nopress=0
delete object 1
inc object
if object>5 then object=1
if object=1 then load object "Media/t-pot.x",1:scale object 1,4500,4500,4500:scale object texture 1,0,4,3
if object=2 then load object "Media/Tosus.x",1:scale object texture 1,0,1.5,1
if object=3 then make object box 1,75,75,75
if object=4 then make object sphere 1,75,40,40:scale object texture 1,0,2,1
if object=5 then make object cylinder 1,75:scale object texture 1,0,3,1
texture object 1,0,1:texture object 1,1,2:Apply Cube Map To Object 1,2,1:texture object 1,3,3
set object effect 1,1
nopress=1
endif
if scancode()=0 then nopress=0
`Text
center text sw/2,15,"T - Change Texture Space - Change Object R - Rotate Object L - Move Light"
center text sw/2,40,"FPS "+str$(screen fps())
`Render shadow map
set effect technique 1,"DepthMap"
Render Dynamic Cube Map 1,1,object position x(20),object position y(20),object position z(20)
`Main Scene
set effect technique 1,"ShadowMapping"
sync mask 2^0
`End loop
sync
loop
And the code for my effort:
`====================================================
` Cubic Shadow Mapping
`====================================================
` By EVOLVED
`====================================================
`Setup Display
load dll "user32.dll",1
Sw=call dll(1,"GetSystemMetrics",0)
Sh=call dll(1,"GetSystemMetrics",1)
delete dll 1
set display mode Sw,Sh,32,1
sync on : sync rate 0
hide mouse
`Set Text
set text font "Verdana"
set text size 15
set text to bold
ink rgb(200,200,0),0
`Setup Camera
backdrop on
color backdrop rgb(128,128,128)
autocam off
position camera 0,0,-150
set camera fov 85
xrotate camera 30
CamDis#=-100
`Load Textures
load image "Media/rockwall.dds",1 : image=1
load image "Media/rockwall_n.dds",2
make camera 1
set camera to image 1,3,512,512
color backdrop 1,rgb(255,255,255)
Make Dynamic Cube Map 1,512
load image "Media/Floor.dds",4 : image=1
load image "Media/Floor_n.dds",5
//Second camera for light 2
make camera 2
set camera to image 2,6,512,512
color backdrop 2,rgb(255,255,255)
Make Dynamic Cube Map 2,512
`Create Object
load effect "FX/CubicShadowMapping.fx",1,0
load object "Media/t-pot.x",1 : object=1
scale object 1,4500,4500,4500
scale object texture 1,0,4,3
texture object 1,0,1
texture object 1,1,2
Apply Cube Map To Object 1,2,1
//New
Apply Cube Map To Object 1,3,2
set object effect 1,1
`Create floor
make object box 2,500,1,500
position object 2,0,-65,0
texture object 2,0,4
texture object 2,1,5
Apply Cube Map To Object 2,2,1
scale object texture 2,0,5,5
//New
Apply Cube Map To Object 2,3,2
set object effect 2,1
`Light Flare
load image "Media/Flare.JPG",20
make object plain 20,50,50
texture object 20,20
ghost object on 20
set object ambient 20,0
vec=make vector4(1)
//Light 2
Clone Object 21,20
`Start loop
do
`Control Camera
position camera 0,0,0
if mouseclick()=0
xrotate camera camera angle x()-mousemovey()*0.25
yrotate camera camera angle y()+mousemovex()*0.25
if camera angle x()>80 then xrotate camera 80
if camera angle x()<-60 then xrotate camera -60
endif
if mouseclick()>0 then CamDis#=CamDis#-mousemovey()*0.25
CamDis#=CamDis#+mousemovez()*0.25
if upkey()=1 then CamDis#=CamDis#+2.5
if downkey()=1 then CamDis#=CamDis#-2.5
if CamDis#>-20 then CamDis#=-20
if CamDis#<-300 then CamDis#=-300
move camera CamDis#
if camera position y()<-60 then position camera camera position x(),-60,camera position z()
mousemovex()=0
mousemovey()=0
`Rotate object
if KEYSTATE(19)=1 and rotate=1 and nopress=0 then rotate=0:nopress=1
if KEYSTATE(19)=1 and rotate=0 and nopress=0 then rotate=1:nopress=1
if rotate=0
rotate object 1,0,object angle y(1)+0.5,0
endif
`Move Light(s)
if KEYSTATE(38)=1 and MoveLight=1 and nopress=0 then MoveLight=0:nopress=1
if KEYSTATE(38)=1 and MoveLight=0 and nopress=0 then MoveLight=1:nopress=1
if MoveLight=0
ang#=wrapvalue(ang#+0.25)
position object 20,cos(ang#)*90,80+cos(ang#)*25,sin(ang#)*90
set vector4 1,object position x(20),object position y(20),object position z(20),0
set effect constant vector 1,"LPos_1",1
ang2#=wrapvalue(ang#+90)
position object 21,cos(ang2#)*90,80+cos(ang2#)*25,sin(ang2#)*90
set vector4 1,object position x(21),object position y(21),object position z(21),0
set effect constant vector 1,"LPos_2",1
endif
point object 20,camera position x(),camera position y(),camera position z()
point object 21,camera position x(),camera position y(),camera position z()
`Change Texture
if KEYSTATE(20)=1 and nopress=0
delete image 1:delete image 2
inc image
if image>4 then image=1
if image=1 then load image "Media/rockwall.dds",1 :load image "Media/rockwall_n.dds",2
if image=2 then load image "Media/wall1.dds",1:load image "Media/wall1_n.dds",2
if image=3 then load image "Media/oldwall3.dds",1:load image "Media/oldwall3_n.dds",2
if image=4 then load image "Media/rock4_1.dds",1 :load image "Media/rock4_1_n.dds",2
texture object 1,0,1
texture object 1,1,2
nopress=1
endif
`Change Object
if Spacekey()=1 and nopress=0
delete object 1
inc object
if object>5 then object=1
if object=1 then load object "Media/t-pot.x",1:scale object 1,4500,4500,4500:scale object texture 1,0,4,3
if object=2 then load object "Media/Tosus.x",1:scale object texture 1,0,1.5,1
if object=3 then make object box 1,75,75,75
if object=4 then make object sphere 1,75,40,40:scale object texture 1,0,2,1
if object=5 then make object cylinder 1,75:scale object texture 1,0,3,1
texture object 1,0,1:texture object 1,1,2:Apply Cube Map To Object 1,2,1:texture object 1,3,3
set object effect 1,1
nopress=1
endif
if scancode()=0 then nopress=0
`Text
center text sw/2,15,"T - Change Texture Space - Change Object R - Rotate Object L - Move Light"
center text sw/2,40,"FPS "+str$(screen fps())
`Render shadow map(s)
set effect technique 1,"DepthMap_L1"
Render Dynamic Cube Map 1,1,object position x(20),object position y(20),object position z(20)
set effect technique 1,"DepthMap_L2"
Render Dynamic Cube Map 2,2,object position x(21),object position y(21),object position z(21)
//render dynamic cube map 1, -1, lightPosition(1).x, lightPosition(1).y, lightPosition(1).z ` cube map id, temporary camera id, camX, camY, camZ
`Main Scene
set effect technique 1,"ShadowMapping"
sync mask 2^0
//%0001 || %0010
//2^0
`End loop
sync
loop