931d512595edad6ddadd5c1a0e7e32db449fd6c1
[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 vec2 uTime;\n"
40 "uniform vec4 uTransform;\n"
41 "\n"
42 "out vec4 aColour;\n"
43 "out vec3 aWorldCo;\n"
44 "out vec3 aCo;\n"
45 "\n"
46 "vec2 rand_hash22( vec2 p )\n"
47 "{\n"
48 " vec3 p3 = fract(vec3(p.xyx) * 213.8976123);\n"
49 " p3 += dot(p3, p3.yzx+19.19);\n"
50 " return fract(vec2((p3.x + p3.y)*p3.z, (p3.x+p3.z)*p3.y));\n"
51 "}\n"
52 "\n"
53 "vec3 mapP( vec3 p )\n"
54 "{\n"
55 " return p;\n"
56 " float t = max(0.0,uTime.y);\n"
57 "\n"
58 " vec3 lco = p * 20.0;\n"
59 " vec3 grid = vec3(floor(lco.x),lco.y,floor(lco.z));//fract(p);\n"
60 "\n"
61 " return mix(p,grid * (1.0/20.0), t)*vec3(1.0+t,1.0-t,1.0+t);\n"
62 "}\n"
63 "\n"
64 "void main()\n"
65 "{\n"
66 " vec3 mco = a_co.xyz * uTransform.w + uTransform.xyz;\n"
67 "\n"
68 "\n"
69 " vec3 center = vec3(0.5);\n"
70 " vec3 lco = mapP(mco);\n"
71 "\n"
72 " vec3 world_pos0 = uMdl * vec4( mco, 1.0 );\n"
73 " vec4 vproj0 = uPv * vec4( world_pos0, 1.0 );\n"
74 " vec4 vproj1 = uPvmPrev * vec4( mco, 1.0 );\n"
75 "\n"
76 " float t = max(0.0,uTime.y);\n"
77 " float scaler = smoothstep(0.6,0.58,length(lco.xz));\n"
78 "\n"
79 " vs_motion_out( vproj0, vproj1 );\n"
80 "\n"
81 " gl_Position = vproj0;\n"
82 " gl_PointSize = (9.0*uTransform.w*scaler) / (gl_Position.z + 0.01);\n"
83 " aWorldCo = world_pos0;\n"
84 " aColour = a_colour*scaler;\n"
85 " aCo = mco;\n"
86 "}\n"
87 ""},
88 .fs =
89 {
90 .orig_file = "shaders/cloud.fs",
91 .static_src =
92 "out vec4 FragColor;\n"
93 "\n"
94 "#line 1 1 \n"
95 "const float k_motion_lerp_amount = 0.01;\n"
96 "\n"
97 "#line 2 0 \n"
98 "\n"
99 "layout (location = 1) out vec2 oMotionVec;\n"
100 "\n"
101 "in vec3 aMotionVec0;\n"
102 "in vec3 aMotionVec1;\n"
103 "\n"
104 "void compute_motion_vectors()\n"
105 "{\n"
106 " // Write motion vectors\n"
107 " vec2 vmotion0 = aMotionVec0.xy / aMotionVec0.z;\n"
108 " vec2 vmotion1 = aMotionVec1.xy / aMotionVec1.z;\n"
109 "\n"
110 " oMotionVec = (vmotion1-vmotion0) * (1.0/k_motion_lerp_amount);\n"
111 "}\n"
112 "\n"
113 "#line 4 0 \n"
114 "\n"
115 "uniform vec3 uCamera;\n"
116 "\n"
117 "in vec4 aColour;\n"
118 "in vec3 aCo;\n"
119 "in vec3 aWorldCo;\n"
120 "\n"
121 "void main()\n"
122 "{\n"
123 " vec2 ssuv = gl_FragCoord.xy;\n"
124 " vec3 vDither = vec3( dot( vec2( 171.0, 231.0 ), ssuv) );\n"
125 " float dither = fract( vDither.g / 71.0 ) - 0.5;\n"
126 "\n"
127 " float diff = length(gl_PointCoord.xy-vec2(0.5));\n"
128 " if( diff+dither>0.5 )discard;\n"
129 "\n"
130 " compute_motion_vectors();\n"
131 " FragColor = aColour;\n"
132 "}\n"
133 ""},
134 };
135
136 static GLuint _uniform_point_map_uMdl;
137 static GLuint _uniform_point_map_uNormMtx;
138 static GLuint _uniform_point_map_uPv;
139 static GLuint _uniform_point_map_uPvmPrev;
140 static GLuint _uniform_point_map_uTime;
141 static GLuint _uniform_point_map_uTransform;
142 static GLuint _uniform_point_map_uCamera;
143 static void shader_point_map_uMdl(m4x3f m){
144 glUniformMatrix4x3fv(_uniform_point_map_uMdl,1,GL_FALSE,(float*)m);
145 }
146 static void shader_point_map_uNormMtx(m3x3f m){
147 glUniformMatrix3fv(_uniform_point_map_uNormMtx,1,GL_FALSE,(float*)m);
148 }
149 static void shader_point_map_uPv(m4x4f m){
150 glUniformMatrix4fv(_uniform_point_map_uPv,1,GL_FALSE,(float*)m);
151 }
152 static void shader_point_map_uPvmPrev(m4x4f m){
153 glUniformMatrix4fv(_uniform_point_map_uPvmPrev,1,GL_FALSE,(float*)m);
154 }
155 static void shader_point_map_uTime(v2f v){
156 glUniform2fv(_uniform_point_map_uTime,1,v);
157 }
158 static void shader_point_map_uTransform(v4f v){
159 glUniform4fv(_uniform_point_map_uTransform,1,v);
160 }
161 static void shader_point_map_uCamera(v3f v){
162 glUniform3fv(_uniform_point_map_uCamera,1,v);
163 }
164 static void shader_point_map_register(void){
165 vg_shader_register( &_shader_point_map );
166 }
167 static void shader_point_map_use(void){ glUseProgram(_shader_point_map.id); }
168 static void shader_point_map_link(void){
169 _uniform_point_map_uMdl = glGetUniformLocation( _shader_point_map.id, "uMdl" );
170 _uniform_point_map_uNormMtx = glGetUniformLocation( _shader_point_map.id, "uNormMtx" );
171 _uniform_point_map_uPv = glGetUniformLocation( _shader_point_map.id, "uPv" );
172 _uniform_point_map_uPvmPrev = glGetUniformLocation( _shader_point_map.id, "uPvmPrev" );
173 _uniform_point_map_uTime = glGetUniformLocation( _shader_point_map.id, "uTime" );
174 _uniform_point_map_uTransform = glGetUniformLocation( _shader_point_map.id, "uTransform" );
175 _uniform_point_map_uCamera = glGetUniformLocation( _shader_point_map.id, "uCamera" );
176 }
177 #endif /* SHADER_point_map_H */