fix non-shadowed renders

main
Dominik Madarász 2024-08-30 12:54:28 +02:00
parent bd4261ed4f
commit 5459b8aaf8
9 changed files with 241 additions and 31 deletions

View File

@ -1158,6 +1158,7 @@ typedef struct light_t {
float specularPower;
float innerCone, outerCone;
bool cast_shadows;
bool processed_shadows;
unsigned shadow_technique;
float shadow_distance;
float shadow_bias;

View File

@ -0,0 +1,167 @@
// material demo
// - rlyeh, public domain
//
// @todo: object_print(obj, "");
#include "v4k.h"
const char *skyboxes[][2] = { // reflection, env, metadata
{"hdr/Tokyo_BigSight_1k.hdr","hdr/Tokyo_BigSight_Env.hdr"},
{"hdr/graffiti_shelter_4k.hdr","hdr/graffiti_shelter_Env.hdr"},
{"hdr/music_hall_01_4k.hdr","hdr/music_hall_01_Env.hdr"},
{"hdr/the_sky_is_on_fire_2k.hdr","hdr/the_sky_is_on_fire_Env.hdr"},
{"hdr/GCanyon_C_YumaPoint_1k.hdr","hdr/GCanyon_C_YumaPoint_Env.hdr"},
{"hdr/Factory_Catwalk_1k.hdr","hdr/Factory_Catwalk_Env.hdr"},
{"hdr/MonValley_G_DirtRoad_1k.hdr","hdr/MonValley_G_DirtRoad_Env.hdr"},
{"hdr/Shiodome_Stairs_1k.hdr","hdr/Shiodome_Stairs_Env.hdr"},
{"hdr/mesto.hdr","hdr/mesto_Env.hdr"},
};
int main() {
// create the window
window_create( 0.75f, 0 );
window_color( GRAY );
// create camera
camera_t cam = camera();
// fx: load all post fx files in all subdirs.
fx_load("fx**.fs");
fx_enable(fx_find("fxTonemapACES.fs"), 1);
// load video, RGB texture, no audio
video_t *v = video( "pexels-pachon-in-motion-17486489.mp4", VIDEO_RGB | VIDEO_NO_AUDIO | VIDEO_LOOP ); video_seek(v, 30);
// load texture
texture_t t1 = texture("kgirl/g01_texture.png", 0);
texture_t t2 = texture("matcaps/material3", 0);
// load model
model_t m1 = model("suzanne.obj", MODEL_NO_ANIMATIONS);
model_t m5 = model("suzanne.obj", MODEL_NO_ANIMATIONS);
model_t m2 = model("suzanne.obj", MODEL_NO_ANIMATIONS|MODEL_MATCAPS);
model_t m3 = model("damagedhelmet.gltf", MODEL_NO_ANIMATIONS|MODEL_PBR);
// model_t m3 = model("Scutum_low.fbx", MODEL_NO_ANIMATIONS|MODEL_PBR);
model_t m4 = model("cube.obj", MODEL_NO_ANIMATIONS);
// model_t m4 = model("avp/scene.gltf", MODEL_NO_ANIMATIONS|MODEL_PBR);
// model_t m3 = model("Cerberus_LP.FBX", MODEL_NO_ANIMATIONS|MODEL_PBR);
array(char*) list = 0;
for each_array( vfs_list("demos/art/shadertoys/**.fs"), char*, dir ) {
array_push(list, STRDUP(file_name(dir)));
}
shadertoy_t sh = shadertoy(*list, SHADERTOY_IGNORE_MOUSE|SHADERTOY_FLIP_Y); // 0:no flags
sh.dims.x = 1024;
sh.dims.y = 1024;
shadertoy_render(&sh, 0);
// spawn object1 (diffuse)
object_t* obj1 = scene_spawn();
object_model(obj1, m1);
object_diffuse(obj1, t1);
object_scale(obj1, vec3(3,3,3));
object_move(obj1, vec3(-10+5*0,0,-10));
object_pivot(obj1, vec3(0,90,0));
// spawn object2 (matcap)
object_t* obj2 = scene_spawn();
object_model(obj2, m2);
object_diffuse(obj2, t2);
object_scale(obj2, vec3(3,3,3));
object_move(obj2, vec3(-10+5*2,0,-10));
object_pivot(obj2, vec3(0,90,0));
// spawn object3 (video)
object_t* obj3 = scene_spawn();
object_model(obj3, m5);
object_diffuse(obj3, video_textures(v)[0]);
object_scale(obj3, vec3(3,3,3));
object_move(obj3, vec3(-10+5*1,0,-10));
object_pivot(obj3, vec3(0,90,0));
// spawn object4 (pbr)
object_t* obj4 = scene_spawn();
object_model(obj4, m3);
object_scale(obj4, vec3(3,3,3));
object_move(obj4, vec3(-10+6*3,0,-10));
// object_pivot(obj4, vec3(0,0,90));
object_pivot(obj4, vec3(0,90,0));
// spawn object5 (shadertoy)
object_t* obj5 = scene_spawn();
object_model(obj5, m4);
object_diffuse(obj5, sh.tx);
object_scale(obj5, vec3(3,3,3));
object_move(obj5, vec3(-10+8*3,0,-10));
object_pivot(obj5, vec3(0,90,0));
// create point light
// scene_spawn_light(); // sun
light_t* l = scene_spawn_light();
light_type(l, LIGHT_POINT);
// l->diffuse = vec3(0,0,0);
// load skybox
scene_get_active()->skybox = skybox_pbr(skyboxes[0][0], skyboxes[0][0], skyboxes[0][1]);
while(window_swap() && !input(KEY_ESC)) {
// draw environment
// ddraw_grid(0);
// update video
video_decode( v );
// update light position
light_teleport(l, cam.position);
// update shadertoy
shadertoy_render(&sh, window_delta());
// draw scene
fx_begin();
scene_render(SCENE_FOREGROUND|SCENE_BACKGROUND|SCENE_UPDATE_SH_COEF);
fx_end();
// fps camera
bool active = ui_active() || ui_hover() || gizmo_active() ? false : input(MOUSE_L) || input(MOUSE_M) || input(MOUSE_R);
if( active ) cam.speed = clampf(cam.speed + input_diff(MOUSE_W) / 10, 0.05f, 5.0f);
vec2 mouselook = scale2(vec2(input_diff(MOUSE_X), -input_diff(MOUSE_Y)), 0.2f * active);
vec3 wasdec = scale3(vec3(input(KEY_D)-input(KEY_A),input(KEY_E)-input(KEY_C),input(KEY_W)-input(KEY_S)), cam.speed);
camera_moveby(&cam, wasdec);
camera_fps(&cam, mouselook.x,mouselook.y);
window_cursor( !active );
if (ui_panel("FXs", 0)) {
ui_fxs();
ui_panel_end();
}
if( ui_panel( "Viewer", 0 ) ) {
for( int i = 0; i < countof(skyboxes); i++ ) {
const char *filename = skyboxes[i][0];
// bool selected = !strcmp(g_skybox.reflection->filename, file_name(filename));
bool selected = false;
if( ui_bool( filename, &selected ) ) {
scene_get_active()->skybox = skybox_pbr(skyboxes[i][0], skyboxes[i][0], skyboxes[i][1]);
}
}
ui_panel_end();
}
static int selected = 0;
if( ui_panel("Shadertoy", 1)) {
for( int i = 0; i < array_count(list); ++i ) {
bool in_use = i == selected;
if( ui_bool(list[i], &in_use) ) {
sh = shadertoy( list[selected = i], SHADERTOY_IGNORE_MOUSE|SHADERTOY_FLIP_Y );
sh.dims.x = 1024;
sh.dims.y = 1024;
shadertoy_render(&sh, 0);
object_diffuse(obj5, sh.tx);
}
}
ui_panel_end();
}
}
}

View File

@ -18,6 +18,7 @@ struct light_t {
float radius;
float innerCone;
float outerCone;
bool processed_shadows;
// falloff
float constant;

View File

@ -102,6 +102,7 @@ vec4 shadowmap(in vec4 peye, in vec4 neye) {
light_t light = u_lights[i];
float factor = 0.0;
if (light.processed_shadows) {
if (light.type == LIGHT_DIRECTIONAL) {
total_casters++;
factor += shadow_pcf(-peye.z, light.dir, i);
@ -112,6 +113,7 @@ vec4 shadowmap(in vec4 peye, in vec4 neye) {
vec4 sc = inv_view * vec4(dir, 0.0);
factor += shadow_vsm(length(dir), -sc.xyz, i);
}
}
shadowFactor += factor;
}

View File

@ -17283,6 +17283,7 @@ typedef struct light_t {
// Shadowmapping
bool cast_shadows;
bool processed_shadows;
unsigned shadow_technique;
float shadow_distance;
float shadow_bias;
@ -383219,6 +383220,7 @@ light_t light() {
l.innerCone = 0.85f;// 31 deg
l.outerCone = 0.9f; // 25 deg
l.cast_shadows = true;
l.processed_shadows = false;
l.shadow_distance = 200.0f;
l.shadow_bias = 0.01f;
return l;
@ -383299,11 +383301,14 @@ void light_update(unsigned num_lights, light_t *lv) {
shader_float(va("u_lights[%d].quadratic", i), lv[i].falloff.quadratic);
shader_float(va("u_lights[%d].innerCone", i), lv[i].innerCone);
shader_float(va("u_lights[%d].outerCone", i), lv[i].outerCone);
shader_bool(va("u_lights[%d].processed_shadows", i), lv[i].processed_shadows);
if (lv[i].processed_shadows) {
for (int j = 0; j < NUM_SHADOW_CASCADES; j++) {
shader_mat44(va("u_lights[%d].shadow_matrix[%d]", i, j), lv[i].shadow_matrix[j]);
}
}
}
}
// -----------------------------------------------------------------------------
@ -383535,6 +383540,7 @@ static void shadowmap_light_point(shadowmap_t *s, light_t *l, int dir) {
copy44(s->V, V);
copy44(s->PV, PV);
l->processed_shadows = true;
s->shadow_technique = l->shadow_technique = SHADOW_VSM;
model_setpass(RENDER_PASS_SHADOW_VSM);
}
@ -383640,6 +383646,7 @@ static void shadowmap_light_directional(shadowmap_t *s, light_t *l, int dir, flo
copy44(s->PV, PV);
copy44(l->shadow_matrix[s->cascade_index], PV);
l->processed_shadows = true;
s->shadow_technique = l->shadow_technique = SHADOW_PCF;
model_setpass(RENDER_PASS_SHADOW_PCF);
}
@ -383753,6 +383760,7 @@ void shadowmap_clear_fbo() {
}
void shadowmap_light(shadowmap_t *s, light_t *l, mat44 cam_proj, mat44 cam_view) {
l->processed_shadows = false;
if (l->cast_shadows) {
int step = s->step - 1;
@ -385981,9 +385989,8 @@ void model_set_uniforms(model_t m, int shader, mat44 mv, mat44 proj, mat44 view,
}
// shadow receiving
if (m.shadow_receiver) {
ASSERT(m.shadow_map);
shader_bool("u_shadow_receiver", GL_TRUE);
if (m.shadow_map && m.shadow_receiver) {
shader_bool("u_shadow_receiver", m.shadow_receiver);
for (int i = 0; i < MAX_LIGHTS; i++) {
shader_cubemap(va("shadowMap[%d]", i), m.shadow_map->maps[i].texture);
for (int j = 0; j < NUM_SHADOW_CASCADES; j++) {
@ -385992,7 +385999,14 @@ void model_set_uniforms(model_t m, int shader, mat44 mv, mat44 proj, mat44 view,
shader_float(va("u_cascade_distances[%d]", j), m.shadow_map->cascade_distances[j]);
}
}
} else {
}
else if (m.shadow_map == NULL || !m.shadow_receiver) {
for (int i = 0; i < MAX_LIGHTS; i++) {
shader_cubemap(va("shadowMap[%d]", i), 0);
for (int j = 0; j < NUM_SHADOW_CASCADES; j++) {
shader_texture_unit(va("shadowMap2D[%d]", i * NUM_SHADOW_CASCADES + j), 0, texture_unit());
}
}
shader_bool("u_shadow_receiver", GL_FALSE);
}
@ -387107,7 +387121,6 @@ void model_draw_call(model_t m, int shader, int pass, vec3 cam_pos, mat44 model_
rs = &m.rs[rs_idx];
renderstate_apply(rs);
}
if (rs_idx < RENDER_PASS_SHADOW_BEGIN || rs_idx > RENDER_PASS_SHADOW_END) {
if (m.shading != SHADING_PBR) {
shader_texture_unit("u_texture2d", q->textures[i], texture_unit());

View File

@ -1483,6 +1483,7 @@ light_t light() {
l.innerCone = 0.85f;// 31 deg
l.outerCone = 0.9f; // 25 deg
l.cast_shadows = true;
l.processed_shadows = false;
l.shadow_distance = 200.0f;
l.shadow_bias = 0.01f;
return l;
@ -1563,11 +1564,14 @@ void light_update(unsigned num_lights, light_t *lv) {
shader_float(va("u_lights[%d].quadratic", i), lv[i].falloff.quadratic);
shader_float(va("u_lights[%d].innerCone", i), lv[i].innerCone);
shader_float(va("u_lights[%d].outerCone", i), lv[i].outerCone);
shader_bool(va("u_lights[%d].processed_shadows", i), lv[i].processed_shadows);
if (lv[i].processed_shadows) {
for (int j = 0; j < NUM_SHADOW_CASCADES; j++) {
shader_mat44(va("u_lights[%d].shadow_matrix[%d]", i, j), lv[i].shadow_matrix[j]);
}
}
}
}
// -----------------------------------------------------------------------------
@ -1799,6 +1803,7 @@ static void shadowmap_light_point(shadowmap_t *s, light_t *l, int dir) {
copy44(s->V, V);
copy44(s->PV, PV);
l->processed_shadows = true;
s->shadow_technique = l->shadow_technique = SHADOW_VSM;
model_setpass(RENDER_PASS_SHADOW_VSM);
}
@ -1904,6 +1909,7 @@ static void shadowmap_light_directional(shadowmap_t *s, light_t *l, int dir, flo
copy44(s->PV, PV);
copy44(l->shadow_matrix[s->cascade_index], PV);
l->processed_shadows = true;
s->shadow_technique = l->shadow_technique = SHADOW_PCF;
model_setpass(RENDER_PASS_SHADOW_PCF);
}
@ -2017,6 +2023,7 @@ void shadowmap_clear_fbo() {
}
void shadowmap_light(shadowmap_t *s, light_t *l, mat44 cam_proj, mat44 cam_view) {
l->processed_shadows = false;
if (l->cast_shadows) {
int step = s->step - 1;
@ -4245,9 +4252,8 @@ void model_set_uniforms(model_t m, int shader, mat44 mv, mat44 proj, mat44 view,
}
// shadow receiving
if (m.shadow_receiver) {
ASSERT(m.shadow_map);
shader_bool("u_shadow_receiver", GL_TRUE);
if (m.shadow_map && m.shadow_receiver) {
shader_bool("u_shadow_receiver", m.shadow_receiver);
for (int i = 0; i < MAX_LIGHTS; i++) {
shader_cubemap(va("shadowMap[%d]", i), m.shadow_map->maps[i].texture);
for (int j = 0; j < NUM_SHADOW_CASCADES; j++) {
@ -4256,7 +4262,14 @@ void model_set_uniforms(model_t m, int shader, mat44 mv, mat44 proj, mat44 view,
shader_float(va("u_cascade_distances[%d]", j), m.shadow_map->cascade_distances[j]);
}
}
} else {
}
else if (m.shadow_map == NULL || !m.shadow_receiver) {
for (int i = 0; i < MAX_LIGHTS; i++) {
shader_cubemap(va("shadowMap[%d]", i), 0);
for (int j = 0; j < NUM_SHADOW_CASCADES; j++) {
shader_texture_unit(va("shadowMap2D[%d]", i * NUM_SHADOW_CASCADES + j), 0, texture_unit());
}
}
shader_bool("u_shadow_receiver", GL_FALSE);
}
@ -5371,7 +5384,6 @@ void model_draw_call(model_t m, int shader, int pass, vec3 cam_pos, mat44 model_
rs = &m.rs[rs_idx];
renderstate_apply(rs);
}
if (rs_idx < RENDER_PASS_SHADOW_BEGIN || rs_idx > RENDER_PASS_SHADOW_END) {
if (m.shading != SHADING_PBR) {
shader_texture_unit("u_texture2d", q->textures[i], texture_unit());

View File

@ -315,6 +315,7 @@ typedef struct light_t {
// Shadowmapping
bool cast_shadows;
bool processed_shadows;
unsigned shadow_technique;
float shadow_distance;
float shadow_bias;

View File

@ -18282,6 +18282,7 @@ light_t light() {
l.innerCone = 0.85f;// 31 deg
l.outerCone = 0.9f; // 25 deg
l.cast_shadows = true;
l.processed_shadows = false;
l.shadow_distance = 200.0f;
l.shadow_bias = 0.01f;
return l;
@ -18362,11 +18363,14 @@ void light_update(unsigned num_lights, light_t *lv) {
shader_float(va("u_lights[%d].quadratic", i), lv[i].falloff.quadratic);
shader_float(va("u_lights[%d].innerCone", i), lv[i].innerCone);
shader_float(va("u_lights[%d].outerCone", i), lv[i].outerCone);
shader_bool(va("u_lights[%d].processed_shadows", i), lv[i].processed_shadows);
if (lv[i].processed_shadows) {
for (int j = 0; j < NUM_SHADOW_CASCADES; j++) {
shader_mat44(va("u_lights[%d].shadow_matrix[%d]", i, j), lv[i].shadow_matrix[j]);
}
}
}
}
// -----------------------------------------------------------------------------
@ -18598,6 +18602,7 @@ static void shadowmap_light_point(shadowmap_t *s, light_t *l, int dir) {
copy44(s->V, V);
copy44(s->PV, PV);
l->processed_shadows = true;
s->shadow_technique = l->shadow_technique = SHADOW_VSM;
model_setpass(RENDER_PASS_SHADOW_VSM);
}
@ -18703,6 +18708,7 @@ static void shadowmap_light_directional(shadowmap_t *s, light_t *l, int dir, flo
copy44(s->PV, PV);
copy44(l->shadow_matrix[s->cascade_index], PV);
l->processed_shadows = true;
s->shadow_technique = l->shadow_technique = SHADOW_PCF;
model_setpass(RENDER_PASS_SHADOW_PCF);
}
@ -18816,6 +18822,7 @@ void shadowmap_clear_fbo() {
}
void shadowmap_light(shadowmap_t *s, light_t *l, mat44 cam_proj, mat44 cam_view) {
l->processed_shadows = false;
if (l->cast_shadows) {
int step = s->step - 1;
@ -21044,9 +21051,8 @@ void model_set_uniforms(model_t m, int shader, mat44 mv, mat44 proj, mat44 view,
}
// shadow receiving
if (m.shadow_receiver) {
ASSERT(m.shadow_map);
shader_bool("u_shadow_receiver", GL_TRUE);
if (m.shadow_map && m.shadow_receiver) {
shader_bool("u_shadow_receiver", m.shadow_receiver);
for (int i = 0; i < MAX_LIGHTS; i++) {
shader_cubemap(va("shadowMap[%d]", i), m.shadow_map->maps[i].texture);
for (int j = 0; j < NUM_SHADOW_CASCADES; j++) {
@ -21055,7 +21061,14 @@ void model_set_uniforms(model_t m, int shader, mat44 mv, mat44 proj, mat44 view,
shader_float(va("u_cascade_distances[%d]", j), m.shadow_map->cascade_distances[j]);
}
}
} else {
}
else if (m.shadow_map == NULL || !m.shadow_receiver) {
for (int i = 0; i < MAX_LIGHTS; i++) {
shader_cubemap(va("shadowMap[%d]", i), 0);
for (int j = 0; j < NUM_SHADOW_CASCADES; j++) {
shader_texture_unit(va("shadowMap2D[%d]", i * NUM_SHADOW_CASCADES + j), 0, texture_unit());
}
}
shader_bool("u_shadow_receiver", GL_FALSE);
}
@ -22170,7 +22183,6 @@ void model_draw_call(model_t m, int shader, int pass, vec3 cam_pos, mat44 model_
rs = &m.rs[rs_idx];
renderstate_apply(rs);
}
if (rs_idx < RENDER_PASS_SHADOW_BEGIN || rs_idx > RENDER_PASS_SHADOW_END) {
if (m.shading != SHADING_PBR) {
shader_texture_unit("u_texture2d", q->textures[i], texture_unit());

View File

@ -3350,6 +3350,7 @@ typedef struct light_t {
// Shadowmapping
bool cast_shadows;
bool processed_shadows;
unsigned shadow_technique;
float shadow_distance;
float shadow_bias;