Debian Codegraph @

src/scene/glsl/source/main_shading_gouraud.vs

raw
1 /* Gouraud shading GLSL vertex shader. */
2
3 /* Plug into PLUG-DECLARATIONS-EARLY things that need to be defined
4 before uniforms below, like definition of CASTLE_HAS_NORMALS.
5 TODO: Can we unify this with PLUG-DECLARATIONS? */
6 /* PLUG-DECLARATIONS-EARLY */
7
8 uniform mat4 castle_ModelViewMatrix;
9 uniform mat4 castle_ProjectionMatrix;
10 attribute vec4 castle_Vertex;
11
12 #if defined(CASTLE_HAS_NORMALS)
13 uniform mat3 castle_NormalMatrix;
14 attribute vec3 castle_Normal;
15 #endif
16
17 /* PLUG-DECLARATIONS */
18
19 varying vec4 castle_vertex_eye;
20 varying vec3 castle_normal_eye;
21 varying vec4 castle_Color;
22
23 #if defined(COLOR_PER_VERTEX_RGB)
24 attribute vec3 castle_ColorPerVertex;
25 #elif defined(COLOR_PER_VERTEX_RGB_ALPHA)
26 attribute vec4 castle_ColorPerVertex;
27 #endif
28
29 /* Apply per-vertex color, over the base/diffuse/emissive color + alpha. */
30 vec4 castle_apply_color_per_vertex(vec4 color)
31 {
32 return
33 #if defined(COLOR_PER_VERTEX_REPLACE)
34 #if defined(COLOR_PER_VERTEX_RGB)
35 vec4(castle_ColorPerVertex, color.a);
36 #elif defined(COLOR_PER_VERTEX_RGB_ALPHA)
37 castle_ColorPerVertex;
38 #endif
39 #elif defined(COLOR_PER_VERTEX_MODULATE)
40 #if defined(COLOR_PER_VERTEX_RGB)
41 vec4(castle_ColorPerVertex * color.rgb, color.a);
42 #elif defined(COLOR_PER_VERTEX_RGB_ALPHA)
43 castle_ColorPerVertex * color;
44 #endif
45 #else
46 color;
47 #endif
48 }
49
50 /* Include fragment shader utilities used by both Gouraud and Phong shading. */
51 /* CASTLE-COMMON-CODE */
52
53 /* CASTLE-LIGHTING-MODEL */
54
55 void main(void)
56 {
57 vec4 vertex_object = castle_Vertex;
58 vec3 normal_object =
59 #if defined(CASTLE_HAS_NORMALS)
60 castle_Normal;
61 #else
62 /* When CASTLE_HAS_NORMALS not defined, then TShader.NeedsNormals = false.
63 Renderer may then not define castle_Normal attribute at all,
64 so we cannot use it (using it causes invisible objects on ATI GPUs,
65 even though undefined normal_object value is not used by anything;
66 see
67 https://github.com/castle-engine/castle-engine/issues/462
68 https://trello.com/c/QH9d9A8o/92-bug-unable-to-see-and-use-gizmos )
69 */
70 vec3(0.0, 0.0, 1.0);
71 #endif
72
73 /* PLUG: vertex_object_space_change (vertex_object, normal_object) */
74 /* PLUG: vertex_object_space (vertex_object, normal_object) */
75
76 #ifdef CASTLE_BUGGY_GLSL_READ_VARYING
77 /* use local variables, instead of reading + writing to varying variables,
78 in this case */
79 vec4 temp_castle_vertex_eye;
80 vec3 temp_castle_normal_eye;
81 vec4 temp_castle_Color;
82 #define castle_vertex_eye temp_castle_vertex_eye
83 #define castle_normal_eye temp_castle_normal_eye
84 #define castle_Color temp_castle_Color
85 #endif
86
87 castle_vertex_eye = castle_ModelViewMatrix * vertex_object;
88 castle_normal_eye =
89 #if defined(CASTLE_HAS_NORMALS)
90 normalize(castle_NormalMatrix * normal_object);
91 #else
92 vec3(0.0, 0.0, 1.0);
93 #endif
94
95 /* PLUG: vertex_eye_space (castle_vertex_eye, castle_normal_eye) */
96
97 calculate_lighting(castle_Color, castle_vertex_eye, castle_normal_eye);
98
99 /* PLUG: lighting_apply (castle_Color, castle_vertex_eye, castle_normal_eye) */
100
101 gl_Position = castle_ProjectionMatrix * castle_vertex_eye;
102
103 #ifdef CASTLE_BUGGY_GLSL_READ_VARYING
104 #undef castle_vertex_eye
105 #undef castle_normal_eye
106 #undef castle_Color
107 castle_vertex_eye = temp_castle_vertex_eye;
108 castle_normal_eye = temp_castle_normal_eye;
109 castle_Color = temp_castle_Color;
110 #endif
111 }