[25] | 1 | /****************************************************************************** |
---|
| 2 | Copyright (c) W.J. van der Laan |
---|
| 3 | |
---|
| 4 | Permission is hereby granted, free of charge, to any person obtaining a copy of |
---|
| 5 | this software and associated documentation files (the "Software"), to deal in |
---|
| 6 | the Software without restriction, including without limitation the rights to use, |
---|
| 7 | copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the |
---|
| 8 | Software, and to permit persons to whom the Software is furnished to do so, subject |
---|
| 9 | to the following conditions: |
---|
| 10 | |
---|
| 11 | The above copyright notice and this permission notice shall be included in all copies |
---|
| 12 | or substantial portions of the Software. |
---|
| 13 | |
---|
| 14 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, |
---|
| 15 | INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A |
---|
| 16 | PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT |
---|
| 17 | HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION |
---|
| 18 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,OUT OF OR IN CONNECTION WITH THE |
---|
| 19 | SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
---|
| 20 | ******************************************************************************/ |
---|
| 21 | /** Deferred shading framework |
---|
| 22 | // W.J. :wumpus: van der Laan 2005 // |
---|
| 23 | |
---|
| 24 | Post shader: Multipass, one light |
---|
| 25 | */ |
---|
| 26 | uniform sampler2D tex0; |
---|
| 27 | uniform sampler2D tex1; |
---|
| 28 | |
---|
| 29 | varying vec2 texCoord; |
---|
| 30 | varying vec3 projCoord; |
---|
| 31 | |
---|
| 32 | // Attributes of light |
---|
| 33 | uniform vec4 lightPos0; |
---|
| 34 | uniform vec3 lightDiffuseColor0; |
---|
| 35 | uniform vec3 lightSpecularColor0; |
---|
| 36 | |
---|
| 37 | // Global parameters for lights |
---|
| 38 | struct LightGlobal |
---|
| 39 | { |
---|
| 40 | vec3 position; |
---|
| 41 | vec3 normal; |
---|
| 42 | vec3 viewDir; |
---|
| 43 | }; |
---|
| 44 | |
---|
| 45 | // Current state of light |
---|
| 46 | struct LightAccum |
---|
| 47 | { |
---|
| 48 | vec3 light_diffuse; |
---|
| 49 | vec3 light_specular; |
---|
| 50 | }; |
---|
| 51 | |
---|
| 52 | // Do lighting calculations for one light |
---|
| 53 | void processLight( |
---|
| 54 | inout LightAccum accum, |
---|
| 55 | LightGlobal global, |
---|
| 56 | vec4 lightPos, |
---|
| 57 | vec3 lightDiffuseColor, |
---|
| 58 | vec3 lightSpecularColor) |
---|
| 59 | { |
---|
| 60 | vec3 lightVec = vec3(lightPos) - global.position; |
---|
| 61 | vec3 lightDir = normalize(lightVec); |
---|
| 62 | accum.light_diffuse += max(0.0,dot(lightDir, global.normal)) * lightDiffuseColor; |
---|
| 63 | |
---|
| 64 | vec3 h = normalize(global.viewDir + lightDir); |
---|
| 65 | accum.light_specular += pow(dot(global.normal, h),32.0) * lightSpecularColor; |
---|
| 66 | } |
---|
| 67 | |
---|
| 68 | void main() |
---|
| 69 | { |
---|
| 70 | vec4 a0 = texture2D(tex0, texCoord); // Attribute 0: Diffuse color+shininess |
---|
| 71 | vec4 a1 = texture2D(tex1, texCoord); // Attribute 1: Normal+depth |
---|
| 72 | |
---|
| 73 | // Attributes |
---|
| 74 | vec3 colour = a0.rgb; |
---|
| 75 | float alpha = a0.a; // Specularity |
---|
| 76 | float distance = a1.w; // Distance from viewer -- is zero if no lighting wanted |
---|
| 77 | |
---|
| 78 | LightGlobal global; |
---|
| 79 | global.normal = a1.xyz; |
---|
| 80 | global.position = projCoord*distance; |
---|
| 81 | |
---|
| 82 | // Apply light |
---|
| 83 | LightAccum accum; |
---|
| 84 | accum.light_diffuse = vec3(0,0,0); |
---|
| 85 | accum.light_specular = vec3(0,0,0); |
---|
| 86 | global.viewDir = -normalize(global.position); |
---|
| 87 | |
---|
| 88 | processLight(accum, global, lightPos0, lightDiffuseColor0, lightSpecularColor0); |
---|
| 89 | |
---|
| 90 | // Calcalate total lighting for this fragment |
---|
| 91 | vec3 total_light_contrib; |
---|
| 92 | total_light_contrib = accum.light_diffuse+alpha*accum.light_specular; |
---|
| 93 | |
---|
| 94 | // Calculate colour of fragment |
---|
| 95 | gl_FragColor = vec4( total_light_contrib*colour ,0); |
---|
| 96 | } |
---|