Realtime Stencil Shadow Effect

Blitz3D Forums/Blitz3D Programming/Realtime Stencil Shadow Effect

Totally faked shadow effect. I was working on a routine for a demo to test some routines and it didn't work out - but still looks coolish. ;) LEFT and RIGHT arrows move the "light".

	Graphics3D 800,600
	camera=CreateCamera()
	PositionEntity camera,0,2,0
	
	sphere=CreateSphere(64)		;add the shapes in here
	PositionEntity sphere,0,2,10
	cone=CreateCone(32)
	PositionEntity cone,0,3,10
	ScaleEntity cone,1,2,1

	light=CreateLight(2)
	LightConeAngles light,1,90
	PositionEntity light,0,6,10
	plane=CreatePlane()
	EntityFX plane,1
	PositionEntity plane,0,-5,0
	shadowcube=CreateCube()
	ScaleEntity shadowcube,10,.01,10
	PositionEntity shadowcube,0,-2,10
	shadowtex=CreateTexture(128,128)
	EntityTexture shadowcube,shadowtex
	lightsphere=CreateSphere(8,light)
	ScaleEntity lightsphere,.1,.1,.1
	EntityFX lightsphere,1
	camera2=CreateCamera(light)
	CameraViewport camera2,0,0,128,128
	CameraRange camera2,.1,100
	PointEntity camera2,sphere
	HideEntity camera
	shadowimage=CreateImage(128,128)
	SetBuffer BackBuffer()
	While Not KeyHit(1)
		HideEntity lightsphere
		HideEntity shadowcube
		HideEntity camera
		HideEntity plane
		ShowEntity camera2
		If EntityX(light) > 0 Then TurnEntity camera2,0,0,-90 Else TurnEntity camera2,0,0,90
			;main camera doesn't move, so i cheat and just use 0 as the reference
			;rather than figuring out position relative to camera
		CameraClsColor camera2,255,255,255
		AmbientLight 50,50,50
		LightColor light,0,0,0
		UpdateWorld
		RenderWorld
		GrabImage shadowimage,0,0
		MaskImage shadowimage,255,0,255
		ShowEntity shadowcube
		ShowEntity lightsphere
		HideEntity camera2
		ShowEntity camera
		ShowEntity plane
		CameraClsColor camera,0,0,0
		LightColor light,255,255,255
		SetBuffer TextureBuffer(shadowtex)
			Cls
			DrawImage shadowimage,0,0
			SetBuffer BackBuffer()
		PositionEntity shadowcube,-EntityX(light),-2,10
		PointEntity camera2,sphere
		UpdateWorld
		RenderWorld
		Flip
		If KeyDown(203) And EntityX(light)>-9 Then TranslateEntity light,-.1,0,0
		If KeyDown(205) And EntityX(light)<9 Then TranslateEntity light,.1,0,0
		Wend
	End


+BlackD

ps. Yes, I know your projectors are better halo.. but I suck at 3D. :P

Pretty neat! :)

Yeah! and nice Anti-Halo tag too.!

BlackD - Good idead but aren't your shadows facing the wrong direction from the lightsource?

Only problem is that they only render correctly from the X axis. Try shifting it along Z and it'll kill itself.

Like I said, it's faked shadow code that doesn't work. :)

And no Stevie G.. unless ur vid card is doing something strange(?) then the shadows will be on the opposite side of the object from the light, which is correct. If you're getting the shadow on the same side as the light source, then maybe ur vid card isn't rotating meshes correctly? Try commenting out this line If EntityX(light) > 0 Then TurnEntity camera2,0,0,-90 Else TurnEntity camera2,0,0,90 and see if it makes a difference.

+BlackD