e110193ceb9e81968c7a36a019c15383bcfce342
[laserbrain_demo] / src / geom.h
1 #ifndef GEOMOBJ_H_
2 #define GEOMOBJ_H_
3
4 #include <gmath/gmath.h>
5
6 enum GeomObjectType {
7         GOBJ_UNKNOWN,
8         GOBJ_SPHERE,
9         GOBJ_AABOX,
10         GOBJ_PLANE
11 };
12
13 class GeomObject;
14
15 struct HitPoint {
16         float dist;                     // parametric distance along the ray
17         Vec3 pos;                       // position of intersection (orig + dir * dist)
18         Vec3 normal;            // normal at the point of intersection
19         Ray ray, local_ray;
20         const GeomObject *obj;  // pointer to the intersected geom-object
21         void *data;                     // place to hang extra data
22 };
23
24 class GeomObject {
25 public:
26         virtual ~GeomObject();
27         virtual GeomObjectType get_type() const = 0;
28
29         virtual void set_union(const GeomObject *obj1, const GeomObject *obj2) = 0;
30         virtual void set_intersection(const GeomObject *obj1, const GeomObject *obj2) = 0;
31
32         virtual bool intersect(const Ray &ray, HitPoint *hit = 0) const = 0;
33         virtual bool contains(const Vec3 &pt) const = 0;
34
35         virtual float distance(const Vec3 &v) const = 0;
36 };
37
38 class Sphere : public GeomObject {
39 public:
40         Vec3 center;
41         float radius;
42
43         Sphere();
44         Sphere(const Vec3 &center, float radius);
45
46         GeomObjectType get_type() const;
47
48         void set_union(const GeomObject *obj1, const GeomObject *obj2);
49         void set_intersection(const GeomObject *obj1, const GeomObject *obj2);
50
51         bool intersect(const Ray &ray, HitPoint *hit = 0) const;
52         bool contains(const Vec3 &pt) const;
53
54         float distance(const Vec3 &v) const;
55 };
56
57 class AABox : public GeomObject {
58 public:
59         Vec3 min, max;
60
61         AABox();
62         AABox(const Vec3 &min, const Vec3 &max);
63
64         GeomObjectType get_type() const;
65
66         void set_union(const GeomObject *obj1, const GeomObject *obj2);
67         void set_intersection(const GeomObject *obj1, const GeomObject *obj2);
68
69         bool intersect(const Ray &ray, HitPoint *hit = 0) const;
70         bool contains(const Vec3 &pt) const;
71
72         float distance(const Vec3 &v) const;
73 };
74
75 class Plane : public GeomObject {
76 public:
77         Vec3 pt, normal;
78
79         Plane();
80         Plane(const Vec3 &pt, const Vec3 &normal);
81         Plane(const Vec3 &p1, const Vec3 &p2, const Vec3 &p3);
82         Plane(const Vec3 &normal, float dist);
83
84         GeomObjectType get_type() const;
85
86         void set_union(const GeomObject *obj1, const GeomObject *obj2);
87         void set_intersection(const GeomObject *obj1, const GeomObject *obj2);
88
89         bool intersect(const Ray &ray, HitPoint *hit = 0) const;
90         bool contains(const Vec3 &pt) const;
91
92         float distance(const Vec3 &v) const;
93 };
94
95 #endif  // GEOMOBJ_H_