[2755] | 1 | #include <queue>
|
---|
| 2 | #include <stack>
|
---|
| 3 | #include <fstream>
|
---|
| 4 | #include <iostream>
|
---|
| 5 |
|
---|
| 6 | #include "BvhLoader.h"
|
---|
| 7 |
|
---|
| 8 |
|
---|
| 9 |
|
---|
| 10 | namespace CHCDemo
|
---|
| 11 | {
|
---|
| 12 |
|
---|
| 13 | using namespace std;
|
---|
| 14 |
|
---|
| 15 |
|
---|
| 16 | #define TYPE_INTERIOR -2
|
---|
| 17 | #define TYPE_LEAF -3
|
---|
| 18 |
|
---|
| 19 |
|
---|
| 20 |
|
---|
| 21 | BvhNode *BvhLoader::LoadNextNode(ifstream &stream, BvhInterior *parent)
|
---|
| 22 | {
|
---|
| 23 | int nodeType;
|
---|
| 24 | stream.read(reinterpret_cast<char *>(&nodeType), sizeof(int));
|
---|
| 25 |
|
---|
[2760] | 26 | BvhNode *node;
|
---|
| 27 |
|
---|
[2755] | 28 | if (nodeType == TYPE_LEAF)
|
---|
[2760] | 29 | node = new BvhLeaf(parent);
|
---|
| 30 | else if (nodeType == TYPE_INTERIOR)
|
---|
| 31 | node = new BvhInterior(parent);
|
---|
| 32 | else
|
---|
[2763] | 33 | cerr << "error: wrong node type: " << nodeType << endl;
|
---|
| 34 |
|
---|
[2755] | 35 |
|
---|
[2760] | 36 | Vector3 bMin, bMax;
|
---|
| 37 |
|
---|
| 38 | stream.read(reinterpret_cast<char *>(&(node->mFirst)), sizeof(int));
|
---|
| 39 | stream.read(reinterpret_cast<char *>(&(node->mLast)), sizeof(int));
|
---|
[2762] | 40 |
|
---|
[2760] | 41 | stream.read(reinterpret_cast<char *>(&bMin), sizeof(Vector3));
|
---|
| 42 | stream.read(reinterpret_cast<char *>(&bMax), sizeof(Vector3));
|
---|
| 43 |
|
---|
| 44 | node->mBox = AxisAlignedBox3(bMin, bMax);
|
---|
| 45 | node->mArea = node->mBox.SurfaceArea();
|
---|
| 46 |
|
---|
| 47 | //cout << "box: " << node->mBox << " area: " << node->mArea << endl;
|
---|
[2763] | 48 |
|
---|
[2760] | 49 | return node;
|
---|
[2755] | 50 | }
|
---|
| 51 |
|
---|
| 52 |
|
---|
[2760] | 53 | Bvh *BvhLoader::Load(const string &filename,
|
---|
| 54 | const SceneEntityContainer &entities)
|
---|
[2755] | 55 | {
|
---|
[2761] | 56 | queue<BvhNode *> tQueue;
|
---|
[2755] | 57 | ifstream stream(filename.c_str(), ifstream::binary);
|
---|
| 58 |
|
---|
| 59 | if (!stream.is_open()) return NULL;
|
---|
| 60 |
|
---|
| 61 | cout << "loading bvh" << endl;
|
---|
| 62 |
|
---|
[2760] | 63 | Bvh *bvh = new Bvh(entities);
|
---|
[2755] | 64 | bvh->mRoot = LoadNextNode(stream, NULL);
|
---|
| 65 |
|
---|
[2761] | 66 | tQueue.push(bvh->mRoot);
|
---|
[2755] | 67 | bvh->mNumNodes = 1;
|
---|
| 68 |
|
---|
[2761] | 69 | while(!tQueue.empty())
|
---|
[2755] | 70 | {
|
---|
[2761] | 71 | BvhNode *node = tQueue.front();
|
---|
| 72 | tQueue.pop();
|
---|
[2755] | 73 |
|
---|
| 74 | if (!node->IsLeaf())
|
---|
| 75 | {
|
---|
| 76 | bvh->mNumNodes += 2;
|
---|
| 77 |
|
---|
| 78 | BvhInterior *interior = static_cast<BvhInterior *>(node);
|
---|
| 79 |
|
---|
[2761] | 80 | BvhNode *front = LoadNextNode(stream, interior);
|
---|
[2755] | 81 | BvhNode *back = LoadNextNode(stream, interior);
|
---|
[2761] | 82 |
|
---|
[2755] | 83 | interior->mFront = front;
|
---|
| 84 | interior->mBack = back;
|
---|
| 85 |
|
---|
| 86 | front->mDepth = interior->mDepth + 1;
|
---|
| 87 | back->mDepth = interior->mDepth + 1;
|
---|
| 88 |
|
---|
[2761] | 89 | tQueue.push(front);
|
---|
| 90 | tQueue.push(back);
|
---|
[2755] | 91 | }
|
---|
| 92 | }
|
---|
| 93 |
|
---|
| 94 | cout << "... finished loading " << bvh->mNumNodes << " nodes, updating boxes" << endl;
|
---|
| 95 |
|
---|
[2760] | 96 | ///////////
|
---|
[2761] | 97 | //-- post process nodes
|
---|
[2755] | 98 |
|
---|
[2761] | 99 | bvh->PostProcess();
|
---|
| 100 | // set virtual leaves for 500 triangles
|
---|
[2762] | 101 | bvh->SetVirtualLeaves(0);
|
---|
[2761] | 102 |
|
---|
[2760] | 103 | bvh->UpdateNumLeaves(bvh->mRoot);
|
---|
[2755] | 104 | // compute unique ids
|
---|
[2760] | 105 | bvh->ComputeIds();
|
---|
| 106 | // specify bounds for occlusion tests
|
---|
[2762] | 107 | //bvh->RecomputeBounds();
|
---|
[2760] | 108 | // compute and print stats
|
---|
| 109 | bvh->ComputeBvhStats();
|
---|
| 110 | bvh->PrintBvhStats();
|
---|
[2763] | 111 |
|
---|
[2755] | 112 | return bvh;
|
---|
| 113 | }
|
---|
| 114 |
|
---|
| 115 |
|
---|
| 116 | }
|
---|