OLD | NEW |
(Empty) | |
| 1 |
| 2 /* |
| 3 ** Copyright (c) 2012 The Khronos Group Inc. |
| 4 ** |
| 5 ** Permission is hereby granted, free of charge, to any person obtaining a |
| 6 ** copy of this software and/or associated documentation files (the |
| 7 ** "Materials"), to deal in the Materials without restriction, including |
| 8 ** without limitation the rights to use, copy, modify, merge, publish, |
| 9 ** distribute, sublicense, and/or sell copies of the Materials, and to |
| 10 ** permit persons to whom the Materials are furnished to do so, subject to |
| 11 ** the following conditions: |
| 12 ** |
| 13 ** The above copyright notice and this permission notice shall be included |
| 14 ** in all copies or substantial portions of the Materials. |
| 15 ** |
| 16 ** THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 17 ** EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 18 ** MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
| 19 ** IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY |
| 20 ** CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, |
| 21 ** TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE |
| 22 ** MATERIALS OR THE USE OR OTHER DEALINGS IN THE MATERIALS. |
| 23 */ |
| 24 |
| 25 |
| 26 attribute vec4 gtf_Color; |
| 27 attribute vec4 gtf_Vertex; |
| 28 attribute vec3 gtf_Normal; |
| 29 attribute vec4 gtf_MultiTexCoord0; |
| 30 |
| 31 uniform mat4 gtf_ModelViewProjectionMatrix; |
| 32 uniform mat3 gtf_NormalMatrix; |
| 33 |
| 34 varying vec4 gtf_TexCoord[1]; |
| 35 varying vec4 color; |
| 36 |
| 37 vec4 Ambient; |
| 38 vec4 Diffuse; |
| 39 vec4 Specular; |
| 40 |
| 41 const vec3 lightPosition = vec3(0.0, 0.0, 1.0); |
| 42 const vec3 spotDirection = vec3(0.0, 0.0, -1.0); |
| 43 const float spotCutoff = 180.0; |
| 44 const float spotExponent = 0.0; |
| 45 |
| 46 const float lightAttenuationConstant = 1.0; |
| 47 const float lightAttenuationLinear = 0.0; |
| 48 const float lightAttenuationQuadratic = 0.0; |
| 49 |
| 50 const vec4 lightAmbient = vec4(0.0, 0.0, 0.0, 0.0); |
| 51 vec4 lightDiffuse = vec4(1.0, 1.0, 1.0, 1.0); |
| 52 vec4 lightSpecular = vec4(1.0, 1.0, 1.0, 1.0); |
| 53 |
| 54 const float materialShininess = 0.0; |
| 55 |
| 56 const vec4 sceneColor = vec4(0.0, 0.0, 0.0, 0.0); |
| 57 |
| 58 void spotLight(in int i, |
| 59 in vec3 normal, |
| 60 in vec3 eye, |
| 61 in vec3 ecPosition3 |
| 62 ) |
| 63 { |
| 64 float nDotVP; // normal . light direction |
| 65 float nDotHV; // normal . light half vector |
| 66 float pf; // power factor |
| 67 float spotDot; // cosine of angle between spotlight |
| 68 float spotAttenuation; // spotlight attenuation factor |
| 69 float attenuation; // computed attenuation factor |
| 70 float d; // distance from surface to light source |
| 71 vec3 VP; // direction from surface to light position |
| 72 vec3 halfVector; // direction of maximum highlights |
| 73 |
| 74 // Compute vector from surface to light position |
| 75 VP = lightPosition - ecPosition3; |
| 76 |
| 77 // Compute distance between surface and light position |
| 78 d = length(VP); |
| 79 |
| 80 // Normalize the vector from surface to light position |
| 81 VP = normalize(VP); |
| 82 |
| 83 // Compute attenuation |
| 84 attenuation = 1.0 / (lightAttenuationConstant + |
| 85 lightAttenuationLinear * d + |
| 86 lightAttenuationQuadratic * d * d); |
| 87 |
| 88 // See if point on surface is inside cone of illumination |
| 89 spotDot = dot(-VP, normalize(spotDirection)); |
| 90 |
| 91 if (spotDot < cos(radians(spotCutoff))) |
| 92 spotAttenuation = 0.0; // light adds no contribution |
| 93 else |
| 94 spotAttenuation = pow(spotDot, spotExponent); |
| 95 |
| 96 // Combine the spotlight and distance attenuation. |
| 97 attenuation *= spotAttenuation; |
| 98 |
| 99 halfVector = normalize(VP + eye); |
| 100 |
| 101 nDotVP = max(0.0, dot(normal, VP)); |
| 102 nDotHV = max(0.0, dot(normal, halfVector)); |
| 103 |
| 104 if (nDotVP == 0.0) |
| 105 pf = 0.0; |
| 106 else |
| 107 pf = pow(nDotHV, materialShininess); |
| 108 |
| 109 Ambient += lightAmbient * attenuation; |
| 110 Diffuse += lightDiffuse * nDotVP * attenuation; |
| 111 Specular += lightSpecular * pf * attenuation; |
| 112 } |
| 113 |
| 114 vec3 fnormal(void) |
| 115 { |
| 116 //Compute the normal |
| 117 vec3 normal = gtf_NormalMatrix * gtf_Normal; |
| 118 normal = normalize(normal); |
| 119 |
| 120 return normal; |
| 121 } |
| 122 |
| 123 void flight(in vec3 normal, in vec4 ecPosition, float alphaFade) |
| 124 { |
| 125 vec3 ecPosition3; |
| 126 vec3 eye; |
| 127 |
| 128 ecPosition3 = (vec3 (ecPosition)) / ecPosition.w; |
| 129 eye = vec3 (0.0, 0.0, 1.0); |
| 130 |
| 131 // Clear the light intensity accumulators |
| 132 Ambient = vec4 (0.0); |
| 133 Diffuse = vec4 (0.0); |
| 134 Specular = vec4 (0.0); |
| 135 |
| 136 //lightSpecular = gtf_Color; |
| 137 |
| 138 spotLight(0, normal, eye, ecPosition3); |
| 139 |
| 140 color = sceneColor + |
| 141 Ambient * gtf_Color + |
| 142 Diffuse * gtf_Color; |
| 143 color += Specular * gtf_Color; |
| 144 color = clamp( color, 0.0, 1.0 ); |
| 145 |
| 146 color.a *= alphaFade; |
| 147 } |
| 148 |
| 149 void main (void) |
| 150 { |
| 151 vec3 transformedNormal; |
| 152 float alphaFade = 1.0; |
| 153 |
| 154 vec4 ecPosition = gtf_Vertex; |
| 155 |
| 156 color = gtf_Color; |
| 157 gtf_TexCoord[0] = gtf_MultiTexCoord0; |
| 158 gl_Position = gtf_ModelViewProjectionMatrix * gtf_Vertex; |
| 159 transformedNormal = fnormal(); |
| 160 flight(transformedNormal, ecPosition, alphaFade); |
| 161 } |
OLD | NEW |