#include "BvhLoader.h" #include "BvhConstructor.h" #include "gzstream.h" #include #include #include #include #ifdef _CRT_SET #define _CRTDBG_MAP_ALLOC #include #include // redefine new operator #define DEBUG_NEW new(_NORMAL_BLOCK, __FILE__, __LINE__) #define new DEBUG_NEW #endif namespace CHCDemoEngine { using namespace std; #define TYPE_INTERIOR -2 #define TYPE_LEAF -3 BvhNode *BvhFactory::LoadNextNode(igzstream &stream, BvhInterior *parent) { int nodeType; stream.read(reinterpret_cast(&nodeType), sizeof(int)); BvhNode *node; if (nodeType == TYPE_LEAF) node = new BvhLeaf(parent); else if (nodeType == TYPE_INTERIOR) node = new BvhInterior(parent); else cerr << "error: wrong node type: " << nodeType << endl; Vector3 bMin, bMax; stream.read(reinterpret_cast(&(node->mFirst)), sizeof(int)); stream.read(reinterpret_cast(&(node->mLast)), sizeof(int)); stream.read(reinterpret_cast(&bMin), sizeof(Vector3)); stream.read(reinterpret_cast(&bMax), sizeof(Vector3)); node->mBox = AxisAlignedBox3(bMin, bMax); node->mArea = node->mBox.SurfaceArea(); //cout << "box: " << node->mBox << " area: " << node->mArea << endl; return node; } Bvh *BvhFactory::Create(const string &filename, const SceneEntityContainer &staticEntities, const SceneEntityContainer &dynamicEntities, int maxDepthForTestingChildren) { queue tQueue; igzstream stream(filename.c_str()); if (!stream.is_open()) return NULL; cout << "\n*******************\nloading bvh from file " << filename << " ..." << endl; Bvh *bvh = new Bvh(staticEntities, dynamicEntities, maxDepthForTestingChildren); BvhNode *root = LoadNextNode(stream, NULL); bvh->mStaticRoot = root; // we have at least the root, the static and the dynamic branch bvh->mNumNodes = 3; cout << "static bvh bb: " << root->mBox << endl; tQueue.push(root); while (!tQueue.empty()) { BvhNode *node = tQueue.front(); tQueue.pop(); if (!node->IsLeaf()) { bvh->mNumNodes += 2; BvhInterior *interior = static_cast(node); BvhNode *front = LoadNextNode(stream, interior); BvhNode *back = LoadNextNode(stream, interior); interior->mFront = front; interior->mBack = back; front->mDepth = interior->mDepth + 1; back->mDepth = interior->mDepth + 1; tQueue.push(front); tQueue.push(back); } } cout << "... finished loading " << bvh->mNumNodes - 2 << " static bvh nodes" << endl; /////////// //-- create dynamic part of the hierarchy bvh->CreateDynamicBranch(); /////////// //-- post process nodes bvh->PostProcess(); cout << "bvh bb: " << bvh->mBox << endl; return bvh; } Bvh *BvhFactory::Create(const SceneEntityContainer &staticEntities, const SceneEntityContainer &dynamicEntities, int maxDepthForTestingChildren) { Bvh *bvh = new Bvh(staticEntities, dynamicEntities, maxDepthForTestingChildren); BvhConstructor bvhConstructor(bvh->mGeometry, 0, (int)bvh->mStaticGeometrySize - 1); int numNodes; bvh->mStaticRoot = bvhConstructor.Construct(numNodes); bvh->mNumNodes = 2 + numNodes; cout << "created " << bvh->mNumNodes - 2 << " static bvh nodes" << endl; /////////// //-- create dynamic part of the hierarchy bvh->CreateDynamicBranch(); /////////// //-- post process nodes bvh->PostProcess(); cout << "bvh bb: " << bvh->mBox << endl; return bvh; } }