added 3dengfx into the repo, probably not the correct version for this
[summerhack] / src / 3dengfx / src / n3dmath2 / n3dmath2_qdr.hpp
1 /*
2 This file is part of the n3dmath2 library.
3
4 Copyright (c) 2004, 2005 John Tsiombikas <nuclear@siggraph.org>
5
6 The n3dmath2 library is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 The n3dmath2 library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with the n3dmath2 library; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19 */
20
21 #ifndef _N3DMATH2_QDR_HPP_
22 #define _N3DMATH2_QDR_HPP_
23
24 #include "n3dmath2.hpp"
25
26 /* used to store intersections of a ray on the surface
27  * of a quadratic object
28  */
29 struct SurfPoint {
30         Vector3 pos, normal;            // position and normal of surface at this position
31         scalar_t t;                                     // parametric distance of intersection along ray
32         scalar_t pre_ior, post_ior;     // obviously these come handy with raytracing
33 };
34
35 // Abstract base class for surfaces
36 class Surface {
37 protected:
38         Vector3 pos;
39         Quaternion rot;
40
41 public:
42         Surface(const Vector3 &pos=Vector3(0,0,0));
43         virtual ~Surface();
44
45         virtual void set_position(const Vector3 &pos);
46         virtual Vector3 get_position() const;
47
48         virtual void set_rotation(const Quaternion &rot);
49         virtual Quaternion get_rotation() const;
50
51         virtual Vector2 inv_map(const Vector3 &pt) const = 0;
52         
53         virtual bool check_intersection(const Ray &ray) const = 0;
54         virtual bool find_intersection(const Ray &ray, SurfPoint *isect) const = 0;
55 };
56
57 // sphere (x² + y² + z² = r²)
58 class Sphere : public Surface {
59 protected:
60         scalar_t radius;
61         
62 public:
63         Sphere(const Vector3 &pos=Vector3(0,0,0), scalar_t rad=1.0);
64         virtual ~Sphere();
65
66         virtual void set_radius(scalar_t rad);
67         virtual scalar_t get_radius() const;
68         
69         virtual Vector2 inv_map(const Vector3 &pt) const;
70         
71         virtual bool check_intersection(const Ray &ray) const;
72         virtual bool find_intersection(const Ray &ray, SurfPoint *isect) const;
73 };
74
75 // plane (ax + by + cz + d = 0)
76 class Plane : public Surface {
77 protected:
78         Vector3 normal;
79         
80 public:
81         Plane(const Vector3 &pos=Vector3(0,0,0), const Vector3 &normal=Vector3(0,0,-1));
82         Plane(const Vector3 &p1, const Vector3 &p2, const Vector3 &p3);
83         virtual ~Plane();
84
85         virtual void set_normal(const Vector3 &normal);
86         virtual Vector3 get_normal() const;
87         
88         virtual Vector2 inv_map(const Vector3 &pt) const;
89         
90         virtual bool check_intersection(const Ray &ray) const;
91         virtual bool find_intersection(const Ray &ray, SurfPoint *isect) const;
92 };
93
94 class Box : public Surface {
95 protected:
96         Vector3 verts[8];
97
98 public:
99         Box(const Vector3 &min_vec = Vector3(-1,-1,-1), const Vector3 &max_vec = Vector3(1,1,1));
100         Box(const Vector3 &v0, const Vector3 &v1, const Vector3 &v2, const Vector3 &v3,
101                 const Vector3 &v4, const Vector3 &v5, const Vector3 &v6, const Vector3 &v7);
102         Box(const Vector3 *array);
103
104         virtual Vector2 inv_map(const Vector3 &pt) const;
105
106         virtual bool check_intersection(const Ray &ray) const;
107         virtual bool find_intersection(const Ray &ray, SurfPoint *isect) const;
108 };
109
110 // utility functions
111 bool point_over_plane(const Plane &plane, const Vector3 &point);
112 bool check_tri_ray_intersection(const Vector3 &v1, const Vector3 &v2, const Vector3 &v3, const Ray &ray);
113
114 #endif  // _N3DMATH2_QDR_HPP_