Shadows

Blitz3D Forums/Blitz3D Programming/Shadows

Hi All,
Heres my shadow code. realtime, dynamic specular shadows. If you use the cursors the shadows dynaically change to the direction the light is pointing. If you want to use it, you'll have to fill in the missing files yourself, or send me a mail and I'll forward them on to you to get you started. :) Looks very nice when it's up and running :)

cu,
Mike

;******************
;* *
;* Shadow Casting *
;* *
;* by *
;* *
;* Mike Oakley *
;* *
;******************


Global screenx=320
Global screeny=240

Graphics3D screenx,screeny,16
SetBuffer BackBuffer()

AmbientLight 128,128,128

Global light=CreateLight()
MoveEntity light,0,5,-10

Global shadsizex=128
Global shadsizey=128

Global shadowcamera=CreateCamera()
PositionEntity shadowcamera,0,5,-10 ;x,y,z

RotateEntity shadowcamera,20,0,0

Global sphere=LoadMesh("spotlight.3ds")
Global shadowfloor=LoadMesh("shadowmodel.3ds")
Global softshadow=LoadMesh("soft-shadow.3ds",shadowcamera)
Global stepsFloor=LoadMesh("stepsfloor.3ds")
Global player=LoadMesh("horse.3ds")

Global black=LoadTexture("black.bmp")
Global white=LoadTexture("white.bmp")
Global tex=LoadTexture("texture.bmp")
Global objtexture=LoadTexture("T-PageHead.bmp")
Global Floortexture=LoadTexture("floor2.bmp")

Global shadtexture=CreateTexture(shadsizex,shadsizey)
Global softtexture=CreateTexture(shadsizex,shadsizey)

Global lightring=LoadTexture("lightring.bmp")
Global lightringsprite=CreateSprite(shadowcamera)
SpriteViewMode lightringsprite,1
EntityTexture lightringsprite,lightring

EntityBlend softshadow,2
EntityFX softshadow,1
MoveEntity softshadow,0,0,15
ScaleEntity softshadow,0.15,0.15,0.15
EntityTexture softshadow,shadtexture

MoveEntity lightringsprite,-0.15,0,50
ScaleSprite lightringsprite,50,50

EntityBlend shadowfloor,2
EntityFX shadowfloor,1

EntityFX sphere,1

MoveEntity player,0,-3.3,0
MoveEntity stepsfloor,0,-1,-2
MoveEntity shadowfloor,0,-1,-2

EntityTexture player,tex

EntityTexture stepsfloor,floortexture

;EntityTexture shadowfloor,black
EntityTexture shadowfloor,shadtexture

Global r#

Global pp#
pp#=6

Global gg#
gg#=10

Global ff#
ff#=-8

Global rot#
rot#=20

Global rotb#
rotb#=30

;********
;********
;* Main *
;********
;********

While Not KeyDown(1)

SetBuffer BackBuffer()

yyy#=0
xxx#=0

If KeyDown(205)=1 Then pp#=pp#+0.1
If KeyDown(203)=1 Then pp#=pp#-0.1

If KeyDown(30)=1 Then ff#=ff#+0.1
If KeyDown(44)=1 Then ff#=ff#-0.1

If KeyDown(200)=1 Then gg#=gg#+0.1
If KeyDown(208)=1 Then gg#=gg#-0.1

If KeyDown(2)=1 Then rot#=rot#+1
If KeyDown(3)=1 Then rot#=rot#-1

If KeyDown(4)=1 Then rotb#=rotb#+1
If KeyDown(5)=1 Then rotb#=rotb#-1

If KeyDown(19)=1 Then pp#=0:gg#=5:ff#=-8:rot#=20:rotb#=0

PositionEntity sphere,pp#,gg#,ff#
RotateEntity sphere,20,0,0
RotateEntity sphere,rot#,rotb#,0

r#=r#+1
RotateEntity player,0,r#,0
MoveEntity player,xxx#,yyy#,0

MoveEntity light,pp#,gg#,ff#
RotateEntity light,rot#,rotb#,0

makeshadow()

Flip

Wend

;***************************
;* Camera Mapping Function *
;***************************

Function cameramap(mesh)

Local xx#
Local yy#

For b=1 To CountSurfaces(mesh)
surf=GetSurface(mesh,b)

For a=0 To CountVertices(surf)-1

x#=VertexX(surf,a)
y#=VertexY(surf,a)
z#=VertexZ(surf,a)

TFormPoint x#,y#,z#,mesh,0

x=TFormedX()
y=TFormedY()
z=TFormedZ()

CameraProject shadowcamera,x#,y#,z#

xx#=ProjectedX()
yy#=ProjectedY()

u#=(xx#/shadsizex)
v#=(yy#/shadsizey)

If u#>1 Then u#=1
If v#>1 Then v#=1

If u#<0 Then u#=0
If v#<0 Then v#=0

; VertexTexCoords surf,a,VertexU(surf,a),VertexV(surf,a),0
VertexTexCoords surf,a,u#,v#
Next
Next
End Function

;*******************
;* Make the shadow *
;*******************

Function makeshadow()

ShowEntity player

CameraClsColor shadowcamera,255,255,255

CameraViewport shadowcamera,0,0,shadsizex,shadsizey
PositionEntity shadowcamera,pp#,gg#,ff#
RotateEntity shadowcamera,rot#,rotb#,0

AmbientLight 255,255,255

ShowEntity lightringsprite
HideEntity softshadow
HideEntity stepsfloor
HideEntity shadowfloor
EntityTexture player,black

UpdateWorld
RenderWorld

CameraClsColor shadowcamera,255,255,255

SetBuffer TextureBuffer(softtexture)
CopyRect 0,0,shadsizex,shadsizey,0,0,BackBuffer(),TextureBuffer(softtexture)
EntityTexture player,objtexture

HideEntity lightringsprite
HideEntity player
EntityTexture softshadow,softtexture
ShowEntity softshadow
UpdateWorld
RenderWorld

SetBuffer TextureBuffer(shadtexture)
CopyRect 0,0,shadsizex,shadsizey,0,0,BackBuffer(),TextureBuffer(shadtexture)

cameramap(shadowfloor)

AmbientLight 30,30,30

SetBuffer BackBuffer()

CameraClsColor shadowcamera,0,0,0
HideEntity softshadow
HideEntity lightringsprite
ShowEntity stepsfloor
ShowEntity shadowfloor
ShowEntity player
CameraViewport shadowcamera,0,0,screenx,screeny
PositionEntity shadowcamera,5,10,-17
RotateEntity shadowcamera,30,20,0
; HideEntity player

RenderWorld

End Function