From 1fc69f183bf8f105048b9d47e096ccd402107bbb Mon Sep 17 00:00:00 2001 From: Omniscient <17525998+omnisci3nce@users.noreply.github.com> Date: Sun, 21 Apr 2024 12:34:00 +1000 Subject: misc cleanup --- assets/shaders/blinn_phong.frag | 2 +- assets/shaders/blinn_phong.vert | 2 + examples/gltf_loading/ex_gltf_loading.c | 2 +- examples/obj_loading/ex_obj_loading.c | 2 +- src/animation.h | 2 +- src/maths/maths.h | 65 ++++++++++++++++----------------- src/renderer/render.c | 31 +--------------- src/renderer/render_types.h | 9 ++--- 8 files changed, 42 insertions(+), 73 deletions(-) diff --git a/assets/shaders/blinn_phong.frag b/assets/shaders/blinn_phong.frag index adcc53e..a0ba905 100644 --- a/assets/shaders/blinn_phong.frag +++ b/assets/shaders/blinn_phong.frag @@ -56,7 +56,7 @@ void main() { result += CalcPointLight(pointLights[i], norm, fs_in.FragPos, viewDir); } - // FragColor = vec4(result, 1.0); +// FragColor = vec4(result, 1.0); FragColor = fs_in.Color + 0.5; } diff --git a/assets/shaders/blinn_phong.vert b/assets/shaders/blinn_phong.vert index 6028178..06dc5e7 100644 --- a/assets/shaders/blinn_phong.vert +++ b/assets/shaders/blinn_phong.vert @@ -15,6 +15,7 @@ out VS_OUT { vec3 Normal; vec2 TexCoords; vec4 FragPosLightSpace; + vec4 Color; } vs_out; void main() { @@ -22,5 +23,6 @@ void main() { vs_out.Normal = inNormal; vs_out.TexCoords = inTexCoords; vs_out.FragPosLightSpace = lightSpaceMatrix * vec4(vs_out.FragPos, 1.0); + vs_out.Color = vec4(1.0); gl_Position = projection * view * model * vec4(inPos, 1.0); } \ No newline at end of file diff --git a/examples/gltf_loading/ex_gltf_loading.c b/examples/gltf_loading/ex_gltf_loading.c index b181426..900cf1b 100644 --- a/examples/gltf_loading/ex_gltf_loading.c +++ b/examples/gltf_loading/ex_gltf_loading.c @@ -56,7 +56,7 @@ int main() { 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)) { + while (!should_exit(core)) { currentFrame = glfwGetTime(); deltaTime = currentFrame - lastFrame; lastFrame = currentFrame; diff --git a/examples/obj_loading/ex_obj_loading.c b/examples/obj_loading/ex_obj_loading.c index aaed2a0..906816b 100644 --- a/examples/obj_loading/ex_obj_loading.c +++ b/examples/obj_loading/ex_obj_loading.c @@ -55,7 +55,7 @@ int main() { memcpy(&our_scene.point_lights, &point_lights, sizeof(point_light[4])); // --- Enter Main loop - while (!glfwWindowShouldClose(core->renderer.window)) { + while (!should_exit(core)) { input_update(&core->input); threadpool_process_results(&core->threadpool, 1); diff --git a/src/animation.h b/src/animation.h index 9d5d03b..5462e65 100644 --- a/src/animation.h +++ b/src/animation.h @@ -29,7 +29,7 @@ typedef struct keyframes { } keyframes; typedef struct joint { - char* name; // optional + char* name; // optional transform transform_components; mat4 inverse_bind_matrix; mat4 local_transform; diff --git a/src/maths/maths.h b/src/maths/maths.h index 8e48435..e0d39d7 100644 --- a/src/maths/maths.h +++ b/src/maths/maths.h @@ -91,45 +91,42 @@ static inline quat quat_slerp(quat a, quat b, f32 percentage) { quat q1 = quat_normalise(b); // Compute the cosine of the angle between the two vectors. - f32 dot = quat_dot(q0, q1); - - // If the dot product is negative, slerp won't take - // the shorter path. Note that v1 and -v1 are equivalent when - // the negation is applied to all four components. Fix by - // reversing one quaternion. - if (dot < 0.0f) { - q1.x = -q1.x; - q1.y = -q1.y; - q1.z = -q1.z; - q1.w = -q1.w; - dot = -dot; - } + f32 dot = quat_dot(q0, q1); + + // If the dot product is negative, slerp won't take + // the shorter path. Note that v1 and -v1 are equivalent when + // the negation is applied to all four components. Fix by + // reversing one quaternion. + if (dot < 0.0f) { + q1.x = -q1.x; + q1.y = -q1.y; + q1.z = -q1.z; + q1.w = -q1.w; + dot = -dot; + } - const f32 DOT_THRESHOLD = 0.9995f; - if (dot > DOT_THRESHOLD) { - // If the inputs are too close for comfort, linearly interpolate - // and normalize the result. - out_quaternion = (quat){q0.x + ((q1.x - q0.x) * percentage), - q0.y + ((q1.y - q0.y) * percentage), - q0.z + ((q1.z - q0.z) * percentage), - q0.w + ((q1.w - q0.w) * percentage)}; + const f32 DOT_THRESHOLD = 0.9995f; + if (dot > DOT_THRESHOLD) { + // If the inputs are too close for comfort, linearly interpolate + // and normalize the result. + out_quaternion = + (quat){ q0.x + ((q1.x - q0.x) * percentage), q0.y + ((q1.y - q0.y) * percentage), + q0.z + ((q1.z - q0.z) * percentage), q0.w + ((q1.w - q0.w) * percentage) }; - return quat_normalise(out_quaternion); - } + return quat_normalise(out_quaternion); + } - // Since dot is in range [0, DOT_THRESHOLD], acos is safe - f32 theta_0 = cos(dot); // theta_0 = angle between input vectors - f32 theta = theta_0 * percentage; // theta = angle between v0 and result - f32 sin_theta = sin(theta); // compute this value only once - f32 sin_theta_0 = sin(theta_0); // compute this value only once + // Since dot is in range [0, DOT_THRESHOLD], acos is safe + f32 theta_0 = cos(dot); // theta_0 = angle between input vectors + f32 theta = theta_0 * percentage; // theta = angle between v0 and result + f32 sin_theta = sin(theta); // compute this value only once + f32 sin_theta_0 = sin(theta_0); // compute this value only once - f32 s0 = - cos(theta) - - dot * sin_theta / sin_theta_0; // == sin(theta_0 - theta) / sin(theta_0) - f32 s1 = sin_theta / sin_theta_0; + f32 s0 = cos(theta) - dot * sin_theta / sin_theta_0; // == sin(theta_0 - theta) / sin(theta_0) + f32 s1 = sin_theta / sin_theta_0; - return (quat){(q0.x * s0) + (q1.x * s1), (q0.y * s0) + (q1.y * s1), - (q0.z * s0) + (q1.z * s1), (q0.w * s0) + (q1.w * s1)}; + return (quat){ (q0.x * s0) + (q1.x * s1), (q0.y * s0) + (q1.y * s1), (q0.z * s0) + (q1.z * s1), + (q0.w * s0) + (q1.w * s1) }; } // --- Matrix Implementations diff --git a/src/renderer/render.c b/src/renderer/render.c index b688613..b1e2a46 100644 --- a/src/renderer/render.c +++ b/src/renderer/render.c @@ -176,7 +176,6 @@ void draw_mesh(renderer* ren, mesh* mesh, mat4* model_tf, material* mat, mat4* v bind_texture(lighting_shader, &mat->specular_texture, 1); // bind to slot 1 uniform_f32(lighting_shader.program_id, "material.shininess", 32.); - // upload model, view, and projection matrices uniform_mat4f(lighting_shader.program_id, "model", model_tf); uniform_mat4f(lighting_shader.program_id, "view", view); @@ -295,35 +294,6 @@ void model_upload_meshes(renderer* ren, model* model) { // TRACE("Uploading vertex array data: %d verts", num_vertices); total_verts += num_vertices; - // TODO: convert this garbage into a function - f32 verts[num_vertices * 8]; - // for each face - // for (int i = 0; i < (num_vertices / 3); i++) { - // // for each vert in face - // for (int j = 0; j < 3; j++) { - // size_t stride = (i * 24) + j * 8; - // // printf("i: %d, stride: %ld, loc %d\n", i, stride, i * 3 + j); - // vertex vert = model->meshes->data[mesh_i].vertices->data[i]; - // // printf("pos %f %f %f\n", vert.position.x, vert.position.y, vert.position.z); - // // printf("norm %f %f %f\n", vert.normal.x, vert.normal.y, vert.normal.z); - // // printf("tex %f %f\n", vert.uv.x, vert.uv.y); - // verts[stride + 0] = - // ((vertex*)model->meshes->data[mesh_i].vertices->data)[i * 3 + j].position.x; - // verts[stride + 1] = - // ((vertex*)model->meshes->data[mesh_i].vertices->data)[i * 3 + j].position.y; - // verts[stride + 2] = - // ((vertex*)model->meshes->data[mesh_i].vertices->data)[i * 3 + j].position.z; - // verts[stride + 3] = - // ((vertex*)model->meshes->data[mesh_i].vertices->data)[i * 3 + j].normal.x; - // verts[stride + 4] = - // ((vertex*)model->meshes->data[mesh_i].vertices->data)[i * 3 + j].normal.y; - // verts[stride + 5] = - // ((vertex*)model->meshes->data[mesh_i].vertices->data)[i * 3 + j].normal.z; - // verts[stride + 6] = ((vertex*)model->meshes->data[mesh_i].vertices->data)[i * 3 + - // j].uv.x; verts[stride + 7] = ((vertex*)model->meshes->data[mesh_i].vertices->data)[i * 3 - // + j].uv.y; - // } - // } size_t static_vertex_size = 2 * sizeof(vec3) + sizeof(vec2); size_t skinned_vertex_size = 2 * sizeof(vec3) + sizeof(vec2) + 4 * sizeof(u32) + sizeof(vec4); size_t vertex_size = mesh.is_skinned ? skinned_vertex_size : static_vertex_size; @@ -335,6 +305,7 @@ void model_upload_meshes(renderer* ren, model* model) { assert(vertex_size == sizeof(vertex)); assert(vertex_size == 8 * sizeof(float)); } + size_t buffer_size = vertex_size * num_vertices; u8* bytes = malloc(buffer_size); diff --git a/src/renderer/render_types.h b/src/renderer/render_types.h index 6e69708..423b58f 100644 --- a/src/renderer/render_types.h +++ b/src/renderer/render_types.h @@ -127,17 +127,16 @@ typedef struct vertex_bone_data { #include "animation.h" #ifndef TYPED_VERTEX_ARRAY -KITC_DECL_TYPED_ARRAY(vertex) // creates "vertex_darray" +KITC_DECL_TYPED_ARRAY(vertex) // creates "vertex_darray" KITC_DECL_TYPED_ARRAY(vertex_bone_data) // creates "skinned_vertex_darray" KITC_DECL_TYPED_ARRAY(joint) #define TYPED_VERTEX_ARRAY #endif - typedef struct mesh { - vertex_darray* vertices; - vertex_bone_data_darray* vertex_bone_data; // only used if model needs it - joint_darray* bones; + vertex_darray *vertices; + vertex_bone_data_darray *vertex_bone_data; // only used if model needs it + joint_darray *bones; bool is_skinned; u32 vertex_size; /** size in bytes of each vertex including necessary padding */ bool has_indices; -- cgit v1.2.3-70-g09d2