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

Revision 3282, 3.6 KB checked in by mattausch, 15 years ago (diff)

probably found export error

Line 
1#include "BvhLoader.h"
2#include "BvhConstructor.h"
3#include "gzstream.h"
4
5#include <queue>
6#include <stack>
7#include <fstream>
8#include <iostream>
9
10
11
12#ifdef _CRT_SET
13        #define _CRTDBG_MAP_ALLOC
14        #include <stdlib.h>
15        #include <crtdbg.h>
16
17        // redefine new operator
18        #define DEBUG_NEW new(_NORMAL_BLOCK, __FILE__, __LINE__)
19        #define new DEBUG_NEW
20#endif
21
22
23namespace CHCDemoEngine
24{
25
26using namespace std;
27
28
29#define TYPE_INTERIOR -2
30#define TYPE_LEAF -3
31
32
33
34BvhNode *BvhFactory::LoadNextNode(igzstream &stream, BvhInterior *parent)
35{
36        int nodeType;
37        stream.read(reinterpret_cast<char *>(&nodeType), sizeof(int));
38
39        BvhNode *node;
40
41        if (nodeType == TYPE_LEAF)
42                node = new BvhLeaf(parent);
43        else if (nodeType == TYPE_INTERIOR)
44                node = new BvhInterior(parent);
45        else
46                cerr << "error: wrong node type: " << nodeType << endl;
47               
48
49        Vector3 bMin, bMax;
50
51        stream.read(reinterpret_cast<char *>(&(node->mFirst)), sizeof(int));
52        stream.read(reinterpret_cast<char *>(&(node->mLast)), sizeof(int));
53
54        stream.read(reinterpret_cast<char *>(&bMin), sizeof(Vector3));
55        stream.read(reinterpret_cast<char *>(&bMax), sizeof(Vector3));
56
57        node->mBox = AxisAlignedBox3(bMin, bMax);
58        node->mArea = node->mBox.SurfaceArea();
59
60        //cout << "box: " << node->mBox << " area: " << node->mArea << endl;
61       
62        return node;
63}
64
65
66Bvh *BvhFactory::Create(const string &filename,
67                                                const SceneEntityContainer &staticEntities,
68                                                const SceneEntityContainer &dynamicEntities,
69                                                int maxDepthForTestingChildren)
70
71{
72        queue<BvhNode *> tQueue;
73        igzstream stream(filename.c_str());
74
75        if (!stream.is_open()) return NULL;
76
77        cout << "\n*******************\nloading bvh from file " << filename << " ..." << endl;
78
79        Bvh *bvh = new Bvh(staticEntities, dynamicEntities, maxDepthForTestingChildren);
80
81        BvhNode *root = LoadNextNode(stream, NULL);
82
83        bvh->mStaticRoot = root;
84        // we have at least the root, the static and the dynamic branch
85        bvh->mNumNodes = 3;
86
87        cout << "static bvh bb: " << root->mBox << endl;
88
89        tQueue.push(root);
90       
91        while (!tQueue.empty())
92        {
93                BvhNode *node = tQueue.front();
94                tQueue.pop();
95
96                if (!node->IsLeaf())
97                {
98                        bvh->mNumNodes += 2;
99
100                        BvhInterior *interior = static_cast<BvhInterior *>(node);
101
102                        BvhNode *front = LoadNextNode(stream, interior);
103                        BvhNode *back = LoadNextNode(stream, interior);
104
105                        interior->mFront = front;
106                        interior->mBack = back;
107
108                        front->mDepth = interior->mDepth + 1;
109                        back->mDepth = interior->mDepth + 1;
110
111                        tQueue.push(front);                     
112                        tQueue.push(back);
113                }
114        }
115
116        cout << "... finished loading " << bvh->mNumNodes - 2 << " static bvh nodes" << endl;
117
118
119        ///////////
120        //-- create dynamic part of the hierarchy
121
122        bvh->CreateDynamicBranch();
123
124        ///////////
125        //-- post process nodes
126
127        bvh->PostProcess();
128
129        cout << "bvh bb: " << bvh->mBox << endl;
130
131        return bvh;
132}
133
134
135Bvh *BvhFactory::Create(const SceneEntityContainer &staticEntities,
136                                                const SceneEntityContainer &dynamicEntities,
137                                                int maxDepthForTestingChildren)
138{
139
140        Bvh *bvh = new Bvh(staticEntities, dynamicEntities, maxDepthForTestingChildren);
141
142        BvhConstructor bvhConstructor(bvh->mGeometry, 0,
143                                                                  (int)bvh->mStaticGeometrySize - 1);
144
145        int numNodes;
146        bvh->mStaticRoot = bvhConstructor.Construct(numNodes);
147
148        bvh->mNumNodes = 2 + numNodes;
149
150        cout << "created " << bvh->mNumNodes - 2 << " static bvh nodes" << endl;
151
152
153        ///////////
154        //-- create dynamic part of the hierarchy
155
156        bvh->CreateDynamicBranch();
157
158
159        ///////////
160        //-- post process nodes
161
162        bvh->PostProcess();
163
164        cout << "bvh bb: " << bvh->mBox << endl;
165
166        return bvh;
167}
168
169
170}
Note: See TracBrowser for help on using the repository browser.