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