coeng

view src/geom.h @ 8:8cce82794f90

seems to work nicely
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 15 Feb 2015 05:14:20 +0200
parents af24cfbdf9b6
children
line source
1 #ifndef GEOM_H_
2 #define GEOM_H_
4 #include <vmath/vmath.h>
6 class GeomShape;
8 struct HitPoint {
9 float dist;
10 Vector3 pos;
11 Vector3 normal;
12 const GeomShape *shape;
13 void *data;
14 };
16 class GeomShape {
17 public:
18 enum Type { GEOM_UNKNOWN, GEOM_SPHERE, GEOM_PLANE, GEOM_AABOX };
20 protected:
21 Matrix4x4 xform, inv_xform;
22 Type type;
24 public:
25 GeomShape();
26 virtual ~GeomShape();
28 virtual Type get_type() const;
30 // apply an arbitrary transformation before testing intersections and collisions
31 virtual void set_transform(const Matrix4x4 &m);
32 virtual const Matrix4x4 &get_transform() const;
33 virtual const Matrix4x4 &get_inv_transform() const;
35 virtual bool contains(const Vector3 &pt) const = 0;
36 virtual bool intersect(const Ray &ray, HitPoint *hit = 0) const = 0;
37 virtual bool collide(const GeomShape *geom, HitPoint *hit = 0) const = 0;
39 virtual float distance(const Vector3 &pt) const = 0;
40 virtual float distance_sq(const Vector3 &pt) const = 0;
42 // visualize the shape
43 virtual void draw() const = 0;
44 };
46 class Sphere : public GeomShape {
47 public:
48 Vector3 center;
49 float radius;
51 Sphere();
52 Sphere(const Vector3 &c, float rad);
54 bool contains(const Vector3 &pt) const;
55 bool intersect(const Ray &ray, HitPoint *hit = 0) const;
56 bool collide(const GeomShape *geom, HitPoint *hit = 0) const;
58 float distance(const Vector3 &pt) const;
59 float distance_sq(const Vector3 &pt) const;
61 void draw() const;
62 };
64 class Plane : public GeomShape {
65 public:
66 Vector3 normal;
67 float dist;
69 Plane();
70 Plane(const Vector3 &n, float d);
71 Plane(float a, float b, float c, float d);
72 Plane(const Vector3 &pos, const Vector3 &norm);
73 Plane(const Vector3 &p1, const Vector3 &p2, const Vector3 &p3);
75 bool contains(const Vector3 &pt) const;
76 bool intersect(const Ray &ray, HitPoint *hit = 0) const;
77 bool collide(const GeomShape *geom, HitPoint *hit = 0) const;
79 float distance(const Vector3 &pt) const;
80 float distance_sq(const Vector3 &pt) const;
82 void draw() const;
83 };
85 class AABox : public GeomShape {
86 public:
87 Vector3 min, max;
89 AABox();
90 AABox(const Vector3 &min, const Vector3 &max);
92 void set_union(const GeomShape *obj1, const GeomShape *obj2);
93 void set_intersection(const GeomShape *obj1, const GeomShape *obj2);
95 bool contains(const Vector3 &pt) const;
96 bool intersect(const Ray &ray, HitPoint *hit = 0) const;
97 bool collide(const GeomShape *geom, HitPoint *hit = 0) const;
99 float distance(const Vector3 &pt) const;
100 float distance_sq(const Vector3 &pt) const;
102 void draw() const;
103 };
105 #endif // GEOM_H_