From ac0dd11cc18abb81cf9d83c6dce687da9d080ce7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominik=20Madar=C3=A1sz?= Date: Wed, 28 Aug 2024 17:39:46 +0200 Subject: [PATCH] further improve sh calcs --- demos/09-envmap.c | 11 +++++++---- engine/joint/v4k.h | 12 +++++++++++- engine/split/v4k_render.c | 12 +++++++++++- engine/v4k.c | 12 +++++++++++- 4 files changed, 40 insertions(+), 7 deletions(-) diff --git a/demos/09-envmap.c b/demos/09-envmap.c index 5f01bc7..41d127b 100644 --- a/demos/09-envmap.c +++ b/demos/09-envmap.c @@ -46,6 +46,9 @@ int main(int argc, char** argv) { sky = skybox(flag("--mie") ? 0 : SKY_DIRS[SKY_DIR], 0); env_probe = skybox(0, 0); mdl = model(OBJ_MDLS[OBJ_MDL], 0); + shader_bind(mdl.program); + vec3 clr[9]={0}; + shader_vec3v("u_coefficients_sh", 9, clr); // rotation44(mdl.pivot, 0, 1,0,0); // @fixme: -90,1,0,0 -> should we rotate SHMs as well? compensate rotation in shader? } @@ -82,16 +85,16 @@ int main(int argc, char** argv) { if (follow_cam) { probe_pos = cam.position; } - unsigned tex_size = 128; + unsigned tex_size = 256; cubemap_bake_begin(&env_probe.cubemap, probe_pos, tex_size, tex_size); while (cubemap_bake_step(&env_probe.cubemap, probe_proj, probe_view)) { skybox_render(&sky, probe_proj, probe_view); - shader_bind(mdl.program); - shader_vec3v("u_coefficients_sh", 9, sky.cubemap.sh); + shader_bind(mdl.program); + cubemap_sh_shader(0); model_render(mdl, probe_proj, probe_view, mdl.pivot, 0); } - cubemap_bake_end(&env_probe.cubemap, 8, 1.0f); + cubemap_bake_end(&env_probe.cubemap, 4, 1.0f); } ddraw_sphere(probe_pos, 0.1f); diff --git a/engine/joint/v4k.h b/engine/joint/v4k.h index c857456..901fdf6 100644 --- a/engine/joint/v4k.h +++ b/engine/joint/v4k.h @@ -383877,7 +383877,12 @@ void cubemap_sh_reset(cubemap_t *c) { } void cubemap_sh_shader(cubemap_t *c) { - shader_vec3v("u_coefficients_sh", 9, c->sh); + if (c) { + shader_vec3v("u_coefficients_sh", 9, c->sh); + } else { + vec3 clr[9] = {0}; + shader_vec3v("u_coefficients_sh", 9, clr); + } } void cubemap_sh_add_light(cubemap_t *c, vec3 light, vec3 dir, float strength) { @@ -383895,6 +383900,11 @@ void cubemap_sh_add_light(cubemap_t *c, vec3 light, vec3 dir, float strength) { } void cubemap_sh_blend(vec3 pos, float max_dist, unsigned count, cubemap_t *probes) { + if (count == 0) { + cubemap_sh_shader(0); + return; + } + float total_weight = 0.0f; vec3 final_sh[9] = {0}; diff --git a/engine/split/v4k_render.c b/engine/split/v4k_render.c index fad1957..452004e 100644 --- a/engine/split/v4k_render.c +++ b/engine/split/v4k_render.c @@ -2190,7 +2190,12 @@ void cubemap_sh_reset(cubemap_t *c) { } void cubemap_sh_shader(cubemap_t *c) { - shader_vec3v("u_coefficients_sh", 9, c->sh); + if (c) { + shader_vec3v("u_coefficients_sh", 9, c->sh); + } else { + vec3 clr[9] = {0}; + shader_vec3v("u_coefficients_sh", 9, clr); + } } void cubemap_sh_add_light(cubemap_t *c, vec3 light, vec3 dir, float strength) { @@ -2208,6 +2213,11 @@ void cubemap_sh_add_light(cubemap_t *c, vec3 light, vec3 dir, float strength) { } void cubemap_sh_blend(vec3 pos, float max_dist, unsigned count, cubemap_t *probes) { + if (count == 0) { + cubemap_sh_shader(0); + return; + } + float total_weight = 0.0f; vec3 final_sh[9] = {0}; diff --git a/engine/v4k.c b/engine/v4k.c index 9f2c3c4..d8de628 100644 --- a/engine/v4k.c +++ b/engine/v4k.c @@ -18989,7 +18989,12 @@ void cubemap_sh_reset(cubemap_t *c) { } void cubemap_sh_shader(cubemap_t *c) { - shader_vec3v("u_coefficients_sh", 9, c->sh); + if (c) { + shader_vec3v("u_coefficients_sh", 9, c->sh); + } else { + vec3 clr[9] = {0}; + shader_vec3v("u_coefficients_sh", 9, clr); + } } void cubemap_sh_add_light(cubemap_t *c, vec3 light, vec3 dir, float strength) { @@ -19007,6 +19012,11 @@ void cubemap_sh_add_light(cubemap_t *c, vec3 light, vec3 dir, float strength) { } void cubemap_sh_blend(vec3 pos, float max_dist, unsigned count, cubemap_t *probes) { + if (count == 0) { + cubemap_sh_shader(0); + return; + } + float total_weight = 0.0f; vec3 final_sh[9] = {0};