|
1 | 1 | #include <tray/image.hpp>
|
2 | 2 | #include <tray/io.hpp>
|
3 | 3 | #include <tray/ray.hpp>
|
4 |
| -#include <tray/vec.hpp> |
| 4 | +#include <tray/viewport.hpp> |
5 | 5 | #include <iostream>
|
6 | 6 | #include <span>
|
7 | 7 |
|
8 | 8 | using namespace tray;
|
9 | 9 |
|
10 | 10 | int main() {
|
11 | 11 | static constexpr auto extent = uvec2{400U, 300U};
|
12 |
| - static constexpr auto origin = fvec3{0.0f, 0.0f, 0.0f}; |
13 |
| - static constexpr auto horizontal = fvec3(extent.x() / 100.0f, 0.0f, 0.0f); |
14 |
| - static constexpr auto vertical = fvec3(0.0f, extent.y() / 100.0f, 0.0f); |
15 |
| - static constexpr auto lower_left = origin - horizontal / 2.0f - vertical / 2.0f - fvec3{0.0f, 0.0f, 1.0f}; |
| 12 | + static constexpr auto viewport = Viewport::make(extent, 2.0f, 2.0f); |
| 13 | + static constexpr auto origin = fvec3{}; |
| 14 | + |
| 15 | + static constexpr auto horizontal = fvec3{viewport.extent.x(), 0.0f, 0.0f}; |
| 16 | + static constexpr auto vertical = fvec3{0.0f, viewport.extent.y(), 0.0f}; |
| 17 | + static constexpr auto top_left = origin + 0.5f * (-horizontal + vertical) + fvec3{0.0f, 0.0f, -viewport.depth}; |
| 18 | + |
| 19 | + static constexpr fvec3 gradient[] = {Rgb::from_hex(0xffffff).to_f32(), Rgb::from_hex(0x002277).to_f32()}; |
16 | 20 | auto image = Image{extent};
|
| 21 | + |
17 | 22 | for (std::uint32_t row = 0; row < image.extent().y(); ++row) {
|
18 |
| - auto const yt = static_cast<float>(row) / static_cast<float>(image.extent().y()); |
| 23 | + auto const yt = static_cast<float>(row) / static_cast<float>(image.extent().y() - 1); |
19 | 24 | for (std::uint32_t col = 0; col < image.extent().x(); ++col) {
|
20 |
| - auto const xt = static_cast<float>(col) / static_cast<float>(image.extent().x()); |
21 |
| - auto const ray = Ray{origin, lower_left + xt * horizontal + yt * vertical - origin}; |
22 |
| - auto const t = 0.5f * (normalize(ray.direction.vec()).y() + 1.0f); |
23 |
| - image[{row, col}] = Rgb::from_f32(Rgb::from_hex(0xff0000).to_f32() * t + (1.0f - t) * Rgb::from_hex(0xffffff).to_f32()); |
| 25 | + auto const xt = static_cast<float>(col) / static_cast<float>(image.extent().x() - 1); |
| 26 | + auto const dir = top_left + xt * horizontal - yt * vertical - origin; |
| 27 | + auto const ray = Ray{origin, dir}; |
| 28 | + auto const t = 0.5f * (ray.direction.vec().y() + 1.0f); |
| 29 | + image[{row, col}] = Rgb::from_f32(lerp(gradient[0], gradient[1], t)); |
24 | 30 | }
|
25 | 31 | }
|
26 | 32 |
|
|
0 commit comments