#ifndef GEOMETRY_UTILITIES_H #define GEOMETRY_UTILITIES_H #include "Triangle.h" inline void quad_create(vec3s center, vec3s forward, vec3s up, float size, uint8_t material_id, triangle_collection_t* collection) { float half_size = size / 2.0f; vec3s right = glms_vec3_cross(forward, up); vec3s scaled_right = glms_vec3_scale(right, half_size); vec3s scaled_up = glms_vec3_scale(up, half_size); vec3s temp_sub = glms_vec3_sub(center, scaled_right); vec3s temp_add = glms_vec3_add(center, scaled_right); vec3s top_left = glms_vec3_add(temp_sub, scaled_up); vec3s top_right = glms_vec3_add(temp_add, scaled_up); vec3s bottom_right = glms_vec3_sub(temp_add, scaled_up); vec3s bottom_left = glms_vec3_sub(temp_sub, scaled_up); vec2s uv0 = {0.0f, 0.0f}; vec2s uv1 = {1.0f, 0.0f}; vec2s uv2 = {1.0f, 1.0f}; vec2s uv3 = {0.0f, 1.0f}; vec3s dp1 = glms_vec3_sub(top_right, top_left); vec3s dp2 = glms_vec3_sub(bottom_right, top_left); vec2s duv1 = glms_vec2_sub(uv1, uv0); vec2s duv2 = glms_vec2_sub(uv3, uv0); float r = 1.0f / (duv1.x * duv2.y - duv1.y * duv2.x); vec3s tangent = glms_vec3_scale(glms_vec3_sub(glms_vec3_scale(dp1, duv2.y), glms_vec3_scale(dp2, duv1.y)), r); vertex_t vertex_0 = {.position = top_left, .normal = forward, .tangent = tangent, .uv = {0.0f, 0.0f}}; vertex_t vertex_1 = {.position = top_right, .normal = forward, .tangent = tangent, .uv = {1.0f, 0.0f}}; vertex_t vertex_2 = {.position = bottom_right, .normal = forward, .tangent = tangent, .uv = {1.0f, 1.0f}}; vertex_t vertex_3 = {.position = bottom_left, .normal = forward, .tangent = tangent, .uv = {0.0f, 1.0f}}; triangle_create(vertex_0, vertex_1, vertex_3, material_id, collection); triangle_create(vertex_1, vertex_2, vertex_3, material_id, collection); } // inline void sphere_create(vec3s center, float radius, uint8_t material_id, triangle_collection_t* collection) // { // int segments = 16; // int rings = 16; // // for (int i = 0; i < rings; i++) // { // float theta1 = (float)i / (float)rings * (float)M_PI; // float theta2 = (float)(i + 1) / (float)rings * (float)M_PI; // // for (int j = 0; j < segments; j++) // { // float phi1 = (float)j / (float)segments * 2.0f * (float)M_PI; // float phi2 = (float)(j + 1) / (float)segments * 2.0f * (float)M_PI; // // vec3s p1 = { // center.x + radius * sinf(theta1) * cosf(phi1), // center.y + radius * cosf(theta1), // center.z + radius * sinf(theta1) * sinf(phi1) // }; // vec3s p2 = { // center.x + radius * sinf(theta2) * cosf(phi1), // center.y + radius * cosf(theta2), // center.z + radius * sinf(theta2) * sinf(phi1) // }; // vec3s p3 = { // center.x + radius * sinf(theta2) * cosf(phi2), // center.y + radius * cosf(theta2), // center.z + radius * sinf(theta2) * sinf(phi2) // }; // vec3s p4 = { // center.x + radius * sinf(theta1) * cosf(phi2), // center.y + radius * cosf(theta1), // center.z + radius * sinf(theta1) * sinf(phi2) // }; // // // vec3s n1 = glms_vec3_normalize(glms_vec3_sub(p1, center)); // // vec3s n2 = glms_vec3_normalize(glms_vec3_sub(p2, center)); // // vec3s n3 = glms_vec3_normalize(glms_vec3_sub(p3, center)); // // vec3s n4 = glms_vec3_normalize(glms_vec3_sub(p4, center)); // // // triangle_create_with_normals(p3, p2, p1, n3, n2, n1, material_id, collection); // // triangle_create_with_normals(p3, p1, p4, n3, n1, n4, material_id, collection); // triangle_create(p3, p2, p1, material_id, collection); // triangle_create(p3, p1, p4, material_id, collection); // } // } // } #endif // GEOMETRY_UTILITIES_H