One more pseudo-stencilShadow code ...

Blitz3D Forums/Blitz3D Programming/One more pseudo-stencilShadow code ...

The forum is locked. These are the original blitzbasic.com forums, kept so they can still be read. No new topics, replies or edits can be made here. To join in, head over to the Blitz3D+ forums.

This is similar to the first demo we saw
using stencil shadows, but this time it is
completely done with Blitz functions and
it only require two RenderWorld()'s.

I would like to hear if someone get an idea...
I think this might be useful for casting simple
pre-made shadow volume onto geometry ...

Read the comments, they are pretty simple:

Graphics3D 640,480,32,1

AmbientLight 70,70,70

l=CreateLight(2)
	PositionEntity l,-5,5,5
	LightRange l,15

cam=CreateCamera()
	CameraRange cam,.01,200
	CameraClsColor cam,64,64,64

;create some scene elements:
c1=CreateCube()
	EntityFX c1,1
	EntityColor c1,64,64,64
	UpdateNormals c1

c3=CreateCylinder(16)
	EntityFX c3,1
	EntityColor c3,255,255,255
	PositionEntity c3,1.5,.5,1.5
	UpdateNormals c3

pl=CreatePlane(16)
	PositionEntity pl,0,-2,0
;--------------------------------


;the next cube is a simulated shadow-volume
c2=CreateCube()
	ScaleMesh c2,1,1,5
	RotateEntity c2,-30,40,0
	PositionEntity c2,1,0,1
	EntityFX c2,1+16:EntityBlend c2,2
	EntityColor c2,128,128,128
;----------------------------------------------------


img=CreateImage(640,480)

do_render=1

While Not KeyDown(1)

	TurnEntity c2,0,0,.3 ;->turn the shadow-volume


	TurnEntity c3,.1,.2,.3

	
	If KeyDown(200) MoveEntity cam,0,0,.1
	If KeyDown(208) MoveEntity cam,0,0,-.1
	If KeyDown(203) MoveEntity cam,-.1,0,0
	If KeyDown(205) MoveEntity cam,.1,0,0
	TurnEntity cam,MouseYSpeed()/12.0,-MouseXSpeed()/12.0,0
	RotateEntity cam,EntityPitch(cam),EntityYaw(cam),0
	MoveMouse GraphicsWidth()/2,GraphicsHeight()/2

	If KeyHit(57) show=Not show
	If KeyHit(28) do_render=Not do_render

	;\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/
	If do_render=1
		EntityFX c1,1:EntityColor c1,64,64,64 ;<-set up all scene elements with a single color (64,64,64)
		EntityFX c3,1:EntityColor c3,64,64,64
		EntityFX pl,1:EntityColor pl,64,64,64
		
		ShowEntity c2 ;<- show "shadow-volumes"
		RenderWorld
	
			;capture the image two times to mask all
			;the pixels except the pixels from the intersection:
			GrabImage img,0,0:MaskImage img,64,64,64
			
			Color 16,16,16:Rect 0,0,640,480,1
			DrawImage img,0,0
			
			GrabImage img,0,0:MaskImage img,16,16,16
			;--------------------------------------------------------
	
		EntityFX c1,0:EntityColor c1,255,0,0 ;<- restore elements to default colors
		EntityFX c3,0:EntityColor c3,0,0,255
		EntityFX pl,0:EntityColor pl,0,255,0
		
		If Not show
		HideEntity c2 ;<- hide shadow-volumes for the next RenderWorld
		EntityBlend c2,2
		Else
		EntityBlend c2,1:EntityAlpha c2,.25
		EndIf
		
	Else
	EntityFX c1,1:EntityColor c1,64,64,64	
	EntityFX c3,1:EntityColor c3,64,64,64
	EntityFX pl,1:EntityColor pl,64,64,64
	ShowEntity c2:EntityBlend c2,2
	EndIf
	;/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
	

	UpdateWorld
	RenderWorld


	;At last the image with the
	;"intersection-pixels" is drawn in front:
	If do_render
	DrawImage img,0,0
	EndIf
	;------------------------------------------
	
	Color 255,255,255
	Text 10,10,"Use Arrows+Mouse to move"
	Text 10,25,"Press SPACE to hide/show the 'shadow-volume'"
	If do_render=1
	Text 10,40,"Press ENTER to see how the effect is done"
	Else
	Text 10,300,"As you can see now, there are 3 colors on the scene:"
	Text 10,315,"1 for the background and all elements (64,64,64),"
	Text 10,330,"1 for the shadow-volumes (16,16,16),"
	Text 10,345,"and 1 for the intersection between the bodies (32,32,32),"
	Text 10,360,"then I GrabImage() the whole screen and mask"
	Text 10,375,"all the pixels except the the ones from the intersection."
	EndIf
	
	Flip
Wend

MoveMouse 400,300:End