| 
									
										
										
										
											2020-06-01 01:49:50 +01:00
										 |  |  | #pragma once
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-06 21:11:09 +01:00
										 |  |  | #include "rtweekend.h"
 | 
					
						
							| 
									
										
										
										
											2020-06-01 01:49:50 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | #include <iostream>
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-06 21:11:09 +01:00
										 |  |  | void write_colour(std::ostream &out, colour pixel_colour, int samples_per_pixel) | 
					
						
							| 
									
										
										
										
											2020-06-01 01:49:50 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2020-06-06 21:11:09 +01:00
										 |  |  |     auto r = pixel_colour.x(); | 
					
						
							|  |  |  |     auto g = pixel_colour.y(); | 
					
						
							|  |  |  |     auto b = pixel_colour.z(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-06 23:01:46 +01:00
										 |  |  |     // divide the colour total by the number of samples and gamme-correct for gamma=2.0
 | 
					
						
							| 
									
										
										
										
											2020-06-06 21:11:09 +01:00
										 |  |  |     auto scale = 1.0 / samples_per_pixel; | 
					
						
							| 
									
										
										
										
											2020-06-06 23:01:46 +01:00
										 |  |  |     r = sqrt(scale * r); | 
					
						
							|  |  |  |     g = sqrt(scale * g); | 
					
						
							|  |  |  |     b = sqrt(scale * b); | 
					
						
							| 
									
										
										
										
											2020-06-06 21:11:09 +01:00
										 |  |  |      | 
					
						
							| 
									
										
										
										
											2020-06-01 01:49:50 +01:00
										 |  |  |     // write the translated [0,255] value of each colour component.
 | 
					
						
							| 
									
										
										
										
											2020-06-06 21:11:09 +01:00
										 |  |  |     out << static_cast<int>(256 * clamp(r, 0.0, 0.999)) << ' ' | 
					
						
							|  |  |  |         << static_cast<int>(256 * clamp(g, 0.0, 0.999)) << ' ' | 
					
						
							|  |  |  |         << static_cast<int>(256 * clamp(b, 0.0, 0.999)) << '\n'; | 
					
						
							| 
									
										
										
										
											2020-06-01 01:49:50 +01:00
										 |  |  | } |