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