From bbd9b4e950ec49247d28dc17914b435353726e20 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominik=20Madar=C3=A1sz?= Date: Thu, 21 Mar 2024 06:36:53 +0100 Subject: [PATCH] support multiple PBR materials --- bind/v4k.lua | 2 +- engine/joint/v4k.h | 34 +++++++++++++++++++--------------- engine/split/v4k_render.c | 32 ++++++++++++++++++-------------- engine/split/v4k_render.h | 2 +- engine/v4k.c | 32 ++++++++++++++++++-------------- engine/v4k.h | 2 +- 6 files changed, 58 insertions(+), 46 deletions(-) diff --git a/bind/v4k.lua b/bind/v4k.lua index 6e23b79..4d0bc0e 100644 --- a/bind/v4k.lua +++ b/bind/v4k.lua @@ -1346,7 +1346,7 @@ typedef struct model_t { handle *textures; char **texture_names; material_t* materials; - pbr_material_t pbr_material; + pbr_material_t* pbr_materials; texture_t sky_refl, sky_env; texture_t lightmap; float *lmdata; diff --git a/engine/joint/v4k.h b/engine/joint/v4k.h index 36d1cb0..deba72a 100644 --- a/engine/joint/v4k.h +++ b/engine/joint/v4k.h @@ -17579,8 +17579,8 @@ typedef struct model_t { handle *textures; char **texture_names; array(material_t) materials; + array(pbr_material_t) pbr_materials; - pbr_material_t pbr_material; texture_t sky_refl, sky_env; texture_t lightmap; @@ -373022,18 +373022,6 @@ void model_set_uniforms(model_t m, int shader, mat44 mv, mat44 proj, mat44 view, } if (m.shading == SHADING_PBR) { - const pbr_material_t *material = &m.pbr_material; - shader_colormap( "map_diffuse", material->diffuse ); - shader_colormap( "map_normals", material->normals ); - shader_colormap( "map_specular", material->specular ); - shader_colormap( "map_albedo", material->albedo ); - shader_colormap( "map_roughness", material->roughness ); - shader_colormap( "map_metallic", material->metallic ); - shader_colormap( "map_ao", material->ao ); - shader_colormap( "map_ambient", material->ambient ); - shader_colormap( "map_emissive", material->emissive ); - shader_float( "specular_shininess", material->specular_shininess ); // unused, basic_specgloss.fs only - shader_vec2( "resolution", vec2(window_width(),window_height())); bool has_tex_skysphere = m.sky_refl.id != texture_checker().id; @@ -373791,6 +373779,18 @@ void model_draw_call(model_t m, int shader) { glActiveTexture(GL_TEXTURE1); glBindTexture(GL_TEXTURE_2D, m.lightmap.id); glUniform1i(glGetUniformLocation(shader, "u_lightmap"), 1 ); + } else { + const pbr_material_t *material = &m.pbr_materials[i]; + shader_colormap( "map_diffuse", material->diffuse ); + shader_colormap( "map_normals", material->normals ); + shader_colormap( "map_specular", material->specular ); + shader_colormap( "map_albedo", material->albedo ); + shader_colormap( "map_roughness", material->roughness ); + shader_colormap( "map_metallic", material->metallic ); + shader_colormap( "map_ao", material->ao ); + shader_colormap( "map_ambient", material->ambient ); + shader_colormap( "map_emissive", material->emissive ); + shader_float( "specular_shininess", material->specular_shininess ); // unused, basic_specgloss.fs only } glDrawElementsInstanced(GL_TRIANGLES, 3*im->num_triangles, GL_UNSIGNED_INT, &tris[im->first_triangle], m.num_instances); @@ -373826,8 +373826,12 @@ void model_shading(model_t *m, int shading) { int flags = m->stored_flags; // load pbr material if SHADING_PBR was selected - if (shading == SHADING_PBR && array_count(m->materials) > 0) { - pbr_material(&m->pbr_material, m->materials[0].name); + if (shading == SHADING_PBR) { + for (int i = 0; i < array_count(m->materials); ++i) { + pbr_material_t mat = {0}; + pbr_material(&mat, m->materials[i].name); + array_push(m->pbr_materials, mat); + } } // rebind shader diff --git a/engine/split/v4k_render.c b/engine/split/v4k_render.c index 70bd8f0..97cafee 100644 --- a/engine/split/v4k_render.c +++ b/engine/split/v4k_render.c @@ -2995,18 +2995,6 @@ void model_set_uniforms(model_t m, int shader, mat44 mv, mat44 proj, mat44 view, } if (m.shading == SHADING_PBR) { - const pbr_material_t *material = &m.pbr_material; - shader_colormap( "map_diffuse", material->diffuse ); - shader_colormap( "map_normals", material->normals ); - shader_colormap( "map_specular", material->specular ); - shader_colormap( "map_albedo", material->albedo ); - shader_colormap( "map_roughness", material->roughness ); - shader_colormap( "map_metallic", material->metallic ); - shader_colormap( "map_ao", material->ao ); - shader_colormap( "map_ambient", material->ambient ); - shader_colormap( "map_emissive", material->emissive ); - shader_float( "specular_shininess", material->specular_shininess ); // unused, basic_specgloss.fs only - shader_vec2( "resolution", vec2(window_width(),window_height())); bool has_tex_skysphere = m.sky_refl.id != texture_checker().id; @@ -3764,6 +3752,18 @@ void model_draw_call(model_t m, int shader) { glActiveTexture(GL_TEXTURE1); glBindTexture(GL_TEXTURE_2D, m.lightmap.id); glUniform1i(glGetUniformLocation(shader, "u_lightmap"), 1 ); + } else { + const pbr_material_t *material = &m.pbr_materials[i]; + shader_colormap( "map_diffuse", material->diffuse ); + shader_colormap( "map_normals", material->normals ); + shader_colormap( "map_specular", material->specular ); + shader_colormap( "map_albedo", material->albedo ); + shader_colormap( "map_roughness", material->roughness ); + shader_colormap( "map_metallic", material->metallic ); + shader_colormap( "map_ao", material->ao ); + shader_colormap( "map_ambient", material->ambient ); + shader_colormap( "map_emissive", material->emissive ); + shader_float( "specular_shininess", material->specular_shininess ); // unused, basic_specgloss.fs only } glDrawElementsInstanced(GL_TRIANGLES, 3*im->num_triangles, GL_UNSIGNED_INT, &tris[im->first_triangle], m.num_instances); @@ -3799,8 +3799,12 @@ void model_shading(model_t *m, int shading) { int flags = m->stored_flags; // load pbr material if SHADING_PBR was selected - if (shading == SHADING_PBR && array_count(m->materials) > 0) { - pbr_material(&m->pbr_material, m->materials[0].name); + if (shading == SHADING_PBR) { + for (int i = 0; i < array_count(m->materials); ++i) { + pbr_material_t mat = {0}; + pbr_material(&mat, m->materials[i].name); + array_push(m->pbr_materials, mat); + } } // rebind shader diff --git a/engine/split/v4k_render.h b/engine/split/v4k_render.h index 9746abe..b16e99a 100644 --- a/engine/split/v4k_render.h +++ b/engine/split/v4k_render.h @@ -557,8 +557,8 @@ typedef struct model_t { handle *textures; char **texture_names; array(material_t) materials; + array(pbr_material_t) pbr_materials; - pbr_material_t pbr_material; texture_t sky_refl, sky_env; texture_t lightmap; diff --git a/engine/v4k.c b/engine/v4k.c index 77e622d..2412928 100644 --- a/engine/v4k.c +++ b/engine/v4k.c @@ -20169,18 +20169,6 @@ void model_set_uniforms(model_t m, int shader, mat44 mv, mat44 proj, mat44 view, } if (m.shading == SHADING_PBR) { - const pbr_material_t *material = &m.pbr_material; - shader_colormap( "map_diffuse", material->diffuse ); - shader_colormap( "map_normals", material->normals ); - shader_colormap( "map_specular", material->specular ); - shader_colormap( "map_albedo", material->albedo ); - shader_colormap( "map_roughness", material->roughness ); - shader_colormap( "map_metallic", material->metallic ); - shader_colormap( "map_ao", material->ao ); - shader_colormap( "map_ambient", material->ambient ); - shader_colormap( "map_emissive", material->emissive ); - shader_float( "specular_shininess", material->specular_shininess ); // unused, basic_specgloss.fs only - shader_vec2( "resolution", vec2(window_width(),window_height())); bool has_tex_skysphere = m.sky_refl.id != texture_checker().id; @@ -20938,6 +20926,18 @@ void model_draw_call(model_t m, int shader) { glActiveTexture(GL_TEXTURE1); glBindTexture(GL_TEXTURE_2D, m.lightmap.id); glUniform1i(glGetUniformLocation(shader, "u_lightmap"), 1 ); + } else { + const pbr_material_t *material = &m.pbr_materials[i]; + shader_colormap( "map_diffuse", material->diffuse ); + shader_colormap( "map_normals", material->normals ); + shader_colormap( "map_specular", material->specular ); + shader_colormap( "map_albedo", material->albedo ); + shader_colormap( "map_roughness", material->roughness ); + shader_colormap( "map_metallic", material->metallic ); + shader_colormap( "map_ao", material->ao ); + shader_colormap( "map_ambient", material->ambient ); + shader_colormap( "map_emissive", material->emissive ); + shader_float( "specular_shininess", material->specular_shininess ); // unused, basic_specgloss.fs only } glDrawElementsInstanced(GL_TRIANGLES, 3*im->num_triangles, GL_UNSIGNED_INT, &tris[im->first_triangle], m.num_instances); @@ -20973,8 +20973,12 @@ void model_shading(model_t *m, int shading) { int flags = m->stored_flags; // load pbr material if SHADING_PBR was selected - if (shading == SHADING_PBR && array_count(m->materials) > 0) { - pbr_material(&m->pbr_material, m->materials[0].name); + if (shading == SHADING_PBR) { + for (int i = 0; i < array_count(m->materials); ++i) { + pbr_material_t mat = {0}; + pbr_material(&mat, m->materials[i].name); + array_push(m->pbr_materials, mat); + } } // rebind shader diff --git a/engine/v4k.h b/engine/v4k.h index 1dcfb37..5eb2c4a 100644 --- a/engine/v4k.h +++ b/engine/v4k.h @@ -3646,8 +3646,8 @@ typedef struct model_t { handle *textures; char **texture_names; array(material_t) materials; + array(pbr_material_t) pbr_materials; - pbr_material_t pbr_material; texture_t sky_refl, sky_env; texture_t lightmap;