[2755] | 1 | #include <queue>
|
---|
| 2 | #include <stack>
|
---|
| 3 | #include <fstream>
|
---|
| 4 | #include <iostream>
|
---|
| 5 |
|
---|
| 6 | #include "BvhLoader.h"
|
---|
[2795] | 7 | #include "gzstream.h"
|
---|
[2755] | 8 |
|
---|
| 9 |
|
---|
[3021] | 10 | #ifdef _CRT_SET
|
---|
| 11 | #define _CRTDBG_MAP_ALLOC
|
---|
| 12 | #include <stdlib.h>
|
---|
| 13 | #include <crtdbg.h>
|
---|
| 14 |
|
---|
| 15 | // redefine new operator
|
---|
| 16 | #define DEBUG_NEW new(_NORMAL_BLOCK, __FILE__, __LINE__)
|
---|
| 17 | #define new DEBUG_NEW
|
---|
| 18 | #endif
|
---|
| 19 |
|
---|
| 20 |
|
---|
[2776] | 21 | namespace CHCDemoEngine
|
---|
[2755] | 22 | {
|
---|
| 23 |
|
---|
| 24 | using namespace std;
|
---|
| 25 |
|
---|
| 26 |
|
---|
| 27 | #define TYPE_INTERIOR -2
|
---|
| 28 | #define TYPE_LEAF -3
|
---|
| 29 |
|
---|
| 30 |
|
---|
| 31 |
|
---|
[2795] | 32 | BvhNode *BvhLoader::LoadNextNode(igzstream &stream, BvhInterior *parent)
|
---|
[2755] | 33 | {
|
---|
| 34 | int nodeType;
|
---|
| 35 | stream.read(reinterpret_cast<char *>(&nodeType), sizeof(int));
|
---|
| 36 |
|
---|
[2760] | 37 | BvhNode *node;
|
---|
| 38 |
|
---|
[2755] | 39 | if (nodeType == TYPE_LEAF)
|
---|
[2760] | 40 | node = new BvhLeaf(parent);
|
---|
| 41 | else if (nodeType == TYPE_INTERIOR)
|
---|
| 42 | node = new BvhInterior(parent);
|
---|
| 43 | else
|
---|
[2763] | 44 | cerr << "error: wrong node type: " << nodeType << endl;
|
---|
| 45 |
|
---|
[2755] | 46 |
|
---|
[2760] | 47 | Vector3 bMin, bMax;
|
---|
| 48 |
|
---|
| 49 | stream.read(reinterpret_cast<char *>(&(node->mFirst)), sizeof(int));
|
---|
| 50 | stream.read(reinterpret_cast<char *>(&(node->mLast)), sizeof(int));
|
---|
[2762] | 51 |
|
---|
[2760] | 52 | stream.read(reinterpret_cast<char *>(&bMin), sizeof(Vector3));
|
---|
| 53 | stream.read(reinterpret_cast<char *>(&bMax), sizeof(Vector3));
|
---|
| 54 |
|
---|
| 55 | node->mBox = AxisAlignedBox3(bMin, bMax);
|
---|
| 56 | node->mArea = node->mBox.SurfaceArea();
|
---|
| 57 |
|
---|
| 58 | //cout << "box: " << node->mBox << " area: " << node->mArea << endl;
|
---|
[2763] | 59 |
|
---|
[2760] | 60 | return node;
|
---|
[2755] | 61 | }
|
---|
| 62 |
|
---|
| 63 |
|
---|
[2760] | 64 | Bvh *BvhLoader::Load(const string &filename,
|
---|
| 65 | const SceneEntityContainer &entities)
|
---|
[2755] | 66 | {
|
---|
[2761] | 67 | queue<BvhNode *> tQueue;
|
---|
[2795] | 68 | igzstream stream(filename.c_str());
|
---|
[2755] | 69 |
|
---|
| 70 | if (!stream.is_open()) return NULL;
|
---|
| 71 |
|
---|
| 72 | cout << "loading bvh" << endl;
|
---|
| 73 |
|
---|
[2760] | 74 | Bvh *bvh = new Bvh(entities);
|
---|
[2755] | 75 |
|
---|
[2962] | 76 | BvhNode *root = LoadNextNode(stream, NULL);
|
---|
| 77 |
|
---|
[3052] | 78 | #if 0
|
---|
[2962] | 79 |
|
---|
| 80 | bvh->mRoot = root;
|
---|
| 81 |
|
---|
| 82 | #else
|
---|
[3052] | 83 | // we are setting a new root node
|
---|
| 84 | // which adds one level of indirection to the bvh but
|
---|
| 85 | // allows us to use dynamic objects
|
---|
| 86 | // the new bvh has two main branches
|
---|
| 87 | // one is the static branch (the old root),
|
---|
| 88 | // one is the dynamic branch
|
---|
| 89 | // we create a 'dynamic' leaf which basically is a container
|
---|
| 90 | // for all dynamic objects underneath
|
---|
| 91 | // the bounding boxes of the dynamic tree must be updated
|
---|
| 92 | // once each frame in order to be able to incorporate
|
---|
| 93 | // the movements of the objects within
|
---|
| 94 |
|
---|
[2962] | 95 | BvhInterior *newRoot = new BvhInterior(NULL);
|
---|
[3052] | 96 | bvh->mRoot = newRoot;
|
---|
[2962] | 97 |
|
---|
| 98 | BvhLeaf *dynamicLeaf = new BvhLeaf(newRoot);
|
---|
| 99 |
|
---|
| 100 | newRoot->mFront = root;
|
---|
[3052] | 101 | newRoot->mBack = dynamicLeaf;
|
---|
[2962] | 102 | root->mParent = newRoot;
|
---|
| 103 |
|
---|
[3052] | 104 | // the separation is a purely logical one
|
---|
| 105 | // the bounding boxes of the child nodes is the
|
---|
| 106 | // same as for the root node
|
---|
| 107 | newRoot->mBox = root->mBox;
|
---|
| 108 | dynamicLeaf->mBox = root->mBox;
|
---|
[2962] | 109 |
|
---|
[3052] | 110 | newRoot->mArea = newRoot->mBox.SurfaceArea();
|
---|
| 111 | dynamicLeaf->mArea = dynamicLeaf->mBox.SurfaceArea();
|
---|
| 112 |
|
---|
| 113 | newRoot->mFirst = root->mFirst;
|
---|
| 114 | newRoot->mLast = root->mLast;
|
---|
| 115 |
|
---|
| 116 | dynamicLeaf->mFirst = 0;
|
---|
| 117 | dynamicLeaf->mLast = 0;
|
---|
[2962] | 118 | #endif
|
---|
| 119 |
|
---|
[3052] | 120 | tQueue.push(root);
|
---|
[2755] | 121 | bvh->mNumNodes = 1;
|
---|
| 122 |
|
---|
[2761] | 123 | while(!tQueue.empty())
|
---|
[2755] | 124 | {
|
---|
[2761] | 125 | BvhNode *node = tQueue.front();
|
---|
| 126 | tQueue.pop();
|
---|
[2755] | 127 |
|
---|
| 128 | if (!node->IsLeaf())
|
---|
| 129 | {
|
---|
| 130 | bvh->mNumNodes += 2;
|
---|
| 131 |
|
---|
| 132 | BvhInterior *interior = static_cast<BvhInterior *>(node);
|
---|
| 133 |
|
---|
[2761] | 134 | BvhNode *front = LoadNextNode(stream, interior);
|
---|
[2755] | 135 | BvhNode *back = LoadNextNode(stream, interior);
|
---|
[2761] | 136 |
|
---|
[2755] | 137 | interior->mFront = front;
|
---|
| 138 | interior->mBack = back;
|
---|
| 139 |
|
---|
| 140 | front->mDepth = interior->mDepth + 1;
|
---|
| 141 | back->mDepth = interior->mDepth + 1;
|
---|
| 142 |
|
---|
[2761] | 143 | tQueue.push(front);
|
---|
| 144 | tQueue.push(back);
|
---|
[2755] | 145 | }
|
---|
| 146 | }
|
---|
| 147 |
|
---|
| 148 |
|
---|
[2796] | 149 | bvh->mBox = bvh->mRoot->GetBox();
|
---|
| 150 |
|
---|
[2964] | 151 | cout << "... finished loading " << bvh->mNumNodes << " nodes" << endl;
|
---|
| 152 | cout << "scene box: " << bvh->mBox << endl;
|
---|
| 153 |
|
---|
[2962] | 154 |
|
---|
[2760] | 155 | ///////////
|
---|
[2761] | 156 | //-- post process nodes
|
---|
[2755] | 157 |
|
---|
[2761] | 158 | bvh->PostProcess();
|
---|
[2795] | 159 |
|
---|
| 160 | // set virtual leaves for specified number of triangles
|
---|
[2800] | 161 | bvh->SetVirtualLeaves(INITIAL_TRIANGLES_PER_VIRTUAL_LEAVES);
|
---|
[2761] | 162 |
|
---|
[2760] | 163 | bvh->UpdateNumLeaves(bvh->mRoot);
|
---|
[2755] | 164 | // compute unique ids
|
---|
[2760] | 165 | bvh->ComputeIds();
|
---|
| 166 | // specify bounds for occlusion tests
|
---|
[2773] | 167 | bvh->RecomputeBounds();
|
---|
[2962] | 168 |
|
---|
[2760] | 169 | // compute and print stats
|
---|
| 170 | bvh->ComputeBvhStats();
|
---|
| 171 | bvh->PrintBvhStats();
|
---|
[2763] | 172 |
|
---|
[2755] | 173 | return bvh;
|
---|
| 174 | }
|
---|
| 175 |
|
---|
| 176 |
|
---|
| 177 | }
|
---|