|
| 1 | +/* |
| 2 | + * This file is part of the Colobot: Gold Edition source code |
| 3 | + * Copyright (C) 2001-2014, Daniel Roux, EPSITEC SA & TerranovaTeam |
| 4 | + * http://epsiteс.ch; http://colobot.info; http://github.com/colobot |
| 5 | + * |
| 6 | + * This program is free software: you can redistribute it and/or modify |
| 7 | + * it under the terms of the GNU General Public License as published by |
| 8 | + * the Free Software Foundation, either version 3 of the License, or |
| 9 | + * (at your option) any later version. |
| 10 | + * |
| 11 | + * This program is distributed in the hope that it will be useful, |
| 12 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 14 | + * See the GNU General Public License for more details. |
| 15 | + * |
| 16 | + * You should have received a copy of the GNU General Public License |
| 17 | + * along with this program. If not, see http://gnu.org/licenses |
| 18 | + */ |
| 19 | + |
| 20 | +// FRAGMENT SHADER |
| 21 | +#version 330 |
| 22 | + |
| 23 | +struct LightParams |
| 24 | +{ |
| 25 | + bool Enabled; |
| 26 | + vec4 Position; |
| 27 | + vec4 Ambient; |
| 28 | + vec4 Diffuse; |
| 29 | + vec4 Specular; |
| 30 | + float Shininess; |
| 31 | + vec3 Attenuation; |
| 32 | +}; |
| 33 | + |
| 34 | +uniform sampler2D uni_PrimaryTexture; |
| 35 | +uniform sampler2D uni_SecondaryTexture; |
| 36 | +uniform sampler2DShadow uni_ShadowTexture; |
| 37 | + |
| 38 | +uniform bool uni_PrimaryTextureEnabled; |
| 39 | +uniform bool uni_SecondaryTextureEnabled; |
| 40 | +uniform bool uni_ShadowTextureEnabled; |
| 41 | + |
| 42 | +uniform bool uni_FogEnabled; |
| 43 | +uniform vec2 uni_FogRange; |
| 44 | +uniform vec4 uni_FogColor; |
| 45 | + |
| 46 | +uniform bool uni_AlphaTestEnabled; |
| 47 | +uniform float uni_AlphaReference; |
| 48 | + |
| 49 | +uniform vec4 uni_AmbientColor; |
| 50 | +uniform vec4 uni_DiffuseColor; |
| 51 | +uniform vec4 uni_SpecularColor; |
| 52 | + |
| 53 | +uniform bool uni_LightingEnabled; |
| 54 | +uniform LightParams uni_Light[8]; |
| 55 | + |
| 56 | +uniform bool uni_SmoothShading; |
| 57 | + |
| 58 | +in VertexData |
| 59 | +{ |
| 60 | + vec3 NormalSmooth; |
| 61 | + flat vec3 NormalFlat; |
| 62 | + vec4 Color; |
| 63 | + vec2 TexCoord0; |
| 64 | + vec2 TexCoord1; |
| 65 | + vec4 ShadowCoord; |
| 66 | + vec4 Position; |
| 67 | + float Distance; |
| 68 | +} data; |
| 69 | + |
| 70 | +out vec4 out_FragColor; |
| 71 | + |
| 72 | +void main() |
| 73 | +{ |
| 74 | + vec4 color = data.Color; |
| 75 | + |
| 76 | + if (uni_LightingEnabled) |
| 77 | + { |
| 78 | + vec4 ambient = vec4(0.0f); |
| 79 | + vec4 diffuse = vec4(0.0f); |
| 80 | + vec4 specular = vec4(0.0f); |
| 81 | + |
| 82 | + for(int i=0; i<8; i++) |
| 83 | + { |
| 84 | + if(uni_Light[i].Enabled) |
| 85 | + { |
| 86 | + ambient += uni_Light[i].Ambient; |
| 87 | + |
| 88 | + vec3 normal = (uni_SmoothShading ? data.NormalSmooth : data.NormalFlat); |
| 89 | + normal = (gl_FrontFacing ? normal : -normal); |
| 90 | + |
| 91 | + vec3 lightDirection = vec3(0.0f); |
| 92 | + float atten; |
| 93 | + |
| 94 | + // Directional light |
| 95 | + if(uni_Light[i].Position[3] == 0.0f) |
| 96 | + { |
| 97 | + lightDirection = uni_Light[i].Position.xyz; |
| 98 | + atten = 1.0f; |
| 99 | + } |
| 100 | + // Point light |
| 101 | + else |
| 102 | + { |
| 103 | + vec3 lightDirection = normalize(uni_Light[i].Position.xyz - data.Position.xyz); |
| 104 | + float dist = distance(uni_Light[i].Position.xyz, data.Position.xyz); |
| 105 | + |
| 106 | + atten = 1.0f / (uni_Light[i].Attenuation.x |
| 107 | + + uni_Light[i].Attenuation.y * dist |
| 108 | + + uni_Light[i].Attenuation.z * dist * dist); |
| 109 | + } |
| 110 | + |
| 111 | + vec3 reflectDirection = -reflect(lightDirection, normal); |
| 112 | + |
| 113 | + diffuse += atten * clamp(dot(normal, lightDirection), 0.0f, 1.0f) * uni_Light[i].Diffuse; |
| 114 | + specular += atten * clamp(pow(dot(normal, lightDirection + reflectDirection), 10.0f), 0.0f, 1.0f) * uni_Light[i].Specular; |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + vec4 result = uni_AmbientColor * ambient |
| 119 | + + uni_DiffuseColor * diffuse |
| 120 | + + uni_SpecularColor * specular; |
| 121 | + |
| 122 | + color.rgb = min(vec3(1.0f), result.rgb); |
| 123 | + color.a = 1.0f; //min(1.0f, 1.0f); |
| 124 | + } |
| 125 | + |
| 126 | + if (uni_PrimaryTextureEnabled) |
| 127 | + { |
| 128 | + color = color * texture(uni_PrimaryTexture, data.TexCoord0); |
| 129 | + } |
| 130 | + |
| 131 | + if (uni_SecondaryTextureEnabled) |
| 132 | + { |
| 133 | + color = color * texture(uni_SecondaryTexture, data.TexCoord1); |
| 134 | + } |
| 135 | + |
| 136 | + if (uni_ShadowTextureEnabled) |
| 137 | + { |
| 138 | + color = color * (0.35f + 0.65f * texture(uni_ShadowTexture, data.ShadowCoord.xyz)); |
| 139 | + } |
| 140 | + |
| 141 | + if (uni_FogEnabled) |
| 142 | + { |
| 143 | + float interpolate = (data.Distance - uni_FogRange.x) / (uni_FogRange.y - uni_FogRange.x); |
| 144 | + |
| 145 | + color = mix(color, uni_FogColor, clamp(interpolate, 0.0f, 1.0f)); |
| 146 | + } |
| 147 | + |
| 148 | + if (uni_AlphaTestEnabled) |
| 149 | + { |
| 150 | + if(color.a < uni_AlphaReference) |
| 151 | + discard; |
| 152 | + } |
| 153 | + |
| 154 | + out_FragColor = color; |
| 155 | +} |
0 commit comments