#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(vertex_t v1, vertex_t v2, vertex_t v3, uint8_t material_id, triangle_collection_t* collection) { vec3s edge1 = glms_vec3_sub(v2.position, v1.position); vec3s edge2 = glms_vec3_sub(v3.position, v1.position); vec3s normal = glms_vec3_normalize(glms_vec3_cross(edge1, edge2)); triangle_t triangle = { .vertices = {v1, v2, v3}, .face_normal = normal, .material_id = material_id, }; if (collection->count >= collection->size) { triangle_collection_resize(collection, collection->size * 2); } collection->buffer[collection->count] = triangle; collection->count++; }