Add diffuse lights.

This commit is contained in:
Faerbit 2020-06-13 22:01:52 +02:00
parent f9605a7da2
commit 7699f26cf2
2 changed files with 39 additions and 14 deletions

View File

@ -18,22 +18,28 @@
#include "moving_sphere.h" #include "moving_sphere.h"
#include "camera.h" #include "camera.h"
Color ray_color(const Ray& r, const Hittable& world, int depth) { Color ray_color(const Ray& r, const Color& background, const Hittable& world, int depth) {
hit_record rec; hit_record rec;
if (depth <= 0) if (depth <= 0)
return Color(0, 0, 0); return Color(0, 0, 0);
if (world.hit(r, 0.001, infinity, rec)) { if (!world.hit(r, 0.001, infinity, rec))
return background;
Ray scattered; Ray scattered;
Color attenuation; Color attenuation;
if (rec.mat_ptr->scatter(r, rec, attenuation, scattered)) Color emitted = rec.mat_ptr->emitted(rec.u, rec.v, rec.p);
return attenuation * ray_color(scattered, world, depth - 1);
return Color(0, 0, 0); if (!rec.mat_ptr->scatter(r, rec, attenuation, scattered))
} return emitted;
Vec3 unit_direction = unit_vector(r.direction());
return emitted + attenuation * ray_color(scattered, background, world, depth - 1);
// fading background
/*Vec3 unit_direction = unit_vector(r.direction());
auto t = 0.5 * (unit_direction.y() + 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); return (1.0 - t) * Color(1.0, 1.0, 1.0) + t * Color(0.5, 0.7, 1.0);*/
} }
Bvh_node setup_random_scene(const int sph_i) { Bvh_node setup_random_scene(const int sph_i) {
@ -123,9 +129,12 @@ int main() {
const int image_width = 768; const int image_width = 768;
//const int image_width = 384; //const int image_width = 384;
const int image_height = static_cast<int>(image_width / aspect_ratio); const int image_height = static_cast<int>(image_width / aspect_ratio);
//const int samples_per_pixel = 10000;
//const int samples_per_pixel = 5000;
//const int samples_per_pixel = 2000; //const int samples_per_pixel = 2000;
//const int samples_per_pixel = 1000; const int samples_per_pixel = 1000;
const int samples_per_pixel = 400; //const int samples_per_pixel = 400;
//const int samples_per_pixel = 50;
const int max_depth = 50; const int max_depth = 50;
const int tile_size = 128; const int tile_size = 128;
@ -148,6 +157,7 @@ int main() {
//auto aperture = 0.0; //auto aperture = 0.0;
Camera cam(lookfrom, lookat, vup, 20, aspect_ratio, aperture, dist_to_focus, 0.0, 1.0); Camera cam(lookfrom, lookat, vup, 20, aspect_ratio, aperture, dist_to_focus, 0.0, 1.0);
const Color background(0, 0, 0);
std::atomic<int> queue_counter; std::atomic<int> queue_counter;
boost::lockfree::queue<render_tile> queue(0); boost::lockfree::queue<render_tile> queue(0);
@ -181,8 +191,8 @@ int main() {
for (int s = 0; s<samples_per_pixel; ++s) { for (int s = 0; s<samples_per_pixel; ++s) {
auto u = double(j + random_double(-0.5, 0.5)) / (image_width - 1); auto u = double(j + random_double(-0.5, 0.5)) / (image_width - 1);
auto v = double(i + random_double(-0.5, 0.5)) / (image_height - 1); auto v = double(i + random_double(-0.5, 0.5)) / (image_height - 1);
Ray r = cam.get_ray(u, v); Ray r = camera.get_ray(u, v);
pixel_color += ray_color(r, world, max_depth); pixel_color += ray_color(r, background, world, max_depth);
} }
image[i*image_width+j] = pixel_color; image[i*image_width+j] = pixel_color;
} }

View File

@ -10,6 +10,7 @@ struct hit_record;
class Material { class Material {
public: public:
virtual bool scatter(const Ray& r_in, const hit_record& rec, Color& attenuation, Ray& scattered) const = 0; virtual bool scatter(const Ray& r_in, const hit_record& rec, Color& attenuation, Ray& scattered) const = 0;
virtual Color emitted(double u, double v, const Point3& p) const { return Color(0, 0, 0); }
}; };
class Lambertian : public Material { class Lambertian : public Material {
@ -42,4 +43,18 @@ private:
double ref_idx; double ref_idx;
}; };
class Diffuse_light : public Material {
public:
Diffuse_light(std::shared_ptr<Texture> a) : emit(a) {}
virtual bool scatter(const Ray& r_in, const hit_record& rec, Color& attenuation, Ray& scattered) const {
return false;
}
virtual Color emitted(double u, double v, const Point3& p) const { return emit->value(u, v, p); }
private:
std::shared_ptr<Texture> emit;
};
#endif // MATERIAL_H #endif // MATERIAL_H