#include "Geometry/Triangle.h" bool triangle_collection_init(size_t size, triangle_collection_t* triangles) { if (size > UINT64_MAX) { size = UINT64_MAX; } triangle_collection_t temp = {0}; temp.buffer = (triangle_t*)malloc(size * sizeof(triangle_t)); if (temp.buffer == NULL) { return false; } temp.size = (uint64_t)size; temp.count = 0; *triangles = temp; return true; } void triangle_collection_resize(triangle_collection_t* collection, size_t size) { if (size > UINT64_MAX) { size = UINT64_MAX; } triangle_t* temp = (triangle_t*)realloc(collection->buffer, size * sizeof(triangle_t)); if (temp != NULL) { collection->buffer = temp; collection->size = (uint64_t)size; } } void triangle_collection_free(triangle_collection_t* collection) { if (collection->buffer != NULL) { free(collection->buffer); collection->buffer = NULL; } } void triangle_create_with_normals(vec3s point1, vec3s point2, vec3s point3, vec3s normal1, vec3s normal2, vec3s normal3, uint8_t material_id, triangle_collection_t* collection) { if (collection->count >= collection->size) { triangle_collection_resize(collection, collection->size * 2); } triangle_t triangle; triangle.point_1 = point1; triangle.point_2 = point2; triangle.point_3 = point3; triangle.material_id = material_id; triangle.normal_1 = normal1; triangle.normal_2 = normal2; triangle.normal_3 = normal3; vec3s edge1 = glms_vec3_sub(point2, point1); vec3s edge2 = glms_vec3_sub(point3, point1); triangle.normal_face = glms_vec3_normalize(glms_vec3_cross(edge1, edge2)); collection->buffer[collection->count] = triangle; collection->count++; } void triangle_create(vec3s point1, vec3s point2, vec3s point3, uint8_t material_id, triangle_collection_t* collection) { vec3s edge1 = glms_vec3_sub(point2, point1); vec3s edge2 = glms_vec3_sub(point3, point1); vec3s normal = glms_vec3_normalize(glms_vec3_cross(edge1, edge2)); triangle_create_with_normals(point1, point2, point3, normal, normal, normal, material_id, collection); }