Skip to content

Sphere #8

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 1 commit 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
3 changes: 3 additions & 0 deletions tray/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
add_executable(${PROJECT_NAME})
target_include_directories(${PROJECT_NAME} PRIVATE src)
target_sources(${PROJECT_NAME} PRIVATE
src/tray/hit.cpp
src/tray/hit.hpp
src/tray/image.cpp
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/sphere.hpp
src/tray/vec.hpp
src/tray/viewport.hpp

Expand Down
7 changes: 7 additions & 0 deletions tray/src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include <tray/hit.hpp>
#include <tray/image.hpp>
#include <tray/io.hpp>
#include <tray/ray.hpp>
Expand All @@ -18,13 +19,19 @@ int main() {

static constexpr fvec3 gradient[] = {Rgb::from_hex(0xffffff).to_f32(), Rgb::from_hex(0x002277).to_f32()};
auto image = Image{extent};
auto const sphere = Sphere{.centre = {0.0f, 0.0f, -5.0f}, .radius = 1.0f};

for (std::uint32_t row = 0; row < image.extent().y(); ++row) {
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() - 1);
auto const dir = top_left + xt * horizontal - yt * vertical - origin;
auto const ray = Ray{origin, dir};
auto hit = Hit{};
if (hit(ray, sphere)) {
image[{row, col}] = Rgb::from_f32(0.5f * (hit.normal.vec() + 1.0f));
continue;
}
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
29 changes: 29 additions & 0 deletions tray/src/tray/hit.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#include <tray/hit.hpp>
#include <span>

namespace tray {
namespace {
constexpr float smallet_positive_root(std::span<float const, 2> roots) {
if (roots[0] < 0.0f) { return roots[1]; }
if (roots[1] < 0.0f) { return roots[0]; }
return std::min(roots[0], roots[1]);
}
} // namespace

bool Hit::operator()(Ray const& ray, Sphere const& sphere) {
auto const co = ray.origin - sphere.centre;
float const b = 2.0f * dot(ray.direction.vec(), co);
float const c = dot(co, co) - sphere.radius * sphere.radius;
float const discriminant = b * b - 4 * c;
if (discriminant < 0.0f) { return false; }

auto const sqd = std::sqrt(discriminant);
float const roots[] = {0.5f * (-b - sqd), 0.5f * (-b + sqd)};
auto const t = smallet_positive_root(roots);
if (t < 0.0f) { return false; }

point = ray.at(t);
normal = point - sphere.centre;
return true;
}
} // namespace tray
13 changes: 13 additions & 0 deletions tray/src/tray/hit.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#pragma once
#include <tray/nvec3.hpp>
#include <tray/ray.hpp>
#include <tray/sphere.hpp>

namespace tray {
struct Hit {
fvec3 point{};
nvec3 normal{};

bool operator()(Ray const& ray, Sphere const& sphere);
};
} // namespace tray
9 changes: 9 additions & 0 deletions tray/src/tray/sphere.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#pragma once
#include <tray/vec.hpp>

namespace tray {
struct Sphere {
fvec3 centre{};
float radius{};
};
} // namespace tray