add reflectance
This commit is contained in:
		
							parent
							
								
									4a478b08a0
								
							
						
					
					
						commit
						7d830d13d1
					
				
					 1 changed files with 9 additions and 1 deletions
				
			
		| 
						 | 
				
			
			@ -58,13 +58,21 @@ pub const Dielectric = struct {
 | 
			
		|||
        const sin_theta = math.sqrt(1.0 - cos_theta * cos_theta);
 | 
			
		||||
 | 
			
		||||
        const cant_refract = refraction_ratio * sin_theta > 1.0;
 | 
			
		||||
        const direction = if (cant_refract) unit_direction.reflect(hit.normal)
 | 
			
		||||
        const should_reflect = reflectance(cos_theta, refraction_ratio) > rng.float(f32);
 | 
			
		||||
        const direction = if (cant_refract or should_reflect) unit_direction.reflect(hit.normal)
 | 
			
		||||
            else unit_direction.refract(hit.normal, refraction_ratio);
 | 
			
		||||
        return ScatteredRay{
 | 
			
		||||
            .ray = Ray{ .origin = hit.p, .direction = direction},
 | 
			
		||||
            .color = attenuation,
 | 
			
		||||
        };
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    fn reflectance(cosine: f32, ref_idx: f32) f32 {
 | 
			
		||||
        // Use Schlick's approximation for reflectance.
 | 
			
		||||
        var r0 = (1 - ref_idx) / (1 + ref_idx);
 | 
			
		||||
        r0 = r0*r0;
 | 
			
		||||
        return r0 + (1 - r0) * math.pow(f32, 1 - cosine, 5);
 | 
			
		||||
    }
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
pub const ScatteredRay = struct {
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue