function widget:GetInfo()
  return {
    name      = "Lighting Shader",
    desc      = "Lighting for Spring Units",
    author    = "Argh",
    date      = "March 14, 2010",
    license   = "(C) Wolfe Games, 2010, GPL v. 2",
    layer     = 0,
    enabled   = true -- loaded by default?
  }
end

function widget:Initialize()
	--Spring.SetDrawGround(false)
   if(gl.CreateShader == nil) then
      Spring.Echo("GLSL Light Shader Failed")
      widgetHandler:RemoveWidget()
   end

GLSL_LIGHT_Shader = gl.CreateShader({
    vertex = [[
varying vec4 diffuse;
void main()
{
   gl_Position = ftransform();
   vec3 normal = gl_NormalMatrix * gl_Normal;
   vec3 lightVector = gl_LightSource[1].position.xyz - ((gl_ModelViewMatrix * gl_Vertex).xyz);
   float dist = length(lightVector);
   float attenuation = 1.0 / (gl_LightSource[1].constantAttenuation +
      gl_LightSource[1].linearAttenuation * dist +
      gl_LightSource[1].quadraticAttenuation * dist * dist);
   lightVector = normalize(lightVector);
   float nxDir = max(0.0, dot(normal, lightVector));
   diffuse = (gl_FrontMaterial.diffuse.xyzw) * (nxDir * attenuation);
}

]],
   fragment = [[
   varying vec4 diffuse;
   void main()
   {
      gl_FragColor = (gl_LightSource[1].ambient + diffuse);
   }
    ]],
})

   if (GLSL_LIGHT_Shader == nil) then
	Spring.Echo("GLSL Light Shader Failed"..gl.GetShaderLog())
	widgetHandler:RemoveWidget()
   else
      Spring.Echo("GLSL Light Shader Succeeded")

      UseShader = 1;
   end
end
----------------------------------------------------------------------------------------------------------END INITIALIZE

----------------------------------------------------------------------------------------------------------LIGHT LIST HANDLER
local tinsert = table.insert
local tremove = table.remove
local LightList = {piece, red, green, blue, alpha}
local GetUnitPiecePosDir = Spring.GetUnitPiecePosDir

function widget:RecvLuaMsg(msg, playerID)
	local u, p, r, g, b, a

	if string.find(msg,"CreateLight") then
		u = tonumber(string.match(msg,"u=(%d+);", 1))
		p = tonumber(string.match(msg,"p=(%d+);", 1))
		r = tonumber(string.match(msg,"r=(%d*%p*%d*);", 1))
		g = tonumber(string.match(msg,"g=(%d*%p*%d*);", 1))
		b = tonumber(string.match(msg,"b=(%d*%p*%d*);", 1))
		a = tonumber(string.match(msg,"a=(%d*%p*%d*);", 1))
		tinsert(LightList,u,{piece = p, red = r, green = g, blue = b, alpha = a})
	end

	if string.find(msg,"DestroyLight") then
		u = tonumber(string.match(msg,"u=(%d+);", 1))
		LightList[u] = nil
	end

end
----------------------------------------------------------------------------------------------------------END LIGHT LIST HANDLER

----------------------------------------------------------------------------------------------------------RENDERING	
local x,y,z = 0,0,0
local glDepthTest = gl.DepthTest
local GL_LEQUAL = GL.LEQUAL
local  glCulling =  gl.Culling
local GL_BACK = GL.BACK
local glBlending = gl.Blending
local glLighting = gl.Lighting
local GetUnitPiecePosDir = Spring.GetUnitPiecePosDir
local glLight = gl.Light
local GL_POSITION = GL.POSITION
local glMaterial = gl.Material
local glUseShader = gl.UseShader
local glPolygonOffset = gl.PolygonOffset
local glUnit = gl.Unit
local glResetState = gl.ResetState
local GetVisibleUnits = Spring.GetVisibleUnits
local GetUnitLosState = Spring.GetUnitLosState
local myTeam = Spring.GetLocalAllyTeamID()
local myState

function widget:DrawWorld()
	if UseShader == 1 then
	if WG.visible[1] == nil then 
		return false 
	else
		for _,unitID in ipairs(WG.visible) do
					if LightList[unitID] ~= nil then
						glDepthTest(GL_LEQUAL)
						glCulling(GL_BACK)
						glBlending(true)
						glLighting(true)
						
						x,y,z = GetUnitPiecePosDir(unitID,LightList[unitID].piece)

						glLight(1,GL_POSITION,x or 0,y or 100,z or 0,1)
						glMaterial ({
						ambient	= {0, 0, 0, 0},
						diffuse	= {LightList[unitID].red,LightList[unitID].green,LightList[unitID].blue,LightList[unitID].alpha},
						ambidiff  = {0, 0, 0, 0},
						emission  = {0, 0, 0, 0},
						specular  = {0, 0, 0, 0},
						shininess = 10
						})
						glUseShader(GLSL_LIGHT_Shader)
						glPolygonOffset(-0.5,-0.5)
						glUnit(unitID,true)
						
						glLight(0,false)
						glPolygonOffset(0,0)
						--glResetState()
						glLighting(false)
					end
			end
		end
	end	
end
----------------------------------------------------------------------------------------------------------END