actually render trails
[carveJwlIkooP6JGAAIwe30JlM.git] / shaders / trail.h
1 #ifndef SHADER_trail_H
2 #define SHADER_trail_H
3 static void shader_trail_link(void);
4 static void shader_trail_register(void);
5 static struct vg_shader _shader_trail = {
6 .name = "trail",
7 .link = shader_trail_link,
8 .vs =
9 {
10 .orig_file = "shaders/trail.vs",
11 .static_src =
12 "layout (location=0) in vec4 a_co;\n"
13 "\n"
14 "#line 1 1 \n"
15 "const float k_motion_lerp_amount = 0.01;\n"
16 "\n"
17 "#line 2 0 \n"
18 "\n"
19 "out vec3 aMotionVec0;\n"
20 "out vec3 aMotionVec1;\n"
21 "\n"
22 "void vs_motion_out( vec4 vproj0, vec4 vproj1 )\n"
23 "{\n"
24 " // This magically solves some artifacting errors!\n"
25 " //\n"
26 " vproj1 = vproj0*(1.0-k_motion_lerp_amount) + vproj1*k_motion_lerp_amount;\n"
27 "\n"
28 " aMotionVec0 = vec3( vproj0.xy, vproj0.w );\n"
29 " aMotionVec1 = vec3( vproj1.xy, vproj1.w );\n"
30 "}\n"
31 "\n"
32 "#line 4 0 \n"
33 "\n"
34 "uniform mat4 uPv;\n"
35 "uniform mat4 uPvPrev;\n"
36 "\n"
37 "out float aAlpha;\n"
38 "\n"
39 "void main(){\n"
40 " vec4 vproj0 = uPv * vec4( a_co.xyz, 1.0 );\n"
41 " vec4 vproj1 = uPvPrev * vec4( a_co.xyz, 1.0 );\n"
42 " vs_motion_out( vproj0, vproj1 );\n"
43 "\n"
44 " gl_Position = vproj0;\n"
45 " aAlpha = a_co.w;\n"
46 "}\n"
47 ""},
48 .fs =
49 {
50 .orig_file = "shaders/trail.fs",
51 .static_src =
52 "layout (location = 0) out vec4 oColour;\n"
53 "in float aAlpha;\n"
54 "uniform vec4 uColour;\n"
55 "\n"
56 "#line 1 1 \n"
57 "const float k_motion_lerp_amount = 0.01;\n"
58 "\n"
59 "#line 2 0 \n"
60 "\n"
61 "layout (location = 1) out vec2 oMotionVec;\n"
62 "\n"
63 "in vec3 aMotionVec0;\n"
64 "in vec3 aMotionVec1;\n"
65 "\n"
66 "void compute_motion_vectors()\n"
67 "{\n"
68 " // Write motion vectors\n"
69 " vec2 vmotion0 = aMotionVec0.xy / aMotionVec0.z;\n"
70 " vec2 vmotion1 = aMotionVec1.xy / aMotionVec1.z;\n"
71 "\n"
72 " oMotionVec = (vmotion1-vmotion0) * (1.0/k_motion_lerp_amount);\n"
73 "}\n"
74 "\n"
75 "#line 6 0 \n"
76 "\n"
77 "void main(){\n"
78 " compute_motion_vectors();\n"
79 "\n"
80 " vec2 ssuv = gl_FragCoord.xy;\n"
81 " vec3 vDither = vec3( dot( vec2( 171.0, 231.0 ), ssuv) );\n"
82 " float dither = fract( vDither.g / 71.0 ) - 0.5;\n"
83 "\n"
84 " if( aAlpha+dither<0.5 )\n"
85 " discard;\n"
86 "\n"
87 " oColour = vec4( uColour.rgb, uColour.a * aAlpha );\n"
88 "}\n"
89 ""},
90 };
91
92 static GLuint _uniform_trail_uPv;
93 static GLuint _uniform_trail_uPvPrev;
94 static GLuint _uniform_trail_uColour;
95 static void shader_trail_uPv(m4x4f m){
96 glUniformMatrix4fv(_uniform_trail_uPv,1,GL_FALSE,(float*)m);
97 }
98 static void shader_trail_uPvPrev(m4x4f m){
99 glUniformMatrix4fv(_uniform_trail_uPvPrev,1,GL_FALSE,(float*)m);
100 }
101 static void shader_trail_uColour(v4f v){
102 glUniform4fv(_uniform_trail_uColour,1,v);
103 }
104 static void shader_trail_register(void){
105 vg_shader_register( &_shader_trail );
106 }
107 static void shader_trail_use(void){ glUseProgram(_shader_trail.id); }
108 static void shader_trail_link(void){
109 _uniform_trail_uPv = glGetUniformLocation( _shader_trail.id, "uPv" );
110 _uniform_trail_uPvPrev = glGetUniformLocation( _shader_trail.id, "uPvPrev" );
111 _uniform_trail_uColour = glGetUniformLocation( _shader_trail.id, "uColour" );
112 }
113 #endif /* SHADER_trail_H */