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 uniform mat4 gtf_ModelViewMatrix; |
| 27 uniform mat4 gtf_ModelViewProjectionMatrix; |
| 28 uniform mat3 gtf_NormalMatrix; |
| 29 |
| 30 attribute vec4 gtf_Vertex; |
| 31 attribute vec4 gtf_Color; |
| 32 attribute vec3 gtf_Normal; |
| 33 |
| 34 varying vec4 color; |
| 35 |
| 36 vec4 Ambient; |
| 37 vec4 Diffuse; |
| 38 vec4 Specular; |
| 39 |
| 40 const vec3 lightPosition = vec3(0.0, 0.0, 10.0); |
| 41 const float lightAttenuationConstant = 1.0; |
| 42 const float lightAttenuationLinear = 0.0; |
| 43 const float lightAttenuationQuadratic = 0.0; |
| 44 |
| 45 const vec4 lightAmbient = vec4(0.0, 0.0, 0.0, 0.0); |
| 46 vec4 lightDiffuse = vec4(1.0, 0.0, 0.0, 1.0); |
| 47 |
| 48 const vec4 materialAmbient = vec4(0.0, 0.0, 0.0, 1.0); |
| 49 const vec4 materialDiffuse = vec4(1.0, 1.0, 1.0, 1.0); |
| 50 const vec4 materialSpecular = vec4(0.0, 0.0, 0.0, 0.0); |
| 51 const float materialShininess = 20.0; |
| 52 |
| 53 const vec4 sceneColor = vec4(0.0, 0.0, 0.0, 0.0); |
| 54 |
| 55 |
| 56 void pointLight(in int i, in vec3 normal, in vec3 eye, in vec3 ecPosition3) |
| 57 { |
| 58 float nDotVP; // normal . light direction |
| 59 float nDotHV; // normal . light half vector |
| 60 float pf; // power factor |
| 61 float attenuation; // computed attenuation factor |
| 62 float d; // distance from surface to light source |
| 63 vec3 VP; // direction from surface to light position |
| 64 vec3 halfVector; // direction of maximum highlights |
| 65 |
| 66 // Compute vector from surface to light position |
| 67 VP = lightPosition - ecPosition3; |
| 68 |
| 69 // Compute distance between surface and light position |
| 70 d = length(VP); |
| 71 |
| 72 // Normalize the vector from surface to light position |
| 73 VP = normalize(VP); |
| 74 |
| 75 // Compute attenuation |
| 76 attenuation = 1.0 / (lightAttenuationConstant + |
| 77 lightAttenuationLinear * d + |
| 78 lightAttenuationQuadratic * d * d); |
| 79 |
| 80 halfVector = normalize(VP + eye); |
| 81 |
| 82 nDotVP = max(0.0, dot(normal, VP)); |
| 83 nDotHV = max(0.0, dot(normal, halfVector)); |
| 84 |
| 85 if (nDotVP == 0.0) |
| 86 { |
| 87 pf = 0.0; |
| 88 } |
| 89 else |
| 90 { |
| 91 pf = pow(nDotHV, materialShininess); |
| 92 |
| 93 } |
| 94 Ambient += lightAmbient * attenuation; |
| 95 Diffuse += lightDiffuse * nDotVP * attenuation; |
| 96 // Specular += lightSpecular * pf * attenuation; |
| 97 } |
| 98 |
| 99 vec3 fnormal(void) |
| 100 { |
| 101 //Compute the normal |
| 102 vec3 normal = gtf_Normal * gtf_NormalMatrix; |
| 103 normal = normalize(normal); |
| 104 |
| 105 // This should change to "return normal" but for this test, we force a n
ormal pointing towards the light |
| 106 // return normal |
| 107 return vec3(0.0, 0.0, 1.0); |
| 108 } |
| 109 |
| 110 void flight(in vec3 normal, in vec4 ecPosition, float alphaFade) |
| 111 { |
| 112 vec3 ecPosition3; |
| 113 vec3 eye; |
| 114 |
| 115 ecPosition3 = (vec3 (ecPosition)) / ecPosition.w; |
| 116 eye = vec3 (0.0, 0.0, 1.0); |
| 117 |
| 118 // Clear the light intensity accumulators |
| 119 Ambient = vec4 (0.0); |
| 120 Diffuse = vec4 (0.0); |
| 121 Specular = vec4 (0.0); |
| 122 |
| 123 lightDiffuse = gtf_Color; |
| 124 |
| 125 pointLight(0, normal, eye, ecPosition3); |
| 126 |
| 127 color = sceneColor + |
| 128 Ambient * materialAmbient + |
| 129 Diffuse * materialDiffuse; |
| 130 color += Specular * materialSpecular; |
| 131 color = clamp( color, 0.0, 1.0 ); |
| 132 |
| 133 color.a *= alphaFade; |
| 134 } |
| 135 |
| 136 |
| 137 void main (void) |
| 138 { |
| 139 vec3 transformedNormal; |
| 140 float alphaFade = 1.0; |
| 141 |
| 142 // Eye-coordinate position of vertex, needed in various calculations |
| 143 vec4 ecPosition = gtf_ModelViewMatrix * gtf_Vertex; |
| 144 |
| 145 // Do fixed functionality vertex transform |
| 146 gl_Position = gtf_ModelViewProjectionMatrix * gtf_Vertex; |
| 147 transformedNormal = fnormal(); |
| 148 flight(transformedNormal, ecPosition, alphaFade); |
| 149 } |
OLD | NEW |