;VividGL 3D Lite.
;
;(c)antony wells 2004.
;
;---[ Vertex Lighting Module ] -[core]
;
;Vivid lite will recieve 


Const glmaxlights=255 ;
Type environment
	Field light.ent[glmaxlights],lc
	Field lid[20],changed
End Type

Global esys.environment
Global lid[20],lst[20]

Function glInitEnvironment()
	esys=New environment
	glEnable(gl_lighting)
	lid[0]=gl_light1
	lid[1]=gl_light2
	lid[2]=gl_light3
	lid[3]=gl_light4
	lid[4]=gl_light5
	lid[5]=gl_light6
	lid[6]=gl_light7
	lid[7]=gl_light8
End Function

Function Render_AmbientLight(red#,green#,blue#,alpha#=1)
	glFloatE(dsys\amblight,red/255.,green/255.,blue/255.,alpha)
	glLightModelfv gl_light_model_ambient,dsys\amblight
End Function

Function Render_DiffuseLight(red#,green#,blue#,alpha#=1)
	glFloatE(dsys\difflight,red/255.,green/255.,blue/255.,alpha)
	Update_Lights()
End Function

Function glUpdateGlobalLights()
	glLightModelfv gl_light_model_ambient,dsys\amblight
End Function

Function Light_Create(lighttype=1,castshadows=False)
	out.ent =New ent
	Select lighttype
		Case 1
			out\etype =1001
		Case 2 
			out\etype =1002
		Case 3
			out\etype =1003
	End Select
	out\vpiv=CreatePivot()
	out\etype=glelight
	esys\light[esys\lc]=out
	esys\lid[esys\lc]=Light_on(out)
	esys\lc=esys\lc+1
	esys\changed=True

	Return Handle(out)
End Function

Function Light_Ambient(light,red#,green#,blue#)
	in.ent=Object.ent(light)
	in\r=red/255.
	in\g=green/255.
	in\b=blue/255.
	esys\changed=True
End Function

Function Light_Specular(light,redspec#,greenspec#,bluespec#)
	in.ent=Object.ent(light)
	in\sr=redspec/255.
	in\sg=greenspec/255.
	in\sb=bluespec/255.
	esys\changed =True
End Function

Function Light_Diffuse(light,reddiff#,greendiff#,bluediff#)
	in.ent=Object.ent(light)
	in\dr=reddiff/255.
	in\dg=greendiff/255.
	in\db=bluediff/255.
	esys\changed=True
End Function

Function Light_on(in.ent)
	For j=0 To 7
		If Not lst[j]
			glEnable(lid[j])
			lst[j]=True
			Return lid[j]
		EndIf
	Next
End Function

Global lstack[255],light_sc

Function Update_Lights()
	t1=glfloat(0,0,0,0)
	t2=glfloat(0,0,0,0)
	t3=glfloat(0,0,0,0)
	t4=glfloat(0,0,0,0)
	For j=0 To esys\lc-1
		glFloatE(t1,esys\light[j]\x,esys\light[j]\y,esys\light[j]\z,1)
		glFloatE(t2,esys\light[j]\r,esys\light[j]\g,esys\light[j]\b,1)
		glFloatE(t3,esys\light[j]\dr,esys\light[j]\dg,esys\light[j]\db,1)
		glFloatE(t4,esys\light[j]\sr,esys\light[j]\sg,esys\light[j]\sb,1)
		glLightfv(esys\lid[j],gl_position,t1)
		glLightfv(esys\lid[j],gl_ambient,t2)
		glLightfv(esys\lid[j],gl_diffuse,t3)
		glLightfv(esys\lid[j],gl_specular,t4)
	  glEnable(esys\lid[j])
	Next
	freeVar t1
	freeVar t2
	freeVar t3
	freeVar t4
	esys\changed=False
End Function
