| 1 |
/* |
| 2 |
Copyright 2010-2020 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 |
Calculate Phong lighting model, in Phong shading. */ |
| 16 |
|
| 17 |
uniform vec4 castle_MaterialDiffuseAlpha; |
| 18 |
uniform vec3 castle_MaterialAmbient; |
| 19 |
uniform vec3 castle_MaterialSpecular; |
| 20 |
uniform float castle_MaterialShininess; |
| 21 |
uniform vec3 castle_MaterialEmissive; |
| 22 |
uniform vec3 castle_GlobalAmbient; |
| 23 |
|
| 24 |
uniform vec4 castle_UnlitColor; |
| 25 |
|
| 26 |
/* Material ambient color * global (light model) ambient. */ |
| 27 |
vec3 get_ambient_color() |
| 28 |
{ |
| 29 |
vec3 ambient = castle_MaterialAmbient; |
| 30 |
/* PLUG: material_light_ambient (ambient) */ |
| 31 |
return ambient * castle_GlobalAmbient; |
| 32 |
} |
| 33 |
|
| 34 |
void calculate_lighting(out vec4 result, const in vec4 vertex_eye, const in vec3 normal_eye) |
| 35 |
{ |
| 36 |
#ifdef LIT |
| 37 |
MaterialInfo material_info; |
| 38 |
|
| 39 |
material_info.ambient = castle_MaterialAmbient; |
| 40 |
/* PLUG: material_ambient (material_info.ambient) */ |
| 41 |
material_info.specular = castle_MaterialSpecular; |
| 42 |
/* PLUG: material_specular (material_info.specular) */ |
| 43 |
material_info.shininess = castle_MaterialShininess; |
| 44 |
/* PLUG: material_shininess (material_info.shininess) */ |
| 45 |
|
| 46 |
material_info.diffuse_alpha = castle_apply_color_per_vertex(castle_MaterialDiffuseAlpha); |
| 47 |
|
| 48 |
main_texture_apply(material_info.diffuse_alpha, normal_eye); |
| 49 |
|
| 50 |
result = vec4(get_ambient_color(), material_info.diffuse_alpha.a); |
| 51 |
|
| 52 |
/* PLUG: add_light (result, vertex_eye, normal_eye, material_info) */ |
| 53 |
|
| 54 |
/* PLUG: material_occlusion (result) */ |
| 55 |
|
| 56 |
vec3 emissive = castle_MaterialEmissive; |
| 57 |
/* PLUG: material_emissive (emissive) */ |
| 58 |
result.rgb += emissive; |
| 59 |
|
| 60 |
/* Clamp sum of lights colors to be <= 1. Fixed-function OpenGL does it too. |
| 61 |
This isn't really mandatory, but scenes with many lights could easily |
| 62 |
have colors > 1 and then the textures will look "burned out". |
| 63 |
Of course, for future HDR rendering we will turn this off. */ |
| 64 |
result.rgb = min(result.rgb, 1.0); |
| 65 |
#else |
| 66 |
/* Unlit case. |
| 67 |
This is only an optimization of the "lit" case |
| 68 |
(e.g. no need to do per-light-source processing). |
| 69 |
|
| 70 |
Diffuse color is zero in this case, and we know that it's not set to non-zero |
| 71 |
by castle_ColorPerVertexFragment (since that would make the shape "lit"). |
| 72 |
So we don't use castle_apply_color_per_vertex, |
| 73 |
and we don't apply RGB of main_texture_apply (as it would be multiplied by zero). |
| 74 |
*/ |
| 75 |
result = castle_UnlitColor; |
| 76 |
|
| 77 |
// Apply emissiveTexture, if exists |
| 78 |
/* PLUG: material_emissive (result.rgb) */ |
| 79 |
|
| 80 |
// Apply alpha from the diffuseTexture (or Appearance.texture). |
| 81 |
// We need to call main_texture_apply for this. |
| 82 |
vec4 color_alpha = result; |
| 83 |
main_texture_apply(color_alpha, normal_eye); |
| 84 |
result.a = color_alpha.a; |
| 85 |
#endif |
| 86 |
} |