Highlights w/ Vertex Lighting??

Blitz3D Forums/Blitz3D Programming/Highlights w/ Vertex Lighting??

memory who has not written the program but this is the version modified from me ;)

no remember

 
Include "fps.bb"

Graphics3D 640,480,16,2
;AmbientLight 0,0,0

Global DS=4
Const NObj=4
Dim sphereobj( NObj, NObj, NObj)

NObj2=NObj+1
Global FactPercent#=100.0/(NObj2*NObj2*NObj2)
Global val#=0

Global Light=CreateSphere(4)
Lightp=CreateLight(2,Light)


;Global Cube = LoadMesh("cubo128.3ds")
;ScaleEntity Cube,20,20,20

val=0
For x=1 To NObj
	For y= 1 To NObj
		For z= 1 To NObj
			sphereobj(x,y,z)=CreateSphere(32)
			EntityPickMode sphereobj(x,y,z),2
			PositionEntity sphereobj(x,y,z),x*DS,y*DS,z*DS
			val=val+FactPercent
			Print "Create Object :"+val
		Next
	Next
Next

For x=1 To NObj
	For y= 1 To NObj
		For z= 1 To NObj
			LightObject(sphereobj(x,y,z), Light)
		Next
	Next
Next



camera=CreateCamera()
PositionEntity camera,12,12,2
RotateEntity camera,0,0,315


Global vrot#=.5
Global vmove#=.2
Global OldTime=timer
Global mla#=.2, mlb#=0

While Not KeyHit(1)

		If KeyDown(63) Then Stop
		If (Not KeyDown(29)) And (Not KeyDown(157)) Then ; CTRL 
			If KeyDown(208) Then TurnEntity camera,vrot,0,0
			If KeyDown(200) Then TurnEntity camera,-vrot,0,0
			If KeyDown(203) Then TurnEntity camera,0,vrot,0
			If KeyDown(205) Then TurnEntity camera,0,-vrot,0
		Else 
			If KeyDown(208) Then MoveEntity camera,0,0,-vmove
			If KeyDown(200) Then MoveEntity camera,0,0, vmove
		EndIf

		mlb=EntityX(Light)
		If mlb>30 Then mla=-mla
		If mlb<0 Then mla=mla

		;PositionEntity Light,mlb,mlb,mlb
		MoveEntity Light,mla,mla,mla		
		PositionEntity Light, EntityX(sphereobj(1,1,1)), EntityY(sphereobj(1,1,1)), EntityZ(sphereobj(1,1,1))
		;LightObject(Cube, Light)

		val=0
;		For x=1 To NObj
;			For y= 1 To NObj
;				For z= 1 To NObj
;					LightObject(sphereobj(x,y,z), Light)
;				Next
;			Next
;		Next

    UpdateWorld
    RenderWorld
		DrawFps()
		Text 5,15 ,TrisRendered()+"   "+EntityX(Light)
    Flip
Wend
End

Function LightObject(entity, Light)
	ApplyAmbient(entity,32,32,32)
                    
	ApplyLight(entity, EntityX(Light), EntityY(Light), EntityZ(Light), 20, 1,255,255,255)                        
	EntityFX entity,3
End Function

Function ApplyAmbient(entity,r,g,b)        
    
    count=CountSurfaces(entity)    
    For n=1 To count
        surf=GetSurface(entity,n)    
        Vcount=CountVertices(surf)-1    
        For v=0 To Vcount
            VertexColor surf,v,r,g,b        
        Next            
    Next

End Function

Function ApplyLight(entity,LX#,LY#,LZ#,radius#,flag,Lr,Lg,Lb)
	
	;Lambert shading in blitz "by Staton Richardson (Arbitrage)"    
	;This function is meant for 1 time lighting not realtime    
	;entity is the entity you wish to light
	;LX, LY, LZ are the coodinates of the light in world space
	;Lr, Lg, Lb are the colors of the light
	;radius is the radius the light affects. Light falls off with distance so half the radius would be half the light
	;flag tells the function to include the current vertex color for multiple lights
	
	Local AttSHW#=0.8
	Local kx#,ky#,kz#

	kx = EntityX(entity)-LX#
	ky = EntityY(entity)-LY#
	kz = EntityZ(entity)-LZ#

	Lrr# = (Lr/radius)
	Lrg# = (Lg/radius)
	Lrb# = (Lb/radius)
	
	count=CountSurfaces(entity) 
	For n=1 To count
		surf=GetSurface(entity,n) 
		Vcount=CountVertices(surf)-1 
		For v=0 To Vcount          
			xd#=VertexX(surf,v)+kx
			yd#=VertexY(surf,v)+ky
			zd#=VertexZ(surf,v)+kz
			mag#=Sqr(xd#*xd# + yd#*yd# + zd#*zd#)
				
			If flag=1
				vcr=VertexRed(surf,v)
				vcg=VertexGreen(surf,v)
				vcb=VertexBlue(surf,v)
			EndIf    
			
			vr=Lr-Lrr*mag#
			vg=Lg-Lrg*mag#
			vb=Lb-Lrb*mag# 
			
			If vr>255 Then vr=255
			If vg>255 Then vg=255
			If vb>255 Then vb=255 
			
			vx#=VertexNX(surf,v)
			vy#=VertexNY(surf,v)
			vz#=VertexNZ(surf,v)
			
			mag#=1/mag#
;			LnX#=xd#*mag#*-1
;			LnY#=yd#*mag#*-1
;			LnZ#=zd#*mag#*-1
			LnX#=-(xd#*mag#)
			LnY#=-(yd#*mag#)
			LnZ#=-(zd#*mag#)
			
			Kh#=((vx)*LnX+(vy)*LnY+(vz)*LnZ)
			
			
			DLx = (LX-xd)
			DLy = (LY-yd)
			DLz = (LZ-zd)
			;Stop
			a = LinePick (xd,yd,zd,DLx,DLy,DLz)
			
			If kh#>0 And a=0
				VertexColor surf,v, vr*Kh#+vcr, vg*Kh#+vcg, vb*Kh#+vcb
			Else
				;VertexColor surf,v, vcr*AttSHW, vcg*AttSHW, vcb*AttSHW
				VertexColor surf,v, 0, 0, 0
			EndIf  
			
		Next   
	Next 
End Function