Basic functional raytracer
This commit is contained in:
parent
9870dc128e
commit
c37be6798f
5 changed files with 159 additions and 33 deletions
89
main.cpp
89
main.cpp
|
|
@ -1,5 +1,6 @@
|
|||
#include <stdio.h>
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "rtweekend.hpp"
|
||||
|
||||
|
|
@ -8,8 +9,63 @@
|
|||
#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<lambertian>(color(0.5, 0.5, 0.5));
|
||||
world.add(make_shared<sphere>(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<material> sphere_material;
|
||||
if (choose_mat < 0.8)
|
||||
{
|
||||
// diffuse
|
||||
color albedo = color::random() * color::random();
|
||||
sphere_material = make_shared<lambertian>(albedo);
|
||||
world.add(make_shared<sphere>(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<metal>(albedo, fuzz);
|
||||
world.add(make_shared<sphere>(center, 0.2, sphere_material));
|
||||
}
|
||||
else
|
||||
{
|
||||
// glass
|
||||
sphere_material = make_shared<dielectric>(1.5);
|
||||
world.add(make_shared<sphere>(center, 0.2, sphere_material));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
auto material1 = make_shared<dielectric>(1.5);
|
||||
world.add(make_shared<sphere>(point3(0, 1, 0), 1.0, material1));
|
||||
|
||||
auto material2 = make_shared<lambertian>(color(0.4, 0.2, 0.1));
|
||||
world.add(make_shared<sphere>(point3(-4, 1, 0), 1.0, material2));
|
||||
|
||||
auto material3 = make_shared<metal>(color(0.7, 0.6, 0.5), 0.0);
|
||||
world.add(make_shared<sphere>(point3(4, 1, 0), 1.0, material3));
|
||||
|
||||
return world;
|
||||
}
|
||||
|
||||
color ray_color(const ray& r, const hittable& world, int32_t depth)
|
||||
{
|
||||
|
|
@ -50,18 +106,16 @@ double hit_sphere(const point3& center, double radius, const ray& r)
|
|||
else
|
||||
return (-half_b - sqrt(discriminant)) / a;
|
||||
}
|
||||
|
||||
int main()
|
||||
int32_t main()
|
||||
{
|
||||
|
||||
|
||||
|
||||
// Image
|
||||
double aspect_ratio = 16.0 / 9;
|
||||
const int32_t image_width = 400;
|
||||
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 = 100;
|
||||
int32_t max_depth = 50;
|
||||
int32_t samples_per_pixel = 500;
|
||||
const int32_t max_depth = 50;
|
||||
|
||||
if (getenv("SPP"))
|
||||
{
|
||||
|
|
@ -71,21 +125,16 @@ int main()
|
|||
|
||||
|
||||
// World
|
||||
hittable_list world;
|
||||
|
||||
std::shared_ptr<lambertian> material_ground = make_shared<lambertian>(color(0.8, 0.8, 0.0));
|
||||
std::shared_ptr<lambertian> material_center = make_shared<lambertian>(color(0.7, 0.3, 0.3));
|
||||
std::shared_ptr<metal> material_left = make_shared<metal>(color(0.8, 0.8, 0.8), 0.3);
|
||||
std::shared_ptr<metal> material_right = make_shared<metal>(color(0.8, 0.6, 0.2), 1.0);
|
||||
|
||||
world.add(make_shared<sphere>(point3( 0.0, -100.5, -1.0), 100.0, material_ground));
|
||||
world.add(make_shared<sphere>(point3( 0.0, 0.0, -1.0), 0.5, material_center));
|
||||
world.add(make_shared<sphere>(point3(-1.0, 0.0, -1.0), 0.5, material_left));
|
||||
world.add(make_shared<sphere>(point3( 1.0, 0.0, -1.0), 0.5, material_right));
|
||||
|
||||
hittable_list world = random_scene();
|
||||
|
||||
// Camera
|
||||
camera cam;
|
||||
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);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue