Skip to content

Commit 3479f6a

Browse files
authored
Diffuse lighting (#9)
1 parent d196649 commit 3479f6a

File tree

3 files changed

+38
-1
lines changed

3 files changed

+38
-1
lines changed

tray/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ target_sources(${PROJECT_NAME} PRIVATE
77
src/tray/image.hpp
88
src/tray/io.cpp
99
src/tray/io.hpp
10+
src/tray/light.hpp
1011
src/tray/nvec3.hpp
1112
src/tray/ray.hpp
1213
src/tray/rgb.hpp

tray/src/main.cpp

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,24 @@
11
#include <tray/hit.hpp>
22
#include <tray/image.hpp>
33
#include <tray/io.hpp>
4+
#include <tray/light.hpp>
45
#include <tray/ray.hpp>
56
#include <tray/viewport.hpp>
7+
#include <algorithm>
68
#include <iostream>
79
#include <span>
810

911
using namespace tray;
1012

13+
namespace {
14+
constexpr fvec3 clamp(fvec3 in, float lo = 0.0f, float hi = 1.0f) {
15+
in.x() = std::clamp(in.x(), lo, hi);
16+
in.y() = std::clamp(in.y(), lo, hi);
17+
in.z() = std::clamp(in.z(), lo, hi);
18+
return in;
19+
}
20+
} // namespace
21+
1122
int main() {
1223
static constexpr auto extent = uvec2{400U, 300U};
1324
static constexpr auto viewport = Viewport::make(extent, 2.0f, 2.0f);
@@ -20,6 +31,10 @@ int main() {
2031
static constexpr fvec3 gradient[] = {Rgb::from_hex(0xffffff).to_f32(), Rgb::from_hex(0x002277).to_f32()};
2132
auto image = Image{extent};
2233
auto const sphere = Sphere{.centre = {0.0f, 0.0f, -5.0f}, .radius = 1.0f};
34+
DirLight const lights[] = {
35+
DirLight{.intensity = {0.0f, 1.0f, 1.0f}, .direction = fvec3{-1.0f}},
36+
DirLight{.intensity = {0.5f, 0.0f, 0.0f}, .direction = fvec3{0.0f, 0.0f, -1.0f}},
37+
};
2338

2439
for (std::uint32_t row = 0; row < image.extent().y(); ++row) {
2540
auto const yt = static_cast<float>(row) / static_cast<float>(image.extent().y() - 1);
@@ -29,7 +44,7 @@ int main() {
2944
auto const ray = Ray{origin, dir};
3045
auto hit = Hit{};
3146
if (hit(ray, sphere)) {
32-
image[{row, col}] = Rgb::from_f32(0.5f * (hit.normal.vec() + 1.0f));
47+
image[{row, col}] = Rgb::from_f32(clamp(DirLight::combine(lights, hit.normal)));
3348
continue;
3449
}
3550
auto const t = 0.5f * (ray.direction.vec().y() + 1.0f);

tray/src/tray/light.hpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#pragma once
2+
#include <tray/nvec3.hpp>
3+
#include <span>
4+
5+
namespace tray {
6+
struct DirLight {
7+
fvec3 intensity{1.0f};
8+
nvec3 direction{};
9+
10+
fvec3 diffuse(nvec3 const normal) const {
11+
auto const coeff = dot(normal.vec(), -direction.vec());
12+
return std::max(coeff, 0.15f) * intensity;
13+
}
14+
15+
static fvec3 combine(std::span<DirLight const> lights, nvec3 const normal) {
16+
auto ret = fvec3{};
17+
for (auto const& light : lights) { ret += light.diffuse(normal); }
18+
return ret;
19+
}
20+
};
21+
} // namespace tray

0 commit comments

Comments
 (0)