Commit e856016b9e1d385c2e17050ad200351d2aa29d9d

Thomas de Grivel 2024-01-15T16:42:06

wip OpenGL 3.3 flies

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
diff --git a/libc3/window/sdl2/demo/flies.c b/libc3/window/sdl2/demo/flies.c
index 4b12317..d78d908 100644
--- a/libc3/window/sdl2/demo/flies.c
+++ b/libc3/window/sdl2/demo/flies.c
@@ -128,8 +128,12 @@ bool flies_load (s_sequence *seq)
     i++;
   }
   fly_init(map);
-  gl_text_init_1(&g_text_flies_in, &g_font_flies, "In 0");
-  gl_text_init_1(&g_text_flies_out, &g_font_flies, "Out 0");
+  if (! gl_text_init_1(&g_text_flies_in, &g_font_flies, "In 0"))
+    return false;
+  gl_text_update(&g_text_flies_in);
+  if (! gl_text_init_1(&g_text_flies_out, &g_font_flies, "Out 0"))
+    return false;
+  gl_text_update(&g_text_flies_out);
   return true;
 }
 
@@ -196,6 +200,7 @@ bool flies_render (s_sequence *seq)
                   GL_LINEAR_MIPMAP_LINEAR);
   gl_matrix_4f_init_identity(&g_ortho.model_matrix);
   gl_matrix_4f_translate(&g_ortho.model_matrix, board_x, 60.0, 0.0);
+  gl_ortho_update_model_matrix(&g_ortho);
   glBlendColor(1.0f, 1.0f, 1.0f, 1.0f);
   buf_init(&buf, false, sizeof(a), a);
   buf_write_1(&buf, "In ");
@@ -218,7 +223,7 @@ bool flies_render (s_sequence *seq)
     g_ortho.model_matrix = matrix;
     gl_matrix_4f_translate(&g_ortho.model_matrix, 0.0, board_item_h, 0.0);
     glBlendColor(0.6f, 0.7f, 0.9f, 1.0f);
-    //glRectd(0, 0, board_w, board_h);
+    gl_ortho_rect(&g_ortho, 0, 0, board_w, board_h);
     address[1] = 0;
     while (address[1] < BOARD_SIZE) {
       y = board_item_h * address[1];
@@ -236,7 +241,7 @@ bool flies_render (s_sequence *seq)
           case BOARD_ITEM_BLOCK:
             glBindTexture(GL_TEXTURE_2D, 0);
             glBlendColor(0.0f, 0.0f, 1.0f, 1.0f);
-            //glRectd(0, 0, board_item_w + 1.0, board_item_h + 1.0);
+            gl_ortho_rect(&g_ortho, 0, 0, board_item_w + 1.0, board_item_h + 1.0);
             break;
           case BOARD_ITEM_FLY:
             matrix_2 = g_ortho.model_matrix; {
diff --git a/libc3/window/sdl2/gl_deprecated.h b/libc3/window/sdl2/gl_deprecated.h
index a0ab259..51b50db 100644
--- a/libc3/window/sdl2/gl_deprecated.h
+++ b/libc3/window/sdl2/gl_deprecated.h
@@ -10,11 +10,11 @@
  * AUTHOR BE CONSIDERED LIABLE FOR THE USE AND PERFORMANCE OF
  * THIS SOFTWARE.
  */
-#ifndef GL_DEPRECATED_H
-#define GL_DEPRECATED_H
+#ifndef C3_GL_DEPRECATED_H
+#define C3_GL_DEPRECATED_H
 
 #include "types.h"
 
 const char * gl_error_string (GLenum error);
 
-#endif /* GL_DEPRECATED_H */
+#endif /* C3_GL_DEPRECATED_H */
diff --git a/libc3/window/sdl2/gl_lines.c b/libc3/window/sdl2/gl_lines.c
index d72c240..e1cadfb 100644
--- a/libc3/window/sdl2/gl_lines.c
+++ b/libc3/window/sdl2/gl_lines.c
@@ -12,6 +12,7 @@
  */
 #include <libc3/c3.h>
 #include "gl_lines.h"
+#include "gl_vertex.h"
 
 s_gl_lines * gl_lines_allocate (s_gl_lines *lines, uw lines_count)
 {
@@ -53,6 +54,8 @@ void gl_lines_render (const s_gl_lines *lines, uw lines_count)
   assert(glGetError() == GL_NO_ERROR);
   glBindVertexArray(lines->gl_vao);
   assert(glGetError() == GL_NO_ERROR);
+  glBindVertexArray(lines->gl_vao);
+  assert(glGetError() == GL_NO_ERROR);
   glDrawArrays(GL_LINES, 0, lines_count * 2);
   assert(glGetError() == GL_NO_ERROR);
 }
@@ -74,20 +77,7 @@ bool gl_lines_update (s_gl_lines *lines, uw lines_count)
   glBufferData(GL_ARRAY_BUFFER, lines_count * 2 * sizeof(s_gl_vertex),
                lines->vertex.data, GL_DYNAMIC_DRAW);
   assert(glGetError() == GL_NO_ERROR);
-  glEnableVertexAttribArray(0);
-  assert(glGetError() == GL_NO_ERROR);
-  glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(s_gl_vertex),
-                        (void *) 0);
-  assert(glGetError() == GL_NO_ERROR);
-  glEnableVertexAttribArray(1);
-  assert(glGetError() == GL_NO_ERROR);
-  glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(s_gl_vertex),
-                        (void *) (3 * sizeof(float)));
-  assert(glGetError() == GL_NO_ERROR);
-  glEnableVertexAttribArray(2);
-  assert(glGetError() == GL_NO_ERROR);
-  glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, sizeof(s_gl_vertex),
-                        (void *) (6 * sizeof(float)));
+  gl_vertex_attrib();
   assert(glGetError() == GL_NO_ERROR);
   return true;
 }
diff --git a/libc3/window/sdl2/gl_object.c b/libc3/window/sdl2/gl_object.c
index a4cd746..fa0215e 100644
--- a/libc3/window/sdl2/gl_object.c
+++ b/libc3/window/sdl2/gl_object.c
@@ -11,6 +11,7 @@
  * THIS SOFTWARE.
  */
 #include <libc3/c3.h>
+#include "gl_deprecated.h"
 #include "gl_object.h"
 #include "gl_vertex.h"
 
@@ -45,7 +46,9 @@ 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);
+  assert(glGetError() == GL_NO_ERROR);
   glGenBuffers(1, &tmp.gl_vbo);
+  assert(glGetError() == GL_NO_ERROR);
   glGenBuffers(1, &tmp.gl_ebo);
   assert(glGetError() == GL_NO_ERROR);
   *object = tmp;
@@ -54,15 +57,22 @@ s_gl_object * gl_object_init (s_gl_object *object)
 
 void gl_object_render (const s_gl_object *object)
 {
+  GLenum error;
   assert(object);
   assert(glGetError() == GL_NO_ERROR);
-  //glBindVertexArray(object->gl_vao);
-  //assert(glGetError() == GL_NO_ERROR);
+  glBindVertexArray(object->gl_vao);
+  assert(glGetError() == GL_NO_ERROR);
   glBindBuffer(GL_ARRAY_BUFFER, object->gl_vbo);
   assert(glGetError() == GL_NO_ERROR);
   glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, object->gl_ebo);
   assert(glGetError() == GL_NO_ERROR);
-  glDrawArrays(GL_TRIANGLES, 0, object->triangle.count * 3);
+  glDrawElements(GL_TRIANGLES, object->triangle.count * 3, GL_UNSIGNED_INT,
+                 NULL);
+  if ((error = glGetError()) != GL_NO_ERROR) {
+    err_write_1("gl_object_render: glDrawElements: ");
+    err_puts(gl_error_string(error));
+    assert(! "gl_object_render: glDrawElements");
+  }
   assert(glGetError() == GL_NO_ERROR);
 }
 
@@ -72,9 +82,12 @@ void gl_object_render_wireframe (const s_gl_object *object)
   assert(glGetError() == GL_NO_ERROR);
   glBindVertexArray(object->gl_vao);
   assert(glGetError() == GL_NO_ERROR);
+  glBindBuffer(GL_ARRAY_BUFFER, object->gl_vbo);
+  assert(glGetError() == GL_NO_ERROR);
   glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, object->gl_ebo);
   assert(glGetError() == GL_NO_ERROR);
-  glDrawElements(GL_LINE_LOOP, object->triangle.count * 3, GL_UNSIGNED_INT, 0);
+  glDrawElements(GL_LINE_LOOP, object->triangle.count * 3, GL_UNSIGNED_INT,
+                 NULL);
   assert(glGetError() == GL_NO_ERROR);
 }
 
@@ -109,27 +122,14 @@ bool gl_object_update (s_gl_object *object)
   glBindBuffer(GL_ARRAY_BUFFER, object->gl_vbo);
   assert(glGetError() == GL_NO_ERROR);
   glBufferData(GL_ARRAY_BUFFER, object->vertex.count * sizeof(s_gl_vertex),
-               object->vertex.data, GL_STATIC_DRAW);
-  assert(glGetError() == GL_NO_ERROR);
-  glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(s_gl_vertex),
-                        (void *) 0);
-  assert(glGetError() == GL_NO_ERROR);
-  glEnableVertexAttribArray(0);
-  assert(glGetError() == GL_NO_ERROR);
-  glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(s_gl_vertex),
-                        (void *) (3 * sizeof(float)));
-  assert(glGetError() == GL_NO_ERROR);
-  glEnableVertexAttribArray(1);
-  assert(glGetError() == GL_NO_ERROR);
-  glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, sizeof(s_gl_vertex),
-                        (void *) (6 * sizeof(float)));
+               object->vertex.data, GL_DYNAMIC_DRAW);
   assert(glGetError() == GL_NO_ERROR);
-  glEnableVertexAttribArray(2);
+  gl_vertex_attrib();
   assert(glGetError() == GL_NO_ERROR);
   glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, object->gl_ebo);
   assert(glGetError() == GL_NO_ERROR);
-  glBufferData(GL_ELEMENT_ARRAY_BUFFER, object->triangle.count * 3,
-               object->triangle.data, GL_STATIC_DRAW);
+  glBufferData(GL_ELEMENT_ARRAY_BUFFER, object->triangle.count * sizeof(s_gl_triangle),
+               object->triangle.data, GL_DYNAMIC_DRAW);
   assert(glGetError() == GL_NO_ERROR);
   return true;
 }
diff --git a/libc3/window/sdl2/gl_ortho.c b/libc3/window/sdl2/gl_ortho.c
index 5efc204..db7aad7 100644
--- a/libc3/window/sdl2/gl_ortho.c
+++ b/libc3/window/sdl2/gl_ortho.c
@@ -12,6 +12,7 @@
  */
 #include <math.h>
 #include <libc3/c3.h>
+#include "gl_deprecated.h"
 #include "gl_ortho.h"
 #include "gl_matrix_4f.h"
 
@@ -159,7 +160,7 @@ void gl_ortho_render (s_gl_ortho *ortho)
                      &ortho->projection_matrix.xx);
   if ((error = glGetError()) != GL_NO_ERROR) {
     err_write_1("gl_ortho_render: glUniformMatrix4fv: ");
-    err_puts((const char *) glewGetErrorString(error));
+    err_puts(gl_error_string(error));
     assert(! "gl_ortho_render: glUniformMatrix4fv");
   }
   assert(glGetError() == GL_NO_ERROR);
diff --git a/libc3/window/sdl2/gl_sphere.c b/libc3/window/sdl2/gl_sphere.c
index 5357d96..b56f17e 100644
--- a/libc3/window/sdl2/gl_sphere.c
+++ b/libc3/window/sdl2/gl_sphere.c
@@ -72,7 +72,7 @@ s_gl_sphere * gl_sphere_init (s_gl_sphere *sphere, uw seg_u, uw seg_v)
   }
   triangle = tmp.object.triangle.data;
   i = 0;
-  while (i < seg_v + 1) {
+  while (i < seg_v) {
     j = 0;
     while (j < seg_u) {
       triangle->a =  i      * seg_u +  j;
diff --git a/libc3/window/sdl2/gl_sprite.c b/libc3/window/sdl2/gl_sprite.c
index 63dc6b3..d812be1 100644
--- a/libc3/window/sdl2/gl_sprite.c
+++ b/libc3/window/sdl2/gl_sprite.c
@@ -121,7 +121,6 @@ s_gl_sprite * gl_sprite_init (s_gl_sprite *sprite, const char *path,
   uw x;
   uw y;
   uw v;
-  uw dimension;
   s_gl_sprite tmp = {0};
   s_gl_triangle *triangle;
   s_gl_vertex *vertex;
@@ -343,14 +342,7 @@ s_gl_sprite * gl_sprite_init (s_gl_sprite *sprite, const char *path,
   free(png_row);
   assert(glGetError() == GL_NO_ERROR);
   gl_object_init(&tmp.object);
-  dimension = 4;
-  array_init(&tmp.object.vertex, sym_1("GL.Vertex"), 1,
-             &dimension);
-  array_allocate(&tmp.object.vertex);
-  dimension = 2;
-  array_init(&tmp.object.triangle, sym_1("GL.Triangle"), 1,
-             &dimension);
-  array_allocate(&tmp.object.triangle);
+  gl_object_allocate(&tmp.object, 4, 2);
   vertex = tmp.object.vertex.data;
   gl_point_3f_init(&vertex[0].position, 0.0, tmp.h, 0.0);
   gl_point_3f_init(&vertex[0].normal, 0.0, 0.0, -1.0);
diff --git a/libc3/window/sdl2/gl_text.c b/libc3/window/sdl2/gl_text.c
index c0e0820..b0ee82d 100644
--- a/libc3/window/sdl2/gl_text.c
+++ b/libc3/window/sdl2/gl_text.c
@@ -26,21 +26,14 @@ void gl_text_clean (s_gl_text *text)
 
 s_gl_text * gl_text_init (s_gl_text *text, const s_gl_font *font)
 {
-  uw dimension;
   s_gl_text tmp = {0};
   assert(glGetError() == GL_NO_ERROR);
   tmp.font = font;
   glGenTextures(1, &tmp.texture);
   assert(glGetError() == GL_NO_ERROR);
   gl_object_init(&tmp.object);
-  dimension = 4;
-  array_init(&tmp.object.vertex, sym_1("GL.Vertex"), 1,
-             &dimension);
-  array_allocate(&tmp.object.vertex);
-  dimension = 2;
-  array_init(&tmp.object.triangle, sym_1("GL.Triangle"), 1,
-             &dimension);
-  array_allocate(&tmp.object.triangle);
+  assert(glGetError() == GL_NO_ERROR);
+  gl_object_allocate(&tmp.object, 4, 2);
   *text = tmp;  
   return text;
 }
@@ -70,6 +63,7 @@ void gl_text_render (const s_gl_text *text)
   assert(text);
   assert(glGetError() == GL_NO_ERROR);
   gl_object_render(&text->object);
+  assert(glGetError() == GL_NO_ERROR);
 }
 
 bool gl_text_render_to_texture (s_gl_text *text)
@@ -222,18 +216,18 @@ bool gl_text_update (s_gl_text *text)
   if (! gl_text_render_to_texture(text))
     return false;
   vertex = text->object.vertex.data;
-  gl_point_3f_init(&vertex[0].position, 0.0, text->h, 0.0);
-  gl_point_3f_init(&vertex[0].normal, 0.0, 0.0, -1.0);
-  gl_point_2f_init(&vertex[0].tex_coord, 0.0, 1.0);
-  gl_point_3f_init(&vertex[1].position, 0.0, 0.0, 0.0);
-  gl_point_3f_init(&vertex[1].normal, 0.0, 0.0, -1.0);
-  gl_point_2f_init(&vertex[1].tex_coord, 0.0, 0.0);
-  gl_point_3f_init(&vertex[2].position, text->w, text->h, 0.0);
-  gl_point_3f_init(&vertex[2].normal, 0.0, 0.0, -1.0);
-  gl_point_2f_init(&vertex[2].tex_coord, 1.0, 1.0);
-  gl_point_3f_init(&vertex[3].position, text->w, 0.0, 0.0);
-  gl_point_3f_init(&vertex[3].normal, 0.0, 0.0, -1.0);
-  gl_point_2f_init(&vertex[3].tex_coord, 1.0, 0.0);
+  gl_point_3f_init(&vertex[0].position,  0.0,     text->h, 0.0);
+  gl_point_3f_init(&vertex[0].normal,    0.0,     0.0,    -1.0);
+  gl_point_2f_init(&vertex[0].tex_coord, 0.0,     1.0);
+  gl_point_3f_init(&vertex[1].position,  0.0,     0.0,     0.0);
+  gl_point_3f_init(&vertex[1].normal,    0.0,     0.0,    -1.0);
+  gl_point_2f_init(&vertex[1].tex_coord, 0.0,     0.0);
+  gl_point_3f_init(&vertex[2].position,  text->w, text->h, 0.0);
+  gl_point_3f_init(&vertex[2].normal,    0.0,     0.0,    -1.0);
+  gl_point_2f_init(&vertex[2].tex_coord, 1.0,     1.0);
+  gl_point_3f_init(&vertex[3].position,  text->w, 0.0,     0.0);
+  gl_point_3f_init(&vertex[3].normal,    0.0,     0.0,    -1.0);
+  gl_point_2f_init(&vertex[3].tex_coord, 1.0,     0.0);
   triangle = text->object.triangle.data;
   gl_triangle_init(triangle + 0, 0, 1, 2);
   gl_triangle_init(triangle + 1, 1, 3, 2);
diff --git a/libc3/window/sdl2/gl_triangle.c b/libc3/window/sdl2/gl_triangle.c
index c2200f1..b54b14b 100644
--- a/libc3/window/sdl2/gl_triangle.c
+++ b/libc3/window/sdl2/gl_triangle.c
@@ -13,8 +13,8 @@
 #include <libc3/c3.h>
 #include "gl_triangle.h"
 
-s_gl_triangle * gl_triangle_init (s_gl_triangle *triangle, u32 a, u32 b,
-                                  u32 c)
+s_gl_triangle * gl_triangle_init (s_gl_triangle *triangle, GLuint a,
+                                  GLuint b, GLuint c)
 {
   assert(triangle);
   triangle->a = a;
diff --git a/libc3/window/sdl2/gl_triangle.h b/libc3/window/sdl2/gl_triangle.h
index 869ea78..31ddae2 100644
--- a/libc3/window/sdl2/gl_triangle.h
+++ b/libc3/window/sdl2/gl_triangle.h
@@ -15,7 +15,7 @@
 
 #include "types.h"
 
-s_gl_triangle * gl_triangle_init (s_gl_triangle *triangle, u32 a, u32 b,
-                                  u32 c);
+s_gl_triangle * gl_triangle_init (s_gl_triangle *triangle, GLuint a,
+                                  GLuint b, GLuint c);
 
 #endif /* GL_TRIANGLE_H */
diff --git a/libc3/window/sdl2/gl_vertex.c b/libc3/window/sdl2/gl_vertex.c
index 8311e82..38aec9a 100644
--- a/libc3/window/sdl2/gl_vertex.c
+++ b/libc3/window/sdl2/gl_vertex.c
@@ -14,6 +14,26 @@
 #include "gl_point_3f.h"
 #include "gl_vertex.h"
 
+void gl_vertex_attrib (void)
+{
+  assert(glGetError() == GL_NO_ERROR);
+  glEnableVertexAttribArray(0);
+  assert(glGetError() == GL_NO_ERROR);
+  glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(s_gl_vertex),
+                        (void *) 0);
+  assert(glGetError() == GL_NO_ERROR);
+  glEnableVertexAttribArray(1);
+  assert(glGetError() == GL_NO_ERROR);
+  glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(s_gl_vertex),
+                        (void *) (3 * sizeof(float)));
+  assert(glGetError() == GL_NO_ERROR);
+  glEnableVertexAttribArray(2);
+  assert(glGetError() == GL_NO_ERROR);
+  glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, sizeof(s_gl_vertex),
+                        (void *) (6 * sizeof(float)));
+  assert(glGetError() == GL_NO_ERROR);
+}
+
 void gl_vertex_transform (s_gl_vertex *vertex,
                           const s_gl_matrix_4f *matrix)
 {
diff --git a/libc3/window/sdl2/gl_vertex.h b/libc3/window/sdl2/gl_vertex.h
index 4578204..d227b0b 100644
--- a/libc3/window/sdl2/gl_vertex.h
+++ b/libc3/window/sdl2/gl_vertex.h
@@ -15,6 +15,7 @@
 
 #include "types.h"
 
+void gl_vertex_attrib (void);
 void gl_vertex_transform (s_gl_vertex *vertex,
                           const s_gl_matrix_4f *matrix);
 
diff --git a/libc3/window/sdl2/types.h b/libc3/window/sdl2/types.h
index 44bf5f9..520cecd 100644
--- a/libc3/window/sdl2/types.h
+++ b/libc3/window/sdl2/types.h
@@ -285,6 +285,7 @@ struct gl_ortho {
   u32 gl_enable_texture_loc;
   u32 gl_texture_loc;
   u32 gl_shader_program;
+  s_gl_square square;
 };
 
 struct gl_sphere {