Skip to content

Diffuse lighting #9

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
1 change: 1 addition & 0 deletions tray/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ target_sources(${PROJECT_NAME} PRIVATE
src/tray/image.hpp
src/tray/io.cpp
src/tray/io.hpp
src/tray/light.hpp
src/tray/nvec3.hpp
src/tray/ray.hpp
src/tray/rgb.hpp
Expand Down
17 changes: 16 additions & 1 deletion tray/src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,13 +1,24 @@
#include <tray/hit.hpp>
#include <tray/image.hpp>
#include <tray/io.hpp>
#include <tray/light.hpp>
#include <tray/ray.hpp>
#include <tray/viewport.hpp>
#include <algorithm>
#include <iostream>
#include <span>

using namespace tray;

namespace {
constexpr fvec3 clamp(fvec3 in, float lo = 0.0f, float hi = 1.0f) {
in.x() = std::clamp(in.x(), lo, hi);
in.y() = std::clamp(in.y(), lo, hi);
in.z() = std::clamp(in.z(), lo, hi);
return in;
}
} // namespace

int main() {
static constexpr auto extent = uvec2{400U, 300U};
static constexpr auto viewport = Viewport::make(extent, 2.0f, 2.0f);
Expand All @@ -20,6 +31,10 @@ 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};
DirLight const lights[] = {
DirLight{.intensity = {0.0f, 1.0f, 1.0f}, .direction = fvec3{-1.0f}},
DirLight{.intensity = {0.5f, 0.0f, 0.0f}, .direction = fvec3{0.0f, 0.0f, -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);
Expand All @@ -29,7 +44,7 @@ int main() {
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));
image[{row, col}] = Rgb::from_f32(clamp(DirLight::combine(lights, hit.normal)));
continue;
}
auto const t = 0.5f * (ray.direction.vec().y() + 1.0f);
Expand Down
21 changes: 21 additions & 0 deletions tray/src/tray/light.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#pragma once
#include <tray/nvec3.hpp>
#include <span>

namespace tray {
struct DirLight {
fvec3 intensity{1.0f};
nvec3 direction{};

fvec3 diffuse(nvec3 const normal) const {
auto const coeff = dot(normal.vec(), -direction.vec());
return std::max(coeff, 0.15f) * intensity;
}

static fvec3 combine(std::span<DirLight const> lights, nvec3 const normal) {
auto ret = fvec3{};
for (auto const& light : lights) { ret += light.diffuse(normal); }
return ret;
}
};
} // namespace tray