Skip to content

Commit d8881c0

Browse files
authored
Cleanup Ray, misc hotfixes (#7)
* Hotfixes - `Rgb`: Fix originally reversed `/` and `*` logic. - `Vec`: add missing friend function for `operator-`. * Add Viewport, cleanup Ray / main
1 parent 6e03973 commit d8881c0

File tree

5 files changed

+39
-12
lines changed

5 files changed

+39
-12
lines changed

tray/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ target_sources(${PROJECT_NAME} PRIVATE
99
src/tray/ray.hpp
1010
src/tray/rgb.hpp
1111
src/tray/vec.hpp
12+
src/tray/viewport.hpp
1213

1314
src/main.cpp
1415
)

tray/src/main.cpp

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,32 @@
11
#include <tray/image.hpp>
22
#include <tray/io.hpp>
33
#include <tray/ray.hpp>
4-
#include <tray/vec.hpp>
4+
#include <tray/viewport.hpp>
55
#include <iostream>
66
#include <span>
77

88
using namespace tray;
99

1010
int main() {
1111
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()};
1620
auto image = Image{extent};
21+
1722
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);
1924
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));
2430
}
2531
}
2632

tray/src/tray/rgb.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ namespace tray {
55
struct Rgb : Vec<std::uint8_t, 3> {
66
using Vec::Vec;
77

8-
static constexpr float to_f32(std::uint8_t channel) { return static_cast<float>(channel) * 0xff; }
9-
static constexpr std::uint8_t to_u8(float channel) { return static_cast<std::uint8_t>(channel / 0xff); }
8+
static constexpr float to_f32(std::uint8_t channel) { return static_cast<float>(channel) / 0xff; }
9+
static constexpr std::uint8_t to_u8(float channel) { return static_cast<std::uint8_t>(channel * 0xff); }
1010

1111
static constexpr Rgb from_f32(fvec3 const& normalized) { return {to_u8(normalized.x()), to_u8(normalized.y()), to_u8(normalized.z())}; }
1212

tray/src/tray/vec.hpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,12 @@ class Vec {
9898
return *this;
9999
}
100100

101+
friend constexpr Vec operator-(Vec const& v) {
102+
auto ret = v;
103+
ret = -ret;
104+
return ret;
105+
}
106+
101107
template <VecOrType<Type, Dim> T>
102108
friend constexpr Vec operator+(Vec const& a, T const& b) {
103109
auto ret = a;

tray/src/tray/viewport.hpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#pragma once
2+
#include <tray/vec.hpp>
3+
4+
namespace tray {
5+
struct Viewport {
6+
fvec2 extent{1.0f};
7+
float depth{1.0f};
8+
9+
static constexpr Viewport make(uvec2 extent, float depth, float scale = 1.0f) {
10+
auto const ar = static_cast<float>(extent.x()) / static_cast<float>(extent.y());
11+
return ar > 1.0f ? Viewport{fvec2{scale, scale / ar}, depth} : Viewport{fvec2{scale * ar, scale}, depth};
12+
}
13+
};
14+
} // namespace tray

0 commit comments

Comments
 (0)