SG_LIGHTING.GLSL 3.76 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

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
31 32 33 34 35
        #ifdef TWO_SIDED_LIGHTING
  	  float ldotn = abs(clamp(dot(vLightDir, vNormal),-1.0,1.0));
        #else
  	  float ldotn = clamp(dot(vLightDir, vNormal), 0.0, 1.0);
        #endif
36 37 38
	cLight += ldotn * cLightDiffuse * g_cDiffuse;
 
	// specular
39
	vec3 h = normalize(vLightDir - vec3(0,0,-1)); // half vector between light and view direction
40 41 42
        #ifdef TWO_SIDED_LIGHTING
        float hdotn = max(abs(dot(h, vNormal)), 0.001); // small epsilon to avoid undefined pow(0, 0) if shininess is also 0
        #else
43
	float hdotn = max(dot(h, vNormal), 0.001); // small epsilon to avoid undefined pow(0, 0) if shininess is also 0
44
        #endif
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
	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
66 67 68 69 70
        #ifdef TWO_SIDED_LIGHTING
  	  float ldotn = abs(clamp(dot(vLightDir, vNormal),-1.0,1.0));
        #else
  	  float ldotn = clamp(dot(vLightDir, vNormal), 0.0, 1.0);
        #endif
71 72 73
	cLight += ldotn * cLightDiffuse * g_cDiffuse;

	// specular
74
	vec3 h = normalize(vLightDir - vec3(0,0,-1)); // half vector between light and view direction
75 76 77 78 79
        #ifdef TWO_SIDED_LIGHTING
        float hdotn = max(abs(dot(h, vNormal)), 0.001); // small epsilon to avoid undefined pow(0, 0) if shininess is also 0
        #else
        float hdotn = max(dot(h, vNormal), 0.001); // small epsilon to avoid undefined pow(0, 0) if shininess is also 0
        #endif
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
	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
107 108 109 110 111
        #ifdef TWO_SIDED_LIGHTING
  	  float ldotn = abs(clamp(dot(vLightDir, vNormal),-1.0,1.0));
        #else
   	  float ldotn = clamp(dot(vLightDir, vNormal), 0.0, 1.0);
        #endif
112 113 114
	cLight += ldotn * cLightDiffuse * g_cDiffuse;

	// specular
115
	vec3 h = normalize(vLightDir - vec3(0,0,-1)); // half vector between light and view direction
116 117 118 119 120
        #ifdef TWO_SIDED_LIGHTING
        float hdotn = max(abs(dot(h, vNormal)), 0.001); // small epsilon to avoid undefined pow(0, 0) if shininess is also 0
        #else
        float hdotn = max(dot(h, vNormal), 0.001); // small epsilon to avoid undefined pow(0, 0) if shininess is also 0
        #endif
121 122 123 124 125 126 127 128 129 130 131 132 133
	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);
}