diff options
Diffstat (limited to 'examples')
-rw-r--r-- | examples/shadow_maps/ex_shadow_maps.c | 83 |
1 files changed, 54 insertions, 29 deletions
diff --git a/examples/shadow_maps/ex_shadow_maps.c b/examples/shadow_maps/ex_shadow_maps.c index 3b647ee..30df618 100644 --- a/examples/shadow_maps/ex_shadow_maps.c +++ b/examples/shadow_maps/ex_shadow_maps.c @@ -1,7 +1,9 @@ #include "celeritas.h" +#include "maths.h" #include "maths_types.h" #include "primitives.h" #include "ral.h" +#include "ral_types.h" #include "render.h" #include "render_types.h" #include "renderpasses.h" @@ -9,19 +11,17 @@ extern core g_core; // Scene / light setup -const vec3 pointlight_positions[4] = { - { -10.0, 10.0, 10.0 }, - { 10.0, 10.0, 10.0 }, - { -10.0, -10.0, 10.0 }, - { 10.0, -10.0, 10.0 }, -}; -point_light point_lights[4]; +vec3 light_position = { -2, 4, -1 }; +mesh s_scene[5]; +transform s_transforms[5]; /* TODO: - keyboard button to switch between main camera and light camera */ +void draw_scene(); + int main() { core_bringup(); arena scratch = arena_create(malloc(1024 * 1024), 1024 * 1024); @@ -31,19 +31,27 @@ int main() { camera cam = camera_create(camera_pos, camera_front, VEC3_Y, deg_to_rad(45.0)); ren_shadowmaps shadows = { .width = 1000, .height = 1000 }; - // ren_shadowmaps_init(&shadows); + ren_shadowmaps_init(&shadows); // Set up the scene // We want: // 1. a ground plane // 2. lights // 3. some boxes - - mesh scene[5]; for (int i = 0; i < 4; i++) { geometry_data geo = geo_create_cuboid(f32x3(2, 2, 2)); - cubes[i] = mesh_create(&geo, true); + s_scene[i] = mesh_create(&geo, true); + s_transforms[i] = transform_create(vec3(4 * i, 0, 0), quat_ident(), 1.0); } + geometry_data plane = geo_create_plane(f32x2(20, 20)); + s_scene[4] = mesh_create(&plane, true); + + geometry_data quad_geo = geo_create_plane(f32x2(2,2)); + mesh quad = mesh_create(&quad_geo, true); + + shader_data model_data = { .data = NULL, .shader_data_get_layout = &model_uniform_layout }; + shader_data lightspace_data = { .data = NULL, + .shader_data_get_layout = &lightspace_uniform_layout }; // Main loop while (!should_exit(&g_core)) { @@ -55,32 +63,37 @@ int main() { gpu_cmd_encoder* enc = gpu_get_default_cmd_encoder(); gpu_cmd_encoder_begin_render(enc, shadows.rpass); + + // calculations + f32 near_plane = 1.0, far_plane = 7.5; + mat4 light_projection = mat4_orthographic(-10.0, 10.0, -10.0, 10.0, near_plane, far_plane); + mat4 light_view = mat4_look_at(light_position, VEC3_ZERO, VEC3_Y); + mat4 light_space_matrix = mat4_mult(light_view, light_projection); + lightspace_tf_uniform lsu = { .lightSpaceMatrix = light_space_matrix }; + encode_bind_pipeline(enc, PIPELINE_GRAPHICS, shadows.static_pipeline); - for (int i = 0; i < 4; i++) { - encode_set_vertex_buffer(enc, cubes[i].vertex_buffer); - encode_set_index_buffer(enc, cubes[i].index_buffer); - encode_draw_indexed(enc, cubes[i].geometry->indices->len); - } - gpu_cmd_encoder_end_render(enc); + lightspace_data.data = &lsu; + encode_bind_shader_data(enc, 0, &lightspace_data); - // gpu_cmd_encoder_begin_render(enc, static_opaque_rpass); + draw_scene(); gpu_cmd_encoder_end_render(enc); - /* - Shadows - render scene into texture + gpu_cmd_encoder_begin_render(enc, shadows.debug_quad->renderpass); + encode_bind_pipeline(enc, PIPELINE_GRAPHICS, shadows.debug_quad); + debug_quad_uniform dqu = {.depthMap = shadows.depth_tex}; + shader_data debug_quad_data = { .data = &dqu, .shader_data_get_layout = debug_quad_layout}; + encode_bind_shader_data(enc, 0, &debug_quad_data); + encode_set_vertex_buffer(enc, quad.vertex_buffer); + encode_set_index_buffer(enc, quad.index_buffer); + encode_draw_indexed(enc, quad.geometry->indices->len); + + gpu_cmd_encoder_end_render(enc); - begin_renderpass() - bind_pipeline() - upload shader data - for each object: - - set buffers - - draw call + // gpu_cmd_encoder_begin_render(enc, static_opaque_rpass); - end_renderpass() - */ + // gpu_cmd_encoder_end_render(enc); gpu_backend_end_frame(); } @@ -89,3 +102,15 @@ int main() { return 0; } + +void draw_scene() { + gpu_cmd_encoder* enc = gpu_get_default_cmd_encoder(); + for (int i = 0; i < 5; i++) { + model_uniform mu = { .model = transform_to_mat(&s_transforms[i]) }; + shader_data model_data = { .data = &mu, .shader_data_get_layout = model_uniform_layout }; + encode_bind_shader_data(enc, 0, &model_data); + encode_set_vertex_buffer(enc, s_scene[i].vertex_buffer); + encode_set_index_buffer(enc, s_scene[i].index_buffer); + encode_draw_indexed(enc, s_scene[i].geometry->indices->len); + } +} |