]> git.tdb.fi Git - libs/gl.git/blob - shaderlib/common.glsl
33fefe8806deb4ddfa7240c5394f1778f082d1c1
[libs/gl.git] / shaderlib / common.glsl
1 import msp_interface;
2 import shadow;
3
4 uniform EnvMap
5 {
6         mat3 env_world_matrix;
7 };
8
9 uniform sampler2D normal_map;
10 uniform samplerCube environment_map;
11 uniform samplerCube irradiance_map;
12
13 layout(constant_id=auto) const bool use_normal_map = false;
14
15 #pragma MSP stage(vertex)
16 virtual vec4 get_vertex_position()
17 {
18         return vertex;
19 }
20
21 virtual vec3 get_vertex_normal()
22 {
23         return normal;
24 }
25
26 virtual mat4 get_vertex_transform()
27 {
28         return world_obj_matrix;
29 }
30
31 virtual mat3 get_normal_transform()
32 {
33         return world_obj_normal_matrix;
34 }
35
36 void standard_transform()
37 {
38         mat4 vertex_tf = get_vertex_transform();
39         mat3 normal_tf = get_normal_transform();
40
41         out vec4 world_vertex = vertex_tf*get_vertex_position();
42         vec4 eye_vertex = eye_world_matrix*world_vertex;
43         gl_Position = clip_eye_matrix*eye_vertex;
44
45         out vec3 world_normal = normal_tf*get_vertex_normal();
46         vec3 world_tangent = normal_tf*tangent;
47         vec3 world_binormal = cross(world_normal, world_tangent);
48         out mat3 world_tbn_matrix = mat3(world_tangent, world_binormal, world_normal);
49
50         vec3 eye_pos = (inverse(eye_world_matrix)*vec4(0.0, 0.0, 0.0, 1.0)).xyz;
51         out vec3 world_look_dir = normalize(world_vertex.xyz-eye_pos);
52
53         out float fog_coord = eye_vertex.z;
54
55         if(use_clipping)
56         {
57                 for(int i=0; i<max_clip_planes; ++i)
58                         gl_ClipDistance[i] = dot(world_vertex, clip_planes[i].equation);
59         }
60 }
61
62 virtual void custom_transform()
63 {
64 }
65
66 void main()
67 {
68         standard_transform();
69         custom_transform();
70         passthrough;
71 }
72
73 #pragma MSP stage(fragment)
74 struct IncomingLight
75 {
76         vec3 direction;
77         vec3 color;
78 };
79
80 virtual vec3 get_fragment_normal()
81 {
82         if(use_normal_map)
83                 return normalize(world_tbn_matrix*(texture(normal_map, texcoord.xy).xyz*2.0-1.0));
84         else
85                 return normalize(world_normal);
86 }
87
88 virtual IncomingLight get_incoming_light(int index, vec3 world_pos)
89 {
90         vec4 light_pos = light_sources[index].position;
91         vec3 rel_pos = light_pos.xyz-world_pos*light_pos.w;
92         float d = length(rel_pos);
93         float attenuation = 1.0/dot(vec3(1.0, d, d*d), light_sources[index].attenuation);
94         return IncomingLight(rel_pos/d, light_sources[index].color*attenuation);
95 }
96
97 virtual vec3 get_environment_sample(vec3 direction, float roughness)
98 {
99         float lod = (2-roughness)*roughness*(textureQueryLevels(environment_map)-1);
100         return textureLod(environment_map, env_world_matrix*direction, lod).rgb;
101 }
102
103 virtual vec3 get_reflection(vec3 normal, vec3 look)
104 {
105         vec3 reflect_dir = reflect(look, normal);
106         return get_environment_sample(reflect_dir, 0.0);
107 }
108
109 virtual vec3 get_irradiance_sample(vec3 normal)
110 {
111         return texture(irradiance_map, env_world_matrix*normal).rgb;
112 }
113
114 vec3 apply_fog(vec3 color)
115 {
116         float fog_value = exp(fog_coord*fog_density);
117         return mix(fog_color.rgb, color, fog_value);
118 }