PageRenderTime 80ms CodeModel.GetById 38ms app.highlight 2ms RepoModel.GetById 38ms app.codeStats 0ms

/Resources/shaders/pointLightingPixel.fsh

http://github.com/tartiflop/ojgl
Fragment Shader File | 121 lines | 84 code | 30 blank | 7 comment | 8 complexity | 425989609b3660c7426c445291e7d076 MD5 | raw file
  1#define MAX_LIGHTS 8
  2
  3struct Light {
  4	vec4 position;
  5	vec4 ambientColor;
  6	vec4 diffuseColor;
  7	vec4 specularColor;
  8	float attenuation;
  9};
 10
 11struct Material {
 12	vec4 ambientColor;
 13	vec4 diffuseColor;
 14	vec4 specularColor;
 15	float shininess;
 16};
 17
 18uniform sampler2D s_texture;
 19uniform bool u_useTexture;
 20
 21uniform Light u_light[MAX_LIGHTS];
 22uniform int u_lightEnabled[MAX_LIGHTS];
 23
 24uniform Material u_material;
 25uniform bool u_lightingEnabled;
 26
 27uniform bool u_includeSpecular;
 28
 29
 30varying vec4 v_ambient;
 31
 32varying vec4 v_ecPosition3;
 33varying vec3 v_normal;
 34varying vec3 v_eye;
 35varying vec2 v_texCoord;
 36
 37const float	c_zero = 0.0;
 38const float	c_one = 1.0;
 39
 40vec4 color;
 41vec4 specular;
 42
 43void pointLight(in int lightIndex,
 44				inout vec4 ambient,
 45				inout vec4 diffuse,
 46				inout vec4 specular) {
 47
 48	float nDotVP;
 49	float eDotRV;
 50	float pf;
 51	float attenuation;
 52	float d;
 53	vec3 VP;
 54	vec3 reflectVector;
 55
 56
 57	// Vector between light position and vertex
 58	VP = vec3(u_light[lightIndex].position - v_ecPosition3);
 59	
 60	// Distance between the two
 61	d = length(VP);
 62	
 63	// Normalise
 64	VP = normalize(VP);
 65	
 66	// Calculate attenuation (only quadratic)
 67	attenuation = c_one / (c_one + u_light[lightIndex].attenuation * d * d);
 68	
 69	// angle between normal and light-vertex vector
 70	nDotVP = max(c_zero, dot(VP, v_normal));
 71	
 72	if (nDotVP > c_zero) {
 73		diffuse += u_light[lightIndex].diffuseColor * nDotVP * attenuation;
 74	
 75		if (u_includeSpecular) {
 76			// reflected vector					
 77			reflectVector = normalize(reflect(-VP, v_normal));
 78			
 79			// angle between eye and reflected vector
 80			eDotRV = max(c_zero, dot(v_eye, reflectVector));
 81			eDotRV = pow(eDotRV, 16.0);
 82		
 83			pf = pow(eDotRV, u_material.shininess);
 84			specular += u_light[lightIndex].specularColor * pf * attenuation;
 85		}
 86	}
 87}
 88
 89void doLighting() {
 90	int i;
 91	vec4 amb = vec4(c_zero);
 92	vec4 diff = vec4(c_zero);
 93	vec4 spec = vec4(c_zero);
 94
 95	if (u_lightingEnabled) {
 96		for (i = int(c_zero); i < MAX_LIGHTS; i++) {
 97			if (u_lightEnabled[i] == 1) {
 98				pointLight(i, amb, diff, spec);
 99			} 	
100		}
101
102		color = v_ambient + diff * u_material.diffuseColor;
103		color.a = u_material.diffuseColor.a;
104		specular = spec * u_material.specularColor;
105		specular.a = u_material.specularColor.a;
106	} else {
107		color = u_material.diffuseColor;
108		specular = spec;
109	}
110}
111
112void main() {
113
114	doLighting();	
115	if (u_useTexture) {
116		color = texture2D(s_texture, v_texCoord) * color;
117	}
118	gl_FragColor = color + specular;
119}
120 
121