]> git.tdb.fi Git - libs/gl.git/blob - shaderlib/common.glsl
Add a separate constant to enable clipping in standard shaders
[libs/gl.git] / shaderlib / common.glsl
1 import msp_interface;
2 import shadow;
3
4 uniform EnvMap
5 {
6         mat3 env_eye_matrix;
7 };
8
9 uniform sampler2D normal_map;
10 uniform samplerCube environment_map;
11
12 layout(constant_id=auto) const bool use_normal_map = false;
13
14 #pragma MSP stage(vertex)
15 virtual vec4 get_vertex_position()
16 {
17         return vertex;
18 }
19
20 virtual vec3 get_vertex_normal()
21 {
22         return normal;
23 }
24
25 virtual vec4 transform_position(vec4 pos)
26 {
27         return eye_obj_matrix*pos;
28 }
29
30 virtual vec3 transform_normal(vec3 nor)
31 {
32         return eye_obj_normal_matrix*nor;
33 }
34
35 void standard_transform()
36 {
37         out vec4 eye_vertex = transform_position(get_vertex_position());
38         gl_Position = projection_matrix*eye_vertex;
39
40         out vec3 eye_normal = transform_normal(get_vertex_normal());
41         vec3 eye_tangent = transform_normal(tangent);
42         vec3 eye_binormal = transform_normal(binormal);
43         out mat3 eye_tbn_matrix = mat3(eye_tangent, eye_binormal, eye_normal);
44
45         out vec3 eye_look_dir = normalize(eye_vertex.xyz);
46         out vec3 tbn_look_dir = eye_look_dir*eye_tbn_matrix;
47
48         out vec3 eye_light_dir = normalize(light_sources[0].position.xyz-eye_vertex.xyz*light_sources[0].position.w);
49         out vec3 tbn_light_dir = eye_light_dir*eye_tbn_matrix;
50
51         out vec3 eye_halfway_dir = normalize(eye_light_dir-eye_look_dir);
52         out vec3 tbn_halfway_dir = eye_halfway_dir*eye_tbn_matrix;
53
54         out vec3 tbn_zenith_dir = eye_zenith_dir*eye_tbn_matrix;
55         out float fog_coord = eye_vertex.z;
56
57         if(use_clipping)
58         {
59                 for(int i=0; i<max_clip_planes; ++i)
60                         gl_ClipDistance[i] = dot(eye_vertex, clip_planes[i].equation);
61         }
62
63         shadow_transform(eye_vertex);
64 }
65
66 virtual void custom_transform()
67 {
68 }
69
70 void main()
71 {
72         standard_transform();
73         custom_transform();
74         passthrough;
75 }
76
77 #pragma MSP stage(fragment)
78 virtual vec3 get_fragment_normal()
79 {
80         if(use_normal_map)
81                 return normalize(texture(normal_map, texcoord.xy).xyz*2.0-1.0);
82         else
83                 return vec3(0.0, 0.0, 1.0);
84 }
85
86 virtual vec4 get_environment_sample(vec3 direction)
87 {
88         return texture(environment_map, direction);
89 }
90
91 virtual vec3 get_reflection(vec3 normal, vec3 look)
92 {
93         vec3 reflect_dir = reflect(look, normal);
94         if(use_normal_map)
95                 reflect_dir = eye_tbn_matrix*reflect_dir;
96
97         return get_environment_sample(env_eye_matrix*reflect_dir).rgb;
98 }
99
100 vec3 apply_fog(vec3 color)
101 {
102         float fog_value = exp(fog_coord*fog_density);
103         return mix(fog_color.rgb, color, fog_value);
104 }