Skip to content

Commit 6e03973

Browse files
karnkaulKouros26
andauthored
Ray (#6)
* [WIP] Add nvec3, Ray * 🐛 Fix magnitude return type * Ray class incorporated * Update main.cpp * constexpr fix, clang format, minor fixups Co-authored-by: Kouros LOTFI <k.lotfi@student.isartdigital.com> Co-authored-by: Kouros26 <65140699+Kouros26@users.noreply.github.com>
1 parent 2854d17 commit 6e03973

File tree

5 files changed

+56
-5
lines changed

5 files changed

+56
-5
lines changed

tray/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ target_sources(${PROJECT_NAME} PRIVATE
55
src/tray/image.hpp
66
src/tray/io.cpp
77
src/tray/io.hpp
8+
src/tray/nvec3.hpp
9+
src/tray/ray.hpp
810
src/tray/rgb.hpp
911
src/tray/vec.hpp
1012

tray/src/main.cpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include <tray/image.hpp>
22
#include <tray/io.hpp>
3+
#include <tray/ray.hpp>
34
#include <tray/vec.hpp>
45
#include <iostream>
56
#include <span>
@@ -8,15 +9,18 @@ using namespace tray;
89

910
int main() {
1011
static constexpr auto extent = uvec2{400U, 300U};
11-
static constexpr auto top = std::array{Rgb::from_hex(0xff0000).to_f32(), Rgb::from_hex(0x00ff00).to_f32()};
12-
static constexpr auto bottom = std::array{Rgb::from_hex(0x0000ff).to_f32(), Rgb::from_hex(0xff00ff).to_f32()};
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};
1316
auto image = Image{extent};
1417
for (std::uint32_t row = 0; row < image.extent().y(); ++row) {
1518
auto const yt = static_cast<float>(row) / static_cast<float>(image.extent().y());
16-
auto const range = std::array{lerp(top[0], bottom[0], yt), lerp(top[1], bottom[1], yt)};
1719
for (std::uint32_t col = 0; col < image.extent().x(); ++col) {
1820
auto const xt = static_cast<float>(col) / static_cast<float>(image.extent().x());
19-
image[{row, col}] = Rgb::from_f32(lerp(range[0], range[1], xt));
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());
2024
}
2125
}
2226

tray/src/tray/nvec3.hpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#pragma once
2+
#include <tray/vec.hpp>
3+
4+
namespace tray {
5+
class nvec3 {
6+
public:
7+
nvec3() = default;
8+
nvec3(fvec3 const vec) : m_vec{normalize(vec)} {}
9+
10+
fvec3 const& vec() const { return m_vec; }
11+
operator fvec3 const&() const { return vec(); }
12+
13+
private:
14+
fvec3 m_vec{forward_v<>};
15+
};
16+
} // namespace tray

tray/src/tray/ray.hpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#pragma once
2+
#include <tray/nvec3.hpp>
3+
4+
namespace tray {
5+
struct Ray {
6+
fvec3 origin{};
7+
nvec3 direction{};
8+
9+
fvec3 at(float const t) const { return origin + t * direction.vec(); }
10+
};
11+
} // namespace tray

tray/src/tray/vec.hpp

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ constexpr Vec<Type, Dim> sqr_mag(Vec<Type, Dim> const& vec) {
174174
}
175175

176176
template <typename Type, std::size_t Dim>
177-
Vec<Type, Dim> magnitude(Vec<Type, Dim> const& vec) {
177+
float magnitude(Vec<Type, Dim> const& vec) {
178178
return std::sqrt(dot(vec, vec));
179179
}
180180

@@ -185,6 +185,24 @@ Vec<float, Dim> normalize(Vec<float, Dim> const& in, float const epsilon = 0.001
185185
return in / mag;
186186
}
187187

188+
template <typename Type>
189+
constexpr auto zero_v = Type{};
190+
template <typename Type>
191+
constexpr auto one_v = static_cast<Type>(1);
192+
193+
template <typename Type = float>
194+
constexpr auto left_v = Vec<Type, 3>{-one_v<Type>, zero_v<Type>, zero_v<Type>};
195+
template <typename Type = float>
196+
constexpr auto right_v = Vec<Type, 3>{one_v<Type>, zero_v<Type>, zero_v<Type>};
197+
template <typename Type = float>
198+
constexpr auto up_v = Vec<Type, 3>{zero_v<Type>, one_v<Type>, zero_v<Type>};
199+
template <typename Type = float>
200+
constexpr auto down_v = Vec<Type, 3>{zero_v<Type>, -one_v<Type>, zero_v<Type>};
201+
template <typename Type = float>
202+
constexpr auto forward_v = Vec<Type, 3>{zero_v<Type>, zero_v<Type>, -one_v<Type>};
203+
template <typename Type = float>
204+
constexpr auto backward_v = Vec<Type, 3>{zero_v<Type>, zero_v<Type>, one_v<Type>};
205+
188206
using fvec2 = Vec<float, 2>;
189207
using uvec2 = Vec<std::uint32_t, 2>;
190208
using ivec2 = Vec<std::int32_t, 2>;

0 commit comments

Comments
 (0)