blob: 0c65e1a845b1396ca036e415da202ac8c1fe219b (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
|
//
// Created by Keuin on 2022/4/12.
//
#ifndef RT_HITLIST_H
#define RT_HITLIST_H
#include "viewport.h"
#include "timer.h"
#include "bitmap.h"
#include "ray.h"
#include "vec.h"
#include "object.h"
#include <cstdlib>
#include <memory>
#include <limits>
#include <vector>
#include <iostream>
#include <cstdint>
// A world, T is color depth
template<typename T>
class hitlist {
std::vector<std::shared_ptr<object>> objects;
public:
hitlist() = default;
hitlist(hitlist &other) = delete; // do not copy the world
// Add an object to the world.
void add_object(std::shared_ptr<object> &&obj) {
objects.push_back(std::move(obj));
}
// Given a ray, compute the color.
pixel<T> color(const ray3d &r) const {
// Detect hits
bool hit = false;
double hit_t = std::numeric_limits<double>::infinity();
std::shared_ptr<object> hit_obj;
// Check the nearest object we hit
for (const auto &obj: objects) {
double t_;
if (obj->hit(r, t_, 0.0) && t_ < hit_t) {
hit = true;
hit_t = t_;
hit_obj = obj;
}
}
if (hit) {
// normal vector on hit point
const auto nv = hit_obj->normal_vector(r.at(hit_t));
// return obj->color();
// visualize normal vector at hit point
return pixel<T>::from_normalized(nv);
}
// Does not hit anything. Get background color (infinity)
const auto u = (r.direction().y + 1.0) * 0.5;
return mix(
pixel<T>::from_normalized(1.0, 1.0, 1.0),
pixel<T>::from_normalized(0.5, 0.7, 1.0),
1.0 - u,
u
);
}
};
using hitlist8b = hitlist<uint8_t>;
#endif //RT_HITLIST_H
|