initial commit, first gear
[antikythera] / src / geom.h
1 #ifndef GEOMOBJ_H_
2 #define GEOMOBJ_H_
3
4 #include "gmath/gmath.h"
5
6 class GeomObject;
7 class SceneNode;
8
9 struct HitPoint {
10         float dist;                             //< parametric distance along the ray
11         Vec3 pos;                       //< position of intersection (orig + dir * dist)
12         Vec3 normal;                    //< normal at the point of intersection
13         const void *obj;                //< pointer to the intersected object
14         const SceneNode *node;
15         Ray ray;
16 };
17
18 class GeomObject {
19 public:
20         virtual ~GeomObject();
21
22         virtual void set_union(const GeomObject *obj1, const GeomObject *obj2) = 0;
23         virtual void set_intersection(const GeomObject *obj1, const GeomObject *obj2) = 0;
24
25         virtual bool intersect(const Ray &ray, HitPoint *hit = 0) const = 0;
26 };
27
28 class Sphere : public GeomObject {
29 public:
30         Vec3 center;
31         float radius;
32
33         Sphere();
34         Sphere(const Vec3 &center, float radius);
35
36         void set_union(const GeomObject *obj1, const GeomObject *obj2);
37         void set_intersection(const GeomObject *obj1, const GeomObject *obj2);
38
39         bool intersect(const Ray &ray, HitPoint *hit = 0) const;
40 };
41
42 class AABox : public GeomObject {
43 public:
44         Vec3 min, max;
45
46         AABox();
47         AABox(const Vec3 &min, const Vec3 &max);
48
49         void set_union(const GeomObject *obj1, const GeomObject *obj2);
50         void set_intersection(const GeomObject *obj1, const GeomObject *obj2);
51
52         bool intersect(const Ray &ray, HitPoint *hit = 0) const;
53 };
54
55 class Plane : public GeomObject {
56 public:
57         Vec3 pt, normal;
58
59         Plane();
60         Plane(const Vec3 &pt, const Vec3 &normal);
61         Plane(const Vec3 &p1, const Vec3 &p2, const Vec3 &p3);
62         Plane(const Vec3 &normal, float dist);
63
64         void set_union(const GeomObject *obj1, const GeomObject *obj2);
65         void set_intersection(const GeomObject *obj1, const GeomObject *obj2);
66
67         bool intersect(const Ray &ray, HitPoint *hit = 0) const;
68 };
69
70 float sphere_distance(const Vec3 &cent, float rad, const Vec3 &pt);
71 float capsule_distance(const Vec3 &a, float ra, const Vec3 &b, float rb, const Vec3 &pt);
72
73 #endif  // GEOMOBJ_H_