diffuse materials
This commit is contained in:
		
							parent
							
								
									4d2012a6a2
								
							
						
					
					
						commit
						92329f633d
					
				@ -10,11 +10,11 @@ void write_colour(std::ostream &out, colour pixel_colour, int samples_per_pixel)
 | 
				
			|||||||
    auto g = pixel_colour.y();
 | 
					    auto g = pixel_colour.y();
 | 
				
			||||||
    auto b = pixel_colour.z();
 | 
					    auto b = pixel_colour.z();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    // divide the colour total by the number of samples
 | 
					    // divide the colour total by the number of samples and gamme-correct for gamma=2.0
 | 
				
			||||||
    auto scale = 1.0 / samples_per_pixel;
 | 
					    auto scale = 1.0 / samples_per_pixel;
 | 
				
			||||||
    r *= scale;
 | 
					    r = sqrt(scale * r);
 | 
				
			||||||
    g *= scale;
 | 
					    g = sqrt(scale * g);
 | 
				
			||||||
    b *= scale;
 | 
					    b = sqrt(scale * b);
 | 
				
			||||||
    
 | 
					    
 | 
				
			||||||
    // write the translated [0,255] value of each colour component.
 | 
					    // write the translated [0,255] value of each colour component.
 | 
				
			||||||
    out << static_cast<int>(256 * clamp(r, 0.0, 0.999)) << ' '
 | 
					    out << static_cast<int>(256 * clamp(r, 0.0, 0.999)) << ' '
 | 
				
			||||||
 | 
				
			|||||||
							
								
								
									
										17
									
								
								src/main.cpp
									
									
									
									
									
								
							
							
						
						
									
										17
									
								
								src/main.cpp
									
									
									
									
									
								
							@ -11,20 +11,27 @@ const double ASPECT_RATIO = 16.0 / 9.0;
 | 
				
			|||||||
const int WIDTH = 384;
 | 
					const int WIDTH = 384;
 | 
				
			||||||
const int HEIGHT = static_cast<int>(WIDTH / ASPECT_RATIO);
 | 
					const int HEIGHT = static_cast<int>(WIDTH / ASPECT_RATIO);
 | 
				
			||||||
const int SAMPLES_PER_PIXEL = 100;
 | 
					const int SAMPLES_PER_PIXEL = 100;
 | 
				
			||||||
 | 
					const int MAX_DEPTH = 50;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
colour ray_colour(const ray& r, const hittable& world)
 | 
					colour ray_colour(const ray& r, const hittable& world, int depth)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
    hit_record rec;
 | 
					    hit_record rec;
 | 
				
			||||||
 | 
					    if (depth <= 0)
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        return colour(0,0,0);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    if (world.hit(r, 0, infinity, rec))
 | 
					    if (world.hit(r, 0, infinity, rec))
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
        return 0.5 * (rec.normal + colour(1,1,1));
 | 
					        point3 target = rec.p + rec.normal + random_in_unit_sphere();
 | 
				
			||||||
 | 
					        return 0.5 * ray_colour(ray(rec.p, target - rec.p), world, depth-1);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    vec3 unit_direction = unit_vector(r.direction());
 | 
					    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);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    auto a = colour(1.0, 0.5, 0.6);
 | 
					    auto a = colour(0.5, 0.6, 0.7);
 | 
				
			||||||
    auto b = colour(0.0, 0.0, 0.0);
 | 
					    auto b = colour(1.0, 1.0, 1.0);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    return lerp(a, b, t);
 | 
					    return lerp(a, b, t);
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
@ -60,7 +67,7 @@ int main()
 | 
				
			|||||||
                auto u = (i + random_double()) / (WIDTH-1);
 | 
					                auto u = (i + random_double()) / (WIDTH-1);
 | 
				
			||||||
                auto v = (j + random_double()) / (HEIGHT-1);
 | 
					                auto v = (j + random_double()) / (HEIGHT-1);
 | 
				
			||||||
                ray r = cam.get_ray(u, v);
 | 
					                ray r = cam.get_ray(u, v);
 | 
				
			||||||
                pixel_colour += ray_colour(r, world);
 | 
					                pixel_colour += ray_colour(r, world, MAX_DEPTH);
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            write_colour(std::cout, pixel_colour, SAMPLES_PER_PIXEL);
 | 
					            write_colour(std::cout, pixel_colour, SAMPLES_PER_PIXEL);
 | 
				
			||||||
 | 
				
			|||||||
							
								
								
									
										22
									
								
								src/vec3.h
									
									
									
									
									
								
							
							
						
						
									
										22
									
								
								src/vec3.h
									
									
									
									
									
								
							@ -3,9 +3,21 @@
 | 
				
			|||||||
#include <cmath>
 | 
					#include <cmath>
 | 
				
			||||||
#include <iostream>
 | 
					#include <iostream>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#include "rtweekend.h"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
class vec3
 | 
					class vec3
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
public:
 | 
					public:
 | 
				
			||||||
 | 
					    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));
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    vec3() : e{0,0,0} {}
 | 
					    vec3() : e{0,0,0} {}
 | 
				
			||||||
    vec3(double e0, double e1, double e2) : e{e0, e1, e2} {}
 | 
					    vec3(double e0, double e1, double e2) : e{e0, e1, e2} {}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -116,3 +128,13 @@ inline vec3 unit_vector(vec3 v)
 | 
				
			|||||||
{
 | 
					{
 | 
				
			||||||
    return v / v.length();
 | 
					    return v / v.length();
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					vec3 random_in_unit_sphere()
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					    while (true)
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        auto p = vec3::random(-1,1);
 | 
				
			||||||
 | 
					        if (p.length_squared() >= 1) continue;
 | 
				
			||||||
 | 
					        return p;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user