diff options
author | omniscient <17525998+omnisci3nce@users.noreply.github.com> | 2024-07-26 23:15:22 +1000 |
---|---|---|
committer | omniscient <17525998+omnisci3nce@users.noreply.github.com> | 2024-07-26 23:15:22 +1000 |
commit | 2e2c57a8c04575eec164279a49947cfdba250853 (patch) | |
tree | b5d20aa256de40ebd9bad7c59ab1e8ebb1c47b52 /src | |
parent | f5e5a6fdf58f3135f3211135bfbcb6e70630309f (diff) |
scenes and pbr cleanup to handle missing texture of param
Diffstat (limited to 'src')
-rw-r--r-- | src/core/core.c | 4 | ||||
-rw-r--r-- | src/new_render/pbr.c | 56 | ||||
-rw-r--r-- | src/new_render/pbr.h | 5 | ||||
-rw-r--r-- | src/new_render/render.c | 19 | ||||
-rw-r--r-- | src/new_render/render.h | 4 | ||||
-rw-r--r-- | src/new_render/render_types.h | 27 | ||||
-rw-r--r-- | src/ral/backends/opengl/backend_opengl.c | 11 | ||||
-rw-r--r-- | src/resources/gltf.c | 41 | ||||
-rw-r--r-- | src/systems/input.c | 2 |
9 files changed, 119 insertions, 50 deletions
diff --git a/src/core/core.c b/src/core/core.c index 20bc813..fffb43d 100644 --- a/src/core/core.c +++ b/src/core/core.c @@ -78,6 +78,4 @@ Core* get_global_core() { return &g_core; } GLFWwindow* Core_GetGlfwWindowPtr(Core* core) { return g_core.window; } -struct Renderer* Core_GetRenderer(Core* core) { - return core->renderer; -}
\ No newline at end of file +struct Renderer* Core_GetRenderer(Core* core) { return core->renderer; }
\ No newline at end of file diff --git a/src/new_render/pbr.c b/src/new_render/pbr.c index 6df4e97..ccc4dcf 100644 --- a/src/new_render/pbr.c +++ b/src/new_render/pbr.c @@ -4,9 +4,11 @@ #include "file.h" #include "log.h" #include "maths.h" +#include "mem.h" #include "ral_common.h" #include "ral_impl.h" #include "ral_types.h" +#include "render.h" #include "render_scene.h" #include "render_types.h" #include "shader_layouts.h" @@ -145,13 +147,57 @@ ShaderDataLayout PBRMaterial_GetLayout(void* data) { .label = "normalMap", .kind = BINDING_TEXTURE, }; + ShaderBinding b5 = { .label = "PBR_Params", + .kind = BINDING_BYTES, + .data.bytes.size = sizeof(PBR_Params) }; if (has_data) { - b1.data.texture.handle = d->mat.pbr_albedo_map; - b2.data.texture.handle = d->mat.pbr_metallic_map; - b3.data.texture.handle = d->mat.pbr_ao_map; - b4.data.texture.handle = d->mat.pbr_normal_map; + TextureHandle white1x1 = Render_GetWhiteTexture(); + if (d->mat.albedo_map.raw != INVALID_TEX_HANDLE.raw) { + b1.data.texture.handle = d->mat.albedo_map; + } else { + b1.data.texture.handle = white1x1; + } + + if (d->mat.metallic_roughness_map.raw != INVALID_TEX_HANDLE.raw) { + b2.data.texture.handle = d->mat.metallic_roughness_map; + } else { + b2.data.texture.handle = white1x1; + } + + if (d->mat.ambient_occlusion_map.raw != INVALID_TEX_HANDLE.raw) { + b3.data.texture.handle = d->mat.ambient_occlusion_map; + } else { + b3.data.texture.handle = white1x1; + } + + if (d->mat.normal_map.raw != INVALID_TEX_HANDLE.raw) { + b4.data.texture.handle = d->mat.normal_map; + } else { + b4.data.texture.handle = white1x1; + } + + arena* frame = Render_GetFrameArena(); + PBR_Params* params = arena_alloc(frame, sizeof(PBR_Params)); + params->albedo = d->mat.base_colour; + params->metallic = d->mat.metallic; + params->roughness = d->mat.roughness; + params->ambient_occlusion = d->mat.ambient_occlusion; + b5.data.bytes.data = params; } - return (ShaderDataLayout){ .bindings = { b1, b2, b3, b4 }, .binding_count = 4 }; + return (ShaderDataLayout){ .bindings = { b1, b2, b3, b4, b5 }, .binding_count = 5 }; +} + +Material PBRMaterialDefault() { + return (Material){ .name = "Standard Material", + .kind = MAT_PBR, + .base_colour = vec3(1.0, 1.0, 1.0), + .metallic = 0.0, + .roughness = 0.5, + .ambient_occlusion = 0.0, + .albedo_map = INVALID_TEX_HANDLE, + .metallic_roughness_map = INVALID_TEX_HANDLE, + .normal_map = INVALID_TEX_HANDLE, + .ambient_occlusion_map = INVALID_TEX_HANDLE }; }
\ No newline at end of file diff --git a/src/new_render/pbr.h b/src/new_render/pbr.h index 914975b..0aa9dfe 100644 --- a/src/new_render/pbr.h +++ b/src/new_render/pbr.h @@ -36,7 +36,7 @@ typedef struct PBR_Params { Vec3 albedo; f32 metallic; f32 roughness; - f32 ao; + f32 ambient_occlusion; } PBR_Params; typedef struct PBR_Textures { @@ -48,10 +48,13 @@ typedef struct PBR_Textures { TextureHandle ao_map; } PBR_Textures; + // --- Internal typedef struct MaterialMap MaterialMap; +Material PBRMaterialDefault(); + GPU_Renderpass* PBR_RPassCreate(); GPU_Pipeline* PBR_PipelineCreate(GPU_Renderpass* rpass); diff --git a/src/new_render/render.c b/src/new_render/render.c index cfe68d7..5202cd3 100644 --- a/src/new_render/render.c +++ b/src/new_render/render.c @@ -40,6 +40,7 @@ struct Renderer { // Text_Storage text; ResourcePools* resource_pools; arena frame_arena; + TextureHandle white_1x1; }; Renderer* get_renderer() { return g_core.renderer; } @@ -62,6 +63,7 @@ bool Renderer_Init(RendererConfig config, Renderer* ren, GLFWwindow** out_window INFO("GLFWwindow pointer was provided!!!! Skipping generic glfw init.."); window = optional_window; } else { + INFO("No GLFWwindow provided - creating one"); // NOTE: all platforms use GLFW at the moment but thats subject to change glfwInit(); @@ -95,11 +97,11 @@ bool Renderer_Init(RendererConfig config, Renderer* ren, GLFWwindow** out_window ren->window = window; *out_window = window; - // glfwMakeContextCurrent(ren->window); + glfwMakeContextCurrent(ren->window); // FIXME // DEBUG("Set up GLFW window callbacks"); - // glfwSetWindowSizeCallback(window, Render_WindowSizeChanged); + glfwSetWindowSizeCallback(window, Render_WindowSizeChanged); // set the RAL backend up if (!GPU_Backend_Init(config.window_name, window, ren->resource_pools)) { @@ -126,6 +128,9 @@ bool Renderer_Init(RendererConfig config, Renderer* ren, GLFWwindow** out_window ren->terrain = malloc(sizeof(Terrain_Storage)); // Terrain_Init(ren->terrain); + // load default textures + ren->white_1x1 = TextureLoadFromFile("assets/textures/white1x1.png"); + return true; } @@ -273,4 +278,14 @@ Shadow_Storage* Render_GetShadowStorage() { Terrain_Storage* Render_GetTerrainStorage() { Renderer* ren = Core_GetRenderer(&g_core); return ren->terrain; +} + +TextureHandle Render_GetWhiteTexture() { + Renderer* ren = Core_GetRenderer(&g_core); + return ren->white_1x1; +} + +arena* Render_GetFrameArena() { + Renderer* ren = Core_GetRenderer(&g_core); + return &ren->frame_arena; }
\ No newline at end of file diff --git a/src/new_render/render.h b/src/new_render/render.h index 2df684c..b8c34b9 100644 --- a/src/new_render/render.h +++ b/src/new_render/render.h @@ -77,4 +77,6 @@ typedef struct Terrain_Storage Terrain_Storage; RenderScene* Render_GetScene(); Shadow_Storage* Render_GetShadowStorage(); -Terrain_Storage* Render_GetTerrainStorage();
\ No newline at end of file +Terrain_Storage* Render_GetTerrainStorage(); +TextureHandle Render_GetWhiteTexture(); +arena* Render_GetFrameArena();
\ No newline at end of file diff --git a/src/new_render/render_types.h b/src/new_render/render_types.h index e0bd76b..f4f108a 100644 --- a/src/new_render/render_types.h +++ b/src/new_render/render_types.h @@ -53,22 +53,23 @@ typedef enum MaterialKind { static const char* material_kind_names[] = { "Blinn Phong", "PBR (Textures)", "PBR (Params)", "Count (This should be an error)" }; +/** + * @brief + * @note mostly references https://google.github.io/filament/Filament.html#materialsystem/standardmodel + */ typedef struct Material { char name[64]; - MaterialKind kind; - // parameterised pbr - Vec3 param_albedo; - f32 param_metallic; - f32 param_roughness; - f32 param_ao; - // textured pbr - TextureHandle pbr_albedo_map; - TextureHandle pbr_normal_map; - bool metal_roughness_combined; - TextureHandle pbr_metallic_map; - TextureHandle pbr_roughness_map; - TextureHandle pbr_ao_map; + MaterialKind kind; // at the moment all materials are PBR materials + Vec3 base_colour; // linear RGB {0,0,0} to {1,1,1} + f32 metallic; + f32 roughness; + f32 ambient_occlusion; + TextureHandle albedo_map; + TextureHandle normal_map; + TextureHandle metallic_roughness_map; + TextureHandle ambient_occlusion_map; } Material; + #ifndef TYPED_MATERIAL_ARRAY KITC_DECL_TYPED_ARRAY(Material) #define TYPED_MATERIAL_ARRAY diff --git a/src/ral/backends/opengl/backend_opengl.c b/src/ral/backends/opengl/backend_opengl.c index ad9969f..7929a16 100644 --- a/src/ral/backends/opengl/backend_opengl.c +++ b/src/ral/backends/opengl/backend_opengl.c @@ -37,10 +37,10 @@ bool GPU_Backend_Init(const char* window_name, struct GLFWwindow* window, BackendPools_Init(&context.pool_arena, &context.gpu_pools); context.resource_pools = res_pools; - // glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4); - // glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 1); - // glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); - // glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); + glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4); + glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 1); + glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); + glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); // glad: load all opengl function pointers if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) { @@ -57,8 +57,7 @@ bool GPU_Backend_Init(const char* window_name, struct GLFWwindow* window, } // All of these are no-ops in OpenGL -void GPU_Backend_Shutdown() { /* TODO */ -} +void GPU_Backend_Shutdown() { /* TODO */ } bool GPU_Device_Create(GPU_Device* out_device) { return true; } void GPU_Device_Destroy(GPU_Device* device) {} bool GPU_Swapchain_Create(GPU_Swapchain* out_swapchain) { return true; } diff --git a/src/resources/gltf.c b/src/resources/gltf.c index eb2647d..c575fb9 100644 --- a/src/resources/gltf.c +++ b/src/resources/gltf.c @@ -12,6 +12,7 @@ #include "maths_types.h" #include "mem.h" #include "path.h" +#include "pbr.h" #include "ral_types.h" #include "render.h" #include "render_types.h" @@ -202,49 +203,53 @@ bool model_load_gltf_str(const char *file_string, const char *filepath, Str8 rel cgltf_material gltf_material = data->materials[m]; cgltf_pbr_metallic_roughness pbr = gltf_material.pbr_metallic_roughness; + Material our_material = PBRMaterialDefault(); + 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]; if (albedo_tex_view.texture != NULL) { + char albedo_map_path[1024]; snprintf(albedo_map_path, sizeof(albedo_map_path), "%s/%s", relative_path.buf, albedo_tex_view.texture->image->uri); + our_material.albedo_map = TextureLoadFromFile(albedo_map_path); } else { WARN("GLTF model has no albedo map"); } cgltf_texture_view metal_rough_tex_view = pbr.metallic_roughness_texture; - char metal_rough_map_path[1024]; if (metal_rough_tex_view.texture != NULL) { + 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); + our_material.metallic_roughness_map = TextureLoadFromFile(metal_rough_map_path); } else { - WARN("GLTF model has no metal/rougnness map"); + WARN("GLTF model has no metal/roughness map"); } cgltf_texture_view normal_tex_view = gltf_material.normal_texture; - char normal_map_path[1024]; if (normal_tex_view.texture != NULL) { + char normal_map_path[1024]; snprintf(normal_map_path, sizeof(normal_map_path), "%s/%s", relative_path.buf, normal_tex_view.texture->image->uri); + our_material.normal_map = TextureLoadFromFile(normal_map_path); } else { WARN("GLTF model has no normal map"); } - TextureHandle albedo_map = TextureLoadFromFile(albedo_map_path); - TextureHandle metal_roughness_map = TextureLoadFromFile(metal_rough_map_path); - TextureHandle normal_map = TextureLoadFromFile(normal_map_path); - - // material our_material = - // pbr_material_load(albedo_map_path, normal_map_path, true, metal_rough_map_path, NULL, - // NULL); - Material our_material = { - .kind = MAT_PBR, - .metal_roughness_combined = true, - .pbr_albedo_map = albedo_map, - .pbr_metallic_map = metal_roughness_map, - .pbr_normal_map = normal_map, - }; + // TextureHandle albedo_map = TextureLoadFromFile(albedo_map_path); + // TextureHandle metal_roughness_map = TextureLoadFromFile(metal_rough_map_path); + // TextureHandle normal_map = TextureLoadFromFile(normal_map_path); + + // Material our_material = { + // .kind = MAT_PBR, + // // .metal_roughness_combined = true, + // .albedo_map = albedo_map, + // .metallic_roughness_map = metal_roughness_map, + // .normal_map= normal_map, + // .ambient_occlusion_map = INVALID_TEX_HANDLE, + + // }; // our_material.name = malloc(strlen(gltf_material.name) + 1); u32 string_length = strlen(gltf_material.name) + 1; diff --git a/src/systems/input.c b/src/systems/input.c index b5cdcdb..152b349 100644 --- a/src/systems/input.c +++ b/src/systems/input.c @@ -33,7 +33,7 @@ bool Input_Init(Input_State *input, GLFWwindow *window) { void Input_Shutdown(Input_State *input) {} void Input_Update(Input_State *input) { - // glfwPollEvents(); + glfwPollEvents(); // --- update keyboard input // if we go from un-pressed -> pressed, set as "just pressed" |