Переглянути джерело

Add bounding box calculation to triangle..

Could be helpful XD
Godzil 4 роки тому
батько
коміт
7a96d42874
3 змінених файлів з 28 додано та 0 видалено
  1. 12 0
      source/include/boundingbox.h
  2. 2 0
      source/include/triangle.h
  3. 14 0
      source/shapes/triangle.cpp

+ 12 - 0
source/include/boundingbox.h

@@ -33,6 +33,18 @@ public:
         if (this->max.z < b.max.z) { this->max.z = b.max.z; }
     }
 
+    void operator|(const Tuple &b) {
+        isReset = false;
+
+        if (this->min.x > b.x) { this->min.x = b.x; }
+        if (this->min.y > b.y) { this->min.y = b.y; }
+        if (this->min.z > b.z) { this->min.z = b.z; }
+
+        if (this->max.x < b.x) { this->max.x = b.x; }
+        if (this->max.y < b.y) { this->max.y = b.y; }
+        if (this->max.z < b.z) { this->max.z = b.z; }
+    }
+
     bool haveFiniteBounds() { return this->min.isRepresentable() && this->max.isRepresentable(); };
 
     bool fitsIn(const BoundingBox &other) {

+ 2 - 0
source/include/triangle.h

@@ -23,6 +23,8 @@ public:
 
 public:
     Triangle(Point p1, Point p2, Point p3);
+    BoundingBox getBounds();
+
 };
 
 #endif /* DORAYME_TRIANGLE_H */

+ 14 - 0
source/shapes/triangle.cpp

@@ -54,4 +54,18 @@ Intersect Triangle::localIntersect(Ray r)
 Tuple Triangle::localNormalAt(Tuple point)
 {
     return this->normal;
+}
+
+BoundingBox Triangle::getBounds()
+{
+    BoundingBox ret;
+
+    ret | p1;
+    ret | p2;
+    ret | p3;
+
+    ret.min = this->objectToWorld(ret.min);
+    ret.max = this->objectToWorld(ret.max);
+
+    return ret;
 }