cpp-raytracer/main.cpp
2021-08-28 01:04:31 +02:00

213 lines
6.2 KiB
C++

#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
// Disable profiling
#define RMT_ENABLED 1
// Lib includes
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-parameter"
#pragma GCC diagnostic ignored "-Wunused-variable"
#pragma GCC diagnostic ignored "-Wsign-compare"
#include <Remotery.c>
#pragma GCC diagnostic pop
// Internal includes
#include "rtweekend.hpp"
#include "color.hpp"
#include "hittable_list.hpp"
#include "sphere.hpp"
#include "camera.hpp"
color ray_color(const ray& r, const hittable& world, int32_t depth);
float hit_sphere(const point3& center, float radius, const ray& r);
void print_timers();
hittable_list<sphere> random_scene();
hittable_list<sphere> random_scene() {
hittable_list<sphere> world;
auto ground_material = make_shared<lambertian>(color(0.5, 0.5, 0.5));
world.add(sphere(point3(0,-1000,0), 1000, ground_material));
for (int32_t a = -11; a < 11; a++)
{
for (int32_t b = -11; b < 11; b++)
{
float choose_mat = random_float();
point3 center(a + 0.9*random_float(), 0.2, b + 0.9*random_float());
if ((center - point3(4, 0.2, 0)).length() > 0.9)
{
shared_ptr<material> sphere_material;
if (choose_mat < 0.8)
{
// diffuse
color albedo = color::random() * color::random();
sphere_material = make_shared<lambertian>(albedo);
world.add(sphere(center, 0.2, sphere_material));
}
else if (choose_mat < 0.95)
{
// metal
color albedo = color::random(0.5, 1);
float fuzz = random_float(0, 0.5);
sphere_material = make_shared<metal>(albedo, fuzz);
world.add(sphere(center, 0.2, sphere_material));
}
else
{
// glass
sphere_material = make_shared<dielectric>(1.5);
world.add(sphere(center, 0.2, sphere_material));
}
}
}
}
auto material1 = make_shared<dielectric>(1.5);
world.add(sphere(point3(0, 1, 0), 1.0, material1));
auto material2 = make_shared<lambertian>(color(0.4, 0.2, 0.1));
world.add(sphere(point3(-4, 1, 0), 1.0, material2));
auto material3 = make_shared<metal>(color(0.7, 0.6, 0.5), 0.0);
world.add(sphere(point3(4, 1, 0), 1.0, material3));
return world;
}
template<typename T>
color ray_color(const ray& r, hittable_list<T>& world, int32_t depth)
{
rmt_ScopedCPUSample(Scatter, RMTSF_Aggregate | RMTSF_Recursive);
if (depth <= 0)
{
return color(0,0,0);
}
hit_record rec;
if (world.hit(r, 0.001, INFINITY, rec))
{
ray scattered;
color attenuation;
rmt_BeginCPUSample(Scatter, RMTSF_Aggregate);
bool visible = rec.mat_ptr->scatter(r, rec, attenuation, scattered);
rmt_EndCPUSample();
if (visible)
{
return attenuation * ray_color(scattered, world, depth-1);
}
else
{
return color(0,0,0);
}
}
vec3 unit_direction = normalize(r.direction);
float t = 0.5 * (unit_direction.y + 1.0);
return (1-t) * color(1,1,1) + t*color(0.5,0.7,1.0);
}
float hit_sphere(const point3& center, float radius, const ray& r)
{
vec3 oc = r.origin - center;
float a = r.direction.length_squared();
float half_b = dot(oc, r.direction);
float c = oc.length_squared() - radius*radius;
float discriminant = half_b*half_b - a*c;
if (discriminant < 0)
return -1;
else
return (-half_b - sqrt(discriminant)) / a;
}
int32_t main()
{
/* Profiling library initialization */
Remotery *rmt;
if (RMT_ERROR_NONE != rmt_CreateGlobalInstance(&rmt))
{
fprintf(stderr, "Error starting Remotery\n");
}
// Image
const float aspect_ratio = 3.0 / 2.0;
const int32_t image_width = 1200;
const int32_t image_height = (int32_t) (image_width / aspect_ratio);
int32_t samples_per_pixel = 500;
const int32_t max_depth = 50;
if (getenv("SPP"))
{
samples_per_pixel = strtol(getenv("SPP"), NULL, 10);
}
// World
hittable_list<sphere> world = random_scene();
// Camera
point3 lookfrom(13,2,3);
point3 lookat(0,0,0);
vec3 vup(0,1,0);
float dist_to_focus = 10.0;
float aperture = 0.1;
camera cam(lookfrom, lookat, vup, 20, aspect_ratio, aperture, dist_to_focus);
// Render
printf("P3\n%d %d\n255\n", image_width, image_height);
for (int32_t j = image_height - 1; j >= 0; --j)
{
rmt_ScopedCPUSample(OuterLoop, RMTSF_Aggregate);
fprintf(stderr, "\rScanlines remaining: %d ", j);
print_timers();
fflush(stderr);
for (int32_t i = 0; i < image_width; ++i)
{
rmt_ScopedCPUSample(InnerLoop, RMTSF_Aggregate);
color pixel_color = color(0,0,0);
for (int32_t s = 0; s < samples_per_pixel; ++s)
{
float u = ((i + random_float()) / (image_width-1));
float v = ((j + random_float()) / (image_height-1));
ray r = cam.get_ray(u,v);
pixel_color += ray_color(r, world, max_depth);
}
write_color(stdout, pixel_color, samples_per_pixel);
}
}
fprintf(stderr, "\nDone\n");
rmt_DestroyGlobalInstance(rmt);
}
debug_record debug_record_array[__COUNTER__];
void print_timers()
{
for (uint32_t i = 0;
i < sizeof(debug_record_array) / sizeof(debug_record_array[0]);
++i)
{
debug_record *record = &debug_record_array[i];
fprintf(stderr,
"%d: %s:%s:%d; "
"Cycles = %ld; "
"Hit count %ld; "
"Cycles/hit %f; "
"Time %f",
i, record->filename, record->function_name, record->line_number,
record->cycles,
record->hit_count,
(double)record->cycles / record->hit_count,
(double)record->cycles / CLOCKS_PER_SEC);
}
}