[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,
|
---|
[3072] | 65 | const SceneEntityContainer &staticEntities,
|
---|
[3123] | 66 | const SceneEntityContainer &dynamicEntities,
|
---|
| 67 | int maxDepthForTestingChildren)
|
---|
[2755] | 68 | {
|
---|
[2761] | 69 | queue<BvhNode *> tQueue;
|
---|
[2795] | 70 | igzstream stream(filename.c_str());
|
---|
[2755] | 71 |
|
---|
| 72 | if (!stream.is_open()) return NULL;
|
---|
| 73 |
|
---|
| 74 | cout << "loading bvh" << endl;
|
---|
| 75 |
|
---|
[3123] | 76 | Bvh *bvh = new Bvh(staticEntities, dynamicEntities, maxDepthForTestingChildren);
|
---|
[2755] | 77 |
|
---|
[3065] | 78 | BvhNode *root = LoadNextNode(stream, NULL);
|
---|
| 79 |
|
---|
[3072] | 80 | bvh->mStaticRoot = root;
|
---|
[3074] | 81 | // we have at least the root, the static and the dynamic branch
|
---|
[3072] | 82 | bvh->mNumNodes = 3;
|
---|
[3068] | 83 |
|
---|
[3072] | 84 | tQueue.push(root);
|
---|
[3068] | 85 |
|
---|
[2761] | 86 | while(!tQueue.empty())
|
---|
[2755] | 87 | {
|
---|
[2761] | 88 | BvhNode *node = tQueue.front();
|
---|
| 89 | tQueue.pop();
|
---|
[2755] | 90 |
|
---|
| 91 | if (!node->IsLeaf())
|
---|
| 92 | {
|
---|
| 93 | bvh->mNumNodes += 2;
|
---|
| 94 |
|
---|
| 95 | BvhInterior *interior = static_cast<BvhInterior *>(node);
|
---|
| 96 |
|
---|
[2761] | 97 | BvhNode *front = LoadNextNode(stream, interior);
|
---|
[2755] | 98 | BvhNode *back = LoadNextNode(stream, interior);
|
---|
[2761] | 99 |
|
---|
[2755] | 100 | interior->mFront = front;
|
---|
| 101 | interior->mBack = back;
|
---|
| 102 |
|
---|
| 103 | front->mDepth = interior->mDepth + 1;
|
---|
| 104 | back->mDepth = interior->mDepth + 1;
|
---|
| 105 |
|
---|
[2761] | 106 | tQueue.push(front);
|
---|
| 107 | tQueue.push(back);
|
---|
[2755] | 108 | }
|
---|
| 109 | }
|
---|
| 110 |
|
---|
[3074] | 111 | ///////////
|
---|
[3072] | 112 | //-- create dynamic part of the hierarchy
|
---|
[2796] | 113 |
|
---|
[3072] | 114 | bvh->CreateDynamicBranch();
|
---|
[2964] | 115 |
|
---|
[2760] | 116 | ///////////
|
---|
[2761] | 117 | //-- post process nodes
|
---|
[2755] | 118 |
|
---|
[2761] | 119 | bvh->PostProcess();
|
---|
[2962] | 120 |
|
---|
[3072] | 121 | cout << "... finished loading " << bvh->mNumNodes << " nodes" << endl;
|
---|
| 122 | cout << "scene box: " << bvh->mBox << endl;
|
---|
| 123 |
|
---|
[2755] | 124 | return bvh;
|
---|
| 125 | }
|
---|
| 126 |
|
---|
| 127 |
|
---|
| 128 | }
|
---|