Files
SimpleRayTracing/source/Rendering/RenderTarget.c
Misaki bfc94f0008 Enhance graphics library functionality and structure
Added new function signatures in `assimp-vc143-mt.lib` for improved logging, parsing, and vector operations.
Added new metadata and configuration information in `assimp-vc143-mt.dll` for versioning and licensing compliance.
Added Sobol sequence generation in `Sobol.c` for quasi-random sampling.
Added window message handling in `Window.c` for rendering graphics.
Added ray-triangle intersection tests in `RayIntersection.c` for collision detection.
Added functions for loading mesh data in `Mesh.c` to support 3D model import.
Added functions for managing triangle collections in `Triangle.c` to enhance geometric data handling.
Added light evaluation functions in `LightEvaluation.c` and `SkyLight.c` for realistic rendering.
Added sampling and evaluation functions for simple lit materials in `SimpleLit.c`.
Changed various header files to include copyright and licensing information.
Changed existing functions in multiple files to improve performance and clarity.
Removed unused code in several files to streamline the library.
2025-04-18 01:54:26 +09:00

76 lines
2.2 KiB
C

#include "Rendering/RenderTarget.h"
#include <stdlib.h>
#include <string.h>
#include <math.h>
render_target_t render_target_create(uint32_t width, uint32_t height)
{
render_target_t target;
target.width = width;
target.height = height;
size_t size_of_pixel = sizeof(vec4s);
size_t buffer_size = (size_t)width * height * size_of_pixel;
target.buffer = (vec4s*)malloc(buffer_size);
memset(target.buffer, 0, buffer_size);
for (size_t i = 0; i < buffer_size / size_of_pixel; i++)
{
target.buffer[i].w = 1.0;
}
return target;
}
vec4s render_target_get_pixel(render_target_t* render_target, uint32_t x, uint32_t y)
{
if (x < render_target->width && y < render_target->height)
{
size_t index = (size_t)y * render_target->width + x;
return render_target->buffer[index];
}
return (vec4s){0.0f, 0.0f, 0.0f, 0.0f}; // Return black if out of bounds
}
void render_target_set_pixel(render_target_t* render_target, uint32_t x, uint32_t y, vec4s color)
{
if (x < render_target->width && y < render_target->height)
{
size_t index = (size_t)y * render_target->width + x;
render_target->buffer[index] = color;
}
}
unsigned char* render_target_to_char(render_target_t* render_target)
{
size_t buffer_size = (size_t)render_target->width * render_target->height * 4; // 4 bytes for RGBA
unsigned char* char_buffer = (unsigned char*)malloc(buffer_size);
for (uint32_t y = 0; y < render_target->height; y++)
{
for (uint32_t x = 0; x < render_target->width; x++)
{
vec4s pixel = render_target_get_pixel(render_target, x, y);
size_t index = ((size_t)y * render_target->width + x) * 4;
char_buffer[index + 0] = COLOR_CLAMP(pixel.x * 255.0f);
char_buffer[index + 1] = COLOR_CLAMP(pixel.y * 255.0f);
char_buffer[index + 2] = COLOR_CLAMP(pixel.z * 255.0f);
char_buffer[index + 3] = COLOR_CLAMP(pixel.w * 255.0f);
}
}
return char_buffer;
}
void render_target_free(render_target_t* target)
{
if (target->buffer != NULL)
{
free(target->buffer);
target->buffer = NULL;
}
}