How to create lightable box?

Blitz3D Forums/Blitz3D Programming/How to create lightable box?

Shifty Geezer(Posted 2005) [#1]
I've created a bottomless box with vertices, but it doesn't receive vertex lighting. The following is an example with 2 lights, my vertex box, and a Blitz cube. I want the box to be shaded like the cube. Using UpdateNormals() creates...splodgy shading.
Graphics3D 1024,768,32,0
pivot=CreatePivot()
camera=CreateCamera(pivot)
PositionEntity camera,0,5,-10
PointEntity camera,pivot

AmbientLight 120,120,120
light1=CreateLight()
LightColor light1,40,100,255
TurnEntity light1,45,-205,0	
light2=CreateLight()
LightColor light2,255,230,180
TurnEntity light2,45,35,0	
	
mdl_block=CreateMesh()
block_surface=CreateSurface(mdl_block)
vA=AddVertex(block_surface,-1,-1,-1)
vB=AddVertex(block_surface,-1,-1,1)
vC=AddVertex(block_surface,-1,1,-1)
vD=AddVertex(block_surface,-1,1,1)
vE=AddVertex(block_surface,1,-1,-1)
vF=AddVertex(block_surface,1,-1,1)
vG=AddVertex(block_surface,1,1,-1)
vH=AddVertex(block_surface,1,1,1)

AddTriangle (block_surface,vA, vC, vD)
AddTriangle (block_surface,vA, vD, vB)
AddTriangle (block_surface,vB, vD, vH)
AddTriangle (block_surface,vB, vH, vF)
AddTriangle (block_surface,vF, vH, vG)
AddTriangle (block_surface,vF, vG, vE)
AddTriangle (block_surface,vE, vG, vC)
AddTriangle (block_surface,vE, vC, vA)
AddTriangle (block_surface,vC, vG, vH)
AddTriangle (block_surface,vC, vH, vD)

brush=CreateBrush()
BrushColor brush,255,255,255
BrushFX brush,0

PaintEntity mdl_block,brush
FlipMesh mdl_block
PositionEntity mdl_block,-2,0,0

cube=CreateCube()
PositionEntity cube,2,0,0

While Not KeyHit(1)
	If KeyDown(205)
		TurnEntity pivot,0,1.0,0
	ElseIf KeyDown(203)
		TurnEntity pivot,0,-1.0,0
	EndIf
	If KeyDown(200)
		TurnEntity pivot,1.0,0,0
	ElseIf KeyDown(208)
		TurnEntity pivot,-1.0,0,0
	EndIf
	RenderWorld
	Flip
Wend

Use cursor keys to pan around.


big10p(Posted 2005) [#2]
You need to make each side of your cube a separate (unwelded) quad, and then set the normals manually using the VertexNormal command. You can't simply use UpdateNormals as this will smooth them.


Stevie G(Posted 2005) [#3]
To create a cube like the native version you need 24 vertices. Notice that using updatenormals on a normal cube screws it up as the command averages the normals of the vertices which share the same space.
EDIT ... as per Big10p!

Anyway, this is a bottomless box lit as you want ..



Should help.


Shifty Geezer(Posted 2005) [#4]
Thank you very much. Gift wrapped in a nice little function too!


octothorpe(Posted 2005) [#5]
Notice that using updatenormals on a normal cube screws it up as the command averages the normals of the vertices which share the same [position].


There is a function somewhere in the archives which recalculates normals for a mesh, averaging out only connected triangles' normals (meaning that vertices sharing the same position won't be respected.) Using it on a 6 quad cube would produce flatshaded normals. In my experience, it does a better job than UpdateNormals() too - the builtin command sometimes gave me messed up normals that left the occasional triangle all black.

I believe it was called "Caluclate_Normals()"?


Shambler(Posted 2005) [#6]
Yes here it is from sswift,

http://www.blitzbasic.com/codearcs/codearcs.php?code=975

works great on levels with sharp geometry.


big10p(Posted 2005) [#7]
Yeah, I've noticed UpdateNormals is buggy, too. It does a fair job in most situations, though.