From 0c770ecad71bb540d36402311e01a2b6b94242fd Mon Sep 17 00:00:00 2001
From: omnisci3nce <17525998+omnisci3nce@users.noreply.github.com>
Date: Wed, 3 Apr 2024 22:42:24 +1100
Subject: load gltf vertices and indices. need to move to git LFS!

---
 examples/gltf_loading/ex_gltf_loading.c | 67 +++++++++++++++++++++++++++++++++
 1 file changed, 67 insertions(+)
 create mode 100644 examples/gltf_loading/ex_gltf_loading.c

(limited to 'examples/gltf_loading/ex_gltf_loading.c')

diff --git a/examples/gltf_loading/ex_gltf_loading.c b/examples/gltf_loading/ex_gltf_loading.c
new file mode 100644
index 0000000..e00a9de
--- /dev/null
+++ b/examples/gltf_loading/ex_gltf_loading.c
@@ -0,0 +1,67 @@
+#include <glfw3.h>
+
+#include "camera.h"
+#include "core.h"
+#include "loaders.h"
+#include "maths.h"
+#include "render.h"
+#include "render_types.h"
+
+const vec3 pointlight_positions[4] = {
+  { 0.7, 0.2, 2.0 },
+  { 2.3, -3.3, -4.0 },
+  { -4.0, 2.0, -12.0 },
+  { 0.0, 0.0, -3.0 },
+};
+point_light point_lights[4];
+
+int main() {
+  core* core = core_bringup();
+
+  model_handle cube_handle =
+      model_load_gltf(core, "assets/models/gltf/DamagedHelmet/glTF/DamagedHelmet.gltf", false);
+  // model_handle cube_handle = model_load_gltf(core, "assets/models/gltf/scifi_girl/scene.gltf",
+  // false);
+  model* cube = &core->models->data[cube_handle.raw];
+  model_upload_meshes(&core->renderer, cube);
+
+  vec3 camera_pos = vec3(5., 0., 0.);
+  vec3 camera_front = vec3_normalise(vec3_negate(camera_pos));
+  camera cam = camera_create(camera_pos, camera_front, VEC3_NEG_Z, deg_to_rad(45.0));
+  // 4. create lights
+
+  // directional (sun) light setup
+  directional_light dir_light = { .direction = (vec3){ -0.2, -1.0, -0.3 },
+                                  .ambient = (vec3){ 0.2, 0.2, 0.2 },
+                                  .diffuse = (vec3){ 0.5, 0.5, 0.5 },
+                                  .specular = (vec3){ 1.0, 1.0, 1.0 } };
+  // point lights setup
+  for (int i = 0; i < 4; i++) {
+    point_lights[i].position = pointlight_positions[i];
+    point_lights[i].ambient = (vec3){ 0.05, 0.05, 0.05 };
+    point_lights[i].diffuse = (vec3){ 0.8, 0.8, 0.8 };
+    point_lights[i].specular = (vec3){ 1.0, 1.0, 1.0 };
+    point_lights[i].constant = 1.0;
+    point_lights[i].linear = 0.09;
+    point_lights[i].quadratic = 0.032;
+  }
+
+  scene our_scene = { .dir_light = dir_light, .n_point_lights = 4 };
+  memcpy(&our_scene.point_lights, &point_lights, sizeof(point_light[4]));
+
+  while (!glfwWindowShouldClose(core->renderer.window)) {
+    input_update(&core->input);
+    // threadpool_process_results(&core->threadpool, 1);
+
+    render_frame_begin(&core->renderer);
+
+    // Draw the model
+    transform model_tf = transform_create(vec3(0.0, 0.2, 0.0), quat_ident(),
+                                          1.8);  // make the backpack a bit bigger
+    draw_model(&core->renderer, &cam, cube, model_tf, &our_scene);
+
+    render_frame_end(&core->renderer);
+  }
+
+  return 0;
+}
-- 
cgit v1.2.3-70-g09d2


From 1d8d7c04786ab3964778e6f21fae145f661bb533 Mon Sep 17 00:00:00 2001
From: omniscient <17525998+omnisci3nce@users.noreply.github.com>
Date: Thu, 4 Apr 2024 19:45:19 +1100
Subject: loading textures

---
 examples/gltf_loading/ex_gltf_loading.c | 14 ++++++-
 src/maths/maths.h                       | 12 ++++++
 src/resources/gltf.c                    | 72 ++++++++++++++++++++++++++++-----
 3 files changed, 86 insertions(+), 12 deletions(-)

(limited to 'examples/gltf_loading/ex_gltf_loading.c')

diff --git a/examples/gltf_loading/ex_gltf_loading.c b/examples/gltf_loading/ex_gltf_loading.c
index e00a9de..867ddb2 100644
--- a/examples/gltf_loading/ex_gltf_loading.c
+++ b/examples/gltf_loading/ex_gltf_loading.c
@@ -4,6 +4,7 @@
 #include "core.h"
 #include "loaders.h"
 #include "maths.h"
+#include "maths_types.h"
 #include "render.h"
 #include "render_types.h"
 
@@ -16,6 +17,10 @@ const vec3 pointlight_positions[4] = {
 point_light point_lights[4];
 
 int main() {
+  double currentFrame = glfwGetTime();
+  double lastFrame = currentFrame;
+  double deltaTime;
+
   core* core = core_bringup();
 
   model_handle cube_handle =
@@ -50,13 +55,20 @@ int main() {
   memcpy(&our_scene.point_lights, &point_lights, sizeof(point_light[4]));
 
   while (!glfwWindowShouldClose(core->renderer.window)) {
+    currentFrame = glfwGetTime();
+    deltaTime = currentFrame - lastFrame;
+    lastFrame = currentFrame;
     input_update(&core->input);
     // threadpool_process_results(&core->threadpool, 1);
 
     render_frame_begin(&core->renderer);
 
     // Draw the model
-    transform model_tf = transform_create(vec3(0.0, 0.2, 0.0), quat_ident(),
+    static f32 angle = 0.0;
+    static f32 rot_speed = 0.5;
+    quat rot = quat_from_axis_angle(VEC3_Z, fmod(angle, TAU), true);
+    angle += (rot_speed * deltaTime);
+    transform model_tf = transform_create(vec3(0.0, 0.1, -0.1), rot,
                                           1.8);  // make the backpack a bit bigger
     draw_model(&core->renderer, &cam, cube, model_tf, &our_scene);
 
diff --git a/src/maths/maths.h b/src/maths/maths.h
index d832739..95ced1d 100644
--- a/src/maths/maths.h
+++ b/src/maths/maths.h
@@ -66,6 +66,18 @@ static inline quat quat_normalise(quat a) {
 
 static inline quat quat_ident() { return (quat){ .x = 0.0, .y = 0.0, .z = 0.0, .w = 1.0 }; }
 
+static quat quat_from_axis_angle(vec3 axis, f32 angle, bool normalize) {
+    const f32 half_angle = 0.5f * angle;
+    f32 s = sinf(half_angle);
+    f32 c = cosf(half_angle);
+
+    quat q = (quat){s * axis.x, s * axis.y, s * axis.z, c};
+    if (normalize) {
+        return quat_normalise(q);
+    }
+    return q;
+}
+
 // --- Matrix Implementations
 
 static inline mat4 mat4_ident() {
diff --git a/src/resources/gltf.c b/src/resources/gltf.c
index 69400b7..b269fcd 100644
--- a/src/resources/gltf.c
+++ b/src/resources/gltf.c
@@ -1,11 +1,13 @@
 #include <assert.h>
 #include <stdlib.h>
+#include <string.h>
 #include "core.h"
 #include "defines.h"
 #include "file.h"
 #include "loaders.h"
 #include "log.h"
 #include "path.h"
+#include "render.h"
 #include "render_types.h"
 #include "str.h"
 
@@ -23,8 +25,8 @@ KITC_DECL_TYPED_ARRAY(vec2)
 KITC_DECL_TYPED_ARRAY(u32)
 KITC_DECL_TYPED_ARRAY(face)
 
-bool model_load_gltf_str(const char *file_string, const char *filepath, model *out_model,
-                         bool invert_textures_y);
+bool model_load_gltf_str(const char *file_string, const char *filepath, str8 relative_path,
+                         model *out_model, bool invert_textures_y);
 
 model_handle model_load_gltf(struct core *core, const char *path, bool invert_texture_y) {
   size_t arena_size = 1024;
@@ -42,7 +44,8 @@ model_handle model_load_gltf(struct core *core, const char *path, bool invert_te
   model.meshes = mesh_darray_new(1);
   model.materials = material_darray_new(1);
 
-  bool success = model_load_gltf_str(file_string, path, &model, invert_texture_y);
+  bool success =
+      model_load_gltf_str(file_string, path, relative_path.path, &model, invert_texture_y);
 
   if (!success) {
     FATAL("Couldnt load OBJ file at path %s", path);
@@ -59,8 +62,8 @@ model_handle model_load_gltf(struct core *core, const char *path, bool invert_te
 
 // TODO: Brainstorm how I can make this simpler and break it up into more testable pieces
 
-bool model_load_gltf_str(const char *file_string, const char *filepath, model *out_model,
-                         bool invert_textures_y) {
+bool model_load_gltf_str(const char *file_string, const char *filepath, str8 relative_path,
+                         model *out_model, bool invert_textures_y) {
   TRACE("Load GLTF from string");
 
   // Setup temps
@@ -81,6 +84,45 @@ bool model_load_gltf_str(const char *file_string, const char *filepath, model *o
   cgltf_load_buffers(&options, data, filepath);
   DEBUG("loaded buffers");
 
+  // --- Materials
+  TRACE("Num materials %d", data->materials_count);
+  size_t num_materials = data->materials_count;
+  for (size_t m = 0; m < num_materials; m++) {
+    cgltf_material gltf_material = data->materials[m];
+    material our_material = DEFAULT_MATERIAL;
+
+    strcpy(our_material.name, gltf_material.name);
+
+    cgltf_pbr_metallic_roughness pbr = gltf_material.pbr_metallic_roughness;
+    if (gltf_material.has_pbr_metallic_roughness) {
+      // we will use base color texture like blinn phong
+      cgltf_texture_view diff_tex_view = pbr.base_color_texture;
+
+      char diffuse_map_path[1024];
+      snprintf(diffuse_map_path, sizeof(diffuse_map_path), "%s/%s", relative_path.buf,
+               diff_tex_view.texture->image->uri);
+
+      strcpy(our_material.diffuse_tex_path, diffuse_map_path);
+      texture diffuse_texture = texture_data_load(our_material.diffuse_tex_path, false);
+      texture_data_upload(&diffuse_texture);
+      our_material.diffuse_texture = diffuse_texture;
+
+      cgltf_texture_view specular_tex_view = pbr.metallic_roughness_texture;
+
+      char specular_map_path[1024];
+      snprintf(specular_map_path, sizeof(specular_map_path), "%s/%s", relative_path.buf,
+               specular_tex_view.texture->image->uri);
+
+      strcpy(our_material.specular_tex_path, specular_map_path);
+      texture specular_texture = texture_data_load(our_material.specular_tex_path, false);
+      texture_data_upload(&specular_texture);
+      our_material.specular_texture = specular_texture;
+    }
+
+    material_darray_push(out_model->materials, our_material);
+  }
+
+  // --- Meshes
   TRACE("Num meshes %d", data->meshes_count);
   size_t num_meshes = data->meshes_count;
   for (size_t m = 0; m < num_meshes; m++) {
@@ -157,26 +199,34 @@ bool model_load_gltf_str(const char *file_string, const char *filepath, model *o
       for (cgltf_size i = 0; i < indices->count; ++i) {
         vertex vert;
         cgltf_uint index = mesh.indices[i];
-        // printf("Index %d\n", index);
         vert.position = tmp_positions->data[index];
         vert.normal = tmp_normals->data[index];
         vert.uv = tmp_uvs->data[index];
         vertex_darray_push(mesh.vertices, vert);
       }
-
     } else {
       mesh.has_indices = false;
       return false;  // TODO
     }
 
-    // HACK
-    mesh.material_index = 0;
+    if (primitive.material != NULL) {
+      for (int i = 0; i < material_darray_len(out_model->materials); i++) {
+        if (strcmp(primitive.material->name, out_model->materials->data[i].name)) {
+          TRACE("Found material");
+          mesh.material_index = i;
+          break;
+        }
+      }
+    }
 
     mesh_darray_push(out_model->meshes, mesh);
   }
 
-  material_darray_push(out_model->materials, DEFAULT_MATERIAL);
-
+  for (int i = 0; i < out_model->meshes->len; i++) {
+    u32 mat_idx = out_model->meshes->data[i].material_index;
+    printf("Mesh %d Mat index %d Mat name %s\n", i, mat_idx,
+           out_model->materials->data[mat_idx].name);
+  }
   return true;
 }
 
-- 
cgit v1.2.3-70-g09d2