| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283 |
- /***
- phong lighting model
- Parameters:
- position: input vertex position in camera coordinates
- normal: input vertex normal in camera coordinates
- camDir: input camera directions
- matAmbient: input material ambient color
- matDiffuse: input material diffuse color
- ambdiff: output ambient+diffuse color
- spec: output specular color
- Uniforms:
- AmbientLightColor[]
- DiffuseLightColor[]
- DiffuseLightPosition[]
- PointLightColor[]
- PointLightPosition[]
- PointLightLinearDecay[]
- PointLightQuadraticDecay[]
- MatSpecularColor
- MatShininess
- *****/
- void phongModel(vec4 position, vec3 normal, vec3 camDir, vec3 matAmbient, vec3 matDiffuse, out vec3 ambdiff, out vec3 spec) {
- vec3 ambientTotal = vec3(0.0);
- vec3 diffuseTotal = vec3(0.0);
- vec3 specularTotal = vec3(0.0);
- #if AMB_LIGHTS>0
- // Ambient lights
- for (int i = 0; i < AMB_LIGHTS; i++) {
- ambientTotal += AmbientLightColor[i] * matAmbient;
- }
- #endif
- #if DIR_LIGHTS>0
- // Directional lights
- for (int i = 0; i < DIR_LIGHTS; i++) {
- // Diffuse reflection
- // DirLightPosition is the direction of the current light
- vec3 lightDirection = normalize(DirLightPosition(i));
- // Calculates the dot product between the light direction and this vertex normal.
- float dotNormal = max(dot(lightDirection, normal), 0.0);
- diffuseTotal += DirLightColor(i) * matDiffuse * dotNormal;
- // Specular reflection
- // Calculates the light reflection vector
- vec3 ref = reflect(-lightDirection, normal);
- if (dotNormal > 0.0) {
- specularTotal += DirLightColor(i) * MatSpecularColor * pow(max(dot(ref, camDir), 0.0), MatShininess);
- }
- }
- #endif
- #if POINT_LIGHTS>0
- // Point lights
- for (int i = 0; i < POINT_LIGHTS; i++) {
- // Common calculations
- // Calculates the direction and distance from the current vertex to this point light.
- vec3 lightDirection = PointLightPosition(i) - vec3(position);
- float lightDistance = length(lightDirection);
- // Normalizes the lightDirection
- lightDirection = lightDirection / lightDistance;
- // Calculates the attenuation due to the distance of the light
- float attenuation = 1.0 / (1.0 + PointLightLinearDecay(i) * lightDistance +
- PointLightQuadraticDecay(i) * lightDistance * lightDistance);
- // Diffuse reflection
- float dotNormal = max(dot(lightDirection, normal), 0.0);
- diffuseTotal += PointLightColor(i) * matDiffuse * dotNormal * attenuation;
- // Specular reflection
- // Calculates the light reflection vector
- vec3 ref = reflect(-lightDirection, normal);
- if (dotNormal > 0.0) {
- specularTotal += PointLightColor(i) * MatSpecularColor *
- pow(max(dot(ref, camDir), 0.0), MatShininess) * attenuation;
- }
- }
- #endif
- // Sets output colors
- ambdiff = ambientTotal + MatEmissiveColor + diffuseTotal;
- spec = specularTotal;
- }
|