source: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/BvhLoader.cpp @ 2964

Revision 2964, 3.1 KB checked in by mattausch, 16 years ago (diff)
RevLine 
[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
[2776]10namespace CHCDemoEngine
[2755]11{
12
13using namespace std;
14
15
16#define TYPE_INTERIOR -2
17#define TYPE_LEAF -3
18
19
20
[2795]21BvhNode *BvhLoader::LoadNextNode(igzstream &stream, BvhInterior *parent)
[2755]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]53Bvh *BvhLoader::Load(const string &filename, 
54                                         const SceneEntityContainer &entities)
[2755]55{
[2761]56        queue<BvhNode *> tQueue;
[2795]57        igzstream stream(filename.c_str());
[2755]58
59        if (!stream.is_open()) return NULL;
60
61        cout << "loading bvh" << endl;
62
[2760]63        Bvh *bvh = new Bvh(entities);
[2755]64
[2962]65        BvhNode *root = LoadNextNode(stream, NULL);
66
67#if 0
68
69        bvh->mRoot = root;
70
71#else
72        // copy root and set new one for dynamic objects
73        BvhInterior *newRoot = new BvhInterior(NULL);
74       
75        newRoot->mBox = root->mBox;
76        newRoot->mFirst = root->mFirst;
77        newRoot->mLast = root->mLast;
78
[2963]79        // create 'dynamic' leaf which basically is a container
[2962]80        // for all dynamic objects
81        BvhLeaf *dynamicLeaf = new BvhLeaf(newRoot);
82        dynamicLeaf->mBox = root->mBox;
83
84        newRoot->mFront = root;
85        root->mParent = newRoot;
86
87        newRoot->mBack = dynamicLeaf;
88
89        bvh->mRoot = newRoot;
90#endif
91
[2761]92        tQueue.push(bvh->mRoot);
[2755]93        bvh->mNumNodes = 1;
94
[2761]95        while(!tQueue.empty())
[2755]96        {
[2761]97                BvhNode *node = tQueue.front();
98                tQueue.pop();
[2755]99
100                if (!node->IsLeaf())
101                {
102                        bvh->mNumNodes += 2;
103
104                        BvhInterior *interior = static_cast<BvhInterior *>(node);
105
[2761]106                        BvhNode *front = LoadNextNode(stream, interior);
[2755]107                        BvhNode *back = LoadNextNode(stream, interior);
[2761]108
[2755]109                        interior->mFront = front;
110                        interior->mBack = back;
111
112                        front->mDepth = interior->mDepth + 1;
113                        back->mDepth = interior->mDepth + 1;
114
[2761]115                        tQueue.push(front);                     
116                        tQueue.push(back);
[2755]117                }
118        }
119
120
[2796]121        bvh->mBox = bvh->mRoot->GetBox();
122
[2964]123        cout << "... finished loading " << bvh->mNumNodes << " nodes" << endl;
124        cout << "scene box: " << bvh->mBox << endl;
125
[2962]126       
[2760]127        ///////////
[2761]128        //-- post process nodes
[2755]129
[2761]130        bvh->PostProcess();
[2795]131       
132        // set virtual leaves for specified number of triangles
[2800]133        bvh->SetVirtualLeaves(INITIAL_TRIANGLES_PER_VIRTUAL_LEAVES);
[2761]134
[2760]135        bvh->UpdateNumLeaves(bvh->mRoot);
[2755]136        // compute unique ids
[2760]137        bvh->ComputeIds();
138        // specify bounds for occlusion tests
[2773]139        bvh->RecomputeBounds();
[2962]140
[2760]141        // compute and print stats
142        bvh->ComputeBvhStats();
143        bvh->PrintBvhStats();
[2763]144
[2755]145        return bvh;
146}
147
148
149}
Note: See TracBrowser for help on using the repository browser.