#include #include #include // 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 #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); double hit_sphere(const point3& center, double radius, const ray& r); hittable_list random_scene(); hittable_list random_scene() { hittable_list world; auto ground_material = make_shared(color(0.5, 0.5, 0.5)); world.add(make_shared(point3(0,-1000,0), 1000, ground_material)); for (int32_t a = -11; a < 11; a++) { for (int32_t b = -11; b < 11; b++) { double choose_mat = random_double(); point3 center(a + 0.9*random_double(), 0.2, b + 0.9*random_double()); if ((center - point3(4, 0.2, 0)).length() > 0.9) { shared_ptr sphere_material; if (choose_mat < 0.8) { // diffuse color albedo = color::random() * color::random(); sphere_material = make_shared(albedo); world.add(make_shared(center, 0.2, sphere_material)); } else if (choose_mat < 0.95) { // metal color albedo = color::random(0.5, 1); double fuzz = random_double(0, 0.5); sphere_material = make_shared(albedo, fuzz); world.add(make_shared(center, 0.2, sphere_material)); } else { // glass sphere_material = make_shared(1.5); world.add(make_shared(center, 0.2, sphere_material)); } } } } auto material1 = make_shared(1.5); world.add(make_shared(point3(0, 1, 0), 1.0, material1)); auto material2 = make_shared(color(0.4, 0.2, 0.1)); world.add(make_shared(point3(-4, 1, 0), 1.0, material2)); auto material3 = make_shared(color(0.7, 0.6, 0.5), 0.0); world.add(make_shared(point3(4, 1, 0), 1.0, material3)); return world; } color ray_color(const ray& r, const hittable& 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); double t = 0.5 * (unit_direction.y + 1.0); return (1-t) * color(1,1,1) + t*color(0.5,0.7,1.0); } double hit_sphere(const point3& center, double radius, const ray& r) { vec3 oc = r.origin - center; double a = r.direction.length_squared(); double half_b = dot(oc, r.direction); double c = oc.length_squared() - radius*radius; double 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 double 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 world = random_scene(); // Camera point3 lookfrom(13,2,3); point3 lookat(0,0,0); vec3 vup(0,1,0); double dist_to_focus = 10.0; double 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); 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) { double u = ((i + random_double()) / (image_width-1)); double v = ((j + random_double()) / (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); }