improve diffuse approximation
This commit is contained in:
		
							parent
							
								
									92329f633d
								
							
						
					
					
						commit
						b827c777e0
					
				@ -21,9 +21,9 @@ colour ray_colour(const ray& r, const hittable& world, int depth)
 | 
			
		||||
        return colour(0,0,0);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    if (world.hit(r, 0, infinity, rec))
 | 
			
		||||
    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_in_hemisphere(rec.normal);
 | 
			
		||||
        return 0.5 * ray_colour(ray(rec.p, target - rec.p), world, depth-1);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										21
									
								
								src/vec3.h
									
									
									
									
									
								
							
							
						
						
									
										21
									
								
								src/vec3.h
									
									
									
									
									
								
							@ -138,3 +138,24 @@ vec3 random_in_unit_sphere()
 | 
			
		||||
        return p;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
vec3 random_unit_vector()
 | 
			
		||||
{
 | 
			
		||||
    auto a = random_double(0, 2*pi);
 | 
			
		||||
    auto z = random_double(-1,1);
 | 
			
		||||
    auto r = sqrt(1 - z*z);
 | 
			
		||||
    return vec3(r*cos(a), r*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;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user