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.
This commit is contained in:
187
source/main.c
187
source/main.c
@@ -1,100 +1,119 @@
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <svpng.inc>
|
||||
#include <omp.h>
|
||||
#include <math.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#include "MaterialExtra.h"
|
||||
#include "Algorithm/Sobol.h"
|
||||
#include "Lighting/SkyLight.h"
|
||||
#include "Material/SimpleLit.h"
|
||||
#include "Rendering/Scene.h"
|
||||
#include "Algorithm/BSDF.h"
|
||||
#include "Material.h"
|
||||
#include "Triangle.h"
|
||||
#include "Geometry.h"
|
||||
#include "Geometry/GeometryUtilities.h"
|
||||
#include "Window.h"
|
||||
|
||||
|
||||
static inline void save_img(render_target_t* source, const uint32_t width, const uint32_t height, const char* filename)
|
||||
static void save_img(render_target_t* source, const uint32_t width, const uint32_t height, const char* filename)
|
||||
{
|
||||
FILE* fileStream;
|
||||
fopen_s(&fileStream, filename, "wb");
|
||||
if (fileStream == NULL)
|
||||
FILE* file_stream;
|
||||
fopen_s(&file_stream, filename, "wb");
|
||||
if (file_stream == NULL)
|
||||
{
|
||||
fprintf(stderr, "Failed to open file for writing: %s\n", filename);
|
||||
return;
|
||||
}
|
||||
|
||||
unsigned char* img = (unsigned char*)malloc((size_t)width * height * 4);
|
||||
if (img == NULL)
|
||||
{
|
||||
fprintf(stderr, "Failed to allocate memory for image\n");
|
||||
return;
|
||||
}
|
||||
unsigned char* img_buffer = render_target_to_char(source);
|
||||
svpng(file_stream, width, height, img_buffer, 1);
|
||||
|
||||
for (uint32_t y = 0; y < height; y++)
|
||||
{
|
||||
for (uint32_t x = 0; x < width; x++)
|
||||
{
|
||||
size_t index = ((size_t)y * width + x) * (size_t)4.0;
|
||||
vec4s color = render_target_get_pixel(source, x, y);
|
||||
|
||||
img[index] = (unsigned char)fminf(color.x * 255.0f, 255.0f);
|
||||
img[index + 1] = (unsigned char)fminf(color.y * 255.0f, 255.0f);
|
||||
img[index + 2] = (unsigned char)fminf(color.z * 255.0f, 255.0f);
|
||||
img[index + 3] = (unsigned char)fminf(color.w * 255.0f, 255.0f);
|
||||
}
|
||||
}
|
||||
|
||||
svpng(fileStream, width, height, img, 1);
|
||||
fclose(fileStream);
|
||||
free(img);
|
||||
fclose(file_stream);
|
||||
free(img_buffer);
|
||||
}
|
||||
|
||||
int main()
|
||||
// int main()
|
||||
int WINAPI wWinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _In_ PWSTR pCmdLine, _In_ int nCmdShow)
|
||||
{
|
||||
omp_set_num_threads(24);
|
||||
rendering_config_t config = {
|
||||
.width = 1280 / 2,
|
||||
.height = 720 / 2,
|
||||
.sample_count = 64,
|
||||
.max_depth = 4,
|
||||
.bucket_size = 64,
|
||||
};
|
||||
|
||||
const int SAMPLE_COUNT = 256;
|
||||
int result = window_create("Path Tracing", hInstance, config.width, config.height);
|
||||
if (result != 0)
|
||||
{
|
||||
fprintf(stderr, "Failed to create window\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
const uint32_t WIDTH = 640;
|
||||
const uint32_t HEIGHT = 360;
|
||||
#pragma region SceneSetup
|
||||
omp_set_num_threads(16);
|
||||
sobol_init();
|
||||
|
||||
scene_t scene = scene_create(64, 4);
|
||||
scene_t scene = scene_create(64, 8, 4);
|
||||
|
||||
scene.camera.position = (vec3s){0.0f, 0.0f, 5.0f};
|
||||
|
||||
light_entity_t sun = light_create_directional_light(&scene.lights);
|
||||
directional_light_t* sun_light = &scene.lights.directional_lights[sun.id];
|
||||
sun_light->direction = glms_vec3_normalize((vec3s){0.75f, 0.75f, 1.0f});
|
||||
sun_light->color = (vec3s){1.0f, 0.93f, 0.87f};
|
||||
sun_light->intensity = 3.0f;
|
||||
sun_light->angular_diameter = 0.53f;
|
||||
|
||||
scene.lights.sky_light = sky_create_constant_sky(&(constant_sky_data_t){
|
||||
.color = (vec3s){0.73f, 0.82f, 1.0f}
|
||||
});
|
||||
|
||||
simple_lit_data_t gray_lit_data = {
|
||||
.albedo = (vec3s){0.73f, 0.73f, 0.73f},
|
||||
.roughness = 1.0f,
|
||||
.roughness = 0.5f,
|
||||
.metallic = 0.0f,
|
||||
};
|
||||
simple_lit_data_t blue_lit_data = {
|
||||
.albedo = (vec3s){0.0f, 0.0f, 1.0f},
|
||||
.roughness = 0.5f,
|
||||
.metallic = 1.0f,
|
||||
.metallic = 0.0f,
|
||||
};
|
||||
material_t top_light_material = material_create_simple_lit(&gray_lit_data, &scene.materials);
|
||||
material_t gray_material = material_create_simple_lit(&gray_lit_data, &scene.materials);
|
||||
material_t blue_material = material_create_simple_lit(&blue_lit_data, &scene.materials);
|
||||
simple_lit_data_t red_lit_data = {
|
||||
.albedo = (vec3s){1.0f, 0.0f, 0.0f},
|
||||
.roughness = 0.5f,
|
||||
.metallic = 0.0f,
|
||||
};
|
||||
simple_lit_data_t green_lit_data = {
|
||||
.albedo = (vec3s){0.0f, 1.0f, 0.0f},
|
||||
.roughness = 0.5f,
|
||||
.metallic = 0.0f,
|
||||
};
|
||||
simple_lit_data_t floor_lit_data = {
|
||||
.albedo = (vec3s){1.0f, 1.0f, 1.0f},
|
||||
.roughness = 0.75f,
|
||||
.metallic = 0.0f,
|
||||
};
|
||||
material_entity_t gray_material = material_create_simple_lit(&gray_lit_data, &scene.materials);
|
||||
material_entity_t gray_light_material = material_create_simple_lit(&gray_lit_data, &scene.materials);
|
||||
material_entity_t blue_material = material_create_simple_lit(&blue_lit_data, &scene.materials);
|
||||
material_entity_t red_material = material_create_simple_lit(&red_lit_data, &scene.materials);
|
||||
material_entity_t green_material = material_create_simple_lit(&green_lit_data, &scene.materials);
|
||||
material_entity_t floor_material = material_create_simple_lit(&floor_lit_data, &scene.materials);
|
||||
|
||||
scene.materials.buffer[top_light_material.id].emission = (vec3s){4.0f, 4.0f, 4.0f};
|
||||
// scene.materials.buffer[gray_light_material.id].emission = (vec3s){10.0f, 10.0f, 10.0f};
|
||||
|
||||
quad_create(
|
||||
(vec3s){0.0f, 1.95f, 0.0f},
|
||||
(vec3s){0.0f, -1.0f, 0.0f},
|
||||
(vec3s){0.0f, 0.0f, 1.0f},
|
||||
1.0f, top_light_material.id, &scene.triangles
|
||||
1.0f, gray_light_material.id, &scene.triangles
|
||||
);
|
||||
|
||||
triangle_create(
|
||||
(vec3s){-1.0f, -1.0f, 0.0f},
|
||||
(vec3s){1.0f, -1.0f, 0.0f},
|
||||
(vec3s){0.0f, 1.0f, 0.0f},
|
||||
gray_material.id, &scene.triangles
|
||||
);
|
||||
// sphere_create(
|
||||
// (vec3s){0.0f, 0.0f, 0.0f},
|
||||
// 1.0f, gray_light_material.id, &scene.triangles
|
||||
// );
|
||||
|
||||
quad_create(
|
||||
(vec3s){0.0f, -2.0f, 0.0f},
|
||||
(vec3s){0.0f, 1.0f, 0.0f},
|
||||
(vec3s){0.0f, 0.0f, 1.0f},
|
||||
4.0f, blue_material.id, &scene.triangles
|
||||
4.0f, floor_material.id, &scene.triangles
|
||||
);
|
||||
quad_create(
|
||||
(vec3s){0.0f, 2.0f, 0.0f},
|
||||
@@ -106,33 +125,63 @@ int main()
|
||||
(vec3s){0.0f, 0.0f, -2.0f},
|
||||
(vec3s){0.0f, 0.0f, 1.0f},
|
||||
(vec3s){0.0f, 1.0f, 0.0f},
|
||||
4.0f, gray_material.id, &scene.triangles
|
||||
4.0f, green_material.id, &scene.triangles
|
||||
);
|
||||
quad_create(
|
||||
(vec3s){-2.0f, 0.0f, 0.0f},
|
||||
(vec3s){1.0f, 0.0f, 0.0f},
|
||||
(vec3s){0.0f, 1.0f, 0.0f},
|
||||
4.0f, gray_material.id, &scene.triangles
|
||||
4.0f, blue_material.id, &scene.triangles
|
||||
);
|
||||
quad_create(
|
||||
(vec3s){2.0f, 0.0f, 0.0f},
|
||||
(vec3s){-1.0f, 0.0f, 0.0f},
|
||||
(vec3s){0.0f, 1.0f, 0.0f},
|
||||
4.0f, gray_material.id, &scene.triangles
|
||||
4.0f, red_material.id, &scene.triangles
|
||||
);
|
||||
|
||||
rendering_config_t config = {
|
||||
.width = WIDTH,
|
||||
.height = HEIGHT,
|
||||
.sample_count = SAMPLE_COUNT,
|
||||
.max_depth = 4,
|
||||
.tile_size = 16,
|
||||
};
|
||||
#pragma endregion
|
||||
|
||||
render_target_t img = scene_render(&scene, config);
|
||||
render_target_t img = render_target_create(config.width, config.height);
|
||||
// render_target_t img = scene_render(&scene, config);
|
||||
// save_img(&img, config.width, config.height, "output.png");
|
||||
|
||||
save_img(&img, WIDTH, HEIGHT, "output.png");
|
||||
MSG msg;
|
||||
uint16_t tile_index = 0;
|
||||
tile_t current_tile = {0};
|
||||
rendering_context_t ctx = {0};
|
||||
|
||||
while (true)
|
||||
{
|
||||
while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
|
||||
{
|
||||
if (msg.message == WM_QUIT)
|
||||
{
|
||||
goto exit;
|
||||
}
|
||||
|
||||
TranslateMessage(&msg);
|
||||
DispatchMessage(&msg);
|
||||
}
|
||||
|
||||
if (scene_render_tile(&scene, &ctx, &img, config, tile_index, ¤t_tile))
|
||||
{
|
||||
for (uint32_t y = current_tile.y; y < current_tile.y + current_tile.height; y++)
|
||||
{
|
||||
for (uint32_t x = current_tile.x; x < current_tile.x + current_tile.width; x++)
|
||||
{
|
||||
vec4s pixel = render_target_get_pixel(&img, x, y);
|
||||
// pixel = aces_tone_map(pixel);
|
||||
window_update_pixels(pixel, x, y);
|
||||
}
|
||||
}
|
||||
|
||||
window_refresh_region(current_tile.x, current_tile.y, current_tile.width, current_tile.height);
|
||||
tile_index++;
|
||||
}
|
||||
}
|
||||
|
||||
exit:
|
||||
render_target_free(&img);
|
||||
scene_free(&scene);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user