Add additional scattering methods.

This commit is contained in:
Faerbit 2020-06-06 16:16:31 +02:00
parent b86ad047a5
commit 267df23c87
3 changed files with 21 additions and 4 deletions

View File

@ -10,7 +10,7 @@ TARGET = wtracer
all: $(TARGET) run
run:
./wtracer > image.ppm
time -f '%E elapsed' ./wtracer > image.ppm
eog image.ppm
%.o: %.cpp $(DEPS)

15
vec3.h
View File

@ -120,4 +120,19 @@ Vec3 random_in_unit_sphere() {
}
}
Vec3 random_unit_vector() {
auto a = random_double(0, 2*pi);
auto z = random_double(-1, 1);
auto r = std::sqrt(1 - z*z);
return Vec3(r*std::cos(a), r*std::sin(a), z);
}
Vec3 random_in_hemisphere(const Vec3& normal) {
Vec3 in_unit_sphere = random_in_unit_sphere();
if (dot(in_unit_sphere, normal) > 0.0)
return in_unit_sphere;
else
return -in_unit_sphere;
}
#endif // VEC3_H

View File

@ -15,8 +15,10 @@ Color ray_color(const Ray& r, const Hittable& world, int depth) {
if (depth <= 0)
return Color(0, 0, 0);
if (world.hit(r, 0, infinity, rec)) {
Point3 target = rec.p + rec.normal + random_in_unit_sphere();
if (world.hit(r, 0.001, infinity, rec)) {
//Point3 target = rec.p + rec.normal + random_in_unit_sphere();
//Point3 target = rec.p + rec.normal + random_unit_vector();
Point3 target = rec.p + random_in_hemisphere(rec.normal);
return 0.5 * ray_color(Ray(rec.p, target - rec.p), world, depth-1);
}
Vec3 unit_direction = unit_vector(r.direction());
@ -30,7 +32,7 @@ int main() {
const int image_width = 768;
//const int image_width = 384;
const int image_height = static_cast<int>(image_width / aspect_ratio);
const int samples_per_pixel = 250;
const int samples_per_pixel = 400;
const int max_depth = 50;
std::cout << "P3\n" << image_width << " " << image_height << "\n255\n";