Skip to content

Cleanup Ray, misc hotfixes #7

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 2 commits into from
Oct 1, 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
1 change: 1 addition & 0 deletions tray/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ target_sources(${PROJECT_NAME} PRIVATE
src/tray/ray.hpp
src/tray/rgb.hpp
src/tray/vec.hpp
src/tray/viewport.hpp

src/main.cpp
)
Expand Down
26 changes: 16 additions & 10 deletions tray/src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,26 +1,32 @@
#include <tray/image.hpp>
#include <tray/io.hpp>
#include <tray/ray.hpp>
#include <tray/vec.hpp>
#include <tray/viewport.hpp>
#include <iostream>
#include <span>

using namespace tray;

int main() {
static constexpr auto extent = uvec2{400U, 300U};
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};
static constexpr auto viewport = Viewport::make(extent, 2.0f, 2.0f);
static constexpr auto origin = fvec3{};

static constexpr auto horizontal = fvec3{viewport.extent.x(), 0.0f, 0.0f};
static constexpr auto vertical = fvec3{0.0f, viewport.extent.y(), 0.0f};
static constexpr auto top_left = origin + 0.5f * (-horizontal + vertical) + fvec3{0.0f, 0.0f, -viewport.depth};

static constexpr fvec3 gradient[] = {Rgb::from_hex(0xffffff).to_f32(), Rgb::from_hex(0x002277).to_f32()};
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 yt = static_cast<float>(row) / static_cast<float>(image.extent().y() - 1);
for (std::uint32_t col = 0; col < image.extent().x(); ++col) {
auto const xt = static_cast<float>(col) / static_cast<float>(image.extent().x());
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());
auto const xt = static_cast<float>(col) / static_cast<float>(image.extent().x() - 1);
auto const dir = top_left + xt * horizontal - yt * vertical - origin;
auto const ray = Ray{origin, dir};
auto const t = 0.5f * (ray.direction.vec().y() + 1.0f);
image[{row, col}] = Rgb::from_f32(lerp(gradient[0], gradient[1], t));
}
}

Expand Down
4 changes: 2 additions & 2 deletions tray/src/tray/rgb.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ namespace tray {
struct Rgb : Vec<std::uint8_t, 3> {
using Vec::Vec;

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

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

Expand Down
6 changes: 6 additions & 0 deletions tray/src/tray/vec.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,12 @@ class Vec {
return *this;
}

friend constexpr Vec operator-(Vec const& v) {
auto ret = v;
ret = -ret;
return ret;
}

template <VecOrType<Type, Dim> T>
friend constexpr Vec operator+(Vec const& a, T const& b) {
auto ret = a;
Expand Down
14 changes: 14 additions & 0 deletions tray/src/tray/viewport.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#pragma once
#include <tray/vec.hpp>

namespace tray {
struct Viewport {
fvec2 extent{1.0f};
float depth{1.0f};

static constexpr Viewport make(uvec2 extent, float depth, float scale = 1.0f) {
auto const ar = static_cast<float>(extent.x()) / static_cast<float>(extent.y());
return ar > 1.0f ? Viewport{fvec2{scale, scale / ar}, depth} : Viewport{fvec2{scale * ar, scale}, depth};
}
};
} // namespace tray