Vector4 inline definitions, and rearrangement into multiple inl files
[gph-math] / src / ray.h
1 #ifndef GMATH_RAY_H_
2 #define GMATH_RAY_H_
3
4 #include "vector.h"
5 #include "matrix.h"
6
7 namespace gph {
8
9 class Ray {
10 public:
11         Vector3 origin, dir;
12
13         Ray() : dir(0, 0, 1) {}
14         Ray(const Vector3 &o, const Vector3 &d) : origin(o), dir(d) {}
15 };
16
17 inline Ray operator *(const Ray &r, const Matrix4x4 &m)
18 {
19         Matrix4x4 up = m;
20         up[0][3] = up[1][3] = up[2][3] = up[3][0] = up[3][1] = up[3][2] = 0.0;
21         up[3][3] = 1.0;
22
23         return Ray(r.origin * m, r.dir * up);
24 }
25
26 inline Ray operator *(const Matrix4x4 &m, const Ray &r)
27 {
28         Matrix4x4 up = m;
29         up[0][3] = up[1][3] = up[2][3] = up[3][0] = up[3][1] = up[3][2] = 0.0;
30         up[3][3] = 1.0;
31
32         return Ray(m * r.origin, m * r.dir);
33 }
34
35
36 inline Ray reflect(const Ray &ray, const Vector3 &n)
37 {
38         return Ray(ray.origin, reflect(ray.dir, n));
39 }
40
41 inline Ray refract(const Ray &ray, const Vector3 &n, float ior)
42 {
43         return Ray(ray.origin, refract(ray.dir, n, ior));
44 }
45
46 inline Ray refract(const Ray &ray, const Vector3 &n, float from_ior, float to_ior)
47 {
48         return Ray(ray.origin, refract(ray.dir, n, from_ior, to_ior));
49 }
50
51
52 }       // namespace gph
53
54 #endif  // GMATH_RAY_H_