diff options
author | Omni <omniscient.oce@gmail.com> | 2024-06-23 18:18:35 +1000 |
---|---|---|
committer | Omni <omniscient.oce@gmail.com> | 2024-06-23 18:18:35 +1000 |
commit | 41a9e6d84e442cadbf1825c38bb97c516873ad5a (patch) | |
tree | cf63931f3ea7fb4fa418313bd5e587f87cd2889b | |
parent | 0cbc42e89dcdeb47aaa63bb21b5fb911768ea4df (diff) |
change view with keypress
-rw-r--r-- | examples/shadow_maps/ex_shadow_maps.c | 136 | ||||
-rw-r--r-- | src/maths/primitives.c | 6 | ||||
-rw-r--r-- | src/renderer/backends/opengl/backend_opengl.c | 23 | ||||
-rw-r--r-- | src/renderer/render.c | 2 | ||||
-rw-r--r-- | src/systems/keys.h | 42 |
5 files changed, 141 insertions, 68 deletions
diff --git a/examples/shadow_maps/ex_shadow_maps.c b/examples/shadow_maps/ex_shadow_maps.c index 28cab86..c61996a 100644 --- a/examples/shadow_maps/ex_shadow_maps.c +++ b/examples/shadow_maps/ex_shadow_maps.c @@ -1,8 +1,9 @@ #include "celeritas.h" +#include "input.h" +#include "log.h" #include "maths.h" #include "maths_types.h" #include "primitives.h" -#include "log.h" #include "ral.h" #include "ral_types.h" #include "render.h" @@ -16,16 +17,11 @@ 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(); void switch_view(); enum active_view { - SceneView, + SceneView = 0, LightView, DebugQuad, }; @@ -62,10 +58,13 @@ shader_data_layout mvp_uniforms_layout(void* data) { } int main() { + // Stuff that gets changed during program + enum active_view current_view = SceneView; + core_bringup(); arena scratch = arena_create(malloc(1024 * 1024), 1024 * 1024); - vec3 camera_pos = vec3(5,5,5); + vec3 camera_pos = vec3(5, 5, 5); vec3 camera_front = vec3_normalise(vec3_negate(camera_pos)); camera cam = camera_create(camera_pos, camera_front, VEC3_Y, deg_to_rad(45.0)); // TEMP @@ -107,9 +106,11 @@ int main() { }; gpu_pipeline* gfx_pipeline = gpu_graphics_pipeline_create(pipeline_description); - // Texture + // Textures texture_data tex_data = texture_data_load("assets/textures/texture.jpg", false); texture_handle texture = texture_data_upload(tex_data, true); + texture_handle white_tex = + texture_data_upload(texture_data_load("assets/textures/white1x1.png", false), true); // END TEMP @@ -122,23 +123,37 @@ int main() { // 2. lights // 3. some boxes for (int i = 0; i < 4; i++) { - geometry_data geo = geo_create_cuboid(f32x3(1,1,1)); + geometry_data geo = geo_create_cuboid(f32x3(1, 1, 1)); s_scene[i] = mesh_create(&geo, true); s_transforms[i] = transform_create(vec3(-2 + (i * 1.2), 0, 0), quat_ident(), 1.0); } geometry_data plane = geo_create_plane(f32x2(20, 20)); s_scene[4] = mesh_create(&plane, true); + s_transforms[4] = transform_create(vec3(0, -3, 0), quat_ident(), 1.0); 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 }; + // HACK: Swap vertices to make it face us + vertex top0 = quad_geo.vertices->data[0]; + quad_geo.vertices->data[0] = quad_geo.vertices->data[2]; + quad_geo.vertices->data[2] = top0; + vertex top1 = quad_geo.vertices->data[1]; + quad_geo.vertices->data[1] = quad_geo.vertices->data[3]; + quad_geo.vertices->data[3] = top1; + + mesh quad = mesh_create(&quad_geo, true); // Main loop while (!should_exit(&g_core)) { input_update(&g_core.input); + printf("Frame\n"); + + if (key_just_released(KEYCODE_L)) { + current_view = (current_view + 1) % 3; + } + if (key_just_released(KEYCODE_R)) { + // TODO: gpu_pipeline_reload_shaders(gfx_pipeline); + } if (!gpu_backend_begin_frame()) { continue; @@ -148,9 +163,10 @@ int main() { // Shadow draw gpu_cmd_encoder_begin_render(enc, shadows.rpass); + printf("Here\n"); // calculations - f32 near_plane = 1.0, far_plane = 7.5; + f32 near_plane = 1.0, far_plane = 10.0; 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); @@ -158,51 +174,60 @@ int main() { encode_bind_pipeline(enc, PIPELINE_GRAPHICS, shadows.static_pipeline); + shader_data lightspace_data = { .data = NULL, + .shader_data_get_layout = &lightspace_uniform_layout }; lightspace_data.data = &lsu; encode_bind_shader_data(enc, 0, &lightspace_data); draw_scene(); + printf("Here\n"); gpu_cmd_encoder_end_render(enc); // End // Debug quad - 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); - // End - - // gpu_cmd_encoder_begin_render(enc, static_opaque_rpass); - - // gpu_cmd_encoder_end_render(enc); - - - /* gpu_cmd_encoder_begin_render(enc, renderpass); */ - /* encode_bind_pipeline(enc, PIPELINE_GRAPHICS, gfx_pipeline); */ - - /* mat4 view, proj; */ - /* camera_view_projection(&cam, 1000, 1000, */ - /* &view, &proj); */ - /* for (int i = 0; i < 4; i++) { */ - /* mat4 model = transform_to_mat(&s_transforms[i]); */ - /* mvp_uniforms mvp_data = { .model = model, .view = view, .projection = proj }; */ - /* my_shader_bind_group shader_bind_data = { .mvp = mvp_data, .tex = texture }; */ - /* mvp_uniforms_data.data = &shader_bind_data; */ - /* encode_bind_shader_data(enc, 0, &mvp_uniforms_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); */ - /* } */ + if (current_view == DebugQuad) { + 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); + } - /* gpu_cmd_encoder_end_render(enc); */ + // Basic draw + if (current_view == SceneView || current_view == LightView) { + gpu_cmd_encoder_begin_render(enc, renderpass); + encode_bind_pipeline(enc, PIPELINE_GRAPHICS, gfx_pipeline); + + mat4 view, proj; + if (current_view == SceneView) { + camera_view_projection(&cam, 1000, 1000, &view, &proj); + } else { + view = light_view; + proj = light_projection; + } + for (int i = 0; i < 5; i++) { + mat4 model = transform_to_mat(&s_transforms[i]); + mvp_uniforms mvp_data = { .model = model, .view = view, .projection = proj }; + my_shader_bind_group shader_bind_data = { .mvp = mvp_data, .tex = texture }; + if (i == 4) { + shader_bind_data.tex = white_tex; + } + mvp_uniforms_data.data = &shader_bind_data; + encode_bind_shader_data(enc, 0, &mvp_uniforms_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); + } + + gpu_cmd_encoder_end_render(enc); + } // END drawing gpu_cmd_buffer buf = gpu_cmd_encoder_finish(enc); @@ -217,13 +242,18 @@ int main() { void draw_scene() { gpu_cmd_encoder* enc = gpu_get_default_cmd_encoder(); - for (int i = 0; i < 4; i++) { //FIXME: draw plane as well - mat4 model = transform_to_mat(&s_transforms[i]) ; - model_uniform mu = { .model = model}; + for (int i = 0; i < 5; i++) { + mat4 model = transform_to_mat(&s_transforms[i]); + model_uniform mu = { .model = model }; shader_data model_data = { .data = &mu, .shader_data_get_layout = model_uniform_layout }; encode_bind_shader_data(enc, 0, &model_data); + printf("Here1\n"); encode_set_vertex_buffer(enc, s_scene[i].vertex_buffer); + printf("Here2\n"); encode_set_index_buffer(enc, s_scene[i].index_buffer); + printf("Here3\n"); + printf("num %ld \n", s_scene[i].geometry->indices->len); encode_draw_indexed(enc, s_scene[i].geometry->indices->len); + printf("Here4\n"); } } diff --git a/src/maths/primitives.c b/src/maths/primitives.c index ce467ab..753dd83 100644 --- a/src/maths/primitives.c +++ b/src/maths/primitives.c @@ -46,8 +46,10 @@ geometry_data geo_create_plane(f32x2 extents) { VERT_3D(vertices, vert_pos[2], VEC3_Y, vec2(0, 1)); VERT_3D(vertices, vert_pos[3], VEC3_Y, vec2(1, 1)); - push_triangle(indices, 0, 1, 2); - push_triangle(indices, 2, 1, 3); + /* push_triangle(indices, 0, 1, 2); */ + /* push_triangle(indices, 2, 1, 3); */ + push_triangle(indices, 2, 1, 0); + push_triangle(indices, 3, 1, 2); geometry_data geo = { .format = VERTEX_STATIC_3D, .vertices = vertices, diff --git a/src/renderer/backends/opengl/backend_opengl.c b/src/renderer/backends/opengl/backend_opengl.c index 02c27cd..356815d 100644 --- a/src/renderer/backends/opengl/backend_opengl.c +++ b/src/renderer/backends/opengl/backend_opengl.c @@ -153,8 +153,8 @@ gpu_renderpass* gpu_renderpass_create(const gpu_renderpass_desc* description) { } if (description->has_depth_stencil) { gpu_texture* depth_attachment = TEXTURE_GET(description->depth_stencil); - glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, - depth_attachment->id, 0); + glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, depth_attachment->id, + 0); } if (description->has_depth_stencil && !description->has_color_target) { @@ -192,9 +192,7 @@ void gpu_cmd_encoder_begin_render(gpu_cmd_encoder* encoder, gpu_renderpass* rend glClear(GL_COLOR_BUFFER_BIT); } } -void gpu_cmd_encoder_end_render(gpu_cmd_encoder* encoder) { - glBindFramebuffer(GL_FRAMEBUFFER, 0); -} +void gpu_cmd_encoder_end_render(gpu_cmd_encoder* encoder) { glBindFramebuffer(GL_FRAMEBUFFER, 0); } void gpu_cmd_encoder_begin_compute() {} gpu_cmd_encoder* gpu_get_default_cmd_encoder() { return &context.command_buffer; } @@ -238,7 +236,7 @@ void encode_bind_shader_data(gpu_cmd_encoder* encoder, u32 group, shader_data* d for (u32 i = 0; i < sdl.bindings_count; i++) { shader_binding binding = sdl.bindings[i]; - print_shader_binding(binding); + /* print_shader_binding(binding); */ if (binding.type == SHADER_BINDING_BYTES) { buffer_handle b; @@ -270,8 +268,9 @@ void encode_bind_shader_data(gpu_cmd_encoder* encoder, u32 group, shader_data* d } else if (binding.type == SHADER_BINDING_TEXTURE) { gpu_texture* tex = TEXTURE_GET(binding.data.texture.handle); GLint tex_slot = glGetUniformLocation(encoder->pipeline->shader_id, binding.label); + printf("%d slot \n", tex_slot); if (tex_slot == GL_INVALID_VALUE || tex_slot < 0) { - WARN("Invalid binding label for texture - couldn't fetch texture slot uniform"); + WARN("Invalid binding label for texture %s - couldn't fetch texture slot uniform", binding.label); } glUniform1i(tex_slot, i); glActiveTexture(GL_TEXTURE0 + i); @@ -294,6 +293,7 @@ void encode_set_index_buffer(gpu_cmd_encoder* encoder, buffer_handle buf) { } void encode_draw(gpu_cmd_encoder* encoder, u64 count) { glDrawArrays(GL_TRIANGLES, 0, count); } void encode_draw_indexed(gpu_cmd_encoder* encoder, u64 index_count) { + /* printf("Draw %ld indices\n", index_count); */ glDrawElements(GL_TRIANGLES, index_count, GL_UNSIGNED_INT, 0); } void encode_clear_buffer(gpu_cmd_encoder* encoder, buffer_handle buf) {} @@ -408,10 +408,11 @@ void gpu_texture_upload(texture_handle texture, const void* data) {} bytebuffer vertices_as_bytebuffer(arena* a, vertex_format format, vertex_darray* vertices) {} // --- TEMP -bool gpu_backend_begin_frame() { - glClearColor(0.1f, 0.1f, 0.1f, 1.0f); - glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); - return true; } +bool gpu_backend_begin_frame() { + glClearColor(0.1f, 0.1f, 0.1f, 1.0f); + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + return true; +} void gpu_backend_end_frame() { // TODO: Reset all bindings glfwSwapBuffers(context.window); diff --git a/src/renderer/render.c b/src/renderer/render.c index 1c98b0b..f52e2be 100644 --- a/src/renderer/render.c +++ b/src/renderer/render.c @@ -62,7 +62,7 @@ bool renderer_init(renderer* ren) { resource_pools_init(&pool_arena, ren->resource_pools); // Create default rendering pipeline - /* default_pipelines_init(ren); */ + default_pipelines_init(ren); return true; } diff --git a/src/systems/keys.h b/src/systems/keys.h index a76e101..6082a59 100644 --- a/src/systems/keys.h +++ b/src/systems/keys.h @@ -3,10 +3,50 @@ typedef enum keycode { // TODO: add all keycodes KEYCODE_SPACE = 32, + KEYCODE_APOSTROPHE = 39, + KEYCODE_COMMA = 44, + KEYCODE_MINUS = 45, + KEYCODE_PERIOD = 46, + KEYCODE_SLASH = 47, + KEYCODE_0 = 48, + KEYCODE_1 = 49, + KEYCODE_2 = 50, + KEYCODE_3 = 51, + KEYCODE_4 = 52, + KEYCODE_5 = 53, + KEYCODE_6 = 54, + KEYCODE_7 = 55, + KEYCODE_8 = 56, + KEYCODE_9 = 57, + KEYCODE_SEMICOLON = 59, + KEYCODE_EQUAL = 61, KEYCODE_A = 65, + KEYCODE_B = 66, + KEYCODE_C = 67, KEYCODE_D = 68, + KEYCODE_E = 69, + KEYCODE_F = 70, + KEYCODE_G = 71, + KEYCODE_H = 72, + KEYCODE_I = 73, + KEYCODE_J = 74, + KEYCODE_K = 75, + KEYCODE_L = 76, + KEYCODE_M = 77, + KEYCODE_N = 78, + KEYCODE_O = 79, + KEYCODE_P = 80, + KEYCODE_Q = 81, + KEYCODE_R = 82, KEYCODE_S = 83, + KEYCODE_T = 84, + KEYCODE_U = 85, + KEYCODE_V = 86, KEYCODE_W = 87, + KEYCODE_X = 88, + KEYCODE_Y = 89, + KEYCODE_Z = 90, + KEYCODE_ESCAPE = 256, KEYCODE_ENTER = 257, KEYCODE_TAB = 258, @@ -16,4 +56,4 @@ typedef enum keycode { KEYCODE_KEY_DOWN = 264, KEYCODE_KEY_UP = 265, KEYCODE_MAX = 348 -} keycode;
\ No newline at end of file +} keycode; |