|
| 1 | +varying vec3 normal; |
| 2 | +varying vec4 pos; |
| 3 | +varying vec4 vertexColor; |
| 4 | + |
| 5 | +uniform sampler2D diffuse; |
| 6 | +uniform sampler2D specular_map; |
| 7 | +uniform sampler2D emit_map; |
| 8 | + |
| 9 | +uniform vec4 diffuse_color; |
| 10 | +uniform vec4 specular_color; |
| 11 | +uniform vec4 ambient_color; |
| 12 | +uniform float shininess; |
| 13 | + |
| 14 | +float calculateAttenuation(in int i, in float dist) |
| 15 | +{ |
| 16 | + return(1.0 / (gl_LightSource[i].constantAttenuation + |
| 17 | + gl_LightSource[i].linearAttenuation * dist + |
| 18 | + gl_LightSource[i].quadraticAttenuation * dist * dist)); |
| 19 | +} |
| 20 | + |
| 21 | +void pointLight(in int i, in vec3 normal, in vec4 pos, inout vec4 diffuse, inout vec4 specular) { |
| 22 | + vec4 color = diffuse_color; |
| 23 | + vec4 matspec = specular_color; |
| 24 | + float shininess = shininess; |
| 25 | + vec4 lightspec = gl_LightSource[i].specular; |
| 26 | + vec4 lpos = gl_LightSource[i].position; |
| 27 | + vec4 s = pos-lpos; |
| 28 | + vec4 sn = -normalize(s); |
| 29 | + |
| 30 | + vec3 light = sn.xyz; |
| 31 | + vec3 n = normalize(normal); |
| 32 | + vec3 r = -reflect(light, n); |
| 33 | + r = normalize(r); |
| 34 | + vec3 v = -pos.xyz; |
| 35 | + v = normalize(v); |
| 36 | + |
| 37 | + float nDotL = dot(n, sn.xyz); |
| 38 | + if(nDotL > 0.0) { |
| 39 | + float dist = length(s); |
| 40 | + float attenuation = calculateAttenuation(i, dist); |
| 41 | + |
| 42 | + diffuse += color * max(0.0, nDotL) * gl_LightSource[i].diffuse * attenuation; |
| 43 | + |
| 44 | + if (shininess != 0.0) { |
| 45 | + specular += lightspec * matspec * pow(max(0.0,dot(r, v)), shininess) * attenuation; |
| 46 | + } |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | + |
| 51 | +void spotLight(in int i, in vec3 normal, in vec4 pos, inout vec4 diffuse, inout vec4 specular) { |
| 52 | + vec4 color = diffuse_color; |
| 53 | + vec4 matspec = specular_color; |
| 54 | + float shininess = shininess; |
| 55 | + vec4 lightspec = gl_LightSource[i].specular; |
| 56 | + vec4 lpos = gl_LightSource[i].position; |
| 57 | + vec4 s = pos-lpos; |
| 58 | + vec4 sn = -normalize(s); |
| 59 | + |
| 60 | + vec3 light = sn.xyz; |
| 61 | + vec3 n = normalize(normal); |
| 62 | + vec3 r = -reflect(light, n); |
| 63 | + r = normalize(r); |
| 64 | + vec3 v = -pos.xyz; |
| 65 | + v = normalize(v); |
| 66 | + |
| 67 | + float cos_outer_cone_angle = (1.0-gl_LightSource[i].spotExponent) * gl_LightSource[i].spotCosCutoff; |
| 68 | + float cos_cur_angle = dot(-normalize(gl_LightSource[i].spotDirection), sn.xyz); |
| 69 | + float cos_inner_cone_angle = gl_LightSource[i].spotCosCutoff; |
| 70 | + |
| 71 | + float cos_inner_minus_outer_angle = cos_inner_cone_angle - cos_outer_cone_angle; |
| 72 | + float spot = 0.0; |
| 73 | + spot = clamp((cos_cur_angle - cos_outer_cone_angle) / cos_inner_minus_outer_angle, 0.0, 1.0); |
| 74 | + |
| 75 | + float nDotL = dot(n, sn.xyz); |
| 76 | + if(nDotL > 0.0) { |
| 77 | + float dist = length(s); |
| 78 | + float attenuation = calculateAttenuation(i, dist); |
| 79 | + diffuse += color * max(0.0, nDotL) * gl_LightSource[i].diffuse * attenuation * spot; |
| 80 | + |
| 81 | + if (shininess != 0.0) { |
| 82 | + specular += lightspec * matspec * pow(max(0.0,dot(r, v)), shininess) * attenuation * spot; |
| 83 | + } |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +void doLights(in int numLights, in vec3 normal, in vec4 pos, inout vec4 diffuse, inout vec4 specular) { |
| 88 | + for (int i = 0; i < numLights; i++) { |
| 89 | + if (gl_LightSource[i].spotCutoff == 180.0) { |
| 90 | + pointLight(i, normal, pos, diffuse, specular); |
| 91 | + } else { |
| 92 | + spotLight(i, normal, pos, diffuse, specular); |
| 93 | + } |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | + |
| 98 | +void main() |
| 99 | +{ |
| 100 | + vec4 diffuse_val = vec4(0.0); |
| 101 | + vec4 specular_val = vec4(0.0); |
| 102 | + doLights(6, normal, pos, diffuse_val, specular_val); |
| 103 | + |
| 104 | + specular_val.xyz *= texture2D(specular_map, gl_TexCoord[0].st).xyz * gl_FrontMaterial.specular.a; |
| 105 | + |
| 106 | + vec4 emitVal = texture2D(emit_map, gl_TexCoord[0].st); |
| 107 | + vec4 texColor = texture2D(diffuse, gl_TexCoord[0].st); |
| 108 | + |
| 109 | + vec4 color = diffuse_val + ambient_color; |
| 110 | + color = clamp((color*vertexColor*texColor) + specular_val, 0.0, 1.0); |
| 111 | + |
| 112 | + // fog |
| 113 | + const float LOG2 = 1.442695; |
| 114 | + float z = gl_FragCoord.z / gl_FragCoord.w; |
| 115 | + float fogFactor = exp2( -gl_Fog.density * |
| 116 | + gl_Fog.density * |
| 117 | + z * |
| 118 | + z * |
| 119 | + LOG2 ); |
| 120 | + |
| 121 | + fogFactor = clamp(fogFactor, 0.0, 1.0); |
| 122 | + |
| 123 | + color = mix(color, texColor, emitVal); |
| 124 | + |
| 125 | + color = mix(gl_Fog.color, color, fogFactor ); |
| 126 | + color.a = vertexColor.a * texColor.a * diffuse_color.a; |
| 127 | + gl_FragColor = color; |
| 128 | + |
| 129 | +} |
0 commit comments