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

Revision 2762, 2.7 KB checked in by mattausch, 16 years ago (diff)

debug version

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
49        stream.read(reinterpret_cast<char *>(&bMin), sizeof(Vector3));
50        stream.read(reinterpret_cast<char *>(&bMax), sizeof(Vector3));
51
52        //if ((bMin.x >= bMax.x)||(bMin.y >= bMax.y)||(bMin.z >= bMax.z))
53        //      cout << "error: " << bMin << " " << bMax << endl;
54
55        node->mBox = AxisAlignedBox3(bMin, bMax);
56
57        node->mArea = node->mBox.SurfaceArea();
58
59        //cout << "box: " << node->mBox << " area: " << node->mArea << endl;
60        cout << node->mBox << " f " << node->mFirst << " l " << node->mLast << endl;
61
62        return node;
63}
64
65
66Bvh *BvhLoader::Load(const string &filename, 
67                                         const SceneEntityContainer &entities)
68{
69        queue<BvhNode *> tQueue;
70        ifstream stream(filename.c_str(), ifstream::binary);
71
72        if (!stream.is_open()) return NULL;
73
74        cout << "loading bvh" << endl;
75
76        Bvh *bvh = new Bvh(entities);
77        bvh->mRoot = LoadNextNode(stream, NULL);
78
79        tQueue.push(bvh->mRoot);
80        bvh->mNumNodes = 1;
81
82        while(!tQueue.empty())
83        {
84                BvhNode *node = tQueue.front();
85                tQueue.pop();
86
87                if (!node->IsLeaf())
88                {
89                        bvh->mNumNodes += 2;
90
91                        BvhInterior *interior = static_cast<BvhInterior *>(node);
92
93                        BvhNode *front = LoadNextNode(stream, interior);
94                        BvhNode *back = LoadNextNode(stream, interior);
95
96                        interior->mFront = front;
97                        interior->mBack = back;
98
99                        front->mDepth = interior->mDepth + 1;
100                        back->mDepth = interior->mDepth + 1;
101
102                        tQueue.push(front);                     
103                        tQueue.push(back);
104                }
105        }
106
107        cout << "... finished loading " << bvh->mNumNodes << " nodes, updating boxes" << endl;
108
109        ///////////
110        //-- post process nodes
111
112        bvh->PostProcess();
113        // set virtual leaves for 500 triangles
114        bvh->SetVirtualLeaves(0);
115
116        bvh->UpdateNumLeaves(bvh->mRoot);
117        // compute unique ids
118        bvh->ComputeIds();
119        // specify bounds for occlusion tests
120        //bvh->RecomputeBounds();
121        // compute and print stats
122        bvh->ComputeBvhStats();
123        bvh->PrintBvhStats();
124bvh->Dummy();
125        return bvh;
126}
127
128
129}
Note: See TracBrowser for help on using the repository browser.