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

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