add: cubemap_sh_blend

main
Dominik Madarász 2024-08-28 17:02:13 +02:00
parent f7090441c0
commit 5b6afd747f
7 changed files with 86 additions and 1 deletions

View File

@ -1128,6 +1128,7 @@ typedef struct cubemap_t {
void cubemap_sh_reset(cubemap_t *c);
void cubemap_sh_shader(cubemap_t *c);
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);
unsigned fbo( unsigned texture_color, unsigned texture_depth, int wr_flags );
void fbo_bind(unsigned id);
void fbo_unbind();

View File

@ -103,7 +103,7 @@ int main(int argc, char** argv) {
skybox_render(&sky, cam.proj, cam.view);
shader_bind(mdl.program);
shader_vec3v("u_coefficients_sh", 9, env_probe.cubemap.sh);
cubemap_sh_blend(vec3(0,0,0), 10.0f, 1, &env_probe.cubemap);
shader_int("u_textured", false);
model_render(mdl, cam.proj, cam.view, mdl.pivot, 0);

View File

@ -17231,6 +17231,9 @@ API void cubemap_sh_reset(cubemap_t *c);
API void cubemap_sh_shader(cubemap_t *c);
API void cubemap_sh_add_light(cubemap_t *c, vec3 light, vec3 dir, float strength);
// lighting probe blending
API void cubemap_sh_blend(vec3 pos, float max_dist, unsigned count, cubemap_t *probes);
// -----------------------------------------------------------------------------
// fbos
@ -383891,6 +383894,31 @@ void cubemap_sh_add_light(cubemap_t *c, vec3 light, vec3 dir, float strength) {
c->sh[3] = add3(c->sh[3], scale3(scaled_light, -0.488603f * norm_dir.x));
}
void cubemap_sh_blend(vec3 pos, float max_dist, unsigned count, cubemap_t *probes) {
float total_weight = 0.0f;
vec3 final_sh[9] = {0};
// Iterate through each probe
for (unsigned i = 0; i < count; i++) {
float distance = len3(sub3(pos, probes[i].pos));
float weight = 1.0f - (distance / max_dist);
weight = weight * weight;
for (int s = 0; s < 9; s++) {
final_sh[s] = add3(final_sh[s], scale3(probes[i].sh[s], weight));
}
total_weight += weight;
}
// Normalize the final SH coefficients
for (int s = 0; s < 9; s++) {
final_sh[s] = scale3(final_sh[s], 1.0f / total_weight);
}
// Apply SH coefficients to the shader
shader_vec3v("u_coefficients_sh", 9, final_sh);
}
// -----------------------------------------------------------------------------
// skyboxes

View File

@ -2207,6 +2207,31 @@ void cubemap_sh_add_light(cubemap_t *c, vec3 light, vec3 dir, float strength) {
c->sh[3] = add3(c->sh[3], scale3(scaled_light, -0.488603f * norm_dir.x));
}
void cubemap_sh_blend(vec3 pos, float max_dist, unsigned count, cubemap_t *probes) {
float total_weight = 0.0f;
vec3 final_sh[9] = {0};
// Iterate through each probe
for (unsigned i = 0; i < count; i++) {
float distance = len3(sub3(pos, probes[i].pos));
float weight = 1.0f - (distance / max_dist);
weight = weight * weight;
for (int s = 0; s < 9; s++) {
final_sh[s] = add3(final_sh[s], scale3(probes[i].sh[s], weight));
}
total_weight += weight;
}
// Normalize the final SH coefficients
for (int s = 0; s < 9; s++) {
final_sh[s] = scale3(final_sh[s], 1.0f / total_weight);
}
// Apply SH coefficients to the shader
shader_vec3v("u_coefficients_sh", 9, final_sh);
}
// -----------------------------------------------------------------------------
// skyboxes

View File

@ -263,6 +263,9 @@ API void cubemap_sh_reset(cubemap_t *c);
API void cubemap_sh_shader(cubemap_t *c);
API void cubemap_sh_add_light(cubemap_t *c, vec3 light, vec3 dir, float strength);
// lighting probe blending
API void cubemap_sh_blend(vec3 pos, float max_dist, unsigned count, cubemap_t *probes);
// -----------------------------------------------------------------------------
// fbos

View File

@ -19006,6 +19006,31 @@ void cubemap_sh_add_light(cubemap_t *c, vec3 light, vec3 dir, float strength) {
c->sh[3] = add3(c->sh[3], scale3(scaled_light, -0.488603f * norm_dir.x));
}
void cubemap_sh_blend(vec3 pos, float max_dist, unsigned count, cubemap_t *probes) {
float total_weight = 0.0f;
vec3 final_sh[9] = {0};
// Iterate through each probe
for (unsigned i = 0; i < count; i++) {
float distance = len3(sub3(pos, probes[i].pos));
float weight = 1.0f - (distance / max_dist);
weight = weight * weight;
for (int s = 0; s < 9; s++) {
final_sh[s] = add3(final_sh[s], scale3(probes[i].sh[s], weight));
}
total_weight += weight;
}
// Normalize the final SH coefficients
for (int s = 0; s < 9; s++) {
final_sh[s] = scale3(final_sh[s], 1.0f / total_weight);
}
// Apply SH coefficients to the shader
shader_vec3v("u_coefficients_sh", 9, final_sh);
}
// -----------------------------------------------------------------------------
// skyboxes

View File

@ -3298,6 +3298,9 @@ API void cubemap_sh_reset(cubemap_t *c);
API void cubemap_sh_shader(cubemap_t *c);
API void cubemap_sh_add_light(cubemap_t *c, vec3 light, vec3 dir, float strength);
// lighting probe blending
API void cubemap_sh_blend(vec3 pos, float max_dist, unsigned count, cubemap_t *probes);
// -----------------------------------------------------------------------------
// fbos