diff options
author | omniscient <17525998+omnisci3nce@users.noreply.github.com> | 2024-10-05 12:43:38 +1000 |
---|---|---|
committer | omniscient <17525998+omnisci3nce@users.noreply.github.com> | 2024-10-05 12:43:38 +1000 |
commit | 54354e32c6498cc7f8839ab4deb1208d37216cc5 (patch) | |
tree | 7759597b971ba59d6af841a5bed793c229dd4c2b /examples | |
parent | be8ab99b38c25e899008582d68e891150b328a4d (diff) |
Begin simplifying project structure and removing examples
Diffstat (limited to 'examples')
-rw-r--r-- | examples/cube/ex_cube.c | 174 | ||||
-rw-r--r-- | examples/gltf_loading/ex_gltf_loading.c | 88 | ||||
-rw-r--r-- | examples/main_loop/ex_main_loop.c | 34 | ||||
-rw-r--r-- | examples/property_animation/ex_property_animation.c | 103 | ||||
-rw-r--r-- | examples/standard_lib/ex_std.c | 30 | ||||
-rw-r--r-- | examples/triangle.c | 1 |
6 files changed, 1 insertions, 429 deletions
diff --git a/examples/cube/ex_cube.c b/examples/cube/ex_cube.c deleted file mode 100644 index 5a19f2c..0000000 --- a/examples/cube/ex_cube.c +++ /dev/null @@ -1,174 +0,0 @@ -#include <glfw3.h> - -#include "buf.h" -#include "camera.h" -#include "core.h" -#include "file.h" -#include "log.h" -#include "maths.h" -#include "maths_types.h" -#include "mem.h" -#include "primitives.h" -#include "ral.h" -#include "ral_types.h" -#include "render.h" -#include "render_types.h" - -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]; - -// Define the shader data -typedef struct mvp_uniforms { - mat4 model; - mat4 view; - mat4 projection; -} mvp_uniforms; -typedef struct my_shader_bind_group { - mvp_uniforms mvp; - texture_handle tex; -} my_shader_bind_group; - -// We also must create a function that knows how to return a `shader_data_layout` -shader_data_layout mvp_uniforms_layout(void* data) { - my_shader_bind_group* d = (my_shader_bind_group*)data; - bool has_data = data != NULL; - - shader_binding b1 = { .label = "Matrices", - .type = SHADER_BINDING_BYTES, - .stores_data = has_data, - .data = { .bytes = { .size = sizeof(mvp_uniforms) } } }; - - shader_binding b2 = { .label = "texSampler", - .type = SHADER_BINDING_TEXTURE, - .stores_data = has_data }; - if (has_data) { - b1.data.bytes.data = &d->mvp; - b2.data.texture.handle = d->tex; - } - return (shader_data_layout){ .name = "global_ubo", .bindings = { b1, b2 }, .bindings_count = 2 }; -} - -int main() { - core_bringup(); - arena scratch = arena_create(malloc(1024 * 1024), 1024 * 1024); - - vec3 camera_pos = vec3(2., 2., 2.); - vec3 camera_front = vec3_normalise(vec3_negate(camera_pos)); - camera cam = camera_create(camera_pos, camera_front, VEC3_Y, deg_to_rad(45.0)); - - // This is how to do it manually; run `static_3d_vertex_description()` to get this built-in vertex - // format - vertex_description vertex_input = { .use_full_vertex_size = true }; - vertex_input.debug_label = "Standard Static 3D Vertex Format"; - vertex_desc_add(&vertex_input, "inPosition", ATTR_F32x3); - vertex_desc_add(&vertex_input, "inNormal", ATTR_F32x3); - vertex_desc_add(&vertex_input, "inTexCoords", ATTR_F32x2); - vertex_input.use_full_vertex_size = true; - - shader_data mvp_uniforms_data = { .data = NULL, .shader_data_get_layout = &mvp_uniforms_layout }; - - gpu_renderpass_desc pass_description = { .default_framebuffer = true }; - gpu_renderpass* renderpass = gpu_renderpass_create(&pass_description); - - str8 vert_path, frag_path; -#ifdef CEL_REND_BACKEND_OPENGL - vert_path = str8lit("assets/shaders/cube.vert"); - frag_path = str8lit("assets/shaders/cube.frag"); -#else - vert_path = str8lit("build/linux/x86_64/debug/cube.vert.spv"); - frag_path = str8lit("build/linux/x86_64/debug/cube.frag.spv"); -#endif - str8_opt vertex_shader = str8_from_file(&scratch, vert_path); - str8_opt fragment_shader = str8_from_file(&scratch, frag_path); - if (!vertex_shader.has_value || !fragment_shader.has_value) { - ERROR_EXIT("Failed to load shaders from disk") - } - - struct graphics_pipeline_desc pipeline_description = { - .debug_name = "Basic Pipeline", - .vertex_desc = vertex_input, - .data_layouts = { mvp_uniforms_data }, - .data_layouts_count = 1, - .vs = { .debug_name = "Triangle Vertex Shader", - .filepath = vert_path, - .code = vertex_shader.contents, - .is_spirv = true }, - .fs = { .debug_name = "Triangle Fragment Shader", - .filepath = frag_path, - .code = fragment_shader.contents, - .is_spirv = true }, - .renderpass = renderpass, - .wireframe = false, - .depth_test = false - }; - gpu_pipeline* gfx_pipeline = gpu_graphics_pipeline_create(pipeline_description); - - // Geometry - geometry_data cube_data = geo_create_cuboid(f32x3(1, 1, 1)); - mesh cube = mesh_create(&cube_data, false); - - // Texture - texture_data tex_data = texture_data_load("assets/textures/texture.jpg", false); - texture_handle texture = texture_data_upload(tex_data, true); - - static f32 theta = 0.0; - - // Main loop - while (!should_exit(&g_core)) { - input_update(&g_core.input); - - if (!gpu_backend_begin_frame()) { - continue; - } - gpu_cmd_encoder* enc = gpu_get_default_cmd_encoder(); - // begin recording - gpu_cmd_encoder_begin(*enc); - gpu_cmd_encoder_begin_render(enc, renderpass); - encode_bind_pipeline(enc, PIPELINE_GRAPHICS, gfx_pipeline); - encode_set_default_settings(enc); - - theta += 0.01; - transform transform = { .position = vec3(-0.5, -0.5, -0.5), - .rotation = quat_from_axis_angle(VEC3_Y, theta, true), - .scale = 1.0 }; - /* INFO("Swapchain dimensions x %d y %d", g_core.renderer.swapchain.dimensions.x, - * g_core.renderer.swapchain.dimensions.y); */ - - mat4 model = transform_to_mat(&transform); - mat4 view, proj; - camera_view_projection(&cam, 1000, 1000, - /* g_core.renderer.swapchain.dimensions.x, */ - /* g_core.renderer.swapchain.dimensions.y, */ - &view, &proj); - 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); - - // Record draw calls - // draw_mesh(&cube, &model, &cam); - encode_set_vertex_buffer(enc, cube.vertex_buffer); - encode_set_index_buffer(enc, cube.index_buffer); - encode_draw_indexed(enc, cube.geometry->indices->len); - - // End recording - gpu_cmd_encoder_end_render(enc); - - gpu_cmd_buffer buf = gpu_cmd_encoder_finish(enc); - gpu_queue_submit(&buf); - // Submit - gpu_backend_end_frame(); - } - - renderer_shutdown(&g_core.renderer); - - return 0; -} diff --git a/examples/gltf_loading/ex_gltf_loading.c b/examples/gltf_loading/ex_gltf_loading.c deleted file mode 100644 index 85bb389..0000000 --- a/examples/gltf_loading/ex_gltf_loading.c +++ /dev/null @@ -1,88 +0,0 @@ -#include <glfw3.h> - -#include "animation.h" -#include "camera.h" -#include "core.h" -#include "loaders.h" -#include "log.h" -#include "maths.h" -#include "maths_types.h" -#include "render.h" -#include "render_types.h" - -extern core g_core; - -#define MODEL_GET(h) (model_pool_get(&g_core.models, 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() { - double currentFrame = glfwGetTime(); - double lastFrame = currentFrame; - double deltaTime; - - core_bringup(); - - model_handle helmet_handle = - model_load_gltf("assets/models/gltf/DamagedHelmet/glTF/DamagedHelmet.gltf", false); - INFO("GLTF loaded successfully!"); - model* helmet = MODEL_GET(helmet_handle); - - vec3 camera_pos = vec3(5., 0., 0.); - vec3 camera_front = vec3_normalise(vec3_negate(camera_pos)); - camera cam = camera_create(camera_pos, camera_front, VEC3_NEG_Z, 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; - scene_init(&our_scene); - memcpy(&our_scene.point_lights, &point_lights, sizeof(point_light[4])); - our_scene.point_lights_count = 4; - our_scene.dir_light = dir_light; - - while (!should_exit(&g_core)) { - input_update(&g_core.input); - - currentFrame = glfwGetTime(); - deltaTime = currentFrame - lastFrame; - lastFrame = currentFrame; - - render_frame_begin(&g_core.renderer); - - // Draw the model - static f32 angle = 0.0, rot_speed = 0.5; - quat rot = quat_from_axis_angle(VEC3_Z, fmod(angle, TAU), true); - angle += (rot_speed * deltaTime); - transform model_tf = transform_create(vec3(0.0, 0.1, -0.1), rot, 1.8); - mat4 model = transform_to_mat(&model_tf); - - draw_mesh(&helmet->meshes->data[0], &model, &cam); - - render_frame_end(&g_core.renderer); - } - - renderer_shutdown(&g_core.renderer); - - return 0; -} diff --git a/examples/main_loop/ex_main_loop.c b/examples/main_loop/ex_main_loop.c deleted file mode 100644 index 4e31313..0000000 --- a/examples/main_loop/ex_main_loop.c +++ /dev/null @@ -1,34 +0,0 @@ -#include <glfw3.h> - -#include "camera.h" -#include "core.h" -#include "maths.h" -#include "render.h" - -int main() { - core* core = core_bringup(); - - camera camera = camera_create(vec3_create(0, 0, 20), VEC3_NEG_Z, VEC3_Y, deg_to_rad(45.0)); - - // Main loop - while (!glfwWindowShouldClose(core->renderer.window)) { - input_update(&core->input); - // threadpool_process_results(&core->threadpool, 1); - - render_frame_begin(&core->renderer); - - static f32 x = 0.0; - x += 0.01; - mat4 model = mat4_translation(vec3(x, 0, 0)); - - gfx_backend_draw_frame(&core->renderer, &camera, model, NULL); - - // insert work here - - render_frame_end(&core->renderer); - glfwSwapBuffers(core->renderer.window); - glfwPollEvents(); - } - - return 0; -} diff --git a/examples/property_animation/ex_property_animation.c b/examples/property_animation/ex_property_animation.c deleted file mode 100644 index c548db4..0000000 --- a/examples/property_animation/ex_property_animation.c +++ /dev/null @@ -1,103 +0,0 @@ -#include <glfw3.h> - -#include "../example_scene.h" -#include "animation.h" -#include "camera.h" -#include "core.h" -#include "input.h" -#include "keys.h" -#include "log.h" -#include "maths.h" -#include "maths_types.h" -#include "primitives.h" -#include "render.h" -#include "render_backend.h" -#include "render_types.h" - -typedef struct game_state { - camera camera; - vec3 camera_euler; - bool first_mouse_update; // so the camera doesnt lurch when you run the first - // process_camera_rotation -} game_state; - -void update_camera_rotation(input_state* input, game_state* game, camera* cam); - -int main() { - double currentFrame = glfwGetTime(); - double lastFrame = currentFrame; - double deltaTime; - - core* core = core_bringup(); - - model_handle animated_cube_handle = - model_load_gltf(core, "assets/models/gltf/AnimatedCube/glTF/AnimatedCube.gltf", false); - model* cube = &core->models->data[animated_cube_handle.raw]; - model_upload_meshes(&core->renderer, cube); - - scene our_scene = make_default_scene(); - - vec3 cam_pos = vec3_create(5, 5, 5); - game_state game = { - .camera = camera_create(cam_pos, vec3_negate(cam_pos), VEC3_Y, deg_to_rad(45.0)), - .camera_euler = vec3_create(90, 0, 0), - .first_mouse_update = true, - }; - - print_vec3(game.camera.front); - - // Main loop - const f32 camera_lateral_speed = 0.2; - const f32 camera_zoom_speed = 0.15; - - // animation - animation_clip track = cube->animations->data[0]; - f64 total_time = 0.0; - - while (!should_exit(core)) { - input_update(&core->input); - - currentFrame = glfwGetTime(); - deltaTime = currentFrame - lastFrame; - lastFrame = currentFrame; - total_time += deltaTime; - printf("delta time %f\n", deltaTime); - f64 t = fmod(total_time, 1.0); - INFO("Total time: %f", t); - - vec3 translation = VEC3_ZERO; - if (key_is_pressed(KEYCODE_W) || key_is_pressed(KEYCODE_KEY_UP)) { - translation = vec3_mult(game.camera.front, camera_zoom_speed); - } else if (key_is_pressed(KEYCODE_S) || key_is_pressed(KEYCODE_KEY_DOWN)) { - translation = vec3_mult(game.camera.front, -camera_zoom_speed); - } else if (key_is_pressed(KEYCODE_A) || key_is_pressed(KEYCODE_KEY_LEFT)) { - vec3 lateral = vec3_normalise(vec3_cross(game.camera.front, game.camera.up)); - translation = vec3_mult(lateral, -camera_lateral_speed); - } else if (key_is_pressed(KEYCODE_D) || key_is_pressed(KEYCODE_KEY_RIGHT)) { - vec3 lateral = vec3_normalise(vec3_cross(game.camera.front, game.camera.up)); - translation = vec3_mult(lateral, camera_lateral_speed); - } - game.camera.position = vec3_add(game.camera.position, translation); - - // UNUSED: threadpool_process_results(&core->threadpool, 1); - - render_frame_begin(&core->renderer); - - quat rot = animation_sample(track.rotation, t).rotation; - // quat rot = quat_ident(); - transform tf = transform_create(VEC3_ZERO, rot, 1.0); - mat4 model_tf = transform_to_mat(&tf); - draw_model(&core->renderer, &game.camera, cube, &model_tf, &our_scene); - - // gfx_backend_draw_frame(&core->renderer, &game.camera, model, NULL); - - render_frame_end(&core->renderer); - } - - INFO("Shutting down"); - model_destroy(cube); - - core_shutdown(core); - - return 0; -} diff --git a/examples/standard_lib/ex_std.c b/examples/standard_lib/ex_std.c deleted file mode 100644 index 9d474de..0000000 --- a/examples/standard_lib/ex_std.c +++ /dev/null @@ -1,30 +0,0 @@ -#include <stdio.h> -#include <stdlib.h> -#include "file.h" -#include "str.h" - -int main() { - // Examples of how to work with arenas and strings - size_t arena_size = 1024; - arena scratch = arena_create(malloc(arena_size), arena_size); - arena* a = &scratch; - - str8 hello = str8lit("Hello World"); - - // this works but we should be careful because str8 is not *guaranteed* to point to - // a null-terminated string - printf("String before: '%s' (null-terminated: %s) \n ", hello.buf, - str8_is_null_term(hello) ? "true" : "false"); - - char* c = str8_to_cstr(&scratch, hello); - - printf("String after: %s\n", c); - - str8_opt test_file = str8_from_file(&scratch, str8lit("assets/shaders/ui_rect.vert")); - if (test_file.has_value) { - printf("Contents: %.*s \n", (int)test_file.contents.len, test_file.contents.buf); - printf("Null-terminated: %s\n", str8_is_null_term(test_file.contents) ? "true" : "false"); - } - - return 0; -} diff --git a/examples/triangle.c b/examples/triangle.c new file mode 100644 index 0000000..caa427d --- /dev/null +++ b/examples/triangle.c @@ -0,0 +1 @@ +// Example demonstrating basic RAL usage by rendering a triangle |