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

Revision 3244, 2.8 KB checked in by mattausch, 15 years ago (diff)
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 &staticEntities,
66                                         const SceneEntityContainer &dynamicEntities,
67                                         int maxDepthForTestingChildren)
68{
69        queue<BvhNode *> tQueue;
70        igzstream stream(filename.c_str());
71
72        if (!stream.is_open()) return NULL;
73
74        cout << "\n*******************\nloading bvh ..." << endl;
75
76        Bvh *bvh = new Bvh(staticEntities, dynamicEntities, maxDepthForTestingChildren);
77
78        BvhNode *root = LoadNextNode(stream, NULL);
79
80        bvh->mStaticRoot = root;
81        // we have at least the root, the static and the dynamic branch
82        bvh->mNumNodes = 3;
83
84        cout << "static bvh bb: " << root->mBox << endl;
85
86        tQueue.push(root);
87       
88        while (!tQueue.empty())
89        {
90                BvhNode *node = tQueue.front();
91                tQueue.pop();
92
93                if (!node->IsLeaf())
94                {
95                        bvh->mNumNodes += 2;
96
97                        BvhInterior *interior = static_cast<BvhInterior *>(node);
98
99                        BvhNode *front = LoadNextNode(stream, interior);
100                        BvhNode *back = LoadNextNode(stream, interior);
101
102                        interior->mFront = front;
103                        interior->mBack = back;
104
105                        front->mDepth = interior->mDepth + 1;
106                        back->mDepth = interior->mDepth + 1;
107
108                        tQueue.push(front);                     
109                        tQueue.push(back);
110                }
111        }
112
113        cout << "... finished loading " << bvh->mNumNodes - 2 << " static bvh nodes" << endl;
114
115
116        ///////////
117        //-- create dynamic part of the hierarchy
118
119        bvh->CreateDynamicBranch();
120
121        ///////////
122        //-- post process nodes
123
124        bvh->PostProcess();
125
126        cout << "bvh bb: " << bvh->mBox << endl;
127
128        return bvh;
129}
130
131
132}
Note: See TracBrowser for help on using the repository browser.