Commit d9074941778c4eed0cb2d90309d6490db85cdc1f

Thomas de Grivel 2023-12-22T06:25:25

wip OpenGL 4.6

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
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
diff --git a/libc3/window/sdl2/demo/bg_rect.c b/libc3/window/sdl2/demo/bg_rect.c
index a269c6c..dacfce0 100644
--- a/libc3/window/sdl2/demo/bg_rect.c
+++ b/libc3/window/sdl2/demo/bg_rect.c
@@ -50,7 +50,10 @@ bool bg_rect_render (s_sequence *seq, s_window_sdl2 *window,
   rgb.r = color[c1].r * q + color[c2].r * p;
   rgb.g = color[c1].g * q + color[c2].g * p;
   rgb.b = color[c1].b * q + color[c2].b * p;
+  assert(glGetError() == GL_NO_ERROR);
   glClearColor(rgb.r, rgb.g, rgb.b, 1.0);
+  assert(glGetError() == GL_NO_ERROR);
   glClear(GL_COLOR_BUFFER_BIT);
+  assert(glGetError() == GL_NO_ERROR);
   return true;
 }
diff --git a/libc3/window/sdl2/demo/window_sdl2_demo.c b/libc3/window/sdl2/demo/window_sdl2_demo.c
index e087f96..75cd2f4 100644
--- a/libc3/window/sdl2/demo/window_sdl2_demo.c
+++ b/libc3/window/sdl2/demo/window_sdl2_demo.c
@@ -14,6 +14,7 @@
 #include <stdlib.h>
 #include <libc3/c3.h>
 #include "../../window.h"
+#include "../gl_matrix_4d.h"
 #include "../gl_ortho.h"
 #include "../sdl2_font.h"
 #include "../sdl2_sprite.h"
@@ -129,6 +130,7 @@ bool window_sdl2_demo_key (s_window_sdl2 *window, SDL_Keysym *keysym)
 bool window_sdl2_demo_load (s_window_sdl2 *window)
 {
   assert(window);
+  assert(glGetError() == GL_NO_ERROR);
   if (window->sequence_count != WINDOW_SDL2_DEMO_SEQUENCE_COUNT) {
     fprintf(stderr, "window_sdl2_demo_load: "
             "window->sequence_count = %lu\n", window->sequence_count);
@@ -171,59 +173,84 @@ bool window_sdl2_demo_load (s_window_sdl2 *window)
                          1, 1, 1))
     return false;
   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
+  assert(glGetError() == GL_NO_ERROR);
   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
+  assert(glGetError() == GL_NO_ERROR);
   window_sdl2_sequence_init(window->sequence + 4, 120.0,
                             "05. Earth",
                             earth_load, earth_render);
-  window_set_sequence_pos((s_window *) window, 0);
+  assert(glGetError() == GL_NO_ERROR);
+  window_set_sequence_pos((s_window *) window, 0); 
+  assert(glGetError() == GL_NO_ERROR);
   return true;
 }
 
-static void render_text (s_sdl2_font *font, double x, double y,
+static void render_text (s_sdl2_font *font, f64 x, f64 y,
                          const s8 *p)
 {
+  assert(glGetError() == GL_NO_ERROR);
   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
                   GL_LINEAR);
-  glPushMatrix();
-  glTranslated(x, y, 0.0);
-  glPushMatrix();
-  glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
-  glTranslatef(-1.0f, -1.0f, 0.0f);
+  assert(glGetError() == GL_NO_ERROR);
+  gl_matrix_4d_init_identity(&g_ortho.model_matrix);
+  gl_matrix_4d_translate(&g_ortho.model_matrix, x - 1.0, y - 1.0, 0.0);
+  gl_ortho_update_model_matrix(&g_ortho);
+  glBlendColor(1.0f, 1.0f, 1.0f, 1.0f);
+  assert(glGetError() == GL_NO_ERROR);
   sdl2_font_render_text(font, p);
-  glTranslatef( 1.0f,  0.0f, 0.0f);
+  assert(glGetError() == GL_NO_ERROR);
+  gl_matrix_4d_translate(&g_ortho.model_matrix, 1.0, 0.0, 0.0);
+  gl_ortho_update_model_matrix(&g_ortho);
+  assert(glGetError() == GL_NO_ERROR);
   sdl2_font_render_text(font, p);
-  glTranslatef( 1.0f,  0.0f, 0.0f);
+  gl_matrix_4d_translate(&g_ortho.model_matrix, 1.0, 0.0, 0.0);
+  gl_ortho_update_model_matrix(&g_ortho);
   sdl2_font_render_text(font, p);
+  gl_matrix_4d_translate(&g_ortho.model_matrix, 1.0, 0.0, 0.0);
+  gl_ortho_update_model_matrix(&g_ortho);
   glTranslatef( 0.0f,  1.0f, 0.0f);
   sdl2_font_render_text(font, p);
+  gl_matrix_4d_translate(&g_ortho.model_matrix, 1.0, 0.0, 0.0);
+  gl_ortho_update_model_matrix(&g_ortho);
   glTranslatef(-1.0f,  0.0f, 0.0f);
   sdl2_font_render_text(font, p);
+  gl_matrix_4d_translate(&g_ortho.model_matrix, 1.0, 0.0, 0.0);
+  gl_ortho_update_model_matrix(&g_ortho);
   glTranslatef(-1.0f,  0.0f, 0.0f);
   sdl2_font_render_text(font, p);
+  gl_matrix_4d_translate(&g_ortho.model_matrix, 1.0, 0.0, 0.0);
+  gl_ortho_update_model_matrix(&g_ortho);
   glTranslatef( 0.0f,  1.0f, 0.0f);
   sdl2_font_render_text(font, p);
+  gl_matrix_4d_translate(&g_ortho.model_matrix, 1.0, 0.0, 0.0);
+  gl_ortho_update_model_matrix(&g_ortho);
   glTranslatef( 1.0f,  0.0f, 0.0f);
   sdl2_font_render_text(font, p);
+  gl_matrix_4d_translate(&g_ortho.model_matrix, 1.0, 0.0, 0.0);
+  gl_ortho_update_model_matrix(&g_ortho);
   glTranslatef( 1.0f,  0.0f, 0.0f);
   sdl2_font_render_text(font, p);
-  glPopMatrix();
   glColor4f(0.0f, 0.0f, 0.0f, 1.0f);
+  gl_matrix_4d_translate(&g_ortho.model_matrix, -1.0, -1.0, 0.0);
+  gl_ortho_update_model_matrix(&g_ortho);
   sdl2_font_render_text(font, p);
-  glPopMatrix();
+  assert(glGetError() == GL_NO_ERROR);
 }
 
 bool window_sdl2_demo_render (s_window_sdl2 *window, void *context)
 {
   s_sequence *seq;
   assert(window);
+  assert(glGetError() == GL_NO_ERROR);
   if (! window_animate((s_window *) window))
     return false;
   seq = window->sequence + window->sequence_pos;
+  gl_ortho_render(&g_ortho);
   if (! seq->render(seq, window, context))
     return false;
   /* 2D */
-  gl_ortho_render(&g_ortho);
   glDisable(GL_DEPTH_TEST);
+  assert(glGetError() == GL_NO_ERROR);
   sdl2_font_set_size(&g_font_courier_new, 20, window->dpi);
   render_text(&g_font_courier_new, 20.0f, 30.0f, seq->title);
   /* progress bar */
@@ -236,6 +263,7 @@ bool window_sdl2_demo_render (s_window_sdl2 *window, void *context)
   glRectd(20, 12,
           20 + (window->w - 40.0) * seq->t / seq->duration,
           12 + 2);
+  assert(glGetError() == GL_NO_ERROR);
   /* fps */
   s8 fps[32];
   snprintf(fps, sizeof(fps), "%.2f", (f64) seq->frame / seq->t);
@@ -249,8 +277,9 @@ bool window_sdl2_demo_resize (s_window_sdl2 *window,
 {
   assert(window);
   (void) window;
-  (void) w;
-  (void) h;
+  assert(glGetError() == GL_NO_ERROR);
+  gl_ortho_resize(&g_ortho, 0, w, 0, h, -1, 1);
+  assert(glGetError() == GL_NO_ERROR);
   return true;
 }
 
diff --git a/libc3/window/sdl2/gl_object.c b/libc3/window/sdl2/gl_object.c
index 6a03ee7..3547a0b 100644
--- a/libc3/window/sdl2/gl_object.c
+++ b/libc3/window/sdl2/gl_object.c
@@ -42,9 +42,11 @@ void gl_object_clean (s_gl_object *object)
 s_gl_object * gl_object_init (s_gl_object *object)
 {
   s_gl_object tmp = {0};
+  assert(glGetError() == GL_NO_ERROR);
   glGenVertexArrays(1, &tmp.gl_vao);
   glGenBuffers(1, &tmp.gl_vbo);
   glGenBuffers(1, &tmp.gl_ebo);
+  assert(glGetError() == GL_NO_ERROR);
   *object = tmp;
   return object;
 }
@@ -52,6 +54,7 @@ s_gl_object * gl_object_init (s_gl_object *object)
 void gl_object_render (const s_gl_object *object)
 {
   assert(object);
+  assert(glGetError() == GL_NO_ERROR);
   glBindVertexArray(object->gl_vao);
   glBindBuffer(GL_ARRAY_BUFFER, object->gl_vbo);
   glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, object->gl_ebo);
@@ -59,11 +62,13 @@ void gl_object_render (const s_gl_object *object)
   glEnableClientState(GL_NORMAL_ARRAY);
   glEnableClientState(GL_TEXTURE_COORD_ARRAY);
   glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
+  assert(glGetError() == GL_NO_ERROR);
 }
 
 bool gl_object_update (s_gl_object *object)
 {
   assert(object);
+  assert(glGetError() == GL_NO_ERROR);
   glBindVertexArray(object->gl_vao);
   glBindBuffer(GL_ARRAY_BUFFER, object->gl_vbo);
   glBufferData(GL_ARRAY_BUFFER, object->vertex.size,
@@ -80,5 +85,6 @@ bool gl_object_update (s_gl_object *object)
                     (void *) (3 * sizeof(f64)));
   glTexCoordPointer(2, GL_DOUBLE, 8 * sizeof(f64),
                     (void *) (6 * sizeof(f64)));
+  assert(glGetError() == GL_NO_ERROR);
   return true;
 }
diff --git a/libc3/window/sdl2/gl_ortho.c b/libc3/window/sdl2/gl_ortho.c
index 73c5d4a..b8002e2 100644
--- a/libc3/window/sdl2/gl_ortho.c
+++ b/libc3/window/sdl2/gl_ortho.c
@@ -16,12 +16,15 @@
 #include "gl_matrix_4d.h"
 
 static const s8 * g_gl_ortho_vertex_shader_src = "#version 460 core\n"
-"layout (location = 0) in dvec3 aPos;\n"
-"uniform dmat4 matrix;\n"
-"\n"
-"void main() {\n"
-"  gl_Position = vec4(matrix * dvec4(aPos, 1.0));\n"
-"}\n";
+  "layout (location = 0) in dvec3 aPos;\n"
+  "uniform dmat4 projection_matrix;\n"
+  "uniform dmat4 view_matrix;\n"
+  "uniform dmat4 model_matrix;\n"
+  "\n"
+  "void main() {\n"
+  "  gl_Position = vec4(projection_matrix * view_matrix * \n"
+  "                     model_matrix * dvec4(aPos, 1.0));\n"
+  "}\n";
 
 void gl_ortho_clean (s_gl_ortho *ortho)
 {
@@ -37,17 +40,20 @@ void gl_ortho_delete (s_gl_ortho *ortho)
 
 s_gl_ortho * gl_ortho_init (s_gl_ortho *ortho)
 {
+  GLenum error;
   GLint success;
   u32 vertex_shader;
   assert(ortho);
-  ortho->clip_z_far = 1.0;
-  ortho->clip_z_near = -1.0;
+  gl_matrix_4d_init_identity(&ortho->projection_matrix);
+  gl_matrix_4d_ortho(&ortho->projection_matrix, -1, 1, -1, 1, -1, 1);
   ortho->position.x = 0.0;
   ortho->position.y = 0.0;
   ortho->position.z = 0.0;
   ortho->rotation.x = 0.0;
   ortho->rotation.y = 0.0;
   ortho->rotation.z = 0.0;
+  gl_matrix_4d_init_identity(&ortho->view_matrix);
+  gl_matrix_4d_init_identity(&ortho->model_matrix);
   vertex_shader = glCreateShader(GL_VERTEX_SHADER);
   glShaderSource(vertex_shader, 1, &g_gl_ortho_vertex_shader_src,
                  NULL);
@@ -60,11 +66,37 @@ s_gl_ortho * gl_ortho_init (s_gl_ortho *ortho)
     err_puts(info_log);
   }
   ortho->gl_shader_program = glCreateProgram();
+  assert(glGetError() == GL_NO_ERROR);
   glAttachShader(ortho->gl_shader_program, vertex_shader);
+  assert(glGetError() == GL_NO_ERROR);
   glLinkProgram(ortho->gl_shader_program);
+  assert(glGetError() == GL_NO_ERROR);
   glDeleteShader(vertex_shader);
-  ortho->gl_matrix_loc =
-    glGetUniformLocation(ortho->gl_shader_program, "matrix");
+  assert(glGetError() == GL_NO_ERROR);
+  glUseProgram(ortho->gl_shader_program);
+  assert(glGetError() == GL_NO_ERROR);
+  ortho->gl_projection_matrix_loc =
+    glGetUniformLocation(ortho->gl_shader_program, "projection_matrix");
+  glUniformMatrix4dv(ortho->gl_projection_matrix_loc, 1, GL_FALSE,
+                     &ortho->view_matrix.xx);
+  if ((error = glGetError()) != GL_NO_ERROR) {
+    err_write_1("gl_ortho_init: glUniformMatrix4dv: ");
+    err_puts((const s8 *) glewGetErrorString(error));
+    assert(! "gl_ortho_init: glUniformMatrix4dv");
+    return NULL;
+  }
+  ortho->gl_view_matrix_loc =
+    glGetUniformLocation(ortho->gl_shader_program, "view_matrix");
+  glUniformMatrix4dv(ortho->gl_view_matrix_loc, 1, GL_FALSE,
+                     &ortho->view_matrix.xx);
+  ortho->gl_model_matrix_loc =
+    glGetUniformLocation(ortho->gl_shader_program, "model_matrix");
+  assert(glGetError() == GL_NO_ERROR);
+  glUniformMatrix4dv(ortho->gl_model_matrix_loc, 1, GL_FALSE,
+                     &ortho->model_matrix.xx);
+  assert(glGetError() == GL_NO_ERROR);
+  glUseProgram(0);
+  assert(glGetError() == GL_NO_ERROR);
   return ortho;
 }
 
@@ -86,26 +118,57 @@ s_gl_ortho * gl_ortho_new (void)
 void gl_ortho_render (s_gl_ortho *ortho)
 {
   assert(ortho);
-  gl_matrix_4d_init_identity(&ortho->matrix);
-  gl_matrix_4d_ortho(&ortho->matrix, ortho->x1, ortho->x2,
-                     ortho->y1, ortho->y2,
-                     ortho->clip_z_near, ortho->clip_z_far);
-  gl_matrix_4d_translate(&ortho->matrix, ortho->position.x,
-                         ortho->position.y, ortho->position.z);
-  gl_matrix_4d_rotate_axis(&ortho->matrix, ortho->rotation.x,
-                           &(s_gl_point_3d) { 1.0, 0.0, 0.0 });
-  gl_matrix_4d_rotate_axis(&ortho->matrix, ortho->rotation.y,
-                           &(s_gl_point_3d) { 0.0, 1.0, 0.0 });
-  gl_matrix_4d_rotate_axis(&ortho->matrix, ortho->rotation.z,
-                           &(s_gl_point_3d) { 0.0, 0.0, 1.0 });
+  assert(glGetError() == GL_NO_ERROR);
+  glUseProgram(ortho->gl_shader_program);
+  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)
+{
+  assert(ortho);
+  assert(glGetError() == GL_NO_ERROR);
+  gl_matrix_4d_init_identity(&ortho->projection_matrix);
+  gl_matrix_4d_ortho(&ortho->projection_matrix, x1, x2, y1, y2,
+                     clip_z_near, clip_z_far);
   glUseProgram(ortho->gl_shader_program);
-  glUniformMatrix4dv(ortho->gl_matrix_loc, 1, GL_FALSE,
-                     &ortho->matrix.xx);
+  glUniformMatrix4dv(ortho->gl_projection_matrix_loc, 1, GL_FALSE,
+                     &ortho->projection_matrix.xx);
+  assert(glGetError() == GL_NO_ERROR);
 }
 
 void gl_ortho_render_end (s_gl_ortho *ortho)
 {
   assert(ortho);
   (void) ortho;
+  assert(glGetError() == GL_NO_ERROR);
   glUseProgram(0);
+  assert(glGetError() == GL_NO_ERROR);
+}
+
+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,
+                     &ortho->model_matrix.xx);
+  assert(glGetError() == GL_NO_ERROR);
+}
+
+void gl_ortho_update_view_matrix (s_gl_ortho *ortho)
+{
+  assert(ortho);
+  assert(glGetError() == GL_NO_ERROR);
+  gl_matrix_4d_init_identity(&ortho->view_matrix);
+  gl_matrix_4d_translate(&ortho->view_matrix, ortho->position.x,
+                         ortho->position.y, ortho->position.z);
+  gl_matrix_4d_rotate_axis(&ortho->view_matrix, ortho->rotation.x,
+                           &(s_gl_point_3d) { 1.0, 0.0, 0.0 });
+  gl_matrix_4d_rotate_axis(&ortho->view_matrix, ortho->rotation.y,
+                           &(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,
+                     &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 6ccde76..91f4c5d 100644
--- a/libc3/window/sdl2/gl_ortho.h
+++ b/libc3/window/sdl2/gl_ortho.h
@@ -27,5 +27,9 @@ 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_update_model_matrix (s_gl_ortho *ortho);
+void         gl_ortho_update_view_matrix (s_gl_ortho *ortho);
 
 #endif /* GL_ORTHO_H */
diff --git a/libc3/window/sdl2/gl_sphere.c b/libc3/window/sdl2/gl_sphere.c
index 496f193..c57f359 100644
--- a/libc3/window/sdl2/gl_sphere.c
+++ b/libc3/window/sdl2/gl_sphere.c
@@ -87,7 +87,7 @@ s_gl_sphere * gl_sphere_init (s_gl_sphere *sphere, uw seg_u, uw seg_v)
     }
     i++;
   }
-  
+  gl_object_update(&sphere->object);
   *sphere = tmp;
   return sphere;
 }
diff --git a/libc3/window/sdl2/sdl2_font.c b/libc3/window/sdl2/sdl2_font.c
index 65a7d76..c5d8423 100644
--- a/libc3/window/sdl2/sdl2_font.c
+++ b/libc3/window/sdl2/sdl2_font.c
@@ -33,6 +33,7 @@ s_sdl2_font * sdl2_font_init (s_sdl2_font *font, const s8 *path)
     str_clean(&font->path);
     return NULL;
   }
+  assert(glGetError() == GL_NO_ERROR);
   font->ftgl_font = ftglCreateTextureFont(font->real_path.ptr.ps8);
   if (! font->ftgl_font) {
     err_write_1("sdl2_font_init: error loading font: ");
@@ -41,6 +42,7 @@ s_sdl2_font * sdl2_font_init (s_sdl2_font *font, const s8 *path)
     str_clean(&font->real_path);
     return NULL;
   }
+  assert(glGetError() == GL_NO_ERROR);
   font->size = 0;
   font->dpi = 72;
   return font;
@@ -57,5 +59,7 @@ void sdl2_font_set_size (s_sdl2_font *font, u32 size, u32 dpi)
 
 void sdl2_font_render_text (const s_sdl2_font *font, const s8 *p)
 {
+  assert(glGetError() == GL_NO_ERROR);
   ftglRenderFont(font->ftgl_font, p, FTGL_RENDER_ALL);
+  assert(glGetError() == GL_NO_ERROR);
 }
diff --git a/libc3/window/sdl2/sdl2_sprite.c b/libc3/window/sdl2/sdl2_sprite.c
index 5f365f8..55d17dd 100644
--- a/libc3/window/sdl2/sdl2_sprite.c
+++ b/libc3/window/sdl2/sdl2_sprite.c
@@ -18,7 +18,9 @@ void sdl2_sprite_bind (const s_sdl2_sprite *sprite, uw frame)
   assert(sprite);
   assert(frame < sprite->frame_count);
   frame %= sprite->frame_count;
+  assert(glGetError() == GL_NO_ERROR);
   glBindTexture(GL_TEXTURE_2D, sprite->texture[frame]);
+  assert(glGetError() == GL_NO_ERROR);
 }
 
 void sdl2_sprite_clean (s_sdl2_sprite *sprite)
@@ -119,6 +121,7 @@ s_sdl2_sprite * sdl2_sprite_init (s_sdl2_sprite *sprite,
   assert(path);
   assert(dim_x);
   assert(dim_y);
+  assert(glGetError() == GL_NO_ERROR);
   sprite->frame_count = (frame_count > 0) ? frame_count :
     (dim_x * dim_y);
   str_init_copy_1(&sprite->path, path);
@@ -319,6 +322,7 @@ s_sdl2_sprite * sdl2_sprite_init (s_sdl2_sprite *sprite,
 	return NULL;
       }
       glGenerateMipmap(GL_TEXTURE_2D);
+      assert(glGetError() == GL_NO_ERROR);
       i++;
       x++;
     }
diff --git a/libc3/window/sdl2/types.h b/libc3/window/sdl2/types.h
index 8db1e38..42073c0 100644
--- a/libc3/window/sdl2/types.h
+++ b/libc3/window/sdl2/types.h
@@ -208,8 +208,12 @@ struct gl_ortho {
   f64 clip_z_far;
   s_gl_point_3d position;
   s_gl_point_3d rotation;
-  s_gl_matrix_4d matrix;
-  u32 gl_matrix_loc;
+  s_gl_matrix_4d projection_matrix;
+  u32         gl_projection_matrix_loc;
+  s_gl_matrix_4d view_matrix;
+  u32         gl_view_matrix_loc;
+  s_gl_matrix_4d model_matrix;
+  u32         gl_model_matrix_loc;
   u32 gl_shader_program;
 };
 
diff --git a/libc3/window/sdl2/window_sdl2.c b/libc3/window/sdl2/window_sdl2.c
index f112a6b..f07c390 100644
--- a/libc3/window/sdl2/window_sdl2.c
+++ b/libc3/window/sdl2/window_sdl2.c
@@ -210,12 +210,16 @@ bool window_sdl2_run (s_window_sdl2 *window)
   fprintf(stderr, "window_sdl2_run: dpi=%.2f, dpi_w=%.2f, dpi_h=%.2f\n",
           window->dpi, window->dpi_w, window->dpi_h);
   SDL_GL_SetSwapInterval(1);
+  assert(glGetError() == GL_NO_ERROR);
   if (! window->load(window)) {
     err_puts("window_sdl2_run: window->load => false");
     goto ko;
   }
+  assert(glGetError() == GL_NO_ERROR);
   while (! quit) {
+    assert(glGetError() == GL_NO_ERROR);
     while (SDL_PollEvent(&sdl_event) != 0) {
+      assert(glGetError() == GL_NO_ERROR);
       switch (sdl_event.type) {
       case SDL_QUIT:
         err_puts("window_sdl2_run: SDL_QUIT");
@@ -250,18 +254,20 @@ bool window_sdl2_run (s_window_sdl2 *window)
             err_puts("window_sdl2_run: window->resize -> false");
             quit = 1;
           }
+          assert(glGetError() == GL_NO_ERROR);
           window->w = sdl_event.window.data1;
           window->h = sdl_event.window.data2;
           SDL_GL_GetDrawableSize(sdl_window, &gl_w, &gl_h);
+          assert(glGetError() == GL_NO_ERROR);
           window->gl_w = gl_w;
           window->gl_h = gl_h;
-          glViewport(0, 0, window->gl_w, window->gl_h);
         }
         break;
       default:
         break;
       }
     }
+    assert(glGetError() == GL_NO_ERROR);
     //glDrawBuffer(GL_BACK);
     if (! window->render(window, NULL)) {
       err_puts("window_sdl2_run: window->render -> false");