change shader properties to be vg_msg based
[carveJwlIkooP6JGAAIwe30JlM.git] / shaders / point_map.h
1 #ifndef SHADER_point_map_H
2 #define SHADER_point_map_H
3 static void shader_point_map_link(void);
4 static void shader_point_map_register(void);
5 static struct vg_shader _shader_point_map = {
6 .name = "point_map",
7 .link = shader_point_map_link,
8 .vs =
9 {
10 .orig_file = "shaders/cloud.vs",
11 .static_src =
12 "layout (location=0) in vec4 a_co;\n"
13 "layout (location=1) in vec4 a_colour;\n"
14 "\n"
15 "#line 1 1 \n"
16 "const float k_motion_lerp_amount = 0.01;\n"
17 "\n"
18 "#line 2 0 \n"
19 "\n"
20 "out vec3 aMotionVec0;\n"
21 "out vec3 aMotionVec1;\n"
22 "\n"
23 "void vs_motion_out( vec4 vproj0, vec4 vproj1 )\n"
24 "{\n"
25 " // This magically solves some artifacting errors!\n"
26 " //\n"
27 " vproj1 = vproj0*(1.0-k_motion_lerp_amount) + vproj1*k_motion_lerp_amount;\n"
28 "\n"
29 " aMotionVec0 = vec3( vproj0.xy, vproj0.w );\n"
30 " aMotionVec1 = vec3( vproj1.xy, vproj1.w );\n"
31 "}\n"
32 "\n"
33 "#line 5 0 \n"
34 "\n"
35 "uniform mat4x3 uMdl;\n"
36 "uniform mat3 uNormMtx;\n"
37 "uniform mat4 uPv;\n"
38 "uniform mat4 uPvmPrev;\n"
39 "uniform vec4 uAnim;\n"
40 "\n"
41 "out vec4 aColour;\n"
42 "out vec3 aWorldCo;\n"
43 "out vec3 aCo;\n"
44 "\n"
45 "vec2 rand_hash22( vec2 p )\n"
46 "{\n"
47 " vec3 p3 = fract(vec3(p.xyx) * 213.8976123);\n"
48 " p3 += dot(p3, p3.yzx+19.19);\n"
49 " return fract(vec2((p3.x + p3.y)*p3.z, (p3.x+p3.z)*p3.y));\n"
50 "}\n"
51 "\n"
52 "vec3 gridify( vec3 p, float s, float t )\n"
53 "{\n"
54 " vec3 co = p*s;\n"
55 " vec2 r2 = rand_hash22(p.xz);\n"
56 " vec3 grid = (vec3(floor(co.x),co.y,floor(co.z))+vec3(0.5,0.0,0.5)) * (1.0/s);\n"
57 "\n"
58 " float t1 = 1.0-t;\n"
59 " t1 = 1.0-t1*t1;\n"
60 " float t2 = t*t;\n"
61 "\n"
62 " return mix( p, grid, t1 ) + t2*vec3(0.0,r2.y*r2.y*0.2,0.0);\n"
63 "}\n"
64 "\n"
65 "void main()\n"
66 "{\n"
67 " vec3 co = gridify( a_co.xyz, uAnim.x, uAnim.y );\n"
68 "\n"
69 " vec3 world_pos0 = uMdl * vec4( co, 1.0 );\n"
70 " vec4 vproj0 = uPv * vec4( world_pos0, 1.0 );\n"
71 " vec4 vproj1 = uPvmPrev * vec4( co, 1.0 );\n"
72 "\n"
73 " float scaler = smoothstep(0.6,0.58,length(co.xz));\n"
74 "\n"
75 " vs_motion_out( vproj0, vproj1 );\n"
76 "\n"
77 " gl_Position = vproj0;\n"
78 " gl_PointSize = (9.0*scaler) / (max( gl_Position.z, 2.0 ));\n"
79 " aWorldCo = world_pos0;\n"
80 " aColour = a_colour*scaler*(1.0-uAnim.y*uAnim.y);\n"
81 " aCo = co;\n"
82 "}\n"
83 ""},
84 .fs =
85 {
86 .orig_file = "shaders/cloud.fs",
87 .static_src =
88 "out vec4 FragColor;\n"
89 "\n"
90 "#line 1 1 \n"
91 "const float k_motion_lerp_amount = 0.01;\n"
92 "\n"
93 "#line 2 0 \n"
94 "\n"
95 "layout (location = 1) out vec2 oMotionVec;\n"
96 "\n"
97 "in vec3 aMotionVec0;\n"
98 "in vec3 aMotionVec1;\n"
99 "\n"
100 "void compute_motion_vectors()\n"
101 "{\n"
102 " // Write motion vectors\n"
103 " vec2 vmotion0 = aMotionVec0.xy / aMotionVec0.z;\n"
104 " vec2 vmotion1 = aMotionVec1.xy / aMotionVec1.z;\n"
105 "\n"
106 " oMotionVec = (vmotion1-vmotion0) * (1.0/k_motion_lerp_amount);\n"
107 "}\n"
108 "\n"
109 "#line 4 0 \n"
110 "\n"
111 "uniform vec3 uCamera;\n"
112 "\n"
113 "in vec4 aColour;\n"
114 "in vec3 aCo;\n"
115 "in vec3 aWorldCo;\n"
116 "\n"
117 "void main()\n"
118 "{\n"
119 " vec2 ssuv = gl_FragCoord.xy;\n"
120 " vec3 vDither = vec3( dot( vec2( 171.0, 231.0 ), ssuv) );\n"
121 " float dither = fract( vDither.g / 71.0 ) - 0.5;\n"
122 "\n"
123 " float diff = length(gl_PointCoord.xy-vec2(0.5));\n"
124 " if( diff+dither>0.5 )discard;\n"
125 "\n"
126 " compute_motion_vectors();\n"
127 " FragColor = aColour;\n"
128 "}\n"
129 ""},
130 };
131
132 static GLuint _uniform_point_map_uMdl;
133 static GLuint _uniform_point_map_uNormMtx;
134 static GLuint _uniform_point_map_uPv;
135 static GLuint _uniform_point_map_uPvmPrev;
136 static GLuint _uniform_point_map_uAnim;
137 static GLuint _uniform_point_map_uCamera;
138 static void shader_point_map_uMdl(m4x3f m){
139 glUniformMatrix4x3fv(_uniform_point_map_uMdl,1,GL_FALSE,(float*)m);
140 }
141 static void shader_point_map_uNormMtx(m3x3f m){
142 glUniformMatrix3fv(_uniform_point_map_uNormMtx,1,GL_FALSE,(float*)m);
143 }
144 static void shader_point_map_uPv(m4x4f m){
145 glUniformMatrix4fv(_uniform_point_map_uPv,1,GL_FALSE,(float*)m);
146 }
147 static void shader_point_map_uPvmPrev(m4x4f m){
148 glUniformMatrix4fv(_uniform_point_map_uPvmPrev,1,GL_FALSE,(float*)m);
149 }
150 static void shader_point_map_uAnim(v4f v){
151 glUniform4fv(_uniform_point_map_uAnim,1,v);
152 }
153 static void shader_point_map_uCamera(v3f v){
154 glUniform3fv(_uniform_point_map_uCamera,1,v);
155 }
156 static void shader_point_map_register(void){
157 vg_shader_register( &_shader_point_map );
158 }
159 static void shader_point_map_use(void){ glUseProgram(_shader_point_map.id); }
160 static void shader_point_map_link(void){
161 _uniform_point_map_uMdl = glGetUniformLocation( _shader_point_map.id, "uMdl" );
162 _uniform_point_map_uNormMtx = glGetUniformLocation( _shader_point_map.id, "uNormMtx" );
163 _uniform_point_map_uPv = glGetUniformLocation( _shader_point_map.id, "uPv" );
164 _uniform_point_map_uPvmPrev = glGetUniformLocation( _shader_point_map.id, "uPvmPrev" );
165 _uniform_point_map_uAnim = glGetUniformLocation( _shader_point_map.id, "uAnim" );
166 _uniform_point_map_uCamera = glGetUniformLocation( _shader_point_map.id, "uCamera" );
167 }
168 #endif /* SHADER_point_map_H */