-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
64 lines (54 loc) · 1.92 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include "Vec3.hpp"
#include "Ray.hpp"
#include "Color.h"
#include "Utils.hpp"
#include "Sphere.h"
#include <iostream>
/*
* Linearly blends two colors from top to bottom as a vertical gradient.
*
* blendedValue = (1-t)*startValue + t*endValue
*
* [Ref: https://raytracing.github.io/books/RayTracingInOneWeekend.html
* #rays,asimplecamera,andbackground/sendingraysintothescene]
*/
color::Color ray_color(const color::Color bottom,
const color::Color upper,
const Ray& r)
{
const Sphere sphere(Point3(0, 0, -1), 0.5);
const auto record = sphere.hit(r, 0, 1);
if (record && record->t > 0.0) {
return utils::fit_on_viewport(record->normal);
}
const Vec3 direction = r.direction().versor();
const auto t = utils::fit_on_viewport(direction.y);
return bottom*(1.0-t) + upper*t;
}
int main() {
// Image definition
const double aspect_ratio = 16.0 / 9.0;
const int width = 400;
const int height = static_cast<int>(width / aspect_ratio);
// Camera definition
const double viewport_height = 2.0;
const double viewport_width = aspect_ratio * viewport_height;
const double focal_length = 1.0;
const auto origin = Point3(0, 0, 0);
const auto horizontal = Vec3(viewport_width, 0, 0);
const auto vertical = Vec3(0, viewport_height, 0);
// Lower-left corner
const auto llc = origin - horizontal/2 - vertical/2 - Vec3(0, 0, focal_length);
// Render
std::cout << "P3\n" << width << " " << height << "\n255\n";
for (int i = height-1; i >= 0; --i) {
for (int j = 0; j < width; ++j) {
const auto u = double(j) / (width-1);
const auto v = double(i) / (height-1);
const Ray r(origin, llc + horizontal*u + vertical*v - origin);
const auto pixel = ray_color(color::White, color::Black, r);
color::write(std::cout, pixel);
}
}
return 0;
}