SG_LIGHTING.GLSL 2.81 KB
Newer Older
1
#ifndef SHININESS
2
#define SHININESS g_vMaterial.x
3 4 5
#endif

#ifndef TRANSPARENCY
6
#define TRANSPARENCY g_vMaterial.y
7 8 9
#endif

#ifndef ALPHA
10
#define ALPHA g_vMaterial.y
11
#endif
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34

vec3 LitPointLight(vec3 vPosition,
				   vec3 vNormal,
				   vec3 vLightPos,
				   vec3 cLightAmbient,
				   vec3 cLightDiffuse,
				   vec3 cLightSpecular,
				   vec3 vLightAtten)
{
	vec3 vLightVS = vPosition - vLightPos;
	float fLen2 = dot(vLightVS, vLightVS);
	float fLen = sqrt(fLen2);
	
	vec3 vLightDir = vLightVS / fLen;
	
	// ambient
	vec3 cLight = cLightAmbient * g_cAmbient;
	
	// diffuse
	float ldotn = clamp(dot(vLightDir, vNormal), 0.0, 1.0);
	cLight += ldotn * cLightDiffuse * g_cDiffuse;
 
	// specular
35
	vec3 h = normalize(vLightDir - vec3(0,0,-1)); // half vector between light and view direction
36
	float hdotn = max(dot(h, vNormal), 0.001); // small epsilon to avoid undefined pow(0, 0) if shininess is also 0
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
	cLight += (pow(hdotn, SHININESS) * cLightSpecular) * g_cSpecular;
	
	
	// attenuate
	float fAtten = vLightAtten.x + vLightAtten.y * fLen + vLightAtten.z * fLen2;
	
	return cLight / fAtten;
}


vec3 LitDirLight(vec3 vPosition,
				 vec3 vNormal,
				 vec3 vLightDir,
				 vec3 cLightAmbient,
				 vec3 cLightDiffuse,
				 vec3 cLightSpecular)
{	
	// ambient
	vec3 cLight = cLightAmbient * g_cAmbient;
	
	// diffuse
	float ldotn = clamp(dot(vLightDir, vNormal), 0.0, 1.0);
	cLight += ldotn * cLightDiffuse * g_cDiffuse;

	// specular
62
	vec3 h = normalize(vLightDir - vec3(0,0,-1)); // half vector between light and view direction
63
	float hdotn = max(dot(h, vNormal), 0.001); // small epsilon to avoid undefined pow(0, 0) if shininess is also 0
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
	cLight += (pow(hdotn, SHININESS) * cLightSpecular) * g_cSpecular;

	return cLight;
}



vec3 LitSpotLight(vec3 vPosition,
				  vec3 vNormal,
				  vec3 vLightPos,
				  vec3 vSpotLightDir,
				  vec3 cLightAmbient,
				  vec3 cLightDiffuse,
				  vec3 cLightSpecular,
				  vec3 vLightAtten,
				  vec2 vLightSpotAngleExp)
{
	vec3 vLightVS = vPosition - vLightPos;
	float fLen2 = dot(vLightVS, vLightVS);
	float fLen = sqrt(fLen2);
	
	vec3 vLightDir = vLightVS / fLen;
	
	// ambient
	vec3 cLight = cLightAmbient * g_cAmbient;
	
	// diffuse
	float ldotn = clamp(dot(vLightDir, vNormal), 0.0, 1.0);
	cLight += ldotn * cLightDiffuse * g_cDiffuse;

	// specular
95
	vec3 h = normalize(vLightDir - vec3(0,0,-1)); // half vector between light and view direction
96
	float hdotn = max(dot(h, vNormal), 0.001); // small epsilon to avoid undefined pow(0, 0) if shininess is also 0
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
	cLight += (pow(hdotn, SHININESS) * cLightSpecular) * g_cSpecular;

	
	
	// attenuate
	float fAtten = vLightAtten.x + vLightAtten.y * fLen + vLightAtten.z * fLen2;
	
	// spot angle falloff
	float fSpot = -dot(vLightDir, vSpotLightDir);
	fSpot *= step(vLightSpotAngleExp.x, fSpot);
	fSpot *= pow(fSpot, vLightSpotAngleExp.y);
	
	return cLight * (fSpot / fAtten);
}