00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012 #ifndef __BVH__H__
00013 #define __BVH__H__
00014 #include <wx/app.h>
00015 #include <map>
00016 #include <deque>
00017 #include <vector>
00018 #include "bvhnode.h"
00019 #include "moddefs.h"
00020
00021
00022 namespace VRUT
00023 {
00024 class Scene;
00025 class Camera;
00026 struct RayIntersectionInfo;
00027
00029 class BVH
00030 {
00031 public:
00033 struct IntersectionInfo
00034 {
00036 NODE_ID nodeID;
00038 VECTOR3 intersection;
00040 float dist;
00041 };
00042
00043 typedef std::vector<NODE_ID> ItemsList;
00044 typedef std::multimap<float, NODE_ID> BlendedList;
00045
00046 private:
00047 WX_DECLARE_HASH_MAP( NODE_ID, BVHNode *, wxIntegerHash, wxIntegerEqual, SceneToBVHMap );
00049 struct QNode
00050 {
00051 BVHNode ** currentNode;
00052 std::deque<BVHNode *> * itemsPending;
00053 BVHNode * parent;
00054
00055 QNode(BVHNode ** n,
00056 std::deque<BVHNode *> * m,
00057 BVHNode * p) : currentNode(n), itemsPending(m), parent(p) {}
00058 };
00060 enum AXIS { X_AXIS, Y_AXIS, Z_AXIS };
00061
00063 BVHNode * root;
00065 Scene * scene;
00067 SceneToBVHMap bvhNodes;
00068
00074 void splitGeometry(const std::deque<BVHNode *> * geometry,
00075 std::deque<BVHNode *> * geometryL,
00076 std::deque<BVHNode *> * geometryR) const;
00077
00084 void queueUpForRender(BVHNode * node, const Camera * camera, ItemsList * itemsList, BlendedList * blendItemsList, bool inclChildren = false);
00085
00089 static void getGeometryNodes(std::deque<const BVHNode *> * items, const BVHNode * startNode);
00090
00094 static void getGeometryNodes(std::deque<BVHNode *> * items, BVHNode * startNode);
00095
00096 public:
00098 int numberOfNodes;
00099
00100
00102 BVH(Scene * _scene);
00104 ~BVH();
00105
00107 BVHNode * GetRoot() const
00108 {
00109 return root;
00110 }
00111
00113 BVHNode * GetBVHNodeAssignedTo(NODE_ID sceneNodeID) const
00114 {
00115 SceneToBVHMap::const_iterator mapIt = bvhNodes.find(sceneNodeID);
00116 return ( mapIt == bvhNodes.end() ? (BVHNode *)NULL : mapIt->second );
00117 }
00118
00119 void AssignNodeIds();
00120
00122 bool IsBuilt() const { return root != (BVHNode *)NULL; }
00123
00126 static unsigned GetNumGeometryNodes(const BVHNode * startNode);
00127
00131 void GetGeometryNodeIDs(std::deque<NODE_ID> * items, const BVHNode * startNode) const;
00132
00134 void Destroy() { SAFE_DELETE(root); }
00135
00137 void Rebuild();
00138
00141 BVHNode * Remove(BVHNode * node);
00142
00144 void Insert(BVHNode * node);
00145
00147 void Build(std::deque<NODE_ID> * items);
00148
00153 void Cull(Camera * camera, ItemsList * itemsList, BlendedList * blendItemsList);
00154
00159 bool BoundProbe(const Ray & ray, IntersectionInfo * info);
00160
00165 bool CastRay(const Ray & ray, RayIntersectionInfo &info);
00166
00169 void UpdateBV(BVHNode * node = (BVHNode *)NULL);
00170
00172 static void SetInvalid(BVHNode * node);
00173
00174 #ifdef GRAPHVIZ_SUPPORT
00176 void CreateGraphVizFile() const;
00177 #endif
00178
00179 #ifdef __WXDEBUG__
00181 void Dump() const;
00182 #endif
00183
00184 friend class Scene;
00185 };
00186 };
00187
00188 #endif