dot 3 lighting

Blitz3D Forums/Blitz3D Programming/dot 3 lighting

here is the code
im trying to grasp the concept, I got the normal mapping program from nvedia, Nice kit, so i need Blitz to utilise it properly. i'll be moving about the map and the boxes are bumped

Global camera,worldpivot
Global campitch#,camyaw#,mvx#,mvy#,mvz#,temp#,vx#,vy#,vz#



Graphics3D 1024,768,0,2
MoveMouse 1024/2,768/2
FlushMouse
SetBuffer BackBuffer( )
If GfxDriverCaps3D()<110 Then RuntimeError "Sorry, your graphics card does not support cubic environment maps."

;camera
camera = CreateCamera( )
PositionEntity camera, 0, 2, -16


;load model
model = LoadMesh("box_1.b3d")
;model = CreateCube()

;RotateMesh model,-90,0,0

PositionEntity model,0,0,0
ScaleEntity model,.1,.1,.1
EntityFX model,1

normaltex = LoadTexture("ibox_norm.bmp",0)
TextureBlend normaltex,4
EntityTexture model, normaltex,0,0


pivot=CreatePivot()
mylight = CreateSphere(2)
light=CreateLight(1,mylight)
LightRange light,200

ScaleEntity mylight,0.1,0.1,0.1
EntityFX mylight,1
PositionEntity mylight,0,0,-8

AmbientLight 100,100,100

norm=1

While Not KeyHit( 1 )

	If KeyHit(57)
		norm=1-norm
		If norm=1
			ShowEntity model
			HideEntity lomodel
		Else
			ShowEntity lomodel
			HideEntity model		
		EndIf
	EndIf

	;PointEntity camera,model
	;MoveEntity camera,.01,0,0
	;TurnEntity camera,-20,0,0

freelook


	If KeyDown(203) Then TurnEntity(model,0,3.1,0)
	If KeyDown(205) Then TurnEntity(model,0,-3.1,0)

	;mylight
	rot#=rot#+1.5
	
	rrot = EntityYaw(model)
	
	xrot#=Cos(rot)*8
	zrot#=Sin(rot)*8
	yrot#=Cos(rot/2)*8
	PointEntity mylight,model	
	PositionEntity mylight,EntityX(model)+xrot,EntityY(model) + yrot,EntityZ(model)+zrot

	;the dot3 light

	
	PositionEntity pivot, EntityX(model),EntityY(model),EntityZ(model)
	RotateEntity pivot,EntityPitch(mylight),EntityYaw(mylight)+180,0


	
	MoveEntity pivot, 0, 0, 1


	doty# = (EntityY(model)) - EntityY(pivot)

	; then work out the Light possitions :)


	AppTitle dotx


	r#=(EntityX(pivot)+1.0) * 128.0
	g#=(-EntityZ(pivot)+1.0) * 128.0
	b#=(-doty+1.0) * 128.0


	;EntityColor model,r,g,b


	;NormalMapMesh model,0,10,10,0


	entityLight model,mylight,1.6,False
	
	UpdateWorld
	RenderWorld
	
	Text 1024/2,768-16,"- Hit space to toggle -",1,1
	
	If norm
		Text 0,0,"68,176 simulated polygons with normal mapping."
	Else
		Text 0,0,"802 polygons."
	EndIf
	
	Flip
Wend
End


;camera freelook
Function freelook()
	mxspd#=MouseXSpeed()*0.25
	myspd#=MouseYSpeed()*0.25
	;MoveMouse GraphicsWidth()/2,GraphicsHeight()/2
	campitch=campitch+myspd
	If campitch<-85 Then campitch=-85
	If campitch>85 Then campitch=85
	RotateEntity camera,campitch,EntityYaw(camera)-mxspd,0
	;If KeyDown(203) Then mvx=mvx-.3
	;If KeyDown(205) Then mvx=mvx+.3


	
	If KeyDown(200) Then mvz=mvz+.3
	If KeyDown(208) Then mvz=mvz-.3
	If KeyDown(30) Then mvx=mvx-.3
	If KeyDown(32) Then mvx=mvx+.3
	If KeyDown(17) Then mvz=mvz+.3
	If KeyDown(31) Then mvz=mvz-.3
	mvx#=mvx*.8
	mvy#=mvy*.8
	mvz#=mvz*.8
	MoveEntity camera,mvx,mvy,mvz
End Function

Function entityLight(entity,light,inten#=0.5,hull=False)
ox#=EntityX(light)
oy#=EntityY(light)
oz#=EntityZ(light)
;inten =0.5
If Not hull inten=1.-inten
tp=CreateCube()
PositionEntity tp,EntityX(light),EntityY(light),EntityZ(light)
TFormPoint ox,oy,oz,tp,entity
;FreeEntity tp
light=tp
PositionEntity light,TFormedX(),TFormedY(),TFormedZ() ;Transpose light into 
dx# =(EntityX(light)-EntityX( entity));*inten
dy# =(EntityY(light)-EntityY( entity));*inten
dz# =(EntityZ(light)-EntityZ( entity));*inten
nl#=Sqr(dx*dx+dy*dy+dz*dz)
nx#=dx/nl
ny#=dy/nl
nz#=dz/nl
nl#=(nl*10.-255.)/255.
nl=1.-nl
If nl>1. nl=1
If nl<0. nl=0
nl=in
nx=nx*inten
ny=ny*inten
nz=nz*inten
;nz=(Sgn(nz))+nz
;nx=(Sgn(nx))+nx
;ny=(Sgn(ny))+ny
;If nx<0 nx
;FreeEntity tp
;EntityColor entity,128.+(128.*nx),128.+(128.*nz),128.+(128.*ny)
EntityColor entity,128.+(128.*nx),128.+(128.*ny),128.+(128.*nz)
;EntityColor entity,256+(256*nx),256.0+(256*ny),256.0+(256*nz)
;PositionEntity light,ox,oy,oz ;return light.
End Function



Function NormalMapMesh(mesh,x#=-1,y#=-1,z#=1,style=0)
If EntityClass(mesh)="Mesh"
	temp=CreatePivot()
	If style=0
		AlignToVector temp,x,y,z,3
		RotateEntity temp,EntityPitch(temp),EntityYaw(temp),0
		EndIf
	For s=1 To CountSurfaces(mesh)
		surf=GetSurface(mesh,s)
		For v=0 To CountVertices(surf)-1
			Select style
				;Directional light
				Case 0
					TFormNormal VertexNX(surf,v),VertexNY(surf,v),VertexNZ(surf,v),mesh,temp
					r#=(1.0-TFormedX())*127.5
					g#=(1.0-TFormedY())*127.5
					b#=(1.0-TFormedZ())*127.5
					If TFormedZ()>0 b=0
				;Point light
				Case 1
					RotateEntity temp,0,0,0
					TFormPoint VertexX(surf,v),VertexY(surf,v),VertexZ(surf,v),mesh,temp
					AlignToVector temp,TFormedX(),TFormedY(),TFormedZ(),3
					RotateEntity temp,EntityPitch(temp),EntityYaw(temp),0
					TFormNormal VertexNX(surf,v),VertexNY(surf,v),VertexNZ(surf,v),mesh,temp	
					r#=(1.0-TFormedX())*127.5
					g#=(1.0-TFormedY())*127.5
					b#=(1.0-TFormedZ())*127.5
					If TFormedZ()>0 b=0
					TFormPoint VertexX(surf,v),VertexY(surf,v),VertexZ(surf,v),mesh,temp
					d#=PointDistance(TFormedX(),TFormedY(),TFormedZ(),0,0,0)
					range#=200
					If d<range
						d#=Sqr((range-d)/range)
						b#=d*255
						Else
						b=0
						EndIf
				End Select
			VertexColor surf,v,r,g,b
			Next
		Next
	FreeEntity temp
	EndIf
For c=1 To CountChildren(mesh)
	normalmapmesh GetChild(mesh,c)
	Next
End Function

Function PointDistance#(Ax#,Ay#,Az#,Bx#,By#,Bz#) 
Return Sqr((ax-bx)^2.0+(ay-by)^2.0+(az-bz)^2.0) 
End Function 


the media files are

http://www.neomancer.co.uk/ibox_norm.zip