Files
SimpleRayTracing/source/Geometry/Triangle.c
Misaki 17872804c5 Update project files and enhance rendering system
Added:
- Updated `.gitignore` to ignore the `[Bb]uild/` directory.
- Additional tasks added to the roadmap in `README.md` for light unit standardization and GPU backend support.

Changed:
- Removed line in `settings.json` that disabled error squiggles for C/C++ code.
- Modified `Triangle.h` to include `material_id` in `triangle_t` and reorganized properties.
- Reordered parameters in `triangle_collection_init` for clarity.
- Updated `shading_context_t` in `Material.h` and added size parameter to `material_create`.
- Streamlined initialization in `scene_init` and updated `scene_free` for proper resource management.
- Updated `window_create` in `Window.h` to accept a `render_job_t` parameter.
- Introduced `renderer_start` in `Renderer.c` to handle rendering jobs and optimized pixel rendering logic.
2025-04-23 22:24:02 +09:00

82 lines
2.2 KiB
C

#include "Geometry/Triangle.h"
bool triangle_collection_init(triangle_collection_t* triangles, size_t size)
{
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);
}