| 1 |
/* |
| 2 |
Copyright 2010-2022 Michalis Kamburelis. |
| 3 |
|
| 4 |
This file is part of "Castle Game Engine". |
| 5 |
|
| 6 |
"Castle Game Engine" is free software; see the file COPYING.txt, |
| 7 |
included in this distribution, for details about the copyright. |
| 8 |
|
| 9 |
"Castle Game Engine" is distributed in the hope that it will be useful, |
| 10 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 12 |
|
| 13 |
---------------------------------------------------------------------------- |
| 14 |
|
| 15 |
Shader code used for adding light source contribution for Material |
| 16 |
(Phong lighting model). |
| 17 |
This is used by both desktop OpenGL and OpenGLES. |
| 18 |
This is used in both Gouraud and Phong shading, |
| 19 |
so it may go either in vertex or fragment shader. |
| 20 |
*/ |
| 21 |
|
| 22 |
/* Punctual (PointLight, DirectionalLight, SpotLight) parameters */ |
| 23 |
#ifdef LIGHT<Light>_TYPE_PUNCTUAL |
| 24 |
/* Light source position (or direction, if not LIGHT<Light>_TYPE_POSITIONAL) |
| 25 |
in eye coordinates. */ |
| 26 |
uniform vec3 castle_LightSource<Light>Position; |
| 27 |
|
| 28 |
/* SpotLight specific parameters */ |
| 29 |
#ifdef LIGHT<Light>_TYPE_SPOT |
| 30 |
uniform vec3 castle_LightSource<Light>SpotDirection; |
| 31 |
uniform float castle_LightSource<Light>SpotCosCutoff; |
| 32 |
#ifdef LIGHT<Light>_HAS_BEAM_WIDTH |
| 33 |
/* In radians. Note that this differs from gl_LightSource[<Light>].spotCutoff |
| 34 |
that is in degrees. */ |
| 35 |
uniform float castle_LightSource<Light>SpotCutoff; |
| 36 |
uniform float castle_LightSource<Light>BeamWidth; |
| 37 |
#endif |
| 38 |
#ifdef LIGHT<Light>_HAS_SPOT_EXPONENT |
| 39 |
uniform float castle_LightSource<Light>SpotExponent; |
| 40 |
#endif |
| 41 |
#endif |
| 42 |
#endif |
| 43 |
|
| 44 |
#ifdef LIGHT<Light>_HAS_AMBIENT |
| 45 |
uniform vec3 castle_LightSource<Light>AmbientColor; |
| 46 |
#endif |
| 47 |
|
| 48 |
uniform vec3 castle_LightSource<Light>Color; |
| 49 |
|
| 50 |
#ifdef LIGHT<Light>_HAS_ATTENUATION |
| 51 |
/* Attenuation: constant, linear, quadratic. */ |
| 52 |
uniform vec3 castle_LightSource<Light>Attenuation; |
| 53 |
#endif |
| 54 |
|
| 55 |
#ifdef LIGHT<Light>_HAS_RADIUS |
| 56 |
uniform float castle_LightSource<Light>Radius; |
| 57 |
#endif |
| 58 |
|
| 59 |
/* EnvironmentLight parameters */ |
| 60 |
#ifdef LIGHT<Light>_TYPE_ENVIRONMENT |
| 61 |
uniform samplerCube diffuseTexture; |
| 62 |
uniform samplerCube specularTexture; |
| 63 |
#endif |
| 64 |
|
| 65 |
/* Add light contribution. |
| 66 |
Note: this never changes color.a. |
| 67 |
*/ |
| 68 |
void PLUG_add_light(inout vec4 color, |
| 69 |
const in vec4 vertex_eye, |
| 70 |
const in vec3 normal_eye, |
| 71 |
const in MaterialInfo material_info) |
| 72 |
{ |
| 73 |
vec3 light_dir; |
| 74 |
|
| 75 |
/* Calculate light_dir */ |
| 76 |
#ifdef LIGHT<Light>_TYPE_PUNCTUAL |
| 77 |
#ifdef LIGHT<Light>_TYPE_POSITIONAL |
| 78 |
light_dir = castle_LightSource<Light>Position - vec3(vertex_eye); |
| 79 |
float distance_to_light = length(light_dir); |
| 80 |
light_dir /= distance_to_light; |
| 81 |
#else |
| 82 |
light_dir = normalize(castle_LightSource<Light>Position); |
| 83 |
#endif |
| 84 |
#else |
| 85 |
light_dir = normal_eye; |
| 86 |
#endif |
| 87 |
|
| 88 |
#ifdef LIGHT<Light>_TYPE_SPOT |
| 89 |
/* Check SpotCosCutoff first, as we want to add nothing |
| 90 |
(not even ambient term) when were outside of spot light cone. */ |
| 91 |
|
| 92 |
float spot_cos = dot(normalize(castle_LightSource<Light>SpotDirection), -light_dir); |
| 93 |
if (spot_cos < castle_LightSource<Light>SpotCosCutoff) |
| 94 |
return; |
| 95 |
#endif |
| 96 |
|
| 97 |
float scale = 1.0; |
| 98 |
/* PLUG: light_scale (scale, normal_eye, light_dir) */ |
| 99 |
|
| 100 |
#ifdef LIGHT<Light>_TYPE_SPOT |
| 101 |
#ifdef LIGHT<Light>_HAS_BEAM_WIDTH |
| 102 |
/* calculate spot following VRML 2.0/X3D idea of beamWidth */ |
| 103 |
float cutOffAngle = castle_LightSource<Light>SpotCutoff; |
| 104 |
if (castle_LightSource<Light>BeamWidth < cutOffAngle) { |
| 105 |
scale *= clamp( |
| 106 |
( acos(spot_cos) - cutOffAngle) / |
| 107 |
(castle_LightSource<Light>BeamWidth - cutOffAngle), |
| 108 |
0.0, 1.0); |
| 109 |
} |
| 110 |
#endif |
| 111 |
|
| 112 |
#ifdef LIGHT<Light>_HAS_SPOT_EXPONENT |
| 113 |
/* calculate spot like fixed-function pipeline, using exponent */ |
| 114 |
scale *= pow(spot_cos, castle_LightSource<Light>SpotExponent); |
| 115 |
#endif |
| 116 |
#endif |
| 117 |
|
| 118 |
#ifdef LIGHT<Light>_HAS_ATTENUATION |
| 119 |
scale /= max(1.0, |
| 120 |
castle_LightSource<Light>Attenuation.x + |
| 121 |
castle_LightSource<Light>Attenuation.y * distance_to_light + |
| 122 |
castle_LightSource<Light>Attenuation.z * distance_to_light * distance_to_light); |
| 123 |
#endif |
| 124 |
|
| 125 |
#ifdef LIGHT<Light>_HAS_RADIUS |
| 126 |
if (distance_to_light >= castle_LightSource<Light>Radius) |
| 127 |
scale = 0.0; |
| 128 |
#endif |
| 129 |
|
| 130 |
/* add ambient term */ |
| 131 |
vec3 light_color = |
| 132 |
#ifdef LIGHT<Light>_HAS_AMBIENT |
| 133 |
castle_LightSource<Light>AmbientColor * material_info.ambient; |
| 134 |
#else |
| 135 |
vec3(0.0); |
| 136 |
#endif |
| 137 |
|
| 138 |
/* add diffuse term */ |
| 139 |
vec3 diffuse = castle_LightSource<Light>Color * material_info.diffuse_alpha.rgb; |
| 140 |
#ifdef LIGHT<Light>_TYPE_ENVIRONMENT |
| 141 |
diffuse *= textureCube(diffuseTexture, light_dir).rgb; |
| 142 |
#endif |
| 143 |
|
| 144 |
float diffuse_factor = max(dot(normal_eye, light_dir), 0.0); |
| 145 |
light_color += diffuse * diffuse_factor; |
| 146 |
|
| 147 |
#ifdef LIGHT<Light>_HAS_SPECULAR |
| 148 |
/* add specular term */ |
| 149 |
/* halfVector is an average of |
| 150 |
- normalize(light position - vertex_eye) (we already have this |
| 151 |
in light_dir) and |
| 152 |
- normalize(camera position - vertex_eye) |
| 153 |
(and camera position == zero in camera space). */ |
| 154 |
vec3 halfVector = normalize(light_dir - normalize(vec3(vertex_eye))); |
| 155 |
if (diffuse_factor != 0.0) { |
| 156 |
vec3 specular_color = castle_LightSource<Light>Color * material_info.specular; |
| 157 |
|
| 158 |
#ifdef LIGHT<Light>_TYPE_ENVIRONMENT |
| 159 |
specular_color *= textureCube(specularTexture, light_dir).rgb; |
| 160 |
#endif |
| 161 |
|
| 162 |
light_color += specular_color * |
| 163 |
pow(max(dot(halfVector, normal_eye), 0.0), material_info.shininess); |
| 164 |
} |
| 165 |
#endif |
| 166 |
|
| 167 |
color.rgb += light_color * scale; |
| 168 |
} |