summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authoromniscient <17525998+omnisci3nce@users.noreply.github.com>2024-06-10 21:53:14 +1000
committeromniscient <17525998+omnisci3nce@users.noreply.github.com>2024-06-10 21:53:14 +1000
commit76216f91aee936bc57d7e1a1b2c2b63a03ce976a (patch)
tree363da8e607959f1bd39173a0c145e44fdfede436
parenta4c2dfa6c1026859b5c8930b70dfe42317cf32ad (diff)
load pbr textures
-rw-r--r--src/renderer/render.c25
-rw-r--r--src/renderer/render.h11
-rw-r--r--src/renderer/render_types.h58
-rw-r--r--src/resources/gltf.c71
4 files changed, 110 insertions, 55 deletions
diff --git a/src/renderer/render.c b/src/renderer/render.c
index 3a373dc..351a7e0 100644
--- a/src/renderer/render.c
+++ b/src/renderer/render.c
@@ -1,5 +1,6 @@
#include <glfw3.h>
#include "maths_types.h"
+#include "render_types.h"
#define STB_IMAGE_IMPLEMENTATION
#include <stb_image.h>
@@ -260,3 +261,27 @@ texture_handle texture_data_upload(texture_data data, bool free_on_upload) {
}
return handle;
}
+
+/** @brief load all of the texture for a PBR material and returns an unnamed material */
+material pbr_material_load(char* albedo_path, char* normal_path, bool metal_roughness_combined,
+ char* metallic_path, char* roughness_map, char* ao_map) {
+ material m = { 0 };
+ m.kind = MAT_PBR;
+
+ // For now we must have the required textures
+ assert(albedo_path);
+ assert(normal_path);
+ assert(metallic_path);
+ assert(metal_roughness_combined);
+
+ m.mat_data.pbr.metal_roughness_combined = metal_roughness_combined;
+ texture_data tex_data;
+ tex_data = texture_data_load(albedo_path, false);
+ m.mat_data.pbr.albedo_map = texture_data_upload(tex_data, true);
+ tex_data = texture_data_load(normal_path, false);
+ m.mat_data.pbr.normal_map = texture_data_upload(tex_data, true);
+ tex_data = texture_data_load(metallic_path, false);
+ m.mat_data.pbr.metallic_map = texture_data_upload(tex_data, true);
+
+ return m;
+} \ No newline at end of file
diff --git a/src/renderer/render.h b/src/renderer/render.h
index 12c0ce4..35e2899 100644
--- a/src/renderer/render.h
+++ b/src/renderer/render.h
@@ -10,6 +10,7 @@
*/
#pragma once
+#include "file.h"
#include "ral_types.h"
#include "render_types.h"
@@ -64,6 +65,16 @@ texture_data texture_data_load(const char* path, bool invert_y);
*/
texture_handle texture_data_upload(texture_data data, bool free_on_upload);
+/** @brief load all of the texture for a PBR material and returns an unnamed material */
+material pbr_material_load(
+ char* albedo_path,
+ char* normal_path,
+ bool metal_roughness_combined,
+ char* metallic_path,
+ char* roughness_map,
+ char* ao_map
+);
+
buffer_handle buffer_create(const char* debug_name, u64 size);
bool buffer_destroy(buffer_handle buffer);
sampler_handle sampler_create();
diff --git a/src/renderer/render_types.h b/src/renderer/render_types.h
index 3448226..b8ed512 100644
--- a/src/renderer/render_types.h
+++ b/src/renderer/render_types.h
@@ -54,15 +54,6 @@ C side - reload_model():
- remove from transform graph so it isnt tried to be drawn
*/
-CORE_DEFINE_HANDLE(model_handle);
-
-typedef struct model {
- str8 name;
- mesh_darray* meshes;
-} model;
-
-TYPED_POOL(model, model)
-
typedef struct texture {
} texture;
@@ -71,6 +62,13 @@ typedef struct texture_data {
void* image_data;
} texture_data;
+typedef enum material_kind {
+ MAT_BLINN_PHONG,
+ MAT_PBR,
+ MAT_COUNT
+} material_kind;
+static const char* material_kind_names[] = { "Blinn Phong", "PBR", "Count (This should be an error)"};
+
typedef struct blinn_phong_material {
char name[256];
texture diffuse_texture;
@@ -84,18 +82,47 @@ typedef struct blinn_phong_material {
bool is_loaded;
bool is_uploaded;
} blinn_phong_material;
-typedef blinn_phong_material material;
+// typedef blinn_phong_material material;
typedef struct pbr_parameters {
} pbr_parameters;
typedef struct pbr_material {
-
+ texture_handle albedo_map;
+ texture_handle normal_map;
+ bool metal_roughness_combined;
+ texture_handle metallic_map;
+ texture_handle roughness_map;
+ texture_handle ao_map;
} pbr_material;
-// the default blinn-phong material. MUST be initialised with the function below
-extern material DEFAULT_MATERIAL;
+typedef struct material {
+ material_kind kind;
+ union {
+ blinn_phong_material blinn_phong;
+ pbr_material pbr;
+ } mat_data;
+ char* name;
+} material;
+
+#ifndef TYPED_MATERIAL_ARRAY
+KITC_DECL_TYPED_ARRAY(material)
+#define TYPED_MATERIAL_ARRAY
+#endif
+
+CORE_DEFINE_HANDLE(model_handle);
+
+typedef struct model {
+ str8 name;
+ mesh_darray* meshes;
+ material_darray* materials;
+} model;
+
+TYPED_POOL(model, model)
+
+// FIXME: the default blinn-phong material. MUST be initialised with the function below
+// FIXME: extern material DEFAULT_MATERIAL;
void default_material_init();
#ifndef TYPED_MODEL_ARRAY
@@ -103,11 +130,6 @@ KITC_DECL_TYPED_ARRAY(model)
#define TYPED_MODEL_ARRAY
#endif
-#ifndef TYPED_MATERIAL_ARRAY
-KITC_DECL_TYPED_ARRAY(material)
-#define TYPED_MATERIAL_ARRAY
-#endif
-
#ifndef TYPED_ANIMATION_CLIP_ARRAY
#include "animation.h"
KITC_DECL_TYPED_ARRAY(animation_clip)
diff --git a/src/resources/gltf.c b/src/resources/gltf.c
index 373951b..dcb3d96 100644
--- a/src/resources/gltf.c
+++ b/src/resources/gltf.c
@@ -53,6 +53,7 @@ model_handle model_load_gltf(const char *path, bool invert_texture_y) {
model *model = model_pool_alloc(&g_core.models, &handle);
model->name = str8_cstr_view(path);
model->meshes = mesh_darray_new(1);
+ model->materials = material_darray_new(1);
bool success =
model_load_gltf_str(file_string, path, relative_path.path, model, invert_texture_y);
@@ -158,43 +159,38 @@ bool model_load_gltf_str(const char *file_string, const char *filepath, str8 rel
// }
// }
- // // --- 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);
- // }
+ // --- Materials
+ size_t num_materials = data->materials_count;
+ TRACE("Num materials %d", num_materials);
+ for (size_t m = 0; m < num_materials; m++) {
+ cgltf_material gltf_material = data->materials[m];
+ cgltf_pbr_metallic_roughness pbr = gltf_material.pbr_metallic_roughness;
+
+ TRACE("Has PBR metallic roughness ");
+ // we will use base color texture like blinn phong
+ cgltf_texture_view albedo_tex_view = pbr.base_color_texture; // albedo
+ char albedo_map_path[1024];
+ snprintf(albedo_map_path, sizeof(albedo_map_path), "%s/%s", relative_path.buf,
+ albedo_tex_view.texture->image->uri);
+
+ cgltf_texture_view metal_rough_tex_view = pbr.metallic_roughness_texture;
+ char metal_rough_map_path[1024];
+ snprintf(metal_rough_map_path, sizeof(metal_rough_map_path), "%s/%s", relative_path.buf,
+ metal_rough_tex_view.texture->image->uri);
+
+ cgltf_texture_view normal_tex_view = gltf_material.normal_texture;
+ char normal_map_path[1024];
+ snprintf(normal_map_path, sizeof(normal_map_path), "%s/%s", relative_path.buf,
+ normal_tex_view.texture->image->uri);
+
+ material our_material =
+ pbr_material_load(albedo_map_path, normal_map_path, true, metal_rough_map_path, NULL, NULL);
+
+ our_material.name = malloc(strlen(gltf_material.name) + 1);
+ strcpy(our_material.name, gltf_material.name);
+
+ material_darray_push(out_model->materials, our_material);
+ }
// --- Meshes
size_t num_meshes = data->meshes_count;
@@ -291,6 +287,7 @@ bool model_load_gltf_str(const char *file_string, const char *filepath, str8 rel
// mesh.vertex_bone_data = vertex_bone_data_darray_new(1);
if (primitive.material != NULL) {
+ ERROR("Primitive Material %s", primitive.material->name);
// for (int i = 0; i < material_darray_len(out_model->materials); i++) {
// printf("%s vs %s \n", primitive.material->name, out_model->materials->data[i].name);
// if (strcmp(primitive.material->name, out_model->materials->data[i].name) == 0) {