141 lines
3.8 KiB
C
141 lines
3.8 KiB
C
#include <stdint.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <svpng.inc>
|
|
#include <omp.h>
|
|
#include <math.h>
|
|
|
|
#include "MaterialExtra.h"
|
|
#include "Rendering/Scene.h"
|
|
#include "Algorithm/BSDF.h"
|
|
#include "Material.h"
|
|
#include "Triangle.h"
|
|
#include "Geometry.h"
|
|
|
|
|
|
static inline 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)
|
|
{
|
|
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;
|
|
}
|
|
|
|
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);
|
|
}
|
|
|
|
int main()
|
|
{
|
|
omp_set_num_threads(24);
|
|
|
|
const int SAMPLE_COUNT = 256;
|
|
|
|
const uint32_t WIDTH = 640;
|
|
const uint32_t HEIGHT = 360;
|
|
|
|
scene_t scene = scene_create(64, 4);
|
|
|
|
simple_lit_data_t gray_lit_data = {
|
|
.albedo = (vec3s){0.73f, 0.73f, 0.73f},
|
|
.roughness = 1.0f,
|
|
.metallic = 0.0f,
|
|
};
|
|
simple_lit_data_t blue_lit_data = {
|
|
.albedo = (vec3s){0.0f, 0.0f, 1.0f},
|
|
.roughness = 0.5f,
|
|
.metallic = 1.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);
|
|
|
|
scene.materials.buffer[top_light_material.id].emission = (vec3s){4.0f, 4.0f, 4.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
|
|
);
|
|
|
|
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
|
|
);
|
|
|
|
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
|
|
);
|
|
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, gray_material.id, &scene.triangles
|
|
);
|
|
quad_create(
|
|
(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
|
|
);
|
|
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
|
|
);
|
|
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
|
|
);
|
|
|
|
rendering_config_t config = {
|
|
.width = WIDTH,
|
|
.height = HEIGHT,
|
|
.sample_count = SAMPLE_COUNT,
|
|
.max_depth = 4,
|
|
.tile_size = 16,
|
|
};
|
|
|
|
render_target_t img = scene_render(&scene, config);
|
|
|
|
save_img(&img, WIDTH, HEIGHT, "output.png");
|
|
|
|
render_target_free(&img);
|
|
scene_free(&scene);
|
|
|
|
return 0;
|
|
}
|