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

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