summaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authoromniscient <17525998+omnisci3nce@users.noreply.github.com>2024-04-27 11:25:48 +1000
committeromniscient <17525998+omnisci3nce@users.noreply.github.com>2024-04-27 11:25:48 +1000
commit8dae0a94059bd21a5a10e4434cadc843126d8b05 (patch)
treed8d110e0a4a2a03e1e9d6c3b7a2dbf53a9d9e858 /examples
parent7a8d93d2b867cab853ecf8c4ec5061cdc5cab665 (diff)
parentf8375e4587612d6a582eb053be5a67694a59993d (diff)
Merge branch 'brainstorming-systems' into ral
Diffstat (limited to 'examples')
-rw-r--r--examples/gltf_loading/ex_gltf_loading.c76
-rw-r--r--examples/obj_loading/ex_obj_loading.c3
-rw-r--r--examples/transforms/ex_transforms.c88
3 files changed, 166 insertions, 1 deletions
diff --git a/examples/gltf_loading/ex_gltf_loading.c b/examples/gltf_loading/ex_gltf_loading.c
index e69de29..0c47fec 100644
--- a/examples/gltf_loading/ex_gltf_loading.c
+++ b/examples/gltf_loading/ex_gltf_loading.c
@@ -0,0 +1,76 @@
+#include <glfw3.h>
+
+#include "camera.h"
+#include "core.h"
+#include "loaders.h"
+#include "maths.h"
+#include "maths_types.h"
+#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() {
+ double currentFrame = glfwGetTime();
+ double lastFrame = currentFrame;
+ double deltaTime;
+
+ core* core = core_bringup();
+
+ model_handle helmet_handle =
+ model_load_gltf(core, "assets/models/gltf/DamagedHelmet/glTF/DamagedHelmet.gltf", false);
+ model* helmet = &core->models->data[helmet_handle.raw];
+ model_upload_meshes(&core->renderer, helmet);
+
+ 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 = { .dir_light = dir_light, .n_point_lights = 4 };
+ memcpy(&our_scene.point_lights, &point_lights, sizeof(point_light[4]));
+
+ while (!glfwWindowShouldClose(core->renderer.window)) {
+ currentFrame = glfwGetTime();
+ deltaTime = currentFrame - lastFrame;
+ lastFrame = currentFrame;
+ input_update(&core->input);
+
+ render_frame_begin(&core->renderer);
+
+ // Draw the model
+ static f32 angle = 0.0;
+ static f32 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_model(&core->renderer, &cam, helmet, &model, &our_scene);
+
+ render_frame_end(&core->renderer);
+ }
+
+ return 0;
+}
diff --git a/examples/obj_loading/ex_obj_loading.c b/examples/obj_loading/ex_obj_loading.c
index 6e63938..e225cb2 100644
--- a/examples/obj_loading/ex_obj_loading.c
+++ b/examples/obj_loading/ex_obj_loading.c
@@ -62,7 +62,8 @@ int main() {
// 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
- draw_model(&core->renderer, &cam, backpack, model_tf, &our_scene);
+ mat4 model_matrix = transform_to_mat(&model_tf);
+ draw_model(&core->renderer, &cam, backpack, &model_matrix, &our_scene);
render_frame_end(&core->renderer);
}
diff --git a/examples/transforms/ex_transforms.c b/examples/transforms/ex_transforms.c
new file mode 100644
index 0000000..689c49d
--- /dev/null
+++ b/examples/transforms/ex_transforms.c
@@ -0,0 +1,88 @@
+#include <glfw3.h>
+
+#include "core.h"
+#include "maths.h"
+#include "maths_types.h"
+#include "mem.h"
+#include "render.h"
+#include "render_types.h"
+#include "transform_hierarchy.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();
+
+ // Set up scene
+ 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));
+
+ model_handle cube_handle = model_load_obj(core, "assets/models/obj/cube/cube.obj", true);
+ model* cube = &core->models->data[cube_handle.raw];
+ model_upload_meshes(&core->renderer, cube);
+
+ 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]));
+
+ // Create transform hierarchy
+ transform_hierarchy* transform_tree = transform_hierarchy_create();
+ transform_node* root_node = transform_hierarchy_root_node(transform_tree);
+ // Add nodes
+ // -- 4 cubes
+ transform cube1 = transform_create(vec3(0.0, -2.0, 0.0), quat_ident(), 0.8);
+ transform cube2 = transform_create(vec3(0.0, 1.0, 0.0), quat_ident(), 1.0);
+ transform cube3 = transform_create(vec3(0.0, 1.0, 0.0), quat_ident(), 1.0);
+ transform cube4 = transform_create(vec3(0.0, 1.0, 0.0), quat_ident(), 1.0);
+ transform_node* node1 = transform_hierarchy_add_node(root_node, cube_handle, cube1);
+ transform_node* node2 = transform_hierarchy_add_node(node1, cube_handle, cube2);
+ transform_node* node3 = transform_hierarchy_add_node(node2, cube_handle, cube3);
+ transform_node* node4 = transform_hierarchy_add_node(node3, cube_handle, cube4);
+
+ transform_hierarchy_debug_print(root_node, core);
+
+ char* frame_allocator_storage = malloc(1024 * 1024 * 64);
+ arena frame_arena = arena_create(frame_allocator_storage, 1024 * 1024 * 64);
+
+ // Main loop
+ while (!glfwWindowShouldClose(core->renderer.window)) {
+ input_update(&core->input);
+ threadpool_process_results(&core->threadpool, 1);
+
+ render_frame_begin(&core->renderer);
+ transform_hierarchy_propagate_transforms(transform_tree);
+
+ // TODO: Add setters to transform API
+ node1->tf.position.x += 0.004;
+ node1->tf.is_dirty = true;
+ draw_scene(&frame_arena, core->models, &core->renderer, &cam, transform_tree, &our_scene);
+
+ render_frame_end(&core->renderer);
+ arena_free_all(&frame_arena);
+ }
+
+ transform_hierarchy_free(transform_tree);
+
+ return 0;
+}