source: GTP/trunk/App/Demos/Vis/CHC_revisited/BvhLoader.cpp @ 2761

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