feat: add diffusion with gamma-correction
This commit is contained in:
parent
8ec8b93845
commit
497bb5776c
12
color.h
12
color.h
@ -1,16 +1,20 @@
|
||||
#ifndef COLOR_H
|
||||
#define COLOR_H
|
||||
|
||||
#include "vec3.h"
|
||||
#include "rtweekend.h"
|
||||
|
||||
#include <iostream>
|
||||
|
||||
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();
|
||||
|
||||
// Divide the color by the number of samples and gamma-correct for gamma=2.0
|
||||
auto scale = 1.0 / samples_per_pixel;
|
||||
auto r = pixel_color.x() * scale;
|
||||
auto g = pixel_color.y() * scale;
|
||||
auto b = pixel_color.z() * scale;
|
||||
r = sqrt(scale * r);
|
||||
g = sqrt(scale * g);
|
||||
b = sqrt(scale * b);
|
||||
|
||||
// Write the translated [0,255] value of each color component
|
||||
out << static_cast<int>(256 * clamp(r, 0.0, 0.999)) << " "
|
||||
|
17
main.cc
17
main.cc
@ -7,10 +7,16 @@
|
||||
|
||||
#include <iostream>
|
||||
|
||||
color ray_color(const ray& r, const hittable& world) {
|
||||
color ray_color(const ray& r, const hittable& world, int depth) {
|
||||
hit_record rec;
|
||||
|
||||
// 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, infinity, rec)) {
|
||||
return 0.5 * (rec.normal + color(1, 1, 1));
|
||||
point3 target = rec.p + rec.normal + random_in_unit_sphere();
|
||||
return 0.5 * ray_color(ray(rec.p, target - rec.p), world, depth-1);
|
||||
}
|
||||
|
||||
vec3 unit_direction = unit_vector(r.direction());
|
||||
@ -23,7 +29,8 @@ int main() {
|
||||
const auto aspect_ratio = 16.0 / 9.0;
|
||||
const int image_width = 400;
|
||||
const int image_height = static_cast<int>(image_width / aspect_ratio);
|
||||
const int samples_per_pixel = 10;
|
||||
const int samples_per_pixel = 32;
|
||||
const int max_depth = 25;
|
||||
|
||||
// World
|
||||
hittable_list world;
|
||||
@ -34,7 +41,7 @@ int main() {
|
||||
camera cam;
|
||||
|
||||
// Render
|
||||
std::cout<<"P3\n"<<image_width<<" "<<image_height<<"\n255\n";
|
||||
std::cout<<"P3\n" <<image_width<<" "<<image_height<<"\n255\n";
|
||||
for (int j = image_height-1; j >= 0; --j) {
|
||||
std::cerr<<"\rScanlines remaining: "<<j<<" "<<std::flush;
|
||||
for (int i = 0; i < image_width; ++i) {
|
||||
@ -43,7 +50,7 @@ int main() {
|
||||
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);
|
||||
pixel_color += ray_color(r, world, max_depth);
|
||||
}
|
||||
write_color(std::cout, pixel_color, samples_per_pixel);
|
||||
}
|
||||
|
17
vec3.h
17
vec3.h
@ -1,6 +1,7 @@
|
||||
#ifndef VEC3_H
|
||||
#define VEC3_H
|
||||
|
||||
#include "rtweekend.h"
|
||||
#include <cmath>
|
||||
#include <iostream>
|
||||
|
||||
@ -45,6 +46,14 @@ class vec3 {
|
||||
return e[0]*e[0] + e[1]*e[1] + e[2]*e[2];
|
||||
}
|
||||
|
||||
inline static vec3 random() {
|
||||
return vec3(random_double(), random_double(), random_double());
|
||||
}
|
||||
|
||||
inline static vec3 random(double min, double max) {
|
||||
return vec3(random_double(min, max), random_double(min, max), random_double(min, max));
|
||||
}
|
||||
|
||||
public:
|
||||
double e[3];
|
||||
};
|
||||
@ -95,6 +104,14 @@ inline vec3 cross(const vec3 &u, const vec3 &v) {
|
||||
u.e[0] * v.e[1] - u.e[1] * v.e[0]);
|
||||
}
|
||||
|
||||
vec3 random_in_unit_sphere() {
|
||||
while(true) {
|
||||
auto p = vec3::random(-1, 1);
|
||||
if (p.length_squared() >= 1) continue;
|
||||
return p;
|
||||
}
|
||||
}
|
||||
|
||||
inline vec3 unit_vector(vec3 v) {
|
||||
return v / v.length();
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user