-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.h
113 lines (83 loc) · 2.32 KB
/
utils.h
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#ifndef UTILS_H_
#define UTILS_H_
#pragma once
#include <functional>
#include <vector>
#include "color.h"
#include "vector.h"
namespace raytracer {
struct Ray {
Ray();
Ray(const Vector3& s, const Vector3& d);
Vector3 start;
Vector3 dir;
};
inline Ray::Ray() : start(Vector3(0, 0, 0)), dir(Vector3(0, 0, 0)) { }
inline Ray::Ray(const Vector3& s, const Vector3& d) : start(s), dir(d) { }
struct Light {
Light(const Vector3& pos, const Color& color);
Vector3 pos;
Color color;
};
inline Light::Light(const Vector3& v, const Color& c) : pos(v), color(c) { }
class Camera {
public:
Camera(const Vector3& pos, const Vector3& look_at);
Vector3 pos;
Vector3 forward;
Vector3 right;
Vector3 up;
};
struct Surface {
std::function<Color(const Vector3&)> Diffuse;
std::function<Color(const Vector3&)> Specular;
std::function<double(const Vector3&)> Reflect;
double roughness;
};
class SceneObject;
struct ISect {
const SceneObject* thing;
Ray ray;
double dist;
};
class SceneObject {
public:
SceneObject(const Surface& s) : surface(s) { }
virtual ~SceneObject() {}
virtual bool Intersect(const Ray& ray, ISect* isect) const = 0;
virtual Vector3 Normal(const Vector3& pos) const = 0;
Surface surface;
};
class Sphere : public SceneObject {
public:
Sphere(const Surface& surface, const Vector3& center, double radius);
~Sphere();
bool Intersect(const Ray& ray, ISect* isect) const override;
Vector3 Normal(const Vector3& pos) const override;
Vector3 center;
double radius;
};
inline Sphere::Sphere(const Surface& surface, const Vector3& c, double r)
: SceneObject(surface), center(c), radius(r) { }
inline Sphere::~Sphere() { }
class Plane : public SceneObject {
public:
Plane(const Surface& surface, const Vector3& normal, double offset);
~Plane();
bool Intersect(const Ray& ray, ISect* isect) const override;
Vector3 Normal(const Vector3& pos) const override;
Vector3 normal;
double offset;
};
inline Plane::Plane(const Surface& surface, const Vector3& n, double o)
: SceneObject(surface), normal(n), offset(o) { }
inline Plane::~Plane() { }
class Scene {
public:
std::vector<ISect> Intersect(const Ray& ray) const; // XXX: change to ISect*?
std::vector<SceneObject*> things;
std::vector<Light> lights;
Camera camera;
};
} // namespace raytracer
#endif // UTILS_H_