Commit 4269def36e0f71681e515d9d1f37bf50d3203b6a

Thomas de Grivel 2024-01-06T06:23:56

fix shaders

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
diff --git a/libc3/window/sdl2/gl_camera.c b/libc3/window/sdl2/gl_camera.c
index 0021b76..1881b49 100644
--- a/libc3/window/sdl2/gl_camera.c
+++ b/libc3/window/sdl2/gl_camera.c
@@ -16,11 +16,11 @@
 #include "gl_matrix_4d.h"
 
 static const s8 * g_gl_camera_vertex_shader_src = "#version 330 core\n"
-"layout (location = 0) in dvec3 aPos;\n"
-"uniform dmat4 matrix;\n"
+"layout (location = 0) in vec3 aPos;\n"
+"uniform mat4 matrix;\n"
 "\n"
 "void main() {\n"
-"  gl_Position = vec4(matrix * dvec4(aPos, 1.0));\n"
+"  gl_Position = vec4(matrix * vec4(aPos, 1.0));\n"
 "}\n";
 
 void gl_camera_clean (s_gl_camera *camera)
@@ -41,15 +41,15 @@ s_gl_camera * gl_camera_init (s_gl_camera *camera, uw w, uw h)
   u32 vertex_shader;
   assert(camera);
   gl_camera_set_aspect_ratio(camera, w, h);
-  camera->clip_z_far = 1000;
-  camera->clip_z_near = 0.1;
-  camera->fov_y = 90.0;
-  camera->position.x = 0.0;
-  camera->position.y = 0.0;
-  camera->position.z = -10.0;
-  camera->rotation.x = 90.0;
-  camera->rotation.y = 0.0;
-  camera->rotation.z = 0.0;
+  camera->clip_z_far = 1000.0f;
+  camera->clip_z_near = 0.1f;
+  camera->fov_y = 90.0f;
+  camera->position.x = 0.0f;
+  camera->position.y = 0.0f;
+  camera->position.z = -10.0f;
+  camera->rotation.x = 90.0f;
+  camera->rotation.y = 0.0f;
+  camera->rotation.z = 0.0f;
   vertex_shader = glCreateShader(GL_VERTEX_SHADER);
   glShaderSource(vertex_shader, 1, &g_gl_camera_vertex_shader_src,
                  NULL);
@@ -101,7 +101,7 @@ void gl_camera_render (s_gl_camera *camera)
   gl_matrix_4d_rotate_axis(&camera->matrix, camera->rotation.z,
                            &(s_gl_point_3d) { 0.0, 0.0, 1.0 });
   glUseProgram(camera->gl_shader_program);
-  glUniformMatrix4dv(camera->gl_matrix_loc, 1, GL_FALSE,
+  glUniformMatrix4fv(camera->gl_matrix_loc, 1, GL_FALSE,
                      &camera->matrix.xx);
 }
 
diff --git a/libc3/window/sdl2/gl_matrix_4d.c b/libc3/window/sdl2/gl_matrix_4d.c
index a10f928..c9ead3f 100644
--- a/libc3/window/sdl2/gl_matrix_4d.c
+++ b/libc3/window/sdl2/gl_matrix_4d.c
@@ -22,14 +22,14 @@ sw gl_matrix_4d_buf_inspect (s_buf *buf, const s_gl_matrix_4d *matrix)
   sw result = 0;
   assert(buf);
   assert(matrix);
-  const f64 *m;
-  if ((r = buf_write_1(buf, "(F64) {")) < 0)
+  const f32 *m;
+  if ((r = buf_write_1(buf, "(F32) {")) < 0)
     return r;
   result += r;
   m = &matrix->xx;
   i = 0;
   while (i < 16) {
-    if ((r = buf_inspect_f64(buf, m)) < 0)
+    if ((r = buf_inspect_f32(buf, m)) < 0)
       return r;
     result += r;
     m++;
@@ -140,13 +140,13 @@ s_gl_matrix_4d * gl_matrix_4d_new_zero (void)
   return m;
 }
 
-s_gl_matrix_4d * gl_matrix_4d_ortho (s_gl_matrix_4d *m, f64 x1, f64 x2,
-                                     f64 y1, f64 y2, f64 clip_z_near,
-                                     f64 clip_z_far)
+s_gl_matrix_4d * gl_matrix_4d_ortho (s_gl_matrix_4d *m, f32 x1, f32 x2,
+                                     f32 y1, f32 y2, f32 clip_z_near,
+                                     f32 clip_z_far)
 {
-  f64 dx;
-  f64 dy;
-  f64 dz;
+  f32 dx;
+  f32 dy;
+  f32 dz;
   s_gl_matrix_4d ortho;
   assert(m);
   dx = x2 - x1;
@@ -164,15 +164,15 @@ s_gl_matrix_4d * gl_matrix_4d_ortho (s_gl_matrix_4d *m, f64 x1, f64 x2,
   return m;
 }
 
-s_gl_matrix_4d * gl_matrix_4d_perspective (s_gl_matrix_4d *m, f64 fov_y,
-                                           f64 aspect_ratio,
-                                           f64 z_near,
-                                           f64 z_far)
+s_gl_matrix_4d * gl_matrix_4d_perspective (s_gl_matrix_4d *m, f32 fov_y,
+                                           f32 aspect_ratio,
+                                           f32 z_near,
+                                           f32 z_far)
 {
-  f64 dz;
+  f32 dz;
   s_gl_matrix_4d perspective;
-  f64 f;
-  f64 fov_y_2;
+  f32 f;
+  f32 fov_y_2;
   fov_y_2 = fov_y / 2.0;
   f = cos(fov_y_2) / sin(fov_y_2);
   dz = z_near - z_far;
@@ -195,8 +195,8 @@ s_gl_matrix_4d * gl_matrix_4d_product (s_gl_matrix_4d *m,
   return m;
 }
 
-s_gl_matrix_4d * gl_matrix_4d_scale (s_gl_matrix_4d *m, f64 x, f64 y,
-                                     f64 z)
+s_gl_matrix_4d * gl_matrix_4d_scale (s_gl_matrix_4d *m, f32 x, f32 y,
+                                     f32 z)
 {
   s_gl_matrix_4d s = {0};
   s.xx = x;
@@ -207,8 +207,8 @@ s_gl_matrix_4d * gl_matrix_4d_scale (s_gl_matrix_4d *m, f64 x, f64 y,
   return m;
 }
 
-s_gl_matrix_4d * gl_matrix_4d_translate (s_gl_matrix_4d *m, f64 x,
-                                         f64 y, f64 z)
+s_gl_matrix_4d * gl_matrix_4d_translate (s_gl_matrix_4d *m, f32 x,
+                                         f32 y, f32 z)
 {
   m->xt += x;
   m->yt += y;
@@ -216,14 +216,14 @@ s_gl_matrix_4d * gl_matrix_4d_translate (s_gl_matrix_4d *m, f64 x,
   return m;
 }
 
-s_gl_matrix_4d * gl_matrix_4d_rotate_axis (s_gl_matrix_4d *m, f64 rad,
+s_gl_matrix_4d * gl_matrix_4d_rotate_axis (s_gl_matrix_4d *m, f32 rad,
                                            const s_gl_point_3d *axis)
 {
   s_gl_point_3d a;
-  f64 angle;
-  f64 one_minus_x;
-  f64 x;
-  f64 y;
+  f32 angle;
+  f32 one_minus_x;
+  f32 x;
+  f32 y;
   gl_point_3d_init_normalize(&a, axis);
   angle = -rad;
   x = cos(angle);
diff --git a/libc3/window/sdl2/gl_matrix_4d.h b/libc3/window/sdl2/gl_matrix_4d.h
index 4d4be24..39b0c13 100644
--- a/libc3/window/sdl2/gl_matrix_4d.h
+++ b/libc3/window/sdl2/gl_matrix_4d.h
@@ -33,20 +33,20 @@ s_gl_matrix_4d * gl_matrix_4d_new_matrix_mult (const s_gl_matrix_4d *a,
 s_gl_matrix_4d * gl_matrix_4d_new_zero (void);
 
 /* Operators. */
-s_gl_matrix_4d * gl_matrix_4d_ortho (s_gl_matrix_4d *m, f64 x1, f64 x2,
-                                     f64 y1, f64 y2, f64 clip_z_near,
-                                     f64 clip_z_far);
-s_gl_matrix_4d * gl_matrix_4d_perspective (s_gl_matrix_4d *m, f64 fov_y,
-                                           f64 aspect_ratio,
-                                           f64 clip_z_near,
-                                           f64 clip_z_far);
+s_gl_matrix_4d * gl_matrix_4d_ortho (s_gl_matrix_4d *m, f32 x1, f32 x2,
+                                     f32 y1, f32 y2, f32 clip_z_near,
+                                     f32 clip_z_far);
+s_gl_matrix_4d * gl_matrix_4d_perspective (s_gl_matrix_4d *m, f32 fov_y,
+                                           f32 aspect_ratio,
+                                           f32 clip_z_near,
+                                           f32 clip_z_far);
 s_gl_matrix_4d * gl_matrix_4d_product (s_gl_matrix_4d *m,
                                        const s_gl_matrix_4d *a);
-s_gl_matrix_4d * gl_matrix_4d_rotate_axis (s_gl_matrix_4d *m, f64 rad,
+s_gl_matrix_4d * gl_matrix_4d_rotate_axis (s_gl_matrix_4d *m, f32 rad,
                                            const s_gl_point_3d *axis);
-s_gl_matrix_4d * gl_matrix_4d_scale (s_gl_matrix_4d *m, f64 x, f64 y,
-                                     f64 z);
-s_gl_matrix_4d * gl_matrix_4d_translate (s_gl_matrix_4d *m, f64 x,
-                                         f64 y, f64 z);
+s_gl_matrix_4d * gl_matrix_4d_scale (s_gl_matrix_4d *m, f32 x, f32 y,
+                                     f32 z);
+s_gl_matrix_4d * gl_matrix_4d_translate (s_gl_matrix_4d *m, f32 x,
+                                         f32 y, f32 z);
 
 #endif /* GL_MATRIX_4D_H */
diff --git a/libc3/window/sdl2/gl_ortho.c b/libc3/window/sdl2/gl_ortho.c
index 8574398..579e9de 100644
--- a/libc3/window/sdl2/gl_ortho.c
+++ b/libc3/window/sdl2/gl_ortho.c
@@ -16,19 +16,19 @@
 #include "gl_matrix_4d.h"
 
 static const s8 * g_gl_ortho_vertex_shader_src = "#version 330 core\n"
-  "layout (location = 0) in dvec3 aPos;\n"
-  "layout (location = 1) in dvec3 aNorm;\n"
-  "layout (location = 2) in dvec2 aTexCoord;\n"
+  "layout (location = 0) in vec3 aPos;\n"
+  "layout (location = 1) in vec3 aNorm;\n"
+  "layout (location = 2) in vec2 aTexCoord;\n"
   "out vec3 FragNormal;\n"
   "out vec2 TexCoord;\n"
-  "uniform dmat4 projection_matrix;\n"
-  "uniform dmat4 view_matrix;\n"
-  "uniform dmat4 model_matrix;\n"
+  "uniform mat4 projection_matrix;\n"
+  "uniform mat4 view_matrix;\n"
+  "uniform mat4 model_matrix;\n"
   "\n"
   "void main() {\n"
   "  gl_Position = vec4(projection_matrix * view_matrix * \n"
-  "                     model_matrix * dvec4(aPos, 1.0));\n"
-  "  FragNormal = vec3(dmat3(transpose(inverse(model_matrix))) * aNorm);\n"
+  "                     model_matrix * vec4(aPos, 1.0));\n"
+  "  FragNormal = vec3(mat3(transpose(inverse(model_matrix))) * aNorm);\n"
   "  TexCoord = vec2(aTexCoord);\n"
   "}\n";
 
@@ -112,7 +112,7 @@ void gl_ortho_render (s_gl_ortho *ortho)
   assert(glGetError() == GL_NO_ERROR);
   glUseProgram(ortho->gl_shader_program);
   assert(glGetError() == GL_NO_ERROR);
-  glUniformMatrix4dv(ortho->gl_projection_matrix_loc, 1, GL_FALSE,
+  glUniformMatrix4fv(ortho->gl_projection_matrix_loc, 1, GL_FALSE,
                      &ortho->projection_matrix.xx);
   if ((error = glGetError()) != GL_NO_ERROR) {
     err_write_1("gl_ortho_render: glUniformMatrix4dv: ");
@@ -120,16 +120,16 @@ void gl_ortho_render (s_gl_ortho *ortho)
     assert(! "gl_ortho_render: glUniformMatrix4dv");
   }
   assert(glGetError() == GL_NO_ERROR);
-  glUniformMatrix4dv(ortho->gl_view_matrix_loc, 1, GL_FALSE,
+  glUniformMatrix4fv(ortho->gl_view_matrix_loc, 1, GL_FALSE,
                      &ortho->view_matrix.xx);
   assert(glGetError() == GL_NO_ERROR);
-  glUniformMatrix4dv(ortho->gl_model_matrix_loc, 1, GL_FALSE,
+  glUniformMatrix4fv(ortho->gl_model_matrix_loc, 1, GL_FALSE,
                      &ortho->model_matrix.xx);
   assert(glGetError() == GL_NO_ERROR);
 }
 
-void gl_ortho_resize (s_gl_ortho *ortho, f64 x1, f64 x2, f64 y1, f64 y2,
-                      f64 clip_z_near, f64 clip_z_far)
+void gl_ortho_resize (s_gl_ortho *ortho, f32 x1, f32 x2, f32 y1, f32 y2,
+                      f32 clip_z_near, f32 clip_z_far)
 {
   assert(ortho);
   gl_matrix_4d_init_identity(&ortho->projection_matrix);
@@ -150,7 +150,7 @@ void gl_ortho_update_model_matrix (s_gl_ortho *ortho)
 {
   assert(ortho);
   assert(glGetError() == GL_NO_ERROR);
-  glUniformMatrix4dv(ortho->gl_model_matrix_loc, 1, GL_FALSE,
+  glUniformMatrix4fv(ortho->gl_model_matrix_loc, 1, GL_FALSE,
                      &ortho->model_matrix.xx);
   assert(glGetError() == GL_NO_ERROR);
 }
@@ -168,7 +168,7 @@ void gl_ortho_update_view_matrix (s_gl_ortho *ortho)
                            &(s_gl_point_3d) { 0.0, 1.0, 0.0 });
   gl_matrix_4d_rotate_axis(&ortho->view_matrix, ortho->rotation.z,
                            &(s_gl_point_3d) { 0.0, 0.0, 1.0 });
-  glUniformMatrix4dv(ortho->gl_view_matrix_loc, 1, GL_FALSE,
+  glUniformMatrix4fv(ortho->gl_view_matrix_loc, 1, GL_FALSE,
                      &ortho->view_matrix.xx);
   assert(glGetError() == GL_NO_ERROR);
 }
diff --git a/libc3/window/sdl2/gl_ortho.h b/libc3/window/sdl2/gl_ortho.h
index 91f4c5d..a9f6fc5 100644
--- a/libc3/window/sdl2/gl_ortho.h
+++ b/libc3/window/sdl2/gl_ortho.h
@@ -27,8 +27,8 @@ s_gl_ortho * gl_ortho_new (void);
 /* Operators. */
 void         gl_ortho_render (s_gl_ortho *ortho);
 void         gl_ortho_render_end (s_gl_ortho *ortho);
-void         gl_ortho_resize (s_gl_ortho *ortho, f64 x1, f64 x2, f64 y1,
-                              f64 y2, f64 clip_z_near, f64 clip_z_far);
+void         gl_ortho_resize (s_gl_ortho *ortho, f32 x1, f32 x2, f32 y1,
+                              f32 y2, f32 clip_z_near, f32 clip_z_far);
 void         gl_ortho_update_model_matrix (s_gl_ortho *ortho);
 void         gl_ortho_update_view_matrix (s_gl_ortho *ortho);
 
diff --git a/libc3/window/sdl2/types.h b/libc3/window/sdl2/types.h
index 0f8e9de..b311db1 100644
--- a/libc3/window/sdl2/types.h
+++ b/libc3/window/sdl2/types.h
@@ -81,8 +81,8 @@ typedef void (*f_window_sdl2_unload) (s_window_sdl2 *window);
 /* 1 */
 struct gl_font {
   FT_Face ft_face;
-  f64 point_size;
-  f64 pixel_per_point;
+  f32 point_size;
+  f32 pixel_per_point;
   s_str path;
   s_str real_path;
 };
@@ -94,34 +94,34 @@ struct gl_lines {
 };
 
 struct gl_matrix_3d {
-  f64 xx;
-  f64 xy;
-  f64 xz;
-  f64 yx;
-  f64 yy;
-  f64 yz;
-  f64 zx;
-  f64 zy;
-  f64 zz;
+  f32 xx;
+  f32 xy;
+  f32 xz;
+  f32 yx;
+  f32 yy;
+  f32 yz;
+  f32 zx;
+  f32 zy;
+  f32 zz;
 };
 
 struct gl_matrix_4d {
-  f64 xx;
-  f64 xy;
-  f64 xz;
-  f64 xt;
-  f64 yx;
-  f64 yy;
-  f64 yz;
-  f64 yt;
-  f64 zx;
-  f64 zy;
-  f64 zz;
-  f64 zt;
-  f64 tx;
-  f64 ty;
-  f64 tz;
-  f64 tt;
+  f32 xx;
+  f32 xy;
+  f32 xz;
+  f32 xt;
+  f32 yx;
+  f32 yy;
+  f32 yz;
+  f32 yt;
+  f32 zx;
+  f32 zy;
+  f32 zz;
+  f32 zt;
+  f32 tx;
+  f32 ty;
+  f32 tz;
+  f32 tt;
 };
 
 struct gl_object {
@@ -134,14 +134,14 @@ struct gl_object {
 };
 
 struct gl_point_2d {
-  f64 x;
-  f64 y;
+  f32 x;
+  f32 y;
 };
 
 struct gl_point_3d {
-  f64 x;
-  f64 y;
-  f64 z;
+  f32 x;
+  f32 y;
+  f32 z;
 };
 
 struct gl_text {
@@ -160,16 +160,16 @@ struct gl_triangle {
 };
 
 struct rgb {
-  f64 r;
-  f64 g;
-  f64 b;
+  f32 r;
+  f32 g;
+  f32 b;
 };
 
 struct rgba {
-  f64 r;
-  f64 g;
-  f64 b;
-  f64 a;
+  f32 r;
+  f32 g;
+  f32 b;
+  f32 a;
 };
 
 /* Subtype of s_window. See libc3/window/types.h */
@@ -202,10 +202,10 @@ struct window_sdl2 {
 
 /* 2 */
 struct gl_camera {
-  f64 aspect_ratio;
-  f64 clip_z_far;
-  f64 clip_z_near;
-  f64 fov_y;
+  f32 aspect_ratio;
+  f32 clip_z_far;
+  f32 clip_z_near;
+  f32 fov_y;
   s_gl_point_3d position;
   s_gl_point_3d rotation;
   s_gl_matrix_4d matrix;
@@ -220,12 +220,12 @@ struct gl_cylinder {
 };
 
 struct gl_ortho {
-  f64 x1;
-  f64 x2;
-  f64 y1;
-  f64 y2;
-  f64 clip_z_near;
-  f64 clip_z_far;
+  f32 x1;
+  f32 x2;
+  f32 y1;
+  f32 y2;
+  f32 clip_z_near;
+  f32 clip_z_far;
   s_gl_point_3d position;
   s_gl_point_3d rotation;
   s_gl_matrix_4d projection_matrix;