Changed CMakeLists.txt to set the C standard to C11. Added multiple binary image files for new visual assets. Added several new image files to enhance rendering capabilities. Changed stb_image.h to improve support for various image formats. Changed ray tracing engine to enhance ray creation and intersection. Changed triangle structure to use a vertex array for better attribute handling. Changed scene initialization to accommodate new texture management.
84 lines
2.3 KiB
C
84 lines
2.3 KiB
C
#include "Rendering/RenderTarget.h"
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <math.h>
|
|
|
|
bool render_target_init(uint32_t width, uint32_t height, render_target_t* render_target)
|
|
{
|
|
render_target->width = width;
|
|
render_target->height = height;
|
|
|
|
size_t size_of_pixel = sizeof(vec4s);
|
|
size_t image_size = (size_t)width * height;
|
|
size_t buffer_size = image_size * size_of_pixel;
|
|
vec4s* buffer = (vec4s*)malloc(buffer_size);
|
|
if (buffer == NULL)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
memset(buffer, 0, buffer_size);
|
|
|
|
for (size_t i = 0; i < image_size; i++)
|
|
{
|
|
buffer[i].w = 1.0;
|
|
}
|
|
|
|
render_target->buffer = buffer;
|
|
return true;
|
|
}
|
|
|
|
vec4s render_target_get_pixel(const 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);
|
|
if (char_buffer == NULL)
|
|
{
|
|
return NULL;
|
|
}
|
|
|
|
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 != NULL && target->buffer != NULL)
|
|
{
|
|
free(target->buffer);
|
|
}
|
|
}
|