]> git.tdb.fi Git - libs/gl.git/blob - shaderlib/common.glsl
300726dc541c0f7d4cfcdfdcdba9094f4662f242
[libs/gl.git] / shaderlib / common.glsl
1 import msp_interface;
2 import shadow;
3
4 layout(set=1) uniform sampler2D normal_map;
5
6 layout(constant_id=auto) const bool use_instancing = false;
7 layout(constant_id=auto) const bool use_normal_map = false;
8
9 #pragma MSP stage(vertex)
10 virtual vec4 get_vertex_position()
11 {
12         return vertex;
13 }
14
15 virtual vec3 get_vertex_normal()
16 {
17         return normal;
18 }
19
20 virtual mat4 get_vertex_transform()
21 {
22         if(use_instancing)
23                 return transpose(mat4(instance_transform[0], instance_transform[1], instance_transform[2], vec4(0.0, 0.0, 0.0, 1.0)));
24         else
25                 return world_obj_matrix;
26 }
27
28 virtual mat3 get_normal_transform()
29 {
30         if(use_instancing)
31                 return transpose(mat3(instance_transform[0].xyz, instance_transform[1].xyz, instance_transform[2].xyz));
32         else
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 = normalize(normal_tf*get_vertex_normal());
46         if(use_normal_map)
47         {
48                 vec3 world_tangent = normalize(normal_tf*tangent);
49                 vec3 world_binormal = normalize(cross(world_normal, world_tangent));
50                 out mat3 world_tbn_matrix = mat3(world_tangent, world_binormal, world_normal);
51         }
52
53         vec3 eye_pos = world_eye_matrix[3].xyz;
54         out vec3 world_look_dir = normalize(world_vertex.xyz-eye_pos);
55
56         out float fog_coord = eye_vertex.z;
57 }
58
59 virtual void custom_transform()
60 {
61 }
62
63 void main()
64 {
65         standard_transform();
66         custom_transform();
67         passthrough;
68 }
69
70 #pragma MSP stage(fragment)
71 struct IncomingLight
72 {
73         vec3 direction;
74         vec3 color;
75 };
76
77 layout(location=0) out vec4 frag_color;
78
79 virtual vec3 get_fragment_normal()
80 {
81         vec3 normal;
82         float sgn = (gl_FrontFacing ? 1.0 : -1.0);
83         if(use_normal_map)
84                 return sgn*normalize(world_tbn_matrix*(texture(normal_map, texcoord.xy).xyz*2.0-1.0));
85         else
86                 return sgn*normalize(world_normal);
87 }
88
89 virtual IncomingLight get_incoming_light(int index, vec3 world_pos)
90 {
91         vec4 light_pos = light_sources[index].position;
92         vec3 rel_pos = light_pos.xyz-world_pos*light_pos.w;
93         float d = length(rel_pos);
94         float attenuation = 1.0/dot(vec3(1.0, d, d*d), light_sources[index].attenuation);
95         return IncomingLight(rel_pos/d, light_sources[index].color*attenuation);
96 }
97
98 vec3 apply_fog(vec3 color)
99 {
100         float fog_value = exp(fog_coord*fog_density);
101         return mix(fog_color.rgb, color, fog_value);
102 }