summaryrefslogtreecommitdiff
path: root/src/rendering.c
blob: 8f7104ea214aa8e8c9432f953f67a86611929a7e (plain)
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
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
#include <string.h>

#include <glad/gl.h>
#include <GLFW/glfw3.h>
#include <cglm/cam.h>
#include <cglm/vec2.h>
#include <cglm/mat4.h>


#include <daw/daw.h>
#include <daw/utils.h>
#include <daw/rendering.h>
#include <daw/logging.h>


/* Extern globals */
extern Instance* GLOBAL_PLATFORM;

usize
ShaderBufferDataType_size(u16 flags) {
  const ShaderBufferFlag t = ShaderBuffer_get_data_type(flags);
  switch (t) {
  case ShaderBuffer_DataType_nil: return 0;
  case ShaderBuffer_DataType_f32: return sizeof(f32);
  case ShaderBuffer_DataType_f64: return sizeof(f64);
  case ShaderBuffer_DataType_i8:  return sizeof(i8);
  case ShaderBuffer_DataType_i16: return sizeof(i16);
  case ShaderBuffer_DataType_i32: return sizeof(i32);
  case ShaderBuffer_DataType_i64: return sizeof(i64);
  case ShaderBuffer_DataType_u8:  return sizeof(u8);
  case ShaderBuffer_DataType_u16: return sizeof(u16);
  case ShaderBuffer_DataType_u32: return sizeof(u32);
  case ShaderBuffer_DataType_u64: return sizeof(u64);
  default: return 0;
  }
}

ShaderBufferFlag ShaderBuffer_get_access_frequency(u64 flags) { return flags & ( 7 << 0); }
ShaderBufferFlag ShaderBuffer_get_access_type(u64 flags)      { return flags & ( 7 << 3); }
ShaderBufferFlag ShaderBuffer_get_type(u64 flags)             { return flags & ( 7 << 6); }
ShaderBufferFlag ShaderBuffer_get_data_type(u64 flags)        { return flags & (15 << 9); }

u32 ShaderBuffer_get_gl_type(u64 flags) {
  switch(ShaderBuffer_get_type(flags)) {
    case ShaderBuffer_Type_vertexData:
      return GL_ARRAY_BUFFER;
    case ShaderBuffer_Type_vertexPosition:
      return GL_ARRAY_BUFFER;
    case ShaderBuffer_Type_vertexIndex:
      return GL_ELEMENT_ARRAY_BUFFER;
    default:
      return GL_ARRAY_BUFFER;
  }
}


u32 ShaderBuffer_get_gl_accesstype(u64 flags) {
  switch (ShaderBuffer_get_access_frequency(flags)) {
    case ShaderBuffer_AccessFrequency_stream:
      switch (ShaderBuffer_get_access_type(flags)) {
        case ShaderBuffer_AccessType_draw: return GL_STREAM_DRAW;
        case ShaderBuffer_AccessType_read: return GL_STREAM_READ;
        case ShaderBuffer_AccessType_copy: return GL_STREAM_COPY;
        default: return 0;
      }

    case ShaderBuffer_AccessFrequency_static:
      switch (ShaderBuffer_get_access_type(flags)) {
        case ShaderBuffer_AccessType_draw: return GL_STATIC_DRAW;
        case ShaderBuffer_AccessType_read: return GL_STATIC_READ;
        case ShaderBuffer_AccessType_copy: return GL_STATIC_COPY;
        default: return 0;
      }
    case ShaderBuffer_AccessFrequency_dynamic:
      switch (ShaderBuffer_get_access_type(flags)) {
        case ShaderBuffer_AccessType_draw: return GL_DYNAMIC_DRAW;
        case ShaderBuffer_AccessType_read: return GL_DYNAMIC_READ;
        case ShaderBuffer_AccessType_copy: return GL_DYNAMIC_COPY;
        default: return 0;
      }
    default: return 0;
  }
}

ShaderBufferFlag ShaderBuffer_get_gl_datatype(u64 flags) {
  switch (ShaderBuffer_get_data_type(flags)) {
    case ShaderBuffer_DataType_nil: return GL_NONE;

    case ShaderBuffer_DataType_f32: return GL_FLOAT;
    case ShaderBuffer_DataType_f64: return GL_DOUBLE;

    case ShaderBuffer_DataType_i8:  return GL_BYTE;
    case ShaderBuffer_DataType_i16: return GL_SHORT;
    case ShaderBuffer_DataType_i32: return GL_INT;
    case ShaderBuffer_DataType_i64: return GL_INT64_ARB;

    case ShaderBuffer_DataType_u8:  return GL_UNSIGNED_BYTE;
    case ShaderBuffer_DataType_u16: return GL_UNSIGNED_SHORT;
    case ShaderBuffer_DataType_u32: return GL_UNSIGNED_INT;
    case ShaderBuffer_DataType_u64: return GL_UNSIGNED_INT64_ARB;

    default: return GL_NONE;
  }
}

// `RenderBatch` is used for batch rendering. The struct is used as a
// "management" parent structure to keep track of multiple `RenderObject`s that
// are put into a final `RenderObject` to render.
// `RenderObject`s are copied to the internal `models` array, to which the
// pointer to the copied RenderObject is returned, or NULL if an error occurred.
// If changes are made to a render object the batch should be refreshed.
// Renderbatches assumes that all buffer layouts are the same.

// renderbatch_new: Create a new render batch with space for `count` models.
int renderbatch_new(RenderBatch* renderbatch, usize count) {
  /* TODO: Make it such that you can add identical models with different
   * transforms, so you instead of relying on renderobject[n] to copy to the
   * renderobject we have something like
   *
   * model {
   *  renderobj_idx // index in renderobj[n] that this model represents
   *  transform {
   *    size;
   *    pos;
   *    rotation;
   *  };
   * };
   *
   * For this to work we will likely need to extend the shaderbuffer struct to
   * also hold what type of data the buffer contains, s.t. we can apply the
   * transformation to only geometry data.
   *
   * We'll therefore have both data type and buffer type stored somehow,
   * maybe like we did the ShaderBufferDataType.
   * TODO: Also use shaderbuffertype.
   * */
  if (renderbatch == NULL) {
    ERROR("renderbatch was null!");
    return -1;
  }

  usize numisnstances = count;

  if (count == 0) {
    // Just allocate enough for a couple hundred
    count = 256;
    numisnstances = count * 4;
  }


  renderbatch->msize = sizeof(RenderObject) * count;
  renderbatch->mcount = 0;
  renderbatch->inst_size = sizeof(BatchModelInstance) * numisnstances;
  renderbatch->inst_count = 0;
  renderbatch->models = (RenderObject**)calloc(count, sizeof(RenderObject*));

  if (renderbatch->models == NULL) {
    ERROR("Failed to allocate %lu size of bytes for models array!", sizeof(RenderObject*) * count);
    return -1;
  }

  renderbatch->instances = (BatchModelInstance*)calloc(numisnstances, sizeof(BatchModelInstance));

  if (renderbatch->instances == NULL) {
    ERROR("Failed to allocate %lu size of bytes for batch instances array!", sizeof(BatchModelInstance) * numisnstances);
    return -1;
  }

  memset(&(renderbatch->renderobj), 0, sizeof(RenderObject));

  return 0;
}

// Appends the data in src onto dst. More space for `data` is allocated if
// necessary, in which case a pointer to the new ShaderBuffer is returned.
ShaderBuffer* shaderbuffer_cat(ShaderBuffer* dst, ShaderBuffer *restrict src) {
  if (dst == NULL) {
    ERROR("dst is null");
  }
  else if (src == NULL) {
    ERROR("src is null");
  }

  if (ShaderBuffer_get_data_type(dst->buffertype) != ShaderBuffer_get_data_type(src->buffertype)) {
    ERROR("Failed to concatenate shader buffers, incompatible datatypes: %d != %d", dst->buffertype, src->buffertype);
  }
  if (dst->components != src->components) {
    ERROR("Failed to concatenate shader buffers, incompatible number of components: %d != %d", dst->components, src->components);
  }

  // Assume that we single-handedly control the pointer to the data, copy and
  // free the stuff.

  // Verify the size
  const usize sz_src = src->size_elem * src->count;
  const usize sz_dst = dst->size_elem * dst->count;
  if (dst->data == NULL || sz_dst + sz_src >= dst->size) {
    const usize sz_new = (1 + ((sz_src + sz_dst) / 4096)) * 4096;
    // Resize dst size
    dst->data = realloc(dst->data, sz_new);
    dst->size = sz_new;
  }

  memcpy(dst->data + sz_dst, src->data, sz_src);

  dst->count += src->count;

  return dst;
}

// Add a render object to the render batch.
i32 renderbatch_add(RenderBatch* renderbatch, RenderObject* obj, Transform* t) {
  // Check if its a valid renderbatch
  if (renderbatch == NULL) {
    ERROR("renderbatch was null!");
    return -1;
  }

  // Check whether we have initialized models & instance memory
  if (renderbatch->models == NULL) {
    const usize sz = 8 * sizeof(RenderObject*);
    renderbatch->models = calloc(8, sizeof(RenderObject*));
    renderbatch->msize = sz;
    renderbatch->mcount = 0;
  }

  if (renderbatch->instances == NULL) {
    // Allocate enough for 4 times the models
    const usize modelbufsz = renderbatch->msize / sizeof(RenderObject*);
    const usize sz = 4 * modelbufsz * sizeof(BatchModelInstance);
    renderbatch->instances = calloc(4 * modelbufsz, sizeof(BatchModelInstance));
    renderbatch->inst_size = sz;
    renderbatch->inst_count = 0;
  }

  // The index of the model
  isize model_idx = -1;

  // Find the model, to check if it already exists
  for (usize i = 0; i < renderbatch->mcount; i++) {
    // Compare the model pointers
    if (obj == renderbatch->models[i]) {
      model_idx = (isize)i;
      break;
    }
  }

  // Model doesn't exist, add it
  if (-1 == model_idx) {
    // Check if there's room enough
    if ((1 + renderbatch->mcount) * sizeof(RenderObject*) > renderbatch->msize) {
      // Realloc if necessary
      const usize sz = renderbatch->msize * 2;
      renderbatch->models = realloc(renderbatch->models, sz);
      renderbatch->msize = sz;
    }

    // If this is the first model, we want to copy the renderobj, and
    // shaderbuffer parameters.
    if (renderbatch->mcount == 0) {
      // Shader, VAO, modelviewprojection, and texture, are set when the shaderobj
      // is actually created with RenderObject_new later.
      // The number of buffers should be the same.
      //renderbatch->renderobj.shader = obj->shader;
      //renderbatch->renderobj.texture = obj->texture;

      renderbatch->renderobj.buffer_len = obj->buffer_len;
      if (renderbatch->renderobj.buffer == NULL) {
        renderbatch->renderobj.buffer = calloc(obj->buffer_len, sizeof(ShaderBuffer));
      } else {
        ERROR("RenderObj buffer is already initialized!");
        return -1;
      }

      // Copy each buffers parameters
      for (usize i = 0; i < renderbatch->renderobj.buffer_len; i++) {
        renderbatch->renderobj.buffer[i].buffername = 0;
        renderbatch->renderobj.buffer[i].buffertype = obj->buffer[i].buffertype;
        // Size and count should be zero

        renderbatch->renderobj.buffer[i].components = obj->buffer[i].components;
        renderbatch->renderobj.buffer[i].size_elem = obj->buffer[i].size_elem;
        // Data should also be null
      }
    }

    //// Only concatenate the buffers once we refresh
    //for (usize i = 0; i < renderbatch->renderobj.buffer_len; i++) {
    //  shaderbuffer_cat(&renderbatch->renderobj.buffer[i], &obj->buffer[i]);
    //}

    model_idx = (isize)renderbatch->mcount;
    renderbatch->models[renderbatch->mcount++] = obj;
  }

  // Create batch instance
  // Check if there's room enough
  if ((1 + renderbatch->inst_count) * sizeof(BatchModelInstance) > renderbatch->inst_size) {
    // Realloc if necessary
    const usize sz = renderbatch->inst_size * 2;
    renderbatch->instances = realloc(renderbatch->instances, sz);
    renderbatch->inst_size = sz;
  }

  BatchModelInstance inst = {
    .model_idx = (usize)model_idx,
    .transform = *t,
  };

  // Add it to the batch
  renderbatch->instances[renderbatch->inst_count++] = inst;

  // Return instance index
  return (i32)renderbatch->inst_count - 1;
}

void renderbatch_transform(RenderBatch* renderbatch, usize obj_idx, Transform* t) {
  // TODO: Combine transformation, ie. pos' += pos, etc.
  const usize m = renderbatch->instances[obj_idx].model_idx;
  const RenderObject* model = renderbatch->models[m];
  renderbatch->instances[obj_idx].transform = *t;

  if(renderbatch->inst_count < obj_idx) {
    ERROR("renderbatch_transform: object index is outside range!");
    return;
  }
  /* TODO: Update the model data, we might need to
   * 0. Iteratively go through each renderobj buffer, to find a vertexPosition
   *    buffer,
   * 1. Calculate the models start index in the renderobj,
   * 2. Apply transformation to the model in the renderobj buffer.
   * */
  usize b;
  for (b = 0; b < renderbatch->renderobj.buffer_len; b++) {
    if (ShaderBuffer_Type_vertexPosition != ShaderBuffer_get_type(renderbatch->renderobj.buffer[b].buffertype)
        || ShaderBuffer_DataType_f32 != ShaderBuffer_get_data_type(renderbatch->renderobj.buffer[b].buffertype)) {
      continue;
    }
  }

  usize offset = 0;
  for (usize i = 0; i < obj_idx; i++) {
    const usize idx = renderbatch->instances[i].model_idx;
    offset += renderbatch->models[idx]->buffer->size_elem
      * renderbatch->models[idx]->buffer->count;
  }

  float *data = renderbatch->renderobj.buffer[b].data;
  data = &data[offset];
  const usize len = model->buffer[b].count;

  Transform tt = renderbatch->instances[obj_idx].transform;
  if (model->buffer[b].components == 2) {
    for (usize v = 0; v < len; v += 2) {
      // scale
      // rotate
      // offset
      glm_vec2_add(&data[v], tt.position, &data[v]);
    }
  }
  else if (model->buffer[b].components == 3) {
    for (usize v = 0; v < len; v += 3) {
      // scale
      // rotate
      // offset
      glm_vec3_add(&data[v], tt.position, &data[v]);
    }
  }
}

// renderbatch_refresh: Copy all instances/models in the renderbatch to the
// batchs' model.
int renderbatch_refresh(RenderBatch* renderbatch) {
  const usize bufs = renderbatch->renderobj.buffer_len;
  usize *offsets = calloc(bufs, sizeof(usize));

  // Reset renderobj buffers
  for (usize b = 0; b < renderbatch->renderobj.buffer_len; b++) {
    // Zero the old data
    renderbatch->renderobj.buffer[b].count = 0;
    memset(renderbatch->renderobj.buffer[b].data, 0, renderbatch->renderobj.buffer[b].size);
  }

  // Copy the instances models buffers, and vertex position buffers with translations applied
  for (usize i = 0; i < renderbatch->inst_count; i++) {
    const usize m = renderbatch->instances[i].model_idx;
    const RenderObject* model = renderbatch->models[m];
    Transform t = renderbatch->instances[i].transform;

    for (usize b = 0; b < renderbatch->renderobj.buffer_len; b++) {
      shaderbuffer_cat(&renderbatch->renderobj.buffer[b], &model->buffer[b]);

      if (ShaderBuffer_Type_vertexPosition == ShaderBuffer_get_type(renderbatch->renderobj.buffer[b].buffertype)) {
        if (ShaderBuffer_DataType_f32 != ShaderBuffer_get_data_type(renderbatch->renderobj.buffer[b].buffertype)) {
          WARN("Buffer data type is not f32, skipping transformation...");
          continue;
        }
        // Apply transformation in renderbatch buffer-memory

        float *data = renderbatch->renderobj.buffer[b].data;
        const usize len = model->buffer[b].count;
        // Data points to the start of the model in renderobj
        data = &data[renderbatch->renderobj.buffer[b].count - len];

        if (model->buffer[b].components == 2) {
          for (usize v = 0; v < len; v += 2) {
            // scale
            // rotate
            // offset
            glm_vec2_add(&data[v], t.position, &data[v]);
          }
        }
        else if (model->buffer[b].components == 3) {
          for (usize v = 0; v < len; v += 3) {
            // scale
            // rotate
            // offset
            glm_vec3_add(&data[v], t.position, &data[v]);
          }
        }
      }
    }
  }

  free(offsets);
  return 0;
}


/* Implementations */

/* Clear the screen,
 * To be used inbetween draw calls */
void render_begin(Window* w) {
  glfwMakeContextCurrent(w->window);
  ((GladGLContext*)(w->context))->Clear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
}

void render_present(Window* w, RenderObject *restrict default_quad) {
  const GladGLContext *restrict gl = w->context;
  gl->UseProgram(default_quad->shader.program);
  gl->BindTextureUnit(0, default_quad->texture);
  gl->BindVertexArray(default_quad->vao);

  ShaderBuffer* ibo = NULL;
  for (usize i = 0; i < default_quad->buffer_len; i++) {
    const u32 b_gl_type = ShaderBuffer_get_gl_type(default_quad->buffer[i].buffertype);
    if (b_gl_type == GL_ELEMENT_ARRAY_BUFFER) {
      ibo = &default_quad->buffer[i];
    }

    gl->EnableVertexAttribArray((u32)i);
    gl->BindBuffer(b_gl_type, default_quad->buffer[i].buffername);
    gl->VertexAttribPointer(
        // index of the attribute
        (u32)i,
        // number of component
        (i32)default_quad->buffer[i].components,
        // type
        ShaderBuffer_get_gl_datatype(default_quad->buffer[i].buffertype),
        // normalized?
        GL_FALSE,
        // stride
        0,
        // array buffer offset
        (void*)0
        );
  }

  // Draw the model !
  const i32 sz = (i32)(default_quad->buffer->count * default_quad->buffer->size_elem);
  if (ibo) {
    gl->DrawElements(
        GL_TRIANGLES,
        (i32)ibo->count,
        ShaderBuffer_get_gl_datatype(ibo->buffertype),
        (void*)0
        );
  } else {
    // Starting from vertex 0; 3 vertices total -> 1 triangle
    gl->DrawArrays(GL_TRIANGLES, 0, sz);
  }

  for (u32 i = 0; i < default_quad->buffer_len; i++) {
    gl->DisableVertexAttribArray(i);
  }

  gl->BindVertexArray(0);

  glfwSwapBuffers(w->window);
}

void window_reset_drawing(void) {
  // Clear
}

void r_perspective(Camera *c, f32 fov, ivec2 windowsize) {
  const f32 ratio = (f32)windowsize[0] / (f32)windowsize[1];

  c->type = Camera_Perspective;
  c->parameters.perspective.fov = fov;

  glm_perspective(glm_rad(fov), ratio, 0.1f, 100.0f, c->per);
}

void r_perspective_ortho(Camera *c, f32 sz, ivec2 windowsize) {
  const f32 ratio = (f32)windowsize[0] / (f32)windowsize[1];

  c->type = Camera_Orthogonal;
  c->parameters.orthogonal.sz = sz;

  glm_ortho(-sz * ratio, sz * ratio, -sz, sz, -sz * 10.f, sz * 10.f, c->per);
}

void r_set_camera(RenderTargets *restrict t, i32 framebuffer_idx, Camera *restrict c) {
  ASSERT(t != NULL);
  ASSERT(framebuffer_idx == -1 || framebuffer_idx < (i32)t->framebuffer_len);

  t->cam[framebuffer_idx] = c;
}

void r_reset_camera(Camera* c, ivec2 windowsize) {
  if (c->type == Camera_Perspective) {
    r_perspective(c, c->parameters.perspective.fov, windowsize);
  }
  else if (c->type == Camera_Orthogonal) {
    r_perspective_ortho(c, c->parameters.orthogonal.sz, windowsize);
  }
}


void r_draw_model(void *restrict context, RenderTargets *restrict t, u32 framebuffer_idx, RenderObject* o, vec4 pos) {
  ASSERT(context != NULL);
  ASSERT(t != NULL);
  ASSERT(t->framebuffer != NULL);
  ASSERT(t->cam != NULL);
  ASSERT(t->cam[framebuffer_idx] != NULL);

  const GladGLContext *restrict gl = context;
  Camera c = *t->cam[framebuffer_idx];
  //const f32 s = pos[3];

  mat4 view; // view
  vec3 angle; // viewing angle / direction of the camera
  mat4 camera_matrix;

  glm_vec3_sub(c.pos, c.dir, angle);
  glm_lookat(c.pos, angle, GLM_YUP, view);
  glm_mat4_mul(c.per, view, camera_matrix);

  gl->UseProgram(o->shader.program);
  // ENUMERATE FRAMEBUFFER TEXTURES
  gl->BindTextureUnit(0, o->texture);

  {
    mat4 model = GLM_MAT4_IDENTITY_INIT;
    mat4 modelviewprojection;

    model[3][0] = pos[0];
    model[3][1] = pos[1];
    model[3][2] = pos[2];

    // modelviewprojection = p * view * model
    glm_mat4_mul(model, camera_matrix, modelviewprojection);

    // TODO: Do this only once during initialization
    gl->UniformMatrix4fv(o->mvp, 1, GL_FALSE, &modelviewprojection[0][0]);
    gl->UniformMatrix4fv(o->model_position, 1, GL_FALSE, &model[0][0]);
  }

  // TODO the buffers need to be abstracted a bit more
  gl->BindVertexArray(o->vao);

  ShaderBuffer* ibo = NULL;
  for (usize i = 0; i < o->buffer_len; i++) {
    const u32 b_gl_type = ShaderBuffer_get_gl_type(o->buffer[i].buffertype);
    if (b_gl_type == GL_ELEMENT_ARRAY_BUFFER) {
      ibo = &o->buffer[i];
    }

    gl->EnableVertexAttribArray((u32)i);
    gl->BindBuffer(b_gl_type, o->buffer[i].buffername);
    gl->VertexAttribPointer(
        // index of the attribute
        (u32)i,
        // number of component
        (i32)o->buffer[i].components,
        // type
        ShaderBuffer_get_gl_datatype(o->buffer[i].buffertype),
        // normalized?
        GL_FALSE,
        // stride
        0,
        // array buffer offset
        (void*)0
        );
  }

  // Draw the model !
  const i32 sz = (i32)(o->buffer->count * o->buffer->size_elem);
  if (ibo) {
    gl->DrawElements(
        GL_TRIANGLES,
        (i32)ibo->count,
        ShaderBuffer_get_gl_datatype(ibo->buffertype),
        (void*)0
        );
  } else {
    // Starting from vertex 0; 3 vertices total -> 1 triangle
    gl->DrawArrays(GL_TRIANGLES, 0, sz);
  }

  for (u32 i = 0; i < o->buffer_len; i++) {
    gl->DisableVertexAttribArray(i);
  }

  gl->BindVertexArray(0);
}


Texture createTextureFromImageData(unsigned char* image_data, i32 width, i32 height, u8 components) {
  Window* restrict w = GLOBAL_PLATFORM->window;
  Texture t;
  t.width = width;
  t.height = height;

  if (w->renderer != WINDOW_RENDERER_OPENGL) {
    ERROR("createTextureFromImageData not implemented for chosen renderer!");
    return (Texture){.id = 0, .width = 0, .height = 0};
  }

  const GladGLContext* gl = w->context;
  u32 err = gl->GetError();
  if (err) {
    ERROR("There's already something wrong!");
  }

  gl->CreateTextures(GL_TEXTURE_2D, 1, &t.id);
  err = gl->GetError();
  if (err) {
    if (err == GL_INVALID_ENUM) {
      ERROR("Failed to create texture! GL_INVALID_ENUM");
    }
    else if (err == GL_INVALID_VALUE) {
      ERROR("Failed to create texture! GL_INVALID_VALUE");
    }
    else {
      ERROR("Failed to create texture!");
    }
  }

  /* TODO: Support more formats than rgb and rgba, such as gray, gray/alpha, etc.*/
  u32 format = GL_RGB;
  if (components == 4) format = GL_RGBA;

  /* TODO: Don't force internal format to RGB */

  gl->TextureStorage2D(t.id, 1, GL_RGB8, width, height);
  err = gl->GetError();
  if (err) {
    char* errstr = NULL;
    switch (err) {
      case GL_INVALID_ENUM:
        errstr = "GL_INVALID_ENUM"; break;
      case GL_INVALID_VALUE:
        errstr = "GL_INVALID_VALUE"; break;
      case GL_INVALID_OPERATION:
        errstr = "GL_INVALID_OPERATION"; break;
      default:
        errstr = "unknown"; break;
    }
    ERROR("Failed to allocate memory for texture! %dx%d size (%s)", width, height, errstr);
  }
  gl->TextureSubImage2D(t.id, 0,
      // offset, size
      0, 0, width, height,
      format, GL_UNSIGNED_BYTE, image_data);
  err = gl->GetError();
  if (err) {
    ERROR("Failed to copy image data!");
  }

  gl->TextureParameteri(t.id, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  gl->TextureParameteri(t.id, GL_TEXTURE_MIN_FILTER, GL_NEAREST);

  return t;
}

void r_init_renderstack(
    usize num_fbuf, usize num_buf,
    FramebufferParameters *restrict fb_params,
    u32 *restrict buffer_params
    ) {
  window_init_renderstack(GLOBAL_PLATFORM->window, num_fbuf, num_buf, fb_params, buffer_params);
}

void r_create_framebuffers(void* restrict ctx, u32* restrict framebuffer_array,
                       usize num_targets) {
  const GladGLContext* gl = (GladGLContext*)ctx;

  gl->CreateFramebuffers(num_targets, framebuffer_array);
}

void r_destroy_framebuffers(void* restrict ctx, u32* restrict framebuffer_array,
                       usize num_targets) {
  const GladGLContext* gl = (GladGLContext*)ctx;

  gl->DeleteFramebuffers(num_targets, framebuffer_array);
}

void r_create_textures(void* restrict ctx, u32* restrict texture_array,
                       u32* restrict texture_parameters,
                       ivec3* restrict texture_size, usize num_targets) {
  const GladGLContext* gl = (GladGLContext*)ctx;

  u32 texture_dim = BUFFERPARAMETER_TEXTURE_GET_DIMENSION(texture_parameters[0]);
  u32 texture_format = BUFFERPARAMETER_TEXTURE_GET_DIMENSION(texture_parameters[0]);
  u32 gl_texture_format;
  u32 err;

  for (usize i = 1; i < num_targets; i++) {
    ASSERT(texture_dim == BUFFERPARAMETER_TEXTURE_GET_DIMENSION(texture_parameters[i]));
    ASSERT(texture_format == BUFFERPARAMETER_TEXTURE_GET_FORMAT(texture_parameters[i]));
  }

  switch (texture_format) {
    case BUFFERPARAMETER_TEXTURE_FMT_RGBA8: gl_texture_format = GL_RGBA8; break;
    case BUFFERPARAMETER_TEXTURE_FMT_SRGB8: gl_texture_format = GL_SRGB; break;
    case BUFFERPARAMETER_TEXTURE_FMT_SRGBA8: gl_texture_format = GL_SRGB8_ALPHA8; break;
    default:
      ERROR("Failed to convert format to GL internal format!");
      exit(EXIT_FAILURE);
  }

  // Convert the texturetype to GL texturetype
  switch (texture_dim) {
    case BUFFERPARAMETER_TEXTURE_1D:
      gl->CreateTextures(GL_TEXTURE_1D, num_targets, texture_array);
      for (usize i = 0; i < num_targets; i++) {
        gl->TextureStorage1D(texture_array[i], 1, gl_texture_format, *texture_size[0]);
      }
      break;

    case BUFFERPARAMETER_TEXTURE_2D:
      gl->CreateTextures(GL_TEXTURE_2D, num_targets, texture_array);
      for (usize i = 0; i < num_targets; i++) {
        gl->TextureStorage2D(texture_array[i], 1, gl_texture_format, *texture_size[0], *texture_size[1]);
      }
      break;

    case BUFFERPARAMETER_TEXTURE_3D:
      gl->CreateTextures(GL_TEXTURE_3D, num_targets, texture_array);
      for (usize i = 0; i < num_targets; i++) {
        gl->TextureStorage3D(texture_array[i], 1, gl_texture_format, *texture_size[0], *texture_size[1], *texture_size[2]);
      }
      break;

    case BUFFERPARAMETER_TEXTURE_4D:
      ERROR("4D textures are unsupported!");
      exit(EXIT_FAILURE);

    default:
      ERROR("Failed to convert dimensionality to GL_TEXTURE_XD");
      exit(EXIT_FAILURE);
  }
  err = gl->GetError();

  if (err) {
    ERROR("Failed to create textures!");
  }

  // This should probably be changed in the future
  for (usize i = 0; i < num_targets; i++) {
    gl->TextureParameteri(texture_array[i], GL_TEXTURE_MAG_FILTER, GL_NEAREST);
    gl->TextureParameteri(texture_array[i], GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  }

  err = gl->GetError();

  if (err) {
    ERROR("Failed to set texture parameters!");
  }
}

void r_destroy_textures(void *restrict ctx, u32* textures, usize num_textures) {
  const GladGLContext* gl = (GladGLContext*)ctx;
  gl->DeleteTextures(num_textures, textures);
}

void r_attach_buffers(void *restrict ctx, u32 fbo, u32* buffers, u32* buffer_parameters, i32 num_buffers) {
  const GladGLContext *restrict gl = (GladGLContext*)ctx;

  u32 err;

  for (i32 i = 0; i < num_buffers; i++) {
    BufferType t = BUFFERPARAMETER_GET_TYPE(buffer_parameters[i]);

    if (t != BufferType_texture) UNIMPLEMENTED;

    // TODO: FINISH ME
    gl->NamedFramebufferTexture(fbo, GL_COLOR_ATTACHMENT0 + i, buffers[i], 0);

    err = gl->GetError();
    if (err) {
      ERROR("Failed to attach buffer!");
    }
  }
}