Antialiasing plus camera abstraction
This commit is contained in:
parent
1b78b8815f
commit
336b2a5598
@ -5,4 +5,4 @@
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
### Original author Post: https://raytracing.github.io/books/RayTracingInOneWeekend.html#overview
|
### Original author Post: https://raytracing.github.io/books/RayTracingInOneWeekend.html
|
27
src/camera.h
Normal file
27
src/camera.h
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
#pragma once
|
||||||
|
#include "rtweekend.h"
|
||||||
|
|
||||||
|
class camera{
|
||||||
|
public:
|
||||||
|
camera(){
|
||||||
|
auto aspect_ratio = 16.0 / 9.0;
|
||||||
|
auto viewport_height = 2.0;
|
||||||
|
auto viewport_width = aspect_ratio * viewport_height;
|
||||||
|
auto focal_length = 1.0;
|
||||||
|
|
||||||
|
origin = point3(0,0,0);
|
||||||
|
horizontal = vec3(viewport_width, 0, 0);
|
||||||
|
vertical = vec3(0, viewport_height, 0);
|
||||||
|
lower_left_corner = origin - horizontal/2 - vertical/2 - vec3(0,0,focal_length);
|
||||||
|
}
|
||||||
|
|
||||||
|
ray get_ray(double u, double v) const {
|
||||||
|
return ray(origin, lower_left_corner + u * horizontal + v*vertical - origin);
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
point3 origin;
|
||||||
|
point3 lower_left_corner;
|
||||||
|
vec3 horizontal;
|
||||||
|
vec3 vertical;
|
||||||
|
};
|
21
src/color.h
21
src/color.h
@ -3,10 +3,21 @@
|
|||||||
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
void write_color(std::ostream& out, color pixel_color){
|
void write_color(std::ostream& out, color pixel_color, int samples_per_pixel){
|
||||||
// Write the translated [0, 255] value of each color component
|
auto r = pixel_color.x();
|
||||||
|
auto g = pixel_color.y();
|
||||||
|
auto b = pixel_color.z();
|
||||||
|
|
||||||
out << static_cast<int>( 255.999 * pixel_color.x()) << ' '
|
// Divide the color by the number of samples
|
||||||
<< static_cast<int> ( 255.999 * pixel_color.y()) << ' '
|
auto scale = 1.0 / samples_per_pixel;
|
||||||
<< static_cast<int> ( 255.999 * pixel_color.z()) << '\n';
|
r *= scale;
|
||||||
|
g *= scale;
|
||||||
|
b *= scale;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
// Write the translated [0, 255] value of each color component
|
||||||
|
out << static_cast<int>( 256 * clamp(r, 0.0, 0.9999)) << ' '
|
||||||
|
<< static_cast<int> ( 256 * clamp(g, 0.0, 0.9999)) << ' '
|
||||||
|
<< static_cast<int> ( 256 * clamp(b, 0.0, 0.9999)) << '\n';
|
||||||
}
|
}
|
@ -1,5 +1,6 @@
|
|||||||
#include "rtweekend.h"
|
#include "rtweekend.h"
|
||||||
|
|
||||||
|
#include "camera.h"
|
||||||
#include "color.h"
|
#include "color.h"
|
||||||
#include "hittable_list.h"
|
#include "hittable_list.h"
|
||||||
#include "sphere.h"
|
#include "sphere.h"
|
||||||
@ -24,6 +25,7 @@ int main ()
|
|||||||
const auto aspect_ratio = 16.0 /9.0;
|
const auto aspect_ratio = 16.0 /9.0;
|
||||||
const int image_width = 400;
|
const int image_width = 400;
|
||||||
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 = 100;
|
||||||
|
|
||||||
// World
|
// World
|
||||||
hittable_list world;
|
hittable_list world;
|
||||||
@ -32,14 +34,7 @@ int main ()
|
|||||||
|
|
||||||
|
|
||||||
// Camera
|
// Camera
|
||||||
auto viewport_height = 2.0;
|
camera cam;
|
||||||
auto viewport_width = aspect_ratio * viewport_height;
|
|
||||||
auto focal_length = 1.0;
|
|
||||||
|
|
||||||
auto origin = point3(0,0,0);
|
|
||||||
auto horizontal = vec3(viewport_width, 0, 0);
|
|
||||||
auto vertical = vec3(0, viewport_height, 0);
|
|
||||||
auto lower_left_corner = origin - horizontal/2 - vertical/2 - vec3(0,0,focal_length);
|
|
||||||
|
|
||||||
// Render
|
// Render
|
||||||
std::cout<< "P3\n" << image_width << ' ' << image_height << "\n255\n";
|
std::cout<< "P3\n" << image_width << ' ' << image_height << "\n255\n";
|
||||||
@ -48,11 +43,15 @@ int main ()
|
|||||||
std::cerr << "\rScanlines remaining: " << j << ' ' << std::flush;
|
std::cerr << "\rScanlines remaining: " << j << ' ' << std::flush;
|
||||||
for(int i = 0; i < image_width; ++i)
|
for(int i = 0; i < image_width; ++i)
|
||||||
{
|
{
|
||||||
auto u = double(i) / (image_width-1);
|
color pixel_color(0,0,0);
|
||||||
auto v = double(j) / (image_height-1);
|
for(int s = 0; s < samples_per_pixel; ++s){
|
||||||
ray r(origin, lower_left_corner+u*horizontal + v* vertical -origin);
|
auto u = (i + random_double()) / (image_width-1);
|
||||||
color pixel_color = ray_color(r, world);
|
auto v = (j + random_double()) / (image_height-1);
|
||||||
write_color(std::cout , pixel_color);
|
ray r = cam.get_ray(u,v);
|
||||||
|
pixel_color += ray_color(r, world);
|
||||||
|
}
|
||||||
|
|
||||||
|
write_color(std::cout, pixel_color, samples_per_pixel);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -3,6 +3,7 @@
|
|||||||
#include <cmath>
|
#include <cmath>
|
||||||
#include <limits>
|
#include <limits>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
|
#include <cstdlib>
|
||||||
|
|
||||||
// Usings
|
// Usings
|
||||||
using std::shared_ptr;
|
using std::shared_ptr;
|
||||||
@ -19,6 +20,20 @@ inline double degrees_to_radians(double degrees){
|
|||||||
return degrees * pi / 180.0;
|
return degrees * pi / 180.0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
inline double random_double(){
|
||||||
|
return rand() / (RAND_MAX + 1.0);
|
||||||
|
}
|
||||||
|
|
||||||
|
inline double random_double(double min, double max){
|
||||||
|
return min + (max-min)*random_double();
|
||||||
|
}
|
||||||
|
|
||||||
|
inline double clamp(double x, double min, double max){
|
||||||
|
if(x < min) return min;
|
||||||
|
if(x > max) return max;
|
||||||
|
return x;
|
||||||
|
}
|
||||||
|
|
||||||
// Common Headers
|
// Common Headers
|
||||||
#include "ray.h"
|
#include "ray.h"
|
||||||
#include "vector3.h"
|
#include "vector3.h"
|
||||||
|
Loading…
x
Reference in New Issue
Block a user