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