fixed copying of library links in install target
[gph-math] / src / ray.h
1 /*
2 gph-math - math library for graphics programs
3 Copyright (C) 2016 John Tsiombikas <nuclear@member.fsf.org>
4
5 This program is free software. Feel free to use, modify, and/or redistribute
6 it under the terms of the MIT/X11 license. See LICENSE for details.
7 If you intend to redistribute parts of the code without the LICENSE file
8 replace this paragraph with the full contents of the LICENSE file.
9 */
10 #ifndef GMATH_RAY_H_
11 #define GMATH_RAY_H_
12
13 #include "vector.h"
14 #include "matrix.h"
15
16 namespace gph {
17
18 class Ray {
19 public:
20         Vector3 origin, dir;
21
22         Ray() : dir(0, 0, 1) {}
23         Ray(const Vector3 &o, const Vector3 &d) : origin(o), dir(d) {}
24 };
25
26 inline Ray operator *(const Ray &r, const Matrix4x4 &m)
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(r.origin * m, r.dir * up);
33 }
34
35 inline Ray operator *(const Matrix4x4 &m, const Ray &r)
36 {
37         Matrix4x4 up = m;
38         up[0][3] = up[1][3] = up[2][3] = up[3][0] = up[3][1] = up[3][2] = 0.0;
39         up[3][3] = 1.0;
40
41         return Ray(m * r.origin, m * r.dir);
42 }
43
44
45 inline Ray reflect(const Ray &ray, const Vector3 &n)
46 {
47         return Ray(ray.origin, reflect(ray.dir, n));
48 }
49
50 inline Ray refract(const Ray &ray, const Vector3 &n, float ior)
51 {
52         return Ray(ray.origin, refract(ray.dir, n, ior));
53 }
54
55 inline Ray refract(const Ray &ray, const Vector3 &n, float from_ior, float to_ior)
56 {
57         return Ray(ray.origin, refract(ray.dir, n, from_ior, to_ior));
58 }
59
60
61 }       // namespace gph
62
63 #endif  // GMATH_RAY_H_