Changeset 3241


Ignore:
Timestamp:
01/02/09 13:47:05 (15 years ago)
Author:
mattausch
Message:
 
Location:
GTP/trunk/App/Demos/Vis/FriendlyCulling/VisibilitySolutionConverter
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • GTP/trunk/App/Demos/Vis/FriendlyCulling/VisibilitySolutionConverter/VisibilitySolutionConverter.cpp

    r3239 r3241  
    11#include "VisibilitySolutionConverter.h" 
    2 #include "SimpleTri.h" 
    3 #include "SimpleVec.h" 
     2#include "Triangle3.h" 
     3#include "Vector3.h" 
    44#include "gzstream.h" 
    55#include <iostream> 
     
    133133                        { 
    134134                                // no face normals? => create normals 
    135                                 const SimpleTri tri(vertices[indices[idx1]], 
    136                                                         vertices[indices[idx2]],  
    137                                                                         vertices[indices[idx3]]); 
    138  
    139                                 const SimpleVec n = tri.GetNormal(); 
     135                                const CHCDemoEngine::Triangle3  
     136                                        tri(vertices[indices[idx1]], 
     137                                            vertices[indices[idx2]],  
     138                                                vertices[indices[idx3]]); 
     139 
     140                                const CHCDemoEngine::Vector3 n = tri.GetNormal(); 
    140141 
    141142                                faceNormals.push_back(n); 
     
    182183 
    183184        // convert the triangles to geometry 
    184         geom->mVertices = new SimpleVec[numElements]; 
    185         geom->mNormals = new SimpleVec[numElements]; 
     185        geom->mVertices = new CHCDemoEngine::Vector3[numElements]; 
     186        geom->mNormals = new CHCDemoEngine::Vector3[numElements]; 
    186187        geom->mTexCoords = new TexCoord[numElements]; 
    187188 
     
    244245        } 
    245246 
     247        // update bvh bounding boxes with loaded geometry 
     248        UpdateNodeBox(mRoot); 
     249 
     250        cout << "writing scene" << endl; 
     251 
    246252        if (!WriteScene(sceneOutputFilename)) 
    247253        { 
     
    249255                return false; 
    250256        } 
     257 
     258        cout << "writing bvh" << endl; 
    251259 
    252260        if (!WriteBvh(bvhOutputFilename)) 
     
    315323                                case 'n' : 
    316324                                        sscanf(str + 2, "%f %f %f", &x, &y, &z); 
    317                                         tempNormals.push_back(SimpleVec(x, y, z)); 
     325                                        tempNormals.push_back(CHCDemoEngine::Vector3(x, y, z)); 
    318326                                        break; 
    319327                                case 't': 
     
    325333                                        //const float scale = 5e-3f; 
    326334                                        const float scale = 0.1f; 
    327                                         tempVertices.push_back(SimpleVec(x * scale, y * scale, z * scale)); 
     335                                        tempVertices.push_back(CHCDemoEngine::Vector3(x * scale, y * scale, z * scale)); 
    328336                                        //cout <<"v " << x << " " << y << " "<< z << " "; 
    329337                                } 
     
    388396                                        sscanf(str + 1, "%f %f %f", &x, &y, &z); 
    389397                                        const float scale = 0.1f; 
    390                                         vertices.push_back(SimpleVec(x * scale, y * scale, z * scale)); 
     398                                        vertices.push_back(CHCDemoEngine::Vector3(x * scale, y * scale, z * scale)); 
    391399                                        break; 
    392400                                } 
     
    406414        { 
    407415                // no face normals? => create normals 
    408                 const SimpleTri tri(vertices[i + 0], 
    409                             vertices[i + 1],  
    410                                                         vertices[i + 2]); 
    411  
    412                 const SimpleVec n = tri.GetNormal(); 
     416                const CHCDemoEngine::Triangle3 tri(vertices[i + 0], 
     417                                                       vertices[i + 1], 
     418                                                                                   vertices[i + 2]); 
     419 
     420                const CHCDemoEngine::Vector3 n = tri.GetNormal(); 
    413421 
    414422                normals.push_back(n); 
     
    433441        str.write(reinterpret_cast<char *>(&vertexCount), sizeof(int)); 
    434442   
    435         str.write(reinterpret_cast<char *>(geom->mVertices), sizeof(SimpleVec) * vertexCount); 
    436         str.write(reinterpret_cast<char *>(geom->mNormals), sizeof(SimpleVec) * vertexCount); 
     443        str.write(reinterpret_cast<char *>(geom->mVertices), sizeof(CHCDemoEngine::Vector3) * vertexCount); 
     444        str.write(reinterpret_cast<char *>(geom->mNormals), sizeof(CHCDemoEngine::Vector3) * vertexCount); 
    437445 
    438446        int texCoordCount = 0;//geom->mTexcoordCount; 
     
    463471        if (hasMaterial) 
    464472        { 
    465                 SimpleVec ambient, diffuse, spec, emm; 
     473                CHCDemoEngine::Vector3 ambient, diffuse, spec, emm; 
    466474 
    467475                ambient.x = ambient.y = ambient.z = 0.2f; 
     
    472480                diffuse.z = RandomColor(0.5f); 
    473481 
    474                 spec.x    = spec.y    = spec.z    = .0f; 
     482                spec.x = spec.y = spec.z = .0f; 
    475483                emm = spec; 
    476484 
    477485                // only write rgb part of the material 
    478                 str.write(reinterpret_cast<char *>(&ambient), sizeof(SimpleVec)); 
    479                 str.write(reinterpret_cast<char *>(&diffuse), sizeof(SimpleVec)); 
    480                 str.write(reinterpret_cast<char *>(&spec), sizeof(SimpleVec)); 
    481                 str.write(reinterpret_cast<char *>(&emm), sizeof(SimpleVec)); 
     486                str.write(reinterpret_cast<char *>(&ambient), sizeof(CHCDemoEngine::Vector3)); 
     487                str.write(reinterpret_cast<char *>(&diffuse), sizeof(CHCDemoEngine::Vector3)); 
     488                str.write(reinterpret_cast<char *>(&spec), sizeof(CHCDemoEngine::Vector3)); 
     489                str.write(reinterpret_cast<char *>(&emm), sizeof(CHCDemoEngine::Vector3)); 
    482490        } 
    483491} 
     
    550558        vector<TexCoord> _texCoords; 
    551559 
    552         for (size_t i = 0; i < mBvhNodes.size(); ++ i) 
    553         { 
    554                 BvhNode *node = mBvhNodes[i]; 
    555  
    556                 //for (size_t j = 0; j < node->mTriangleIds.size(); ++ j) 
     560        mGeometry.reserve(mBvhLeaves.size()); 
     561 
     562        mNumShapes = (int)mBvhLeaves.size(); 
     563 
     564        for (size_t i = 0; i < mBvhLeaves.size(); ++ i) 
     565        { 
     566                BvhLeaf *node = mBvhLeaves[i]; 
     567 
    557568                for (int j = node->first; j <= node->last; ++ j) 
    558569                { 
    559570                        const int idx = 3 * mGlobalTriangleIds[j]; 
    560  
    561                         //if (idx >= maxVertices * 3) continue; 
    562571 
    563572                        for (int k = 0; k < 3; ++ k) 
     
    569578                } 
    570579 
    571                 if (!_vertices.empty()) 
    572                 { 
    573                         ++ mNumShapes; 
    574                         LoadShape(_vertices, _normals, _texCoords); 
    575                 } 
     580                LoadShape(_vertices, _normals, _texCoords); 
     581                 
     582                node->geometry = mGeometry.back(); 
    576583 
    577584                _vertices.clear(); 
     
    610617        } 
    611618 
    612         //if (mRoot) delete mRoot; 
     619        const size_t numNodes = buffer[5]; 
    613620 
    614621        mNumNodes = 0; // this was set to 1 in constructor! 
     
    616623        mRoot = LoadNode(fr, 0); 
    617624 
    618         if (mNumNodes != buffer[5])  
     625        if (mNumNodes != numNodes)  
    619626        { 
    620627                cerr << "Warning: Loaded " << mNumNodes << 
     
    625632         
    626633        return true; 
     634} 
     635 
     636 
     637void VisibilitySolutionConverter::UpdateLeafBox(BvhLeaf *leaf) 
     638{ 
     639        leaf->box.Initialize(); 
     640 
     641        Geometry *geom = leaf->geometry; 
     642 
     643        for (size_t i = 0; i < geom->mVertexCount; ++ i) 
     644        { 
     645                CHCDemoEngine::Vector3 v = geom->mVertices[i]; 
     646                leaf->box.Include(v); 
     647        } 
    627648} 
    628649 
     
    655676                interior->front = front; 
    656677                interior->back = back; 
    657  
    658                 //interior->bmin = Minimize(front->bmin, back->bmin); 
    659                 //interior->bmax = Maximize(front->bmax, back->bmax); 
    660678         
    661679                return (BvhNode *)interior; 
     
    664682        { 
    665683                // leaf 
    666                 //Debug << "Info: Loading leaf (id: " << numberOfNodes << ", depth: " << depth << ")" << endl; 
    667684                BvhLeaf *leaf = new BvhLeaf(); 
    668685 
     
    673690 
    674691                leaf->depth = depth; 
    675                 //UpdateLeafBox(leaf); 
    676  
    677                 mBvhNodes.push_back(leaf); 
     692 
     693                mBvhLeaves.push_back(leaf); 
    678694         
    679695                return (BvhNode *)leaf; 
     
    765781        vertices.reserve(numTriangles * 3); 
    766782        cout << "loading " << numTriangles * 3 << " vertices ("  
    767                  << numTriangles * 3 * sizeof(SimpleVec) /      (1024 * 1024) << " MB)" << endl; 
     783                << numTriangles * 3 * sizeof(CHCDemoEngine::Vector3) / (1024 * 1024) << " MB)" << endl; 
    768784 
    769785        int i = 0; 
     
    771787        while (1) 
    772788        { 
    773                 SimpleVec v; 
    774                  
    775                 inStream.read(reinterpret_cast<char *>(&v), sizeof(SimpleVec)); 
     789                CHCDemoEngine::Vector3 v; 
     790                inStream.read(reinterpret_cast<char *>(&v), sizeof(CHCDemoEngine::Vector3)); 
    776791 
    777792                // end of file reached 
     
    810825        for (it = vertices.begin(); it != it_end; ++ it) 
    811826        { 
    812                 SimpleVec v = *it; 
    813                 ofile.write(reinterpret_cast<char *>(&v), sizeof(SimpleVec)); 
     827                CHCDemoEngine::Vector3 v = *it; 
     828                ofile.write(reinterpret_cast<char *>(&v), sizeof(CHCDemoEngine::Vector3)); 
    814829        } 
    815830 
     
    832847                nodeType = TYPE_INTERIOR; 
    833848                 
    834         cerr << "error: wrong node type: " << nodeType << endl; 
    835                  
    836849        stream.write(reinterpret_cast<char *>(&nodeType), sizeof(int)); 
    837850 
    838         SimpleVec bMin, bMax; 
     851        CHCDemoEngine::Vector3 bMin = node->box.Min(); 
     852        CHCDemoEngine::Vector3 bMax = node->box.Max(); 
    839853 
    840854        stream.write(reinterpret_cast<char *>(&(node->first)), sizeof(int)); 
    841855        stream.write(reinterpret_cast<char *>(&(node->last)), sizeof(int)); 
    842856 
    843         stream.write(reinterpret_cast<char *>(&node->bmin), sizeof(SimpleVec)); 
    844         stream.write(reinterpret_cast<char *>(&node->bmax), sizeof(SimpleVec)); 
     857        stream.write(reinterpret_cast<char *>(&bMin), sizeof(CHCDemoEngine::Vector3)); 
     858        stream.write(reinterpret_cast<char *>(&bMin), sizeof(CHCDemoEngine::Vector3)); 
     859} 
     860 
     861 
     862void VisibilitySolutionConverter::UpdateNodeBox(BvhNode *node) 
     863{ 
     864        if (!node->IsLeaf()) 
     865        { 
     866                BvhInterior *interior = (BvhInterior *)node; 
     867                node->box = Union(interior->front->box, interior->back->box); 
     868 
     869                UpdateNodeBox(interior->front); 
     870                UpdateNodeBox(interior->back); 
     871        } 
     872        else 
     873        { 
     874                UpdateLeafBox((BvhLeaf *)node); 
     875        } 
    845876} 
    846877 
  • GTP/trunk/App/Demos/Vis/FriendlyCulling/VisibilitySolutionConverter/VisibilitySolutionConverter.h

    r3240 r3241  
    33 
    44 
    5 #include "SimpleVec.h" 
     5#include "Vector3.h" 
     6#include "Triangle3.h" 
    67#include <string> 
    78#include <vector> 
    89#include "AxisAlignedBox3.h" 
    910 
     11         
    1012 
    11 typedef std::vector<SimpleVec> VertexArray; 
     13typedef std::vector<CHCDemoEngine::Vector3> VertexArray; 
    1214typedef std::pair<float, float> TexCoord; 
    1315 
     
    1517 
    1618struct BvhInterior; 
     19 
     20 
     21 
     22struct Geometry 
     23{ 
     24        CHCDemoEngine::Vector3 *mVertices; 
     25        CHCDemoEngine::Vector3 *mNormals; 
     26        TexCoord *mTexCoords; 
     27 
     28        int mVertexCount; 
     29        int mTexcoordCount; 
     30}; 
     31 
    1732 
    1833struct BvhNode 
     
    3651        int Count() const { return last - first + 1; } 
    3752 
    38         SimpleVec bmin; 
    39         SimpleVec bmax; 
     53        CHCDemoEngine::AxisAlignedBox3 box; 
    4054 
    4155        BvhInterior *parent; 
     
    5468struct BvhLeaf: public BvhNode 
    5569{ 
    56         //Color color; 
    57         int texture; 
     70        Geometry *geometry; 
    5871 
    59         BvhLeaf(): BvhNode(), texture(0) {} 
     72        BvhLeaf(): BvhNode(), geometry(NULL) {} 
    6073}; 
    6174 
     
    7992protected: 
    8093 
    81         struct Geometry 
    82         { 
    83                 SimpleVec *mVertices; 
    84                 SimpleVec *mNormals; 
    85                 TexCoord *mTexCoords; 
    86  
    87                 int mVertexCount; 
    88                 int mTexcoordCount; 
    89         }; 
    9094 
    9195        void LoadShape(const VertexArray &vertices, 
     
    136140        void WriteNextNode(ogzstream &stream, BvhNode *parent); 
    137141 
     142        void UpdateLeafBox(BvhLeaf *leaf); 
     143 
     144        void UpdateNodeBox(BvhNode *node); 
    138145 
    139146 
     
    146153        std::vector<int> mGlobalTriangleIds; 
    147154 
    148         std::vector<BvhNode *> mBvhNodes; 
     155        std::vector<BvhLeaf *> mBvhLeaves; 
    149156 
    150157        BvhNode *mRoot; 
Note: See TracChangeset for help on using the changeset viewer.