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

Revision 3021, 3.3 KB checked in by mattausch, 16 years ago (diff)

removed leaks. added class for shaders

Line 
1#include <queue>
2#include <stack>
3#include <fstream>
4#include <iostream>
5
6#include "BvhLoader.h"
7#include "gzstream.h"
8
9
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
21namespace CHCDemoEngine
22{
23
24using namespace std;
25
26
27#define TYPE_INTERIOR -2
28#define TYPE_LEAF -3
29
30
31
32BvhNode *BvhLoader::LoadNextNode(igzstream &stream, BvhInterior *parent)
33{
34        int nodeType;
35        stream.read(reinterpret_cast<char *>(&nodeType), sizeof(int));
36
37        BvhNode *node;
38
39        if (nodeType == TYPE_LEAF)
40                node = new BvhLeaf(parent);
41        else if (nodeType == TYPE_INTERIOR)
42                node = new BvhInterior(parent);
43        else
44                cerr << "error: wrong node type: " << nodeType << endl;
45               
46
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));
51
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;
59       
60        return node;
61}
62
63
64Bvh *BvhLoader::Load(const string &filename, 
65                                         const SceneEntityContainer &entities)
66{
67        queue<BvhNode *> tQueue;
68        igzstream stream(filename.c_str());
69
70        if (!stream.is_open()) return NULL;
71
72        cout << "loading bvh" << endl;
73
74        Bvh *bvh = new Bvh(entities);
75
76        BvhNode *root = LoadNextNode(stream, NULL);
77
78#if 1
79
80        bvh->mRoot = root;
81
82#else
83        // copy root and set new one for dynamic objects
84        BvhInterior *newRoot = new BvhInterior(NULL);
85       
86        newRoot->mBox = root->mBox;
87        newRoot->mFirst = root->mFirst;
88        newRoot->mLast = root->mLast;
89
90        // create 'dynamic' leaf which basically is a container
91        // for all dynamic objects
92        BvhLeaf *dynamicLeaf = new BvhLeaf(newRoot);
93        dynamicLeaf->mBox = root->mBox;
94
95        newRoot->mFront = root;
96        root->mParent = newRoot;
97
98        newRoot->mBack = dynamicLeaf;
99
100        bvh->mRoot = newRoot;
101#endif
102
103        tQueue.push(bvh->mRoot);
104        bvh->mNumNodes = 1;
105
106        while(!tQueue.empty())
107        {
108                BvhNode *node = tQueue.front();
109                tQueue.pop();
110
111                if (!node->IsLeaf())
112                {
113                        bvh->mNumNodes += 2;
114
115                        BvhInterior *interior = static_cast<BvhInterior *>(node);
116
117                        BvhNode *front = LoadNextNode(stream, interior);
118                        BvhNode *back = LoadNextNode(stream, interior);
119
120                        interior->mFront = front;
121                        interior->mBack = back;
122
123                        front->mDepth = interior->mDepth + 1;
124                        back->mDepth = interior->mDepth + 1;
125
126                        tQueue.push(front);                     
127                        tQueue.push(back);
128                }
129        }
130
131
132        bvh->mBox = bvh->mRoot->GetBox();
133
134        cout << "... finished loading " << bvh->mNumNodes << " nodes" << endl;
135        cout << "scene box: " << bvh->mBox << endl;
136
137       
138        ///////////
139        //-- post process nodes
140
141        bvh->PostProcess();
142       
143        // set virtual leaves for specified number of triangles
144        bvh->SetVirtualLeaves(INITIAL_TRIANGLES_PER_VIRTUAL_LEAVES);
145
146        bvh->UpdateNumLeaves(bvh->mRoot);
147        // compute unique ids
148        bvh->ComputeIds();
149        // specify bounds for occlusion tests
150        bvh->RecomputeBounds();
151
152        // compute and print stats
153        bvh->ComputeBvhStats();
154        bvh->PrintBvhStats();
155
156        return bvh;
157}
158
159
160}
Note: See TracBrowser for help on using the repository browser.