Debian Codegraph @

src/scene/glsl/source/main_shading_phong.fs

raw
1 /* Phong shading GLSL fragment shader.
2 Used by ../castlerendererinternalshader.pas to construct the final shader.
3 When you change this file, rerun `make' and then recompile Pascal sources.
4 */
5
6 /* PLUG-DECLARATIONS */
7
8 #ifdef HAS_GEOMETRY_SHADER
9 #define castle_vertex_eye castle_vertex_eye_geoshader
10 #define castle_normal_eye castle_normal_eye_geoshader
11 #endif
12
13 varying vec4 castle_vertex_eye;
14 varying vec3 castle_normal_eye;
15
16 #if defined(COLOR_PER_VERTEX_RGB)
17 varying vec3 castle_ColorPerVertexFragment;
18 #elif defined(COLOR_PER_VERTEX_RGB_ALPHA)
19 varying vec4 castle_ColorPerVertexFragment;
20 #endif
21
22 /* Apply per-vertex color, over the base/diffuse/emissive color + alpha. */
23 vec4 castle_apply_color_per_vertex(vec4 color)
24 {
25 return
26 #if defined(COLOR_PER_VERTEX_REPLACE)
27 #if defined(COLOR_PER_VERTEX_RGB)
28 vec4(castle_ColorPerVertexFragment, color.a);
29 #elif defined(COLOR_PER_VERTEX_RGB_ALPHA)
30 castle_ColorPerVertexFragment;
31 #endif
32 #elif defined(COLOR_PER_VERTEX_MODULATE)
33 #if defined(COLOR_PER_VERTEX_RGB)
34 vec4(castle_ColorPerVertexFragment * color.rgb, color.a);
35 #elif defined(COLOR_PER_VERTEX_RGB_ALPHA)
36 castle_ColorPerVertexFragment * color;
37 #endif
38 #else
39 color;
40 #endif
41 }
42
43 /* Include fragment shader utilities used by both Gouraud and Phong shading. */
44 /* CASTLE-COMMON-CODE */
45
46 void main_texture_apply(inout vec4 fragment_color,
47 const in vec3 normal_eye)
48 {
49 /* PLUG: main_texture_apply (fragment_color, normal_eye) */
50 /* PLUG: texture_apply (fragment_color, normal_eye) */ // deprecated
51 }
52
53 /* CASTLE-LIGHTING-MODEL */
54
55 void main(void)
56 {
57 vec3 normal_eye_fragment = normalize(castle_normal_eye);
58
59 #ifndef CASTLE_BUGGY_FRONT_FACING
60 if (gl_FrontFacing)
61 /* Avoid AMD bug http://forums.amd.com/devforum/messageview.cfm?catid=392&threadid=148827&enterthread=y
62 Observed on fglrx (proprietary ATI Linux driver),
63 with ATI Mobility Radeon HD 4300 (castle computer "czarny"),
64 since Ubuntu 11.4 (fglrx OpenGL version 3.3.10665).
65
66 It causes both (gl_FrontFacing) and (!gl_FrontFacing) to be true...
67 To minimize the number of problems, never use "if (!gl_FrontFacing)",
68 only "if (gl_FrontFacing)".
69
70 See also https://makc3d.wordpress.com/2015/09/17/alternative-to-gl_frontfacing/ ,
71 gl_FrontFacing seems unreliable sometimes.
72 */ ; else
73 normal_eye_fragment = -normal_eye_fragment;
74 #endif
75
76 /* PLUG: fragment_eye_space (castle_vertex_eye, normal_eye_fragment) */
77
78 vec4 fragment_color;
79 calculate_lighting(fragment_color, castle_vertex_eye, normal_eye_fragment);
80
81 /* PLUG: lighting_apply (fragment_color, castle_vertex_eye, normal_eye_fragment) */
82 /* PLUG: steep_parallax_shadow_apply (fragment_color) */
83 /* PLUG: fragment_modify (fragment_color) */
84
85 #ifdef CASTLE_GAMMA_CORRECTION
86 fragment_color.rgb = castle_linear_to_screen(fragment_color.rgb);
87 #else
88 #ifdef CASTLE_TONE_MAPPING
89 fragment_color.rgb = castle_linear_to_screen(fragment_color.rgb);
90 #endif
91 /* Optimization to not call castle_linear_to_screen always:
92 it does nothing when neither CASTLE_GAMMA_CORRECTION nor CASTLE_TONE_MAPPING */
93 #endif
94
95 /* PLUG: fog_apply (fragment_color, normal_eye_fragment) */
96
97 /* NVidia GeForce 450 GTS (kocury) fails to compile a shader when
98 we pass gl_FragColor as inout parameter to functions
99 (testcase even fresnel_and_toon.x3dv).
100 Although on Radeon X1600 (fglrx, chantal) it works OK.
101 So we just use fragment_color everywhere above, and only assign it
102 to gl_FragColor at the end. */
103 gl_FragColor = fragment_color;
104
105 /* PLUG: fragment_end (gl_FragColor) */
106 }