How to create lightable box?

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

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.

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.

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 ..

Graphics3D 640,480,16,0

Pivot = CreatePivot() 
Camera = CreateCamera( Pivot )
PositionEntity camera, 0, 10,-10
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

pivot=CreatePivot()
camera=CreateCamera(pivot)
PositionEntity camera,0,5,-10
PointEntity camera,pivot

Cube = CreateBottomlessCube()

While Not KeyDown(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

;=====================================================
;=====================================================
;=====================================================

Function CreateBottomLessCube()

	temp = CreateCube()
	ts = GetSurface( temp, 1 )
	
	mesh = CreateMesh() 
	s = CreateSurface( mesh ) 
			
	For v = 0 To 19
		Nv = AddVertex( s , VertexX( ts, v ) , VertexY( ts, v ) , VertexZ( ts, v ) )
		VertexNormal s , Nv , VertexNX( ts, v ) , VertexNY( ts, v ) , VertexNZ( ts, v )
	Next
		
	For t = 0 To 9
		v0 = TriangleVertex( ts , t , 0)
		v1 = TriangleVertex( ts , t , 1)
		v2 = TriangleVertex( ts, t , 2 )
		AddTriangle s, v0, v1, v2
	Next

	FreeEntity temp
	Return Mesh
	
End Function


Should help.

Thank you very much. Gift wrapped in a nice little function too!

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()"?

Yes here it is from sswift,

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

works great on levels with sharp geometry.

Yeah, I've noticed UpdateNormals is buggy, too. It does a fair job in most situations, though.