Skip to content

Ray #6

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Sep 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions tray/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ target_sources(${PROJECT_NAME} PRIVATE
src/tray/image.hpp
src/tray/io.cpp
src/tray/io.hpp
src/tray/nvec3.hpp
src/tray/ray.hpp
src/tray/rgb.hpp
src/tray/vec.hpp

Expand Down
12 changes: 8 additions & 4 deletions tray/src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include <tray/image.hpp>
#include <tray/io.hpp>
#include <tray/ray.hpp>
#include <tray/vec.hpp>
#include <iostream>
#include <span>
Expand All @@ -8,15 +9,18 @@ using namespace tray;

int main() {
static constexpr auto extent = uvec2{400U, 300U};
static constexpr auto top = std::array{Rgb::from_hex(0xff0000).to_f32(), Rgb::from_hex(0x00ff00).to_f32()};
static constexpr auto bottom = std::array{Rgb::from_hex(0x0000ff).to_f32(), Rgb::from_hex(0xff00ff).to_f32()};
static constexpr auto origin = fvec3{0.0f, 0.0f, 0.0f};
static constexpr auto horizontal = fvec3(extent.x() / 100.0f, 0.0f, 0.0f);
static constexpr auto vertical = fvec3(0.0f, extent.y() / 100.0f, 0.0f);
static constexpr auto lower_left = origin - horizontal / 2.0f - vertical / 2.0f - fvec3{0.0f, 0.0f, 1.0f};
auto image = Image{extent};
for (std::uint32_t row = 0; row < image.extent().y(); ++row) {
auto const yt = static_cast<float>(row) / static_cast<float>(image.extent().y());
auto const range = std::array{lerp(top[0], bottom[0], yt), lerp(top[1], bottom[1], yt)};
for (std::uint32_t col = 0; col < image.extent().x(); ++col) {
auto const xt = static_cast<float>(col) / static_cast<float>(image.extent().x());
image[{row, col}] = Rgb::from_f32(lerp(range[0], range[1], xt));
auto const ray = Ray{origin, lower_left + xt * horizontal + yt * vertical - origin};
auto const t = 0.5f * (normalize(ray.direction.vec()).y() + 1.0f);
image[{row, col}] = Rgb::from_f32(Rgb::from_hex(0xff0000).to_f32() * t + (1.0f - t) * Rgb::from_hex(0xffffff).to_f32());
}
}

Expand Down
16 changes: 16 additions & 0 deletions tray/src/tray/nvec3.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#pragma once
#include <tray/vec.hpp>

namespace tray {
class nvec3 {
public:
nvec3() = default;
nvec3(fvec3 const vec) : m_vec{normalize(vec)} {}

fvec3 const& vec() const { return m_vec; }
operator fvec3 const&() const { return vec(); }

private:
fvec3 m_vec{forward_v<>};
};
} // namespace tray
11 changes: 11 additions & 0 deletions tray/src/tray/ray.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#pragma once
#include <tray/nvec3.hpp>

namespace tray {
struct Ray {
fvec3 origin{};
nvec3 direction{};

fvec3 at(float const t) const { return origin + t * direction.vec(); }
};
} // namespace tray
20 changes: 19 additions & 1 deletion tray/src/tray/vec.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ constexpr Vec<Type, Dim> sqr_mag(Vec<Type, Dim> const& vec) {
}

template <typename Type, std::size_t Dim>
Vec<Type, Dim> magnitude(Vec<Type, Dim> const& vec) {
float magnitude(Vec<Type, Dim> const& vec) {
return std::sqrt(dot(vec, vec));
}

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

template <typename Type>
constexpr auto zero_v = Type{};
template <typename Type>
constexpr auto one_v = static_cast<Type>(1);

template <typename Type = float>
constexpr auto left_v = Vec<Type, 3>{-one_v<Type>, zero_v<Type>, zero_v<Type>};
template <typename Type = float>
constexpr auto right_v = Vec<Type, 3>{one_v<Type>, zero_v<Type>, zero_v<Type>};
template <typename Type = float>
constexpr auto up_v = Vec<Type, 3>{zero_v<Type>, one_v<Type>, zero_v<Type>};
template <typename Type = float>
constexpr auto down_v = Vec<Type, 3>{zero_v<Type>, -one_v<Type>, zero_v<Type>};
template <typename Type = float>
constexpr auto forward_v = Vec<Type, 3>{zero_v<Type>, zero_v<Type>, -one_v<Type>};
template <typename Type = float>
constexpr auto backward_v = Vec<Type, 3>{zero_v<Type>, zero_v<Type>, one_v<Type>};

using fvec2 = Vec<float, 2>;
using uvec2 = Vec<std::uint32_t, 2>;
using ivec2 = Vec<std::int32_t, 2>;
Expand Down