Metallic material

This commit is contained in:
Armin Friedl 2022-07-30 18:19:32 +02:00
parent c551944e9d
commit 2b653789d8
7 changed files with 175 additions and 65 deletions

View file

@ -6,6 +6,7 @@ set(CMAKE_CXX_STANDARD 17)
add_executable(rtiww src/main.cpp
src/vec3.h src/ray.h
src/color.h
src/material.h
src/hittable.h src/hittable_list.h
src/sphere.h
src/camera.h

View file

@ -52,4 +52,20 @@ private:
}
};
void write_color(std::ostream &out, color pixel_color,
int samples_per_pixel) {
auto r = pixel_color.x();
auto g = pixel_color.y();
auto b = pixel_color.z();
auto scale = 1.0 / samples_per_pixel;
r = sqrt(scale * r);
g = sqrt(scale * g);
b = sqrt(scale * b);
out << static_cast<int>(255.999 * clamp(r, 0.0, 0.999)) << ' '
<< static_cast<int>(255.999 * clamp(g, 0.0, 0.999)) << ' '
<< static_cast<int>(255.999 * clamp(b, 0.0, 0.999)) << '\n';
}
#endif // RTIWW_COLOR_H

View file

@ -2,17 +2,22 @@
#define HITTABLE_H_
#include "ray.h"
#include "rtweekend.h"
#include "vec3.h"
#include <memory>
class material;
struct hit_record {
point3 p;
vec3 normal;
std::shared_ptr<material> mat_ptr;
double t;
bool front_face;
inline void set_face_normal(const ray& r, const vec3& outward_normal) {
inline void set_face_normal(const ray &r, const vec3 &outward_normal) {
front_face = dot(r.direction(), outward_normal) < 0;
normal = front_face ? outward_normal: -outward_normal;
normal = front_face ? outward_normal : -outward_normal;
}
};

View file

@ -4,29 +4,37 @@
#include "color.h"
#include "hittable_list.h"
#include "material.h"
#include "sphere.h"
#include "vec3.h"
#include <iostream>
#include <memory>
color ray_color(const ray &r, const hittable &world, int depth) {
hit_record rec;
if (depth <= 0)
return color(0, 0, 0);
if (world.hit(r, 0.001, infinity, rec)) {
point3 target = rec.p + random_in_hemisphere(rec.normal);
return 0.5 * ray_color(ray(rec.p, target - rec.p), world, depth - 1);
// If we've exceeded the ray bounce limit, no more light is gathered.
if (depth <= 0)
return color(0,0,0);
if (world.hit(r, 0.000001, infinity, rec)) {
ray scattered;
color attenuation;
if (rec.mat_ptr->scatter(r, rec, attenuation, scattered))
return attenuation * ray_color(scattered, world, depth-1);
return color(0,0,0);
}
vec3 unit_direction = unit_vector(r.direction());
auto t = 0.5 * (unit_direction.y() + 1.0);
return (1.0 - t) * color(1.0, 1.0, 1.0) + t * color(0.5, 0.7, 1.0);
auto t = 0.5*(unit_direction.y() + 1.0);
return (1.0-t)*color(1.0, 1.0, 1.0) + t*color(0.5, 0.7, 1.0);
}
int main() {
// Image
const auto aspect_ratio = 16.0 / 9.0;
const int image_width = 400;
const int image_height = static_cast<int>(image_width / aspect_ratio);
@ -34,36 +42,41 @@ int main() {
const int max_depth = 50;
// World
hittable_list world;
world.add(make_shared<sphere>(point3(0, 0, -1), 0.5));
world.add(make_shared<sphere>(point3(0, -100.5, -1), 100));
auto material_ground = make_shared<lambertian>(color(0.8, 0.8, 0.0));
auto material_center = make_shared<metal>(color(0.7, 0.3, 0.3));
auto material_left = make_shared<metal>(color(0.8, 0.8, 0.8));
auto material_right = make_shared<metal>(color(0.8, 0.6, 0.2));
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));
// Camera
camera cam;
// Render
std::cout << "P3\n" << image_width << ' ' << image_height << "\n255\n";
picture p;
std::cout << "P3\n" << image_width << " " << image_height << "\n255\n";
for (int j = image_height - 1; j >= 0; --j) {
for (int j = image_height-1; j >= 0; --j) {
std::cerr << "\rScanlines remaining: " << j << ' ' << std::flush;
for (int i = 0; i < image_width; ++i) {
color pixel_color(0, 0, 0);
for (int s = 0; s < samples_per_pixel; ++s) {
auto u = (i + random_double()) / (image_width - 1);
auto v = (j + random_double()) / (image_height - 1);
auto u = (i + random_double()) / (image_width-1);
auto v = (j + random_double()) / (image_height-1);
ray r = cam.get_ray(u, v);
pixel_color += ray_color(r, world, max_depth);
}
p.set_pixel(j, i, pixel_color);
write_color(std::cout, pixel_color, samples_per_pixel);
}
}
p.write(std::cout, image_height, image_width, samples_per_pixel);
std::cerr << "\nDone.\n";
return 0;
}

57
rtiww/src/material.h Normal file
View file

@ -0,0 +1,57 @@
#ifndef MATERIAL_H_
#define MATERIAL_H_
#include "hittable.h"
#include "rtweekend.h"
#include "vec3.h"
struct hit_record;
class material {
public:
virtual bool scatter(const ray &r_in, const hit_record &rec,
color &attenuation, ray &scattered) const = 0;
};
class lambertian : public material {
public:
lambertian(const color& a) : albedo(a) {}
virtual bool scatter(
const ray& r_in, const hit_record& rec, color& attenuation, ray& scattered
) const override {
auto scatter_direction = rec.normal+random_in_unit_sphere();
// Catch degenerate scatter direction
if (scatter_direction.near_zero())
scatter_direction = rec.normal;
scattered = ray(rec.p, scatter_direction);
attenuation = albedo;
return true;
}
public:
color albedo;
};
class metal : public material {
public:
metal(const color& a) : albedo(a) {}
virtual bool scatter(
const ray& r_in, const hit_record& rec, color& attenuation, ray& scattered
) const override {
vec3 reflected = reflect(unit_vector(r_in.direction()), rec.normal);
scattered = ray(rec.p, reflected);
attenuation = albedo;
return true;
}
public:
color albedo;
};
#endif // MATERIAL_H_

View file

@ -2,39 +2,48 @@
#define SPHERE_H_
#include "hittable.h"
#include "material.h"
class sphere: public hittable {
class sphere : public hittable {
public:
sphere() {}
sphere(point3 cen, double r): center(cen), radius(r) {};
virtual bool hit(const ray& r, double t_min, double t_max, hit_record& rec) const override;
sphere(point3 cen, double r, shared_ptr<material> m)
: center(cen), radius(r), mat_ptr(m) {};
virtual bool hit(
const ray& r, double t_min, double t_max, hit_record& rec) const override;
public:
point3 center;
double radius;
shared_ptr<material> mat_ptr;
};
bool sphere::hit(const ray& r, double t_min, double t_max, hit_record& rec) const {
bool sphere::hit(const ray &r, double t_min, double t_max,
hit_record &rec) const {
vec3 oc = r.origin() - center;
auto a = r.direction().length_squared();
auto half_b = dot(oc, r.direction());
auto c = oc.length_squared() - radius*radius;
auto c = oc.length_squared() - radius * radius;
auto discriminant = half_b*half_b - a*c;
if(discriminant < 0) return false;
auto discriminant = half_b * half_b - a * c;
if (discriminant < 0)
return false;
auto sqrtd = sqrt(discriminant);
// Find the nearest root that lies in the acceptable range
auto root = (-half_b - sqrtd) / a;
if(root < t_min || t_max<root) {
if (root < t_min || t_max < root) {
root = (-half_b + sqrtd) / a;
if (root < t_min || t_max < root) return false;
if (root < t_min || t_max < root)
return false;
}
rec.t = root;
rec.p = r.at(rec.t);
vec3 outward_normal = (rec.p - center) / radius;
rec.set_face_normal(r, outward_normal);
rec.mat_ptr = mat_ptr;
return true;
}

View file

@ -52,6 +52,13 @@ public:
random_double(min, max));
}
bool near_zero() const {
using std::fabs;
// Return true if the vector is close to zero in all dimensions.
const auto s = 1e-8;
return (fabs(e[0]) < s) && (fabs(e[1]) < s) && (fabs(e[2]) < s);
}
public:
double e[3];
};
@ -115,4 +122,6 @@ vec3 random_in_hemisphere(const vec3 &normal) {
return -in_unit_sphere;
}
vec3 reflect(const vec3 &v, const vec3 &n) { return v - 2 * dot(v, n) * n; }
#endif // RTIWW_VEC3_H