| 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 Gouraud 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 |
return ambient * castle_GlobalAmbient; |
| 31 |
} |
| 32 |
|
| 33 |
void calculate_lighting(out vec4 result, const in vec4 vertex_eye, const in vec3 normal_eye) |
| 34 |
{ |
| 35 |
#ifdef LIT |
| 36 |
/* Two-sided lighting in Gouraud shading: |
| 37 |
flip the normal vector to correspond to the face side that we actually see. |
| 38 |
|
| 39 |
Note that we don't flip the normal_eye (we only flip the |
| 40 |
normal_for_lighting), as normal_eye may be useful also for other |
| 41 |
calculations, e.g. cubemap reflections, that don't want this flippping |
| 42 |
(testcase: demo-models/cube_environment_mapping/cubemap_generated_in_dynamic_world.x3dv ) |
| 43 |
|
| 44 |
This is commented out, because it's not perfect, and I'm not sure can |
| 45 |
we efficiently do artifact-free version of two-sided lighting. |
| 46 |
Reproduction of the problem: |
| 47 |
- demo-models/cube_environment_mapping/cubemap_generated_in_dynamic_world.x3dv, |
| 48 |
look at the back side of the box. |
| 49 |
- demo-models/fog/fog_linear, rotate in Examine and look at the thin water |
| 50 |
edges. |
| 51 |
|
| 52 |
The problem: We base our flipping on normal_eye, |
| 53 |
which may be a smoothed (per-vertex) normal vector. |
| 54 |
|
| 55 |
- We cannot calculate here reliably per-face vector (fragment shaders |
| 56 |
can do a trick with dFdx, see |
| 57 |
https://makc3d.wordpress.com/2015/09/17/alternative-to-gl_frontfacing/ , |
| 58 |
but dFdx is only available in fragment shader). |
| 59 |
|
| 60 |
- Fully-correct solutions are inefficient: |
| 61 |
- To pass to vertex shader a face_normal in a special uniform |
| 62 |
means that we have to pass extra data, and also that we have to |
| 63 |
split vertexes to not share vertexes across faces. |
| 64 |
- Calculating light 2x times and then letting fragment shader to choose |
| 65 |
which side to show (this is what fixed-function does, I think). |
| 66 |
|
| 67 |
- If you're OK with being correct (not fast), you can use Phong shading |
| 68 |
where two-sided lighting works easily. |
| 69 |
*/ |
| 70 |
/* vec3 normal_for_lighting = (normal_eye.z > 0.0 ? normal_eye : -normal_eye); */ |
| 71 |
|
| 72 |
MaterialInfo material_info; |
| 73 |
|
| 74 |
material_info.ambient = castle_MaterialAmbient; |
| 75 |
/* PLUG: material_ambient (material_info.ambient) */ |
| 76 |
material_info.specular = castle_MaterialSpecular; |
| 77 |
/* PLUG: material_specular (material_info.specular) */ |
| 78 |
material_info.shininess = castle_MaterialShininess; |
| 79 |
/* PLUG: material_shininess (material_info.shininess) */ |
| 80 |
|
| 81 |
material_info.diffuse_alpha = castle_apply_color_per_vertex(castle_MaterialDiffuseAlpha); |
| 82 |
|
| 83 |
result = vec4(get_ambient_color(), material_info.diffuse_alpha.a); |
| 84 |
|
| 85 |
/* PLUG: add_light (result, vertex_eye, normal_eye, material_info) */ |
| 86 |
|
| 87 |
vec3 emissive = castle_MaterialEmissive; |
| 88 |
result.rgb += emissive; |
| 89 |
|
| 90 |
/* Clamp sum of lights colors to be <= 1. See template_phong.fs for comments. */ |
| 91 |
result.rgb = min(result.rgb, 1.0); |
| 92 |
#else |
| 93 |
/* Unlit case. |
| 94 |
See shading_phong.fs for comments when this is used. |
| 95 |
*/ |
| 96 |
result = castle_UnlitColor; |
| 97 |
#endif |
| 98 |
} |