[3266] | 1 | #include "BvhLoader.h"
|
---|
| 2 | #include "BvhConstructor.h"
|
---|
| 3 | #include "gzstream.h"
|
---|
| 4 |
|
---|
[2755] | 5 | #include <queue>
|
---|
| 6 | #include <stack>
|
---|
| 7 | #include <fstream>
|
---|
| 8 | #include <iostream>
|
---|
| 9 |
|
---|
| 10 |
|
---|
| 11 |
|
---|
[3021] | 12 | #ifdef _CRT_SET
|
---|
| 13 | #define _CRTDBG_MAP_ALLOC
|
---|
| 14 | #include <stdlib.h>
|
---|
| 15 | #include <crtdbg.h>
|
---|
| 16 |
|
---|
| 17 | // redefine new operator
|
---|
| 18 | #define DEBUG_NEW new(_NORMAL_BLOCK, __FILE__, __LINE__)
|
---|
| 19 | #define new DEBUG_NEW
|
---|
| 20 | #endif
|
---|
| 21 |
|
---|
| 22 |
|
---|
[2776] | 23 | namespace CHCDemoEngine
|
---|
[2755] | 24 | {
|
---|
| 25 |
|
---|
| 26 | using namespace std;
|
---|
| 27 |
|
---|
| 28 |
|
---|
| 29 | #define TYPE_INTERIOR -2
|
---|
| 30 | #define TYPE_LEAF -3
|
---|
| 31 |
|
---|
| 32 |
|
---|
| 33 |
|
---|
[3266] | 34 | BvhNode *BvhFactory::LoadNextNode(igzstream &stream, BvhInterior *parent)
|
---|
[2755] | 35 | {
|
---|
| 36 | int nodeType;
|
---|
| 37 | stream.read(reinterpret_cast<char *>(&nodeType), sizeof(int));
|
---|
| 38 |
|
---|
[2760] | 39 | BvhNode *node;
|
---|
| 40 |
|
---|
[2755] | 41 | if (nodeType == TYPE_LEAF)
|
---|
[2760] | 42 | node = new BvhLeaf(parent);
|
---|
| 43 | else if (nodeType == TYPE_INTERIOR)
|
---|
| 44 | node = new BvhInterior(parent);
|
---|
| 45 | else
|
---|
[2763] | 46 | cerr << "error: wrong node type: " << nodeType << endl;
|
---|
| 47 |
|
---|
[2755] | 48 |
|
---|
[2760] | 49 | Vector3 bMin, bMax;
|
---|
| 50 |
|
---|
| 51 | stream.read(reinterpret_cast<char *>(&(node->mFirst)), sizeof(int));
|
---|
| 52 | stream.read(reinterpret_cast<char *>(&(node->mLast)), sizeof(int));
|
---|
[2762] | 53 |
|
---|
[2760] | 54 | stream.read(reinterpret_cast<char *>(&bMin), sizeof(Vector3));
|
---|
| 55 | stream.read(reinterpret_cast<char *>(&bMax), sizeof(Vector3));
|
---|
| 56 |
|
---|
| 57 | node->mBox = AxisAlignedBox3(bMin, bMax);
|
---|
| 58 | node->mArea = node->mBox.SurfaceArea();
|
---|
| 59 |
|
---|
| 60 | //cout << "box: " << node->mBox << " area: " << node->mArea << endl;
|
---|
[2763] | 61 |
|
---|
[2760] | 62 | return node;
|
---|
[2755] | 63 | }
|
---|
| 64 |
|
---|
| 65 |
|
---|
[3266] | 66 | Bvh *BvhFactory::Create(const string &filename,
|
---|
| 67 | const SceneEntityContainer &staticEntities,
|
---|
| 68 | const SceneEntityContainer &dynamicEntities,
|
---|
| 69 | int maxDepthForTestingChildren)
|
---|
| 70 |
|
---|
[2755] | 71 | {
|
---|
[2761] | 72 | queue<BvhNode *> tQueue;
|
---|
[2795] | 73 | igzstream stream(filename.c_str());
|
---|
[2755] | 74 |
|
---|
| 75 | if (!stream.is_open()) return NULL;
|
---|
| 76 |
|
---|
[3266] | 77 | cout << "\n*******************\nloading bvh from file " << filename << " ..." << endl;
|
---|
[2755] | 78 |
|
---|
[3123] | 79 | Bvh *bvh = new Bvh(staticEntities, dynamicEntities, maxDepthForTestingChildren);
|
---|
[2755] | 80 |
|
---|
[3065] | 81 | BvhNode *root = LoadNextNode(stream, NULL);
|
---|
| 82 |
|
---|
[3072] | 83 | bvh->mStaticRoot = root;
|
---|
[3074] | 84 | // we have at least the root, the static and the dynamic branch
|
---|
[3072] | 85 | bvh->mNumNodes = 3;
|
---|
[3068] | 86 |
|
---|
[3244] | 87 | cout << "static bvh bb: " << root->mBox << endl;
|
---|
[3242] | 88 |
|
---|
[3072] | 89 | tQueue.push(root);
|
---|
[3068] | 90 |
|
---|
[3239] | 91 | while (!tQueue.empty())
|
---|
[2755] | 92 | {
|
---|
[2761] | 93 | BvhNode *node = tQueue.front();
|
---|
| 94 | tQueue.pop();
|
---|
[2755] | 95 |
|
---|
| 96 | if (!node->IsLeaf())
|
---|
| 97 | {
|
---|
| 98 | bvh->mNumNodes += 2;
|
---|
| 99 |
|
---|
| 100 | BvhInterior *interior = static_cast<BvhInterior *>(node);
|
---|
| 101 |
|
---|
[2761] | 102 | BvhNode *front = LoadNextNode(stream, interior);
|
---|
[2755] | 103 | BvhNode *back = LoadNextNode(stream, interior);
|
---|
[2761] | 104 |
|
---|
[2755] | 105 | interior->mFront = front;
|
---|
| 106 | interior->mBack = back;
|
---|
| 107 |
|
---|
| 108 | front->mDepth = interior->mDepth + 1;
|
---|
| 109 | back->mDepth = interior->mDepth + 1;
|
---|
| 110 |
|
---|
[2761] | 111 | tQueue.push(front);
|
---|
| 112 | tQueue.push(back);
|
---|
[2755] | 113 | }
|
---|
| 114 | }
|
---|
| 115 |
|
---|
[3244] | 116 | cout << "... finished loading " << bvh->mNumNodes - 2 << " static bvh nodes" << endl;
|
---|
| 117 |
|
---|
| 118 |
|
---|
[3074] | 119 | ///////////
|
---|
[3072] | 120 | //-- create dynamic part of the hierarchy
|
---|
[2796] | 121 |
|
---|
[3072] | 122 | bvh->CreateDynamicBranch();
|
---|
[2964] | 123 |
|
---|
[2760] | 124 | ///////////
|
---|
[2761] | 125 | //-- post process nodes
|
---|
[2755] | 126 |
|
---|
[2761] | 127 | bvh->PostProcess();
|
---|
[2962] | 128 |
|
---|
[3244] | 129 | cout << "bvh bb: " << bvh->mBox << endl;
|
---|
[3072] | 130 |
|
---|
[2755] | 131 | return bvh;
|
---|
| 132 | }
|
---|
| 133 |
|
---|
| 134 |
|
---|
[3266] | 135 | Bvh *BvhFactory::Create(const SceneEntityContainer &staticEntities,
|
---|
| 136 | const SceneEntityContainer &dynamicEntities,
|
---|
| 137 | int maxDepthForTestingChildren)
|
---|
| 138 | {
|
---|
| 139 |
|
---|
| 140 | Bvh *bvh = new Bvh(staticEntities, dynamicEntities, maxDepthForTestingChildren);
|
---|
| 141 |
|
---|
[3282] | 142 | BvhConstructor bvhConstructor(bvh->mGeometry, 0,
|
---|
[3266] | 143 | (int)bvh->mStaticGeometrySize - 1);
|
---|
| 144 |
|
---|
| 145 | int numNodes;
|
---|
| 146 | bvh->mStaticRoot = bvhConstructor.Construct(numNodes);
|
---|
| 147 |
|
---|
| 148 | bvh->mNumNodes = 2 + numNodes;
|
---|
| 149 |
|
---|
| 150 | cout << "created " << bvh->mNumNodes - 2 << " static bvh nodes" << endl;
|
---|
| 151 |
|
---|
| 152 |
|
---|
| 153 | ///////////
|
---|
| 154 | //-- create dynamic part of the hierarchy
|
---|
| 155 |
|
---|
| 156 | bvh->CreateDynamicBranch();
|
---|
| 157 |
|
---|
| 158 |
|
---|
| 159 | ///////////
|
---|
| 160 | //-- post process nodes
|
---|
| 161 |
|
---|
| 162 | bvh->PostProcess();
|
---|
| 163 |
|
---|
| 164 | cout << "bvh bb: " << bvh->mBox << endl;
|
---|
| 165 |
|
---|
| 166 | return bvh;
|
---|
[2755] | 167 | }
|
---|
[3266] | 168 |
|
---|
| 169 |
|
---|
| 170 | }
|
---|