From 6c581ff56dfcc22c25538e305e58efd967dd640a Mon Sep 17 00:00:00 2001 From: omniscient <17525998+omnisci3nce@users.noreply.github.com> Date: Thu, 14 Mar 2024 21:54:55 +1100 Subject: lights, camera, action --- examples/obj_loading/ex_obj_loading.c | 39 ++++++++++++++++++++++++++++++++--- 1 file changed, 36 insertions(+), 3 deletions(-) (limited to 'examples') diff --git a/examples/obj_loading/ex_obj_loading.c b/examples/obj_loading/ex_obj_loading.c index bdadd7e..02ed606 100644 --- a/examples/obj_loading/ex_obj_loading.c +++ b/examples/obj_loading/ex_obj_loading.c @@ -1,4 +1,5 @@ #include +#include #include "camera.h" #include "core.h" @@ -7,6 +8,14 @@ #include "render.h" #include "render_types.h" +const vec3 pointlight_positions[4] = { + {0.7, 0.2, 2.0}, + {2.3, -3.3, -4.0}, + {-4.0, 2.0, -12.0}, + {0.0, 0.0, -3.0}, +}; +point_light point_lights[4]; + int main() { core* core = core_bringup(); @@ -22,6 +31,30 @@ int main() { vec3 camera_pos = vec3(3., 4., 10.); vec3 camera_front = vec3_normalise(vec3_negate(camera_pos)); camera cam = camera_create(camera_pos, camera_front, VEC3_Y, deg_to_rad(45.0)); + // 4. create lights + + // directional (sun) light setup + directional_light dir_light = {.direction = (vec3){-0.2, -1.0, -0.3}, + .ambient = (vec3){0.2, 0.2, 0.2}, + .diffuse = (vec3){0.5, 0.5, 0.5}, + .specular = (vec3){1.0, 1.0, 1.0}}; + // point lights setup + for (int i = 0; i < 4; i++) { + point_lights[i].position = pointlight_positions[i]; + point_lights[i].ambient = (vec3){0.05, 0.05, 0.05}; + point_lights[i].diffuse = (vec3){0.8, 0.8, 0.8}; + point_lights[i].specular = (vec3){1.0, 1.0, 1.0}; + point_lights[i].constant = 1.0; + point_lights[i].linear = 0.09; + point_lights[i].quadratic = 0.032; + } + + scene our_scene = { + .dir_light = dir_light, + .n_point_lights = 4 + }; + memcpy(&our_scene.point_lights, &point_lights, sizeof(point_light[4])); + // --- Enter Main loop while (!glfwWindowShouldClose(core->renderer.window)) { @@ -30,10 +63,10 @@ int main() { render_frame_begin(&core->renderer); - // Draw the cube - transform cube_tf = + // Draw the backpack + transform model_tf = transform_create(VEC3_ZERO, quat_ident(), 2.0); // make the backpack a bit bigger - draw_model(&core->renderer, &cam, backpack, cube_tf); + draw_model(&core->renderer, &cam, backpack, model_tf, &our_scene); render_frame_end(&core->renderer); } -- cgit v1.2.3-70-g09d2 From b240374c23365e33727d78ca74e901bcb383e077 Mon Sep 17 00:00:00 2001 From: omniscient <17525998+omnisci3nce@users.noreply.github.com> Date: Thu, 14 Mar 2024 21:55:25 +1100 Subject: chore: format --- examples/obj_loading/ex_obj_loading.c | 28 ++++++++++++---------------- src/renderer/render.c | 6 +++--- src/resources/obj.c | 8 +++++--- 3 files changed, 20 insertions(+), 22 deletions(-) (limited to 'examples') diff --git a/examples/obj_loading/ex_obj_loading.c b/examples/obj_loading/ex_obj_loading.c index 02ed606..6f1f12a 100644 --- a/examples/obj_loading/ex_obj_loading.c +++ b/examples/obj_loading/ex_obj_loading.c @@ -9,10 +9,10 @@ #include "render_types.h" const vec3 pointlight_positions[4] = { - {0.7, 0.2, 2.0}, - {2.3, -3.3, -4.0}, - {-4.0, 2.0, -12.0}, - {0.0, 0.0, -3.0}, + { 0.7, 0.2, 2.0 }, + { 2.3, -3.3, -4.0 }, + { -4.0, 2.0, -12.0 }, + { 0.0, 0.0, -3.0 }, }; point_light point_lights[4]; @@ -34,28 +34,24 @@ int main() { // 4. create lights // directional (sun) light setup - directional_light dir_light = {.direction = (vec3){-0.2, -1.0, -0.3}, - .ambient = (vec3){0.2, 0.2, 0.2}, - .diffuse = (vec3){0.5, 0.5, 0.5}, - .specular = (vec3){1.0, 1.0, 1.0}}; + directional_light dir_light = { .direction = (vec3){ -0.2, -1.0, -0.3 }, + .ambient = (vec3){ 0.2, 0.2, 0.2 }, + .diffuse = (vec3){ 0.5, 0.5, 0.5 }, + .specular = (vec3){ 1.0, 1.0, 1.0 } }; // point lights setup for (int i = 0; i < 4; i++) { point_lights[i].position = pointlight_positions[i]; - point_lights[i].ambient = (vec3){0.05, 0.05, 0.05}; - point_lights[i].diffuse = (vec3){0.8, 0.8, 0.8}; - point_lights[i].specular = (vec3){1.0, 1.0, 1.0}; + point_lights[i].ambient = (vec3){ 0.05, 0.05, 0.05 }; + point_lights[i].diffuse = (vec3){ 0.8, 0.8, 0.8 }; + point_lights[i].specular = (vec3){ 1.0, 1.0, 1.0 }; point_lights[i].constant = 1.0; point_lights[i].linear = 0.09; point_lights[i].quadratic = 0.032; } - scene our_scene = { - .dir_light = dir_light, - .n_point_lights = 4 - }; + scene our_scene = { .dir_light = dir_light, .n_point_lights = 4 }; memcpy(&our_scene.point_lights, &point_lights, sizeof(point_light[4])); - // --- Enter Main loop while (!glfwWindowShouldClose(core->renderer.window)) { input_update(&core->input); diff --git a/src/renderer/render.c b/src/renderer/render.c index 1c979e5..7884db6 100644 --- a/src/renderer/render.c +++ b/src/renderer/render.c @@ -92,7 +92,7 @@ void draw_model(renderer* ren, camera* camera, model* model, transform tf, scene uniform_vec3f(ren->blinn_phong.program_id, "viewPos", &camera->position); // set light uniforms dir_light_upload_uniforms(ren->blinn_phong, &scene->dir_light); - for (int i = 0; i < scene->n_point_lights; i ++) { + for (int i = 0; i < scene->n_point_lights; i++) { point_light_upload_uniforms(ren->blinn_phong, &scene->point_lights[i], '0' + i); } @@ -264,14 +264,14 @@ void texture_data_upload(texture* tex) { // stbi_image_free(tex->image_data); // data is on gpu now so we dont need it around } -void dir_light_upload_uniforms(shader shader, directional_light *light) { +void dir_light_upload_uniforms(shader shader, directional_light* light) { uniform_vec3f(shader.program_id, "dirLight.direction", &light->direction); uniform_vec3f(shader.program_id, "dirLight.ambient", &light->ambient); uniform_vec3f(shader.program_id, "dirLight.diffuse", &light->diffuse); uniform_vec3f(shader.program_id, "dirLight.specular", &light->specular); } -void point_light_upload_uniforms(shader shader, point_light *light, char index) { +void point_light_upload_uniforms(shader shader, point_light* light, char index) { char position_str[] = "pointLights[x].position"; position_str[12] = (char)index; char ambient_str[] = "pointLights[x].ambient"; diff --git a/src/resources/obj.c b/src/resources/obj.c index 221e3aa..710d5f0 100644 --- a/src/resources/obj.c +++ b/src/resources/obj.c @@ -279,7 +279,7 @@ void create_submesh(mesh_darray *meshes, vec3_darray *tmp_positions, vec3_darray mesh_darray_push(meshes, m); } -bool load_material_lib(const char *path,str8 relative_path, material_darray *materials) { +bool load_material_lib(const char *path, str8 relative_path, material_darray *materials) { TRACE("BEGIN load material lib at %s", path); const char *file_string = string_from_file(path); @@ -340,7 +340,8 @@ bool load_material_lib(const char *path,str8 relative_path, material_darray *mat char diffuse_map_filename[1024]; sscanf(pch + offset, "%s", diffuse_map_filename); char diffuse_map_path[1024]; - snprintf(diffuse_map_path, sizeof(diffuse_map_path), "%s/%s", relative_path.buf, diffuse_map_filename); + snprintf(diffuse_map_path, sizeof(diffuse_map_path), "%s/%s", relative_path.buf, + diffuse_map_filename); printf("load from %s\n", diffuse_map_path); // -------------- @@ -355,7 +356,8 @@ bool load_material_lib(const char *path,str8 relative_path, material_darray *mat char specular_map_filename[1024]; sscanf(pch + offset, "%s", specular_map_filename); char specular_map_path[1024]; - snprintf(specular_map_path, sizeof(specular_map_path), "%s/%s", relative_path.buf, specular_map_filename); + snprintf(specular_map_path, sizeof(specular_map_path), "%s/%s", relative_path.buf, + specular_map_filename); printf("load from %s\n", specular_map_path); // -------------- texture specular_texture = texture_data_load(specular_map_path, true); -- cgit v1.2.3-70-g09d2 From 2af96e3bc19fac5a3dc27f0eedff1b95ef1d473b Mon Sep 17 00:00:00 2001 From: omniscient <17525998+omnisci3nce@users.noreply.github.com> Date: Thu, 14 Mar 2024 22:11:21 +1100 Subject: add screenshot to README --- README.md | 2 ++ examples/obj_loading/backpack_screenshot.png | Bin 0 -> 2545081 bytes 2 files changed, 2 insertions(+) create mode 100644 examples/obj_loading/backpack_screenshot.png (limited to 'examples') diff --git a/README.md b/README.md index ed073fa..b4aa53e 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,7 @@ # celeritas-core +![Backpack model with lighting](examples/obj_loading/backpack_screenshot.png) + Not all files are fleshed out right now. **Work-in-progress**: porting over code from old repository. diff --git a/examples/obj_loading/backpack_screenshot.png b/examples/obj_loading/backpack_screenshot.png new file mode 100644 index 0000000..55b15c5 Binary files /dev/null and b/examples/obj_loading/backpack_screenshot.png differ -- cgit v1.2.3-70-g09d2 From 0ae30006abf7ae8940257be1276e55fdec0774c4 Mon Sep 17 00:00:00 2001 From: omniscient <17525998+omnisci3nce@users.noreply.github.com> Date: Thu, 14 Mar 2024 22:15:12 +1100 Subject: change screenshot --- examples/obj_loading/backpack_screenshot.png | Bin 2545081 -> 2404274 bytes examples/obj_loading/ex_obj_loading.c | 4 ++-- 2 files changed, 2 insertions(+), 2 deletions(-) (limited to 'examples') diff --git a/examples/obj_loading/backpack_screenshot.png b/examples/obj_loading/backpack_screenshot.png index 55b15c5..a16b1f2 100644 Binary files a/examples/obj_loading/backpack_screenshot.png and b/examples/obj_loading/backpack_screenshot.png differ diff --git a/examples/obj_loading/ex_obj_loading.c b/examples/obj_loading/ex_obj_loading.c index 6f1f12a..ecb2e54 100644 --- a/examples/obj_loading/ex_obj_loading.c +++ b/examples/obj_loading/ex_obj_loading.c @@ -28,7 +28,7 @@ int main() { // 2. upload vertex data to gpu model_upload_meshes(&core->renderer, backpack); // 3. create a camera - vec3 camera_pos = vec3(3., 4., 10.); + vec3 camera_pos = vec3(3., 2., 10.); vec3 camera_front = vec3_normalise(vec3_negate(camera_pos)); camera cam = camera_create(camera_pos, camera_front, VEC3_Y, deg_to_rad(45.0)); // 4. create lights @@ -61,7 +61,7 @@ int main() { // Draw the backpack transform model_tf = - transform_create(VEC3_ZERO, quat_ident(), 2.0); // make the backpack a bit bigger + transform_create(vec3(0.0, -0.4, 0.0), quat_ident(), 1.8); // make the backpack a bit bigger draw_model(&core->renderer, &cam, backpack, model_tf, &our_scene); render_frame_end(&core->renderer); -- cgit v1.2.3-70-g09d2 From 51b4a3fc75351d6ecd2142c228d31a1f7ed52152 Mon Sep 17 00:00:00 2001 From: Omniscient <17525998+omnisci3nce@users.noreply.github.com> Date: Sun, 17 Mar 2024 15:00:53 +1100 Subject: fix a bug with dirname seg faulting when passed a string literal it doesnt like things in readonly memory i guess. now we create an arena for the obj load and create a dynamically allocated copy of the string --- examples/obj_loading/ex_obj_loading.c | 4 ++-- src/platform/path.c | 9 +++++++-- src/platform/path.h | 2 +- src/resources/obj.c | 9 ++++++++- src/std/mem.c | 6 ++++-- src/std/mem.h | 1 + 6 files changed, 23 insertions(+), 8 deletions(-) (limited to 'examples') diff --git a/examples/obj_loading/ex_obj_loading.c b/examples/obj_loading/ex_obj_loading.c index ecb2e54..6e63938 100644 --- a/examples/obj_loading/ex_obj_loading.c +++ b/examples/obj_loading/ex_obj_loading.c @@ -60,8 +60,8 @@ int main() { render_frame_begin(&core->renderer); // Draw the backpack - transform model_tf = - transform_create(vec3(0.0, -0.4, 0.0), quat_ident(), 1.8); // make the backpack a bit bigger + transform model_tf = transform_create(vec3(0.0, -0.4, 0.0), quat_ident(), + 1.8); // make the backpack a bit bigger draw_model(&core->renderer, &cam, backpack, model_tf, &our_scene); render_frame_end(&core->renderer); diff --git a/src/platform/path.c b/src/platform/path.c index e67102b..9572941 100644 --- a/src/platform/path.c +++ b/src/platform/path.c @@ -1,12 +1,17 @@ #include "path.h" #include +#include #include +#include "mem.h" #include "str.h" #if defined(CEL_PLATFORM_LINUX) || defined(CEL_PLATFORM_MAC) -path_opt path_parent(const char* path) { - char* path_dirname = dirname(path); +path_opt path_parent(arena* a, const char* path) { + // Duplicate the string because dirname doesnt like const literals + char* path_copy = arena_alloc(a, strlen(path) + 1); + strcpy(path_copy, path); + char* path_dirname = dirname(path_copy); return (path_opt){ .path = str8_cstr_view(path_dirname), .has_value = true }; } #endif diff --git a/src/platform/path.h b/src/platform/path.h index 0ec6993..73063ea 100644 --- a/src/platform/path.h +++ b/src/platform/path.h @@ -13,4 +13,4 @@ typedef struct path_opt { bool has_value; } path_opt; -path_opt path_parent(const char* path); // TODO: convert to using str8 \ No newline at end of file +path_opt path_parent(arena* a, const char* path); // TODO: convert to using str8 \ No newline at end of file diff --git a/src/resources/obj.c b/src/resources/obj.c index 56f885f..c6e9fa6 100644 --- a/src/resources/obj.c +++ b/src/resources/obj.c @@ -15,6 +15,7 @@ #include "file.h" #include "log.h" #include "maths.h" +#include "mem.h" #include "path.h" #include "render.h" #include "render_types.h" @@ -40,8 +41,11 @@ bool model_load_obj_str(const char *file_string, str8 relative_path, model *out_ bool invert_textures_y); model_handle model_load_obj(core *core, const char *path, bool invert_textures_y) { + size_t arena_size = 1024; + arena scratch = arena_create(malloc(arena_size), arena_size); + TRACE("Loading model at Path %s\n", path); - path_opt relative_path = path_parent(path); + path_opt relative_path = path_parent(&scratch, path); if (!relative_path.has_value) { WARN("Couldnt get a relative path for the path to use for loading materials & textures later"); } @@ -61,6 +65,9 @@ model_handle model_load_obj(core *core, const char *path, bool invert_textures_y u32 index = model_darray_len(core->models); model_darray_push(core->models, model); + + arena_free_all(&scratch); + arena_free_storage(&scratch); return (model_handle){ .raw = index }; } diff --git a/src/std/mem.c b/src/std/mem.c index f5b92d4..d7c0f4c 100644 --- a/src/std/mem.c +++ b/src/std/mem.c @@ -16,7 +16,7 @@ void* arena_alloc_align(arena* a, size_t size, size_t align) { ERROR_EXIT("Arena ran out of memory\n"); } void* p = a->begin + padding; - a->begin += padding + size; + a->curr += padding + size; return memset(p, 0, size); } void* arena_alloc(arena* a, size_t size) { return arena_alloc_align(a, size, DEFAULT_ALIGNMENT); } @@ -29,4 +29,6 @@ arena arena_create(void* backing_buffer, size_t capacity) { void arena_free_all(arena* a) { a->curr = a->begin; // pop everything at once and reset to the start. -} \ No newline at end of file +} + +void arena_free_storage(arena* a) { free(a->begin); } \ No newline at end of file diff --git a/src/std/mem.h b/src/std/mem.h index c3ec61d..2f92894 100644 --- a/src/std/mem.h +++ b/src/std/mem.h @@ -22,4 +22,5 @@ arena arena_create(void* backing_buffer, size_t capacity); void* arena_alloc(arena* a, size_t size); void* arena_alloc_align(arena* a, size_t size, size_t align); void arena_free_all(arena* a); +void arena_free_storage(arena* a); // TODO: arena_resize \ No newline at end of file -- cgit v1.2.3-70-g09d2