1
1
#include < tray/hit.hpp>
2
2
#include < tray/image.hpp>
3
3
#include < tray/io.hpp>
4
+ #include < tray/light.hpp>
4
5
#include < tray/ray.hpp>
5
6
#include < tray/viewport.hpp>
7
+ #include < algorithm>
6
8
#include < iostream>
7
9
#include < span>
8
10
9
11
using namespace tray ;
10
12
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
+
11
22
int main () {
12
23
static constexpr auto extent = uvec2{400U , 300U };
13
24
static constexpr auto viewport = Viewport::make (extent, 2 .0f , 2 .0f );
@@ -20,6 +31,10 @@ int main() {
20
31
static constexpr fvec3 gradient[] = {Rgb::from_hex (0xffffff ).to_f32 (), Rgb::from_hex (0x002277 ).to_f32 ()};
21
32
auto image = Image{extent};
22
33
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
+ };
23
38
24
39
for (std::uint32_t row = 0 ; row < image.extent ().y (); ++row) {
25
40
auto const yt = static_cast <float >(row) / static_cast <float >(image.extent ().y () - 1 );
@@ -29,7 +44,7 @@ int main() {
29
44
auto const ray = Ray{origin, dir};
30
45
auto hit = Hit{};
31
46
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 ) ));
33
48
continue ;
34
49
}
35
50
auto const t = 0 .5f * (ray.direction .vec ().y () + 1 .0f );
0 commit comments