| 1 |
/* Phong shading GLSL vertex 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 into PLUG-DECLARATIONS-EARLY things that need to be defined |
| 7 |
before uniforms below, like definition of CASTLE_HAS_NORMALS. |
| 8 |
TODO: Can we unify this with PLUG-DECLARATIONS? */ |
| 9 |
/* PLUG-DECLARATIONS-EARLY */ |
| 10 |
|
| 11 |
uniform mat4 castle_ModelViewMatrix; |
| 12 |
uniform mat4 castle_ProjectionMatrix; |
| 13 |
attribute vec4 castle_Vertex; |
| 14 |
|
| 15 |
#if defined(CASTLE_HAS_NORMALS) |
| 16 |
uniform mat3 castle_NormalMatrix; |
| 17 |
attribute vec3 castle_Normal; |
| 18 |
#endif |
| 19 |
|
| 20 |
/* PLUG-DECLARATIONS */ |
| 21 |
|
| 22 |
varying vec4 castle_vertex_eye; |
| 23 |
varying vec3 castle_normal_eye; |
| 24 |
|
| 25 |
#if defined(COLOR_PER_VERTEX_RGB) |
| 26 |
attribute vec3 castle_ColorPerVertex; |
| 27 |
varying vec3 castle_ColorPerVertexFragment; |
| 28 |
#elif defined(COLOR_PER_VERTEX_RGB_ALPHA) |
| 29 |
attribute vec4 castle_ColorPerVertex; |
| 30 |
varying vec4 castle_ColorPerVertexFragment; |
| 31 |
#endif |
| 32 |
|
| 33 |
/* Include fragment shader utilities used by both Gouraud and Phong shading. */ |
| 34 |
/* CASTLE-COMMON-CODE */ |
| 35 |
|
| 36 |
void main(void) |
| 37 |
{ |
| 38 |
#ifdef COLOR_PER_VERTEX |
| 39 |
castle_ColorPerVertexFragment = castle_ColorPerVertex; |
| 40 |
#endif |
| 41 |
|
| 42 |
vec4 vertex_object = castle_Vertex; |
| 43 |
vec3 normal_object = |
| 44 |
#if defined(CASTLE_HAS_NORMALS) |
| 45 |
castle_Normal; |
| 46 |
#else |
| 47 |
/* When CASTLE_HAS_NORMALS not defined, then TShader.NeedsNormals = false. |
| 48 |
Renderer may then not define castle_Normal attribute at all, |
| 49 |
so we cannot use it (using it causes invisible objects on ATI GPUs, |
| 50 |
even though undefined normal_object value is not used by anything; |
| 51 |
see |
| 52 |
https://github.com/castle-engine/castle-engine/issues/462 |
| 53 |
https://trello.com/c/QH9d9A8o/92-bug-unable-to-see-and-use-gizmos ) |
| 54 |
*/ |
| 55 |
vec3(0.0, 0.0, 1.0); |
| 56 |
#endif |
| 57 |
|
| 58 |
/* PLUG: vertex_object_space_change (vertex_object, normal_object) */ |
| 59 |
/* PLUG: vertex_object_space (vertex_object, normal_object) */ |
| 60 |
|
| 61 |
castle_vertex_eye = castle_ModelViewMatrix * vertex_object; |
| 62 |
|
| 63 |
castle_normal_eye = |
| 64 |
#if defined(CASTLE_HAS_NORMALS) |
| 65 |
/* Although we will normalize it again in the fragment shader |
| 66 |
(otherwise interpolated result could be shorter < 1, imagine a case |
| 67 |
when normals point the almost opposite directions on the opposite |
| 68 |
vertexes), we also have to normalize it in vertex shader (otherwise |
| 69 |
a much longer normal on one vertex would pull all the interpolated |
| 70 |
normals, thus making their direction invalid in fragment shaders). */ |
| 71 |
normalize(castle_NormalMatrix * normal_object); |
| 72 |
#else |
| 73 |
vec3(0.0, 0.0, 1.0); |
| 74 |
#endif |
| 75 |
|
| 76 |
/* PLUG: vertex_eye_space (castle_vertex_eye, castle_normal_eye) */ |
| 77 |
|
| 78 |
gl_Position = castle_ProjectionMatrix * castle_vertex_eye; |
| 79 |
} |