help with normalmap lighting

Blitz3D Forums/Blitz3D Beginners Area/help with normalmap lighting

I have a bug that in this code that I can't figure out:

ClearTextureFilters()

cube = CreateCube()
EntityFX cube, 1+2

bumptex = LoadTexture("normalmap.bmp", 1)
TextureBlend bumptex, 4
EntityTexture cube, bumptex, 0, 0


camera = CreateCamera()
PositionEntity camera, 0, 1.5, -3
PointEntity camera, cube


pivot = CreatePivot()


lightpiv = CreatePivot()
light = CreateSphere(6, lightpiv)
ScaleEntity light, 0.1,0.1, 0.1 
PositionEntity light, 0,2,2




While Not KeyHit(1)	
	If KeyHit(57) Then
		TurnEntity lightpiv, 0, 0.1, 0
	End If
	
	
	s = GetSurface(cube, 1)
	For i = 0 To CountVertices(s)-1
		;Get world coordinates of vertex:
		x# = VertexX(s, i)
		y# = VertexY(s, i)
		z# = VertexZ(s, i)
		TFormPoint x, y, z, cube, 0
		x# = TFormedX()
		y# = TFormedY()
		z# = TFormedZ()
		
		;get true world normal of vertex:
		nx# = VertexNX(s, i)
		ny# = VertexNY(s, i)
		nz# = VertexNZ(s, i)
		TFormNormal nx, ny, nz, cube, 0
		nx# = TFormedX()
		ny# = TFormedY()
		nz# = TFormedZ()
						
		;set position of pivot to vertex and align to normal
		PositionEntity pivot, x, y, z
		AlignToVector pivot, nx, ny, nz, 3
		
		;Get the position of the light relative to pivot:
		TFormPoint 0, 0, 0, light, pivot
		x# = TFormedX()
		y# = TFormedY()
		z# = TFormedZ()
		
		;normalize vector:
		d# = Sqr(x*x + y*y + z*z)
		x = x / d
		y = y / d
		z = z / d
		
		;color vertex according to vector
		r#=(x + 1.0) * 128.0
		g#=(y + 1.0) * 128.0
		b#=(z + 1.0) * 128.0
		VertexColor s, i, r, g, b
	Next	


	SetBuffer BackBuffer()
	Cls
	RenderWorld()
	Color 255,255,0
	Text 1,1, "r=" + Int(r) + ", g=" + Int(g)
	VWait
	Flip
Wend
End


For some reason the transformed position of the light alternates between 2 values each cycle, causing the cube to "flicker".

Can anyone figure out why this is?

it's okay.. . I fixed it by adding this line:

PositionEntity pivot, x, y, z
AlignToVector pivot, nx, ny, nz, 3
RotateEntity pivot, EntityPitch(pivot), EntityYaw(pivot), 0

for some reason the roll was alternating and thinking of it the roll should always be zero anyway. . .