Changeset 3235


Ignore:
Timestamp:
12/26/08 02:44:45 (15 years ago)
Author:
mattausch
Message:
 
Location:
GTP/trunk/App/Demos/Vis/FriendlyCulling
Files:
11 edited
2 moved

Legend:

Unmodified
Added
Removed
  • GTP/trunk/App/Demos/Vis/FriendlyCulling/Converter.vcproj

    r3107 r3235  
    9898                <Configuration 
    9999                        Name="Release|Win32" 
    100                         OutputDirectory="$(SolutionDir)\Converter\$(ConfigurationName)" 
    101                         IntermediateDirectory="$(ConfigurationName)\Converter" 
     100                        OutputDirectory="$(SolutionDir)\$(ProjectName)\$(ConfigurationName)" 
     101                        IntermediateDirectory="$(ConfigurationName)\$(ProjectName)" 
    102102                        ConfigurationType="1" 
    103103                        CharacterSet="1" 
  • GTP/trunk/App/Demos/Vis/FriendlyCulling/FriendlyCulling.sln

    r3212 r3235  
    1717                {03661866-4093-4B02-B26A-028EA91AF023} = {03661866-4093-4B02-B26A-028EA91AF023} 
    1818        EndProjectSection 
     19EndProject 
     20Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "VisibilitySolutionConverter", "VisibilitySolutionConverter.vcproj", "{A07AEF7C-5A28-4881-AE56-F9C5FFA3FEC4}" 
    1921EndProject 
    2022Global 
     
    4446                {91680C49-A358-48AE-A02C-66CB884B7D7F}.Release|Win32.ActiveCfg = Release|Win32 
    4547                {91680C49-A358-48AE-A02C-66CB884B7D7F}.Release|Win32.Build.0 = Release|Win32 
     48                {A07AEF7C-5A28-4881-AE56-F9C5FFA3FEC4}.Debug|Win32.ActiveCfg = Debug|Win32 
     49                {A07AEF7C-5A28-4881-AE56-F9C5FFA3FEC4}.Debug|Win32.Build.0 = Debug|Win32 
     50                {A07AEF7C-5A28-4881-AE56-F9C5FFA3FEC4}.Release|Win32.ActiveCfg = Release|Win32 
     51                {A07AEF7C-5A28-4881-AE56-F9C5FFA3FEC4}.Release|Win32.Build.0 = Release|Win32 
    4652        EndGlobalSection 
    4753        GlobalSection(SolutionProperties) = preSolution 
  • GTP/trunk/App/Demos/Vis/FriendlyCulling/VboFormatConverter.vcproj

    r3220 r3235  
    9898                <Configuration 
    9999                        Name="Release|Win32" 
    100                         OutputDirectory="$(SolutionDir)$\VboFormatConverter\(ConfigurationName)" 
    101                         IntermediateDirectory="$(ConfigurationName)\VboFormatConverter" 
     100                        OutputDirectory="$(SolutionDir)$\$(ProjectName)\(ConfigurationName)" 
     101                        IntermediateDirectory="$(ConfigurationName)\$(ProjectName)" 
    102102                        ConfigurationType="1" 
    103103                        CharacterSet="1" 
  • GTP/trunk/App/Demos/Vis/FriendlyCulling/VisibilitySolutionConverter/VisibilitySolutionConverter.cpp

    r3234 r3235  
    1 #include "ObjConverter.h" 
     1#include "VisibilitySolutionConverter.h" 
    22#include "SimpleTri.h" 
    33#include "SimpleVec.h" 
     
    88using namespace std; 
    99 
    10 //#define USE_TEXTURE 
     10// MAGIC of all bin exports 
     11#ifndef MAGIC  
     12#define MAGIC 0x827923 
     13#endif 
     14 
     15#define BVH_VERSION 2.1 
    1116 
    1217 
     
    1419                                                const VertexArray &vertices,  
    1520                                                const VertexArray &normals,  
    16                                                 const vector<pair<float, float> > &texcoords, 
     21                                                const vector<pair<float, float> > &texCoords, 
    1722                                                VertexArray &faceVertices, 
    1823                                                VertexArray &faceNormals, 
    19                                                 vector<Texcoord> &faceTexcoords 
     24                                                vector<TexCoord> &faceTexCoords 
    2025                                                ) 
    2126{ 
     
    104109                        if (!normals.empty()) 
    105110                        { 
    106                                 //if (dummy < 3) cout << nIndices[idx1] << " " << nIndices[idx2] << " " << nIndices[idx3] << endl; 
    107111                                faceNormals.push_back(normals[nIndices[idx1]]); 
    108112                                faceNormals.push_back(normals[nIndices[idx2]]); 
     
    115119                                                        vertices[indices[idx2]],  
    116120                                                                        vertices[indices[idx3]]); 
     121 
    117122                                const SimpleVec n = tri.GetNormal(); 
    118123 
     
    122127                        } 
    123128 
    124                         ++ dummy; 
    125                         if (!texcoords.empty()) 
     129                        if (!texCoords.empty()) 
    126130                        { 
    127                                 faceTexcoords.push_back(texcoords[tIndices[idx1]]); 
    128                                 faceTexcoords.push_back(texcoords[tIndices[idx2]]); 
    129                                 faceTexcoords.push_back(texcoords[tIndices[idx3]]); 
     131                                faceTexCoords.push_back(texCoords[tIndices[idx1]]); 
     132                                faceTexCoords.push_back(texCoords[tIndices[idx2]]); 
     133                                faceTexCoords.push_back(texCoords[tIndices[idx3]]); 
    130134                        } 
    131135                } 
     
    134138 
    135139 
    136 ObjConverter::ObjConverter() 
     140VisibilitySolutionConverter::VisibilitySolutionConverter() 
    137141{} 
    138142 
    139143 
    140 ObjConverter::~ObjConverter() 
     144VisibilitySolutionConverter::~VisibilitySolutionConverter() 
    141145{ 
    142146        for (size_t i = 0; i < mGeometry.size(); ++ i) 
     
    144148                delete [] mGeometry[i]->mVertices; 
    145149                delete [] mGeometry[i]->mNormals; 
    146                 delete [] mGeometry[i]->mTexcoords; 
     150                delete [] mGeometry[i]->mTexCoords; 
    147151 
    148152                delete mGeometry[i]; 
     
    153157 
    154158 
    155 void ObjConverter::LoadShape(const VertexArray &faceVertices, 
    156                                                          const VertexArray &faceNormals, 
    157                                                          const vector<Texcoord> &faceTexcoords) 
    158 { 
    159         int numElements = (int)faceVertices.size(); 
     159void VisibilitySolutionConverter::LoadShape(const VertexArray &vertices, 
     160                                                                                        const VertexArray &normals, 
     161                                                                                        const vector<TexCoord> &texCoords) 
     162{ 
     163        int numElements = (int)vertices.size(); 
    160164        Geometry *geom = new Geometry(); 
    161165 
     
    163167        geom->mVertices = new SimpleVec[numElements]; 
    164168        geom->mNormals = new SimpleVec[numElements]; 
    165         geom->mTexcoords = new Texcoord[numElements]; 
     169        geom->mTexCoords = new TexCoord[numElements]; 
    166170 
    167171        geom->mVertexCount = numElements; 
    168         geom->mTexcoordCount = (int)faceTexcoords.size(); 
     172        geom->mTexcoordCount = (int)texCoords.size(); 
    169173 
    170174        cout << "number of vertices=" << numElements << endl; 
     
    172176        for (int i = 0; i < numElements; ++ i) 
    173177        { 
    174 #if 0 
     178#if 1 
    175179                // convert to our camera system: change y and z 
    176                 geom->mVertices[i].x = faceVertices[i].x; 
    177                 geom->mVertices[i].y = -faceVertices[i].z; 
    178                 geom->mVertices[i].z = faceVertices[i].y; 
    179          
    180                 geom->mNormals[i].x = faceNormals[i].x; 
    181                 geom->mNormals[i].y = -faceNormals[i].z; 
    182                 geom->mNormals[i].z = faceNormals[i].y; 
     180                geom->mVertices[i].x =  vertices[i].x; 
     181                geom->mVertices[i].y = -vertices[i].z; 
     182                geom->mVertices[i].z =  vertices[i].y; 
     183         
     184                geom->mNormals[i].x =  normals[i].x; 
     185                geom->mNormals[i].y = -normals[i].z; 
     186                geom->mNormals[i].z =  normals[i].y; 
    183187 
    184188#else 
    185                 geom->mVertices[i].x = faceVertices[i].x; 
    186                 geom->mVertices[i].y = faceVertices[i].y; 
    187                 geom->mVertices[i].z = faceVertices[i].z; 
    188          
    189                 geom->mNormals[i].x = faceNormals[i].x; 
    190                 geom->mNormals[i].y = faceNormals[i].y; 
    191                 geom->mNormals[i].z = faceNormals[i].z; 
     189                geom->mVertices[i].x = vertices[i].x; 
     190                geom->mVertices[i].y = vertices[i].y; 
     191                geom->mVertices[i].z = vertices[i].z; 
     192         
     193                geom->mNormals[i].x = normals[i].x; 
     194                geom->mNormals[i].y = normals[i].y; 
     195                geom->mNormals[i].z = normals[i].z; 
    192196 
    193197#endif 
     
    195199                if (i < geom->mTexcoordCount) 
    196200                { 
    197                         geom->mTexcoords[i].first = faceTexcoords[i].first; 
    198                         geom->mTexcoords[i].second = faceTexcoords[i].second; 
     201                        geom->mTexCoords[i].first = texCoords[i].first; 
     202                        geom->mTexCoords[i].second = texCoords[i].second; 
    199203                } 
    200204        } 
     
    204208 
    205209 
    206 bool ObjConverter::Convert(const string &filename, const std::string &outputFilename) 
     210bool VisibilitySolutionConverter::Convert(const std::string &sceneInputFilename, 
     211                                                                                  const std::string &sceneOutputFilename, 
     212                                                                                  const std::string &bvhInputFilename, 
     213                                                                                  const std::string &bvhOutputFilename) 
    207214{ 
    208215        mNumShapes = 0; 
    209216         
     217        // delete previous geometry 
    210218        for (size_t i = 0; i < mGeometry.size(); ++ i) 
    211219        { 
    212220                delete [] mGeometry[i]->mVertices; 
    213221                delete [] mGeometry[i]->mNormals; 
    214                 delete [] mGeometry[i]->mTexcoords; 
     222                delete [] mGeometry[i]->mTexCoords; 
    215223 
    216224                delete mGeometry[i]; 
     
    219227        mGeometry.clear(); 
    220228 
    221         if (!ReadFile(filename)) 
     229        if (!LoadSolution(bvhInputFilename)) 
    222230        { 
    223231                cerr << "could not read file" << endl; 
     
    225233        } 
    226234 
    227         if (!WriteFile(outputFilename)) 
     235        if (!ReadScene(sceneInputFilename)) 
     236        { 
     237                cerr << "could not read file" << endl; 
     238                return false; 
     239        } 
     240 
     241        if (!WriteScene(sceneOutputFilename)) 
    228242        { 
    229243                cerr << "could not write file" << endl; 
     
    231245        } 
    232246 
     247        /*if (!WriteBvh(bvhOutputFilename)) 
     248        { 
     249                cerr << "could not write file" << endl; 
     250                return false; 
     251        }*/ 
    233252 
    234253        return true; 
     
    236255 
    237256 
    238 bool ObjConverter::ReadFile(const string &filename)  
     257bool VisibilitySolutionConverter::ReadScene(const string &filename)  
    239258{ 
    240259        FILE *file; 
     
    247266        VertexArray faceVertices;  
    248267        VertexArray faceNormals; 
    249         vector<Texcoord> faceTexcoords; 
     268        vector<TexCoord> faceTexcoords; 
    250269 
    251270        VertexArray vertices;  
    252271        VertexArray normals; 
    253         vector<Texcoord> texcoords; 
     272        vector<TexCoord> texcoords; 
    254273 
    255274        vector<int> indices; 
     
    261280        while (fgets(str, len, file) != NULL) 
    262281        { 
     282                if (line % 1000000 == 0) 
     283                        cout << line << " " << str << endl; 
     284 
     285                ++ line; 
     286 
    263287                switch (str[0]) 
    264288                { 
     
    266290                        { 
    267291                                float x, y, z; 
     292 
     293                                if (vertices.size() >= 1000000 * 3) continue; 
    268294 
    269295                                switch (str[1])  
     
    280306                                        sscanf(str + 1, "%f %f %f", &x, &y, &z); 
    281307                                        //const float scale = 5e-3f; 
    282                                         const float scale = 1.0f; 
    283                                         vertices.push_back(SimpleVec(x * scale, y * scale, z* scale)); 
     308                                        const float scale = 0.1f; 
     309                                        vertices.push_back(SimpleVec(x * scale, y * scale, z * scale)); 
    284310                                        //cout <<"v " << x << " " << y << " "<< z << " "; 
    285311                                } 
     
    291317                                //-- indices in the current line 
    292318 
     319                                if (faceVertices.size() >= 1000000 * 3) continue; 
     320 
    293321                                LoadIndices(str,  
    294322                                                vertices, normals, texcoords,  
    295323                                                faceVertices, faceNormals, faceTexcoords); 
    296324 
    297                                 if (0 && 
    298                                         (((line % 1000) == 999) && !faceVertices.empty())) 
    299                                 { 
    300                                         ++ mNumShapes; 
    301  
    302                                         LoadShape(faceVertices, faceNormals, faceTexcoords); 
    303  
    304                                         faceVertices.clear(); 
    305                                         faceNormals.clear(); 
    306                                         faceTexcoords.clear(); 
    307                                 } 
    308  
    309325                                break; 
    310                         }   // end face 
    311                 case 'g': // load a new shape 
    312                         {/* 
    313                                 if (!faceVertices.empty()) 
    314                                 { 
    315                                         ++ mNumShapes; 
    316  
    317                                         LoadShape(faceVertices, faceNormals, faceTexcoords); 
    318  
    319                                         faceVertices.clear(); 
    320                                         faceNormals.clear(); 
    321                                         faceTexcoords.clear(); 
    322                                 }*/ 
    323                         } 
     326                        }   
    324327                        break; 
    325328                default: 
     
    327330                        break; 
    328331                } 
    329  
    330                 ++ line; 
    331332        } 
    332333 
     
    335336                ++ mNumShapes; 
    336337 
    337                 LoadShape(faceVertices, faceNormals, faceTexcoords); 
     338                ConstructBvhObjects(faceVertices, faceNormals, faceTexcoords); 
    338339 
    339340                faceVertices.clear(); 
     
    348349 
    349350 
    350 void ObjConverter::WriteGeometry(ogzstream &str, Geometry *geom) 
     351static inline float RandomColor(float x) 
     352{ 
     353        return x + (1.0f - x) * (float)rand() / RAND_MAX;  
     354} 
     355 
     356 
     357void VisibilitySolutionConverter::WriteGeometry(ogzstream &str, Geometry *geom) 
    351358{ 
    352359        int vertexCount = geom->mVertexCount; 
     
    360367 
    361368        if (texCoordCount) 
    362                 str.write(reinterpret_cast<char *>(geom->mTexcoords), sizeof(float) * texCoordCount * 2); 
    363          
     369        { 
     370                str.write(reinterpret_cast<char *>(geom->mTexCoords), sizeof(float) * texCoordCount * 2); 
     371        } 
    364372 
    365373        /////// 
     
    392400                ambient.x = ambient.y = ambient.z = 0.2f; 
    393401                //diffuse.x = diffuse.y = diffuse.z = 1.0f; 
    394                 diffuse.x = 0.7f; diffuse.y = 0.5f; diffuse.z = 0.2f; 
     402 
     403                diffuse.x = RandomColor(0.5f);  
     404                diffuse.y = RandomColor(0.5f); 
     405                diffuse.z = RandomColor(0.5f); 
     406 
    395407                spec.x    = spec.y    = spec.z    =  .0f; 
    396408                emm = spec; 
     
    405417 
    406418 
    407 bool ObjConverter::WriteFile(const string &filename) 
     419bool VisibilitySolutionConverter::WriteScene(const string &filename) 
    408420{ 
    409421        ogzstream ofile(filename.c_str()); 
     
    482494        return true; 
    483495} 
     496 
     497 
     498void VisibilitySolutionConverter::ConstructBvhObjects(const VertexArray &vertices, 
     499                                                                                                          const VertexArray &normals, 
     500                                                                                                          const vector<TexCoord> &texCoords) 
     501{ 
     502        cout << "vtx: " << vertices.size() << endl; 
     503/*      BvhNode n; 
     504         
     505        mGlobalTriangleIds.clear(); 
     506        for (size_t i = 0; i < vertices.size() / 3; ++ i) 
     507        { 
     508                mGlobalTriangleIds.push_back((int)i); 
     509        } 
     510 
     511        for (size_t i = 0; i < vertices.size() / 3; ++ i) 
     512                n.mTriangleIds.push_back((int)i); 
     513 
     514        mBvhNodes.push_back(n); 
     515*/ 
     516        for (size_t i = 0; i < mBvhNodes.size(); ++ i) 
     517        { 
     518                VertexArray _vertices; 
     519                VertexArray _normals; 
     520                vector<TexCoord> _texCoords; 
     521 
     522                BvhNode *node = mBvhNodes[i]; 
     523 
     524                //for (size_t j = 0; j < node->mTriangleIds.size(); ++ j) 
     525                for (int j = node->first; j < node->last; ++ j) 
     526                { 
     527                        const int idx = 3 * mGlobalTriangleIds[j]; 
     528 
     529                        //cout << "idx: " << idx << " "; 
     530 
     531                        if (idx < 1000000 * 3) 
     532                        { 
     533                                for (int k = 0; k < 3; ++ k) 
     534                                { 
     535                                        _vertices.push_back(vertices[idx + k]); 
     536                                        _normals.push_back(normals[idx + k]); 
     537                                        //_texCoords.push_back(texCoords[idx + k]); 
     538                                } 
     539                        } 
     540                } 
     541 
     542                LoadShape(_vertices, _normals, _texCoords); 
     543        } 
     544} 
     545 
     546 
     547bool VisibilitySolutionConverter::WriteBvh(const string &filename) 
     548{ 
     549        ogzstream ofile(filename.c_str()); 
     550 
     551        if (!ofile.is_open()) 
     552                return false; 
     553         
     554 
     555        ///////// 
     556        //-- write textures 
     557 
     558#ifdef USE_TEXTURE 
     559        int textureCount = 1; 
     560#else 
     561        int textureCount = 0; 
     562#endif 
     563        ofile.write(reinterpret_cast<char *>(&textureCount), sizeof(int)); 
     564 
     565        if (textureCount > 0) 
     566        { 
     567                // hack 
     568                const string texName("wood.jpg"); 
     569 
     570                int texnameSize = (int)texName.length() + 1; 
     571                ofile.write(reinterpret_cast<char *>(&texnameSize), sizeof(int)); 
     572 
     573                ofile.write(texName.c_str(), sizeof(char) * texnameSize); 
     574 
     575                int boundS = 1, boundT = 1; 
     576 
     577                ofile.write(reinterpret_cast<char *>(&boundS), sizeof(int)); 
     578                ofile.write(reinterpret_cast<char *>(&boundT), sizeof(int)); 
     579        } 
     580 
     581 
     582        /////////// 
     583        //-- write shapes 
     584 
     585        ofile.write(reinterpret_cast<char *>(&mNumShapes), sizeof(int)); 
     586 
     587        vector<Geometry *>::const_iterator it, it_end = mGeometry.end(); 
     588 
     589        for (it = mGeometry.begin(); it != it_end; ++ it) 
     590        { 
     591                WriteGeometry(ofile, *it); 
     592        } 
     593 
     594 
     595        int entityCount = 1; 
     596        ofile.write(reinterpret_cast<char *>(&entityCount), sizeof(int)); 
     597 
     598 
     599        ////////// 
     600        //-- write single scene entity 
     601 
     602        // no transformation 
     603        bool hasTrafo = false; 
     604        ofile.write(reinterpret_cast<char *>(&hasTrafo), sizeof(bool)); 
     605 
     606        // a dummy lod 
     607        int numLODs = 1; 
     608        ofile.write(reinterpret_cast<char *>(&numLODs), sizeof(int)); 
     609 
     610        float dist = 0; 
     611        ofile.write(reinterpret_cast<char *>(&dist), sizeof(float)); 
     612 
     613        ofile.write(reinterpret_cast<char *>(&mNumShapes), sizeof(int)); 
     614 
     615        // all shapes belong to this scene entity 
     616        for (int i = 0; i < mNumShapes; ++ i) 
     617        { 
     618                int shapeId = i; 
     619                ofile.write(reinterpret_cast<char *>(&shapeId), sizeof(int)); 
     620        } 
     621 
     622        return true; 
     623} 
     624 
     625 
     626bool VisibilitySolutionConverter::ReadBvh(FILE *fr)  
     627{ 
     628        int buffer[6];  
     629        fread(buffer, sizeof(int), 6, fr); 
     630 
     631        if (buffer[0] != MAGIC)  
     632        { 
     633                cerr << "Error: Wrong file type" << endl; 
     634                return false; 
     635        } 
     636 
     637        if (buffer[1] != (int)(1000 * BVH_VERSION))  
     638        { 
     639                cerr << "Error: Wrong BVH version" << endl; 
     640                return false; 
     641        } 
     642 
     643        // load triangle ids 
     644        size_t numTriangles = buffer[2]; 
     645 
     646        for (size_t i = 0; i < numTriangles; ++i)  
     647        { 
     648                int id; 
     649                fread(&id, sizeof(int), 1, fr); 
     650                mGlobalTriangleIds[i] = id; 
     651                //triangles[i] = scene->triangles[id]; 
     652        } 
     653 
     654        //if (mRoot) delete mRoot; 
     655 
     656        mNumNodes = 0; // this was set to 1 in constructor! 
     657 
     658        mRoot = LoadNode(fr, 0); 
     659 
     660        if (mNumNodes != buffer[5])  
     661        { 
     662                cerr << "Warning: Loaded " << mNumNodes << 
     663                            " bvh nodes instead of " << buffer[5] << endl; 
     664        } 
     665 
     666        fclose(fr); 
     667         
     668        return true; 
     669} 
     670 
     671 
     672BvhNode *VisibilitySolutionConverter::LoadNode(FILE *fr, int depth) 
     673{ 
     674        ++ mNumNodes; 
     675         
     676        int buffer[4]; 
     677        fread(buffer, sizeof(int), 4, fr); 
     678 
     679        if (buffer[2] != -1)  
     680        { 
     681                BvhInterior *interior = new BvhInterior(); 
     682                 
     683                interior->first = buffer[0]; 
     684                interior->last  = buffer[1]; 
     685                interior->axis  = buffer[2]; 
     686                interior->id    = buffer[3]; 
     687                interior->depth = depth; 
     688 
     689                BvhNode *front, *back; 
     690 
     691                front = LoadNode(fr, depth + 1); 
     692                back  = LoadNode(fr, depth + 1); 
     693 
     694                // front->parent = (BvhNode *)interior; 
     695                // back->parent = (BvhNode *)interior; 
     696                interior->front = front; 
     697                interior->back = back; 
     698 
     699                //interior->box = Union(front->box, back->box); 
     700         
     701                return (BvhNode *)interior; 
     702        } 
     703        else  
     704        { 
     705                // leaf 
     706                //Debug << "Info: Loading leaf (id: " << numberOfNodes << ", depth: " << depth << ")" << endl; 
     707                BvhLeaf *leaf = new BvhLeaf(); 
     708                leaf->first = buffer[0]; 
     709                leaf->last  = buffer[1]; 
     710                leaf->axis  = buffer[2]; 
     711                leaf->id  = buffer[3]; 
     712 
     713                leaf->depth = depth; 
     714                //UpdateLeafBox(leaf); 
     715 
     716                mBvhNodes.push_back(leaf); 
     717                // leaf->id = numberOfNodes; 
     718                return (BvhNode *)leaf; 
     719        } 
     720} 
     721 
     722 
     723bool VisibilitySolutionConverter::ReadDummyTree(FILE *fr) 
     724{ 
     725        int buffer[256];  
     726        fread(buffer, sizeof(int), 3, fr); 
     727 
     728        // read dummy bounding box 
     729        float dummy[6]; 
     730        fread(dummy, sizeof(float) * 6, 1, fr); 
     731 
     732        int stack = 1; 
     733 
     734        // read dummy tree 
     735        while (stack) 
     736        { 
     737                int axis; 
     738                fread(&axis, sizeof(int), 1, fr); 
     739                -- stack; 
     740 
     741                if (axis != - 1)  
     742                { 
     743                        float dummy; 
     744                        fread(&dummy, sizeof(float), 1, fr); 
     745                         
     746                        stack += 2; 
     747                } 
     748        } 
     749   
     750        return true; 
     751} 
     752 
     753 
     754 
     755bool VisibilitySolutionConverter::LoadSolution(const string &filename) 
     756{ 
     757        FILE *fr = fopen(filename.c_str(), "rb"); 
     758 
     759        cerr << "Info: Loading visibility solution from file '" + filename + "'" << endl; 
     760   
     761        if (fr == NULL)  
     762        { 
     763                cerr << "Error: Cannot open file for reading" << endl; 
     764                return false; 
     765        } 
     766 
     767        float totalSamples, totalTime; 
     768 
     769        fread(&totalSamples, sizeof(float), 1, fr); 
     770        fread(&totalTime, sizeof(float), 1, fr); 
     771 
     772        //viewCellsTree = new ViewCellsTree; 
     773        //bool ok = viewCellsTree->_LoadFromFile(fr); 
     774 
     775        bool ok = ReadDummyTree(fr); 
     776 
     777        if (ok)  
     778        { 
     779                //AllocateLeafViewCells(); 
     780                 
     781                //objectsTree = new Bvh(*scene); 
     782                //ok = objectsTree->_LoadFromFile(fr); 
     783                ok = ReadBvh(fr); 
     784 
     785                /*if (ok)  
     786                {  
     787                    AllocatePvsObjects(); 
     788                        ok = LoadPvs(fr); 
     789                } 
     790                */ 
     791        } 
     792 
     793        fclose(fr); 
     794         
     795        if (ok)  
     796                cout << "Info: visibility solution loaded" << endl; 
     797        else 
     798                cout << "Info: loading visibility solution failed" << endl; 
     799   
     800        return true; 
     801} 
     802 
     803 
     804#if 0 
     805bool VisibilitySolution::LoadPvs(FILE *fw) 
     806 
     807        int number, entries; 
     808   
     809        fread(&number, sizeof(int), 1, fw); 
     810 
     811        if (number == 0)  
     812        { 
     813                Message("Info: Warning empty PVSs in visibility solution"); 
     814                return true; 
     815        } 
     816 
     817        if (number != viewCells.size())  
     818        { 
     819                Message("Info: Number of view cells does not match when loading PVSs!"); 
     820                return false; 
     821        } 
     822 
     823        for (int i=0; i < number; i++)  
     824        { 
     825                fread(&entries, sizeof(int), 1, fw); 
     826                 
     827                for (int j=0; j < entries; j++)  
     828                { 
     829                        int objectId; 
     830                        float time; 
     831                        fread(&objectId, sizeof(int), 1, fw); 
     832                        fread(&time, sizeof(float), 1, fw); 
     833                        viewCells[i]->pvs.Insert(objectId, time); 
     834                } 
     835        } 
     836        return true; 
     837} 
     838 
     839 
     840bool ViewCellsTree::_LoadFromFile(FILE *fr) 
     841{ 
     842        int buffer[256];  
     843        fread(buffer, sizeof(int), 3, fr); 
     844 
     845        if (buffer[0] != MAGIC)  
     846        { 
     847                Message( "Error: Wrong file type"); 
     848                return false; 
     849        } 
     850 
     851        if (buffer[1] != (int)(1000*VIEWCELLS_VERSION))  
     852        { 
     853                Message( "Error: Wrong viewcells version" ); 
     854                 
     855                return false; 
     856        } 
     857 
     858        // get the bounding box 
     859        fread(&box, sizeof(AxisAlignedBox3), 1, fr); 
     860 
     861        stack<ViewCellsTreeNode **> nodeStack; 
     862        nodeStack.push(&root); 
     863 
     864        while(!nodeStack.empty())  
     865        { 
     866                ViewCellsTreeNode *&node = *nodeStack.top(); 
     867                 
     868                nodeStack.pop(); 
     869                node = new ViewCellsTreeNode; 
     870                 
     871                fread(&node->axis, sizeof(int), 1, fr); 
     872                 
     873                if (!node->IsLeaf())  
     874                { 
     875                        fread(&node->position, sizeof(float), 1, fr); 
     876                         
     877                        nodeStack.push(&node->front); 
     878                        nodeStack.push(&node->back); 
     879                } 
     880        } 
     881   
     882        return true; 
     883} 
     884#endif 
  • GTP/trunk/App/Demos/Vis/FriendlyCulling/VisibilitySolutionConverter/VisibilitySolutionConverter.h

    r3234 r3235  
    1 #ifndef _OBJCONVERTER_H 
    2 #define _OBJCONVERTER_H 
     1#ifndef _VISIBILITYSOLUTIONCONVERTER_H 
     2#define _VISIBILITYSOLUTIONCONVERTER_H 
     3 
    34 
    45#include "SimpleVec.h" 
     
    67#include <vector> 
    78 
    8 /*struct SimpleVec 
    9 { 
    10         SimpleVec() {} 
    11         SimpleVec(float _x, float _y, float _z): x(_x), y(_y), z(_z) {} 
    129 
    13         float x, y, z; 
    14 };*/ 
    1510 
    1611typedef std::vector<SimpleVec> VertexArray; 
    17 typedef std::pair<float, float> Texcoord; 
     12typedef std::pair<float, float> TexCoord; 
    1813 
    1914class ogzstream; 
    2015 
     16 
     17struct BvhNode 
     18{ 
     19        int id; 
     20        char axis; 
     21        unsigned char depth; 
     22        short flags; 
     23 
     24        // indices to first and last triangle in the triangle array 
     25        // assumes the traingle are placed in continuous chunk of memory 
     26        // however this need not be a global array! 
     27        int first; 
     28        // one after the last triangle! 
     29        int last; 
     30 
     31        BvhNode(): axis(-1), first(-1), flags(0) {} 
     32 
     33        bool IsLeaf() { return axis == -1; } 
     34        bool Empty() const { return first == -1; } 
     35        int Count() const { return last - first + 1; } 
     36 
     37        std::vector<int> mTriangleIds; 
     38}; 
     39 
     40 
     41struct BvhInterior: public BvhNode 
     42{ 
     43        BvhInterior():back(NULL), front(NULL) { } 
     44        BvhNode *back; 
     45        BvhNode *front; 
     46}; 
     47 
     48 
     49struct BvhLeaf: public BvhNode 
     50{ 
     51        //Color color; 
     52        int texture; 
     53 
     54        BvhLeaf(): BvhNode(), texture(0) {} 
     55}; 
     56 
     57 
    2158/** Converts obj format into objects readable by our format 
    2259*/ 
    23 class ObjConverter 
     60class VisibilitySolutionConverter 
    2461{ 
    2562public: 
    2663 
    27         ObjConverter(); 
     64        VisibilitySolutionConverter(); 
    2865 
    29         bool Convert(const std::string &inputFilename,  
    30                          const std::string &outputFilename); 
    31         // const std::string textureFilename) const; 
     66        ~VisibilitySolutionConverter(); 
    3267 
    33         ~ObjConverter(); 
     68        bool Convert(const std::string &sceneInputFilename, 
     69                                 const std::string &sceneOutputFilename, 
     70                                 const std::string &bvhInputFilename, 
     71                         const std::string &bvhOutputFilename); 
     72         
     73 
    3474 
    3575protected: 
     
    3979                SimpleVec *mVertices; 
    4080                SimpleVec *mNormals; 
    41                 Texcoord *mTexcoords; 
     81                TexCoord *mTexCoords; 
    4282 
    4383                int mVertexCount; 
     
    4585        }; 
    4686 
    47         void LoadShape(const VertexArray &faceVertices, 
    48                            const VertexArray &faceNormals, 
    49                                    const std::vector<Texcoord> &faceTexcoords); 
     87        void LoadShape(const VertexArray &vertices, 
     88                           const VertexArray &normals, 
     89                                   const std::vector<TexCoord> &texCoords); 
    5090 
    5191        void WriteGeometry(ogzstream &str, Geometry *geom); 
    5292 
    53         bool ReadFile(const std::string &inputFilename); 
    54         bool WriteFile(const std::string &outputFilename); 
     93        bool ReadScene(const std::string &iFilename); 
    5594 
     95        bool WriteScene(const std::string &oFilename); 
     96 
     97        bool ReadBvh(FILE *fr); 
     98 
     99        bool WriteBvh(const std::string &oFilename); 
     100 
     101        void ConstructBvhObjects(const VertexArray &vertices, 
     102                                     const VertexArray &normals, 
     103                                                         const std::vector<TexCoord> &texCoords); 
     104 
     105        bool LoadSolution(const std::string &filename); 
     106 
     107 
     108        BvhNode *LoadNode(FILE *fr, int depth); 
     109 
     110        bool ReadDummyTree(FILE *fr); 
     111 
     112 
     113 
     114        ////////////////////////////////// 
    56115 
    57116        std::vector<Geometry *> mGeometry; 
    58117 
    59118        int mNumShapes; 
     119 
     120        std::vector<int> mGlobalTriangleIds; 
     121 
     122        std::vector<BvhNode *> mBvhNodes; 
     123 
     124        BvhNode *mRoot; 
     125 
     126        int mNumNodes; 
    60127}; 
    61128 
  • GTP/trunk/App/Demos/Vis/FriendlyCulling/VisibilitySolutionConverter/main.cpp

    r3234 r3235  
    1 #include "ObjConverter.h" 
     1#include "VisibilitySolutionConverter.h" 
    22#include <iostream> 
    33#include <time.h> 
     
    1111        int returnCode = 0; 
    1212 
    13         ObjConverter converter; 
     13        VisibilitySolutionConverter converter; 
    1414 
    1515        cout << "converting obj to dem format" << endl; 
    1616 
    17         if (!converter.Convert(model_path + argv[1], model_path + argv[2])) 
     17        if (!converter.Convert(model_path + argv[1], model_path + argv[2], model_path + argv[3], model_path + argv[4])) 
    1818        { 
    1919                cerr << "conversion failed" << endl; 
     
    2222                exit(0); 
    2323        } 
    24         //std::cin.get(); 
     24         
     25        std::cin.get(); 
    2526        cout << "conversion successful" << endl; 
    2627 
  • GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Bvh.cpp

    r3214 r3235  
    13261326        { 
    13271327                while (mGeometry[i]->GetWorldCenter()[axis] < position) ++ i; 
     1328                //while ((j > 0) && (position < mGeometry[j]->GetWorldCenter()[axis])) -- j; 
    13281329                while (position < mGeometry[j]->GetWorldCenter()[axis]) -- j; 
    13291330 
  • GTP/trunk/App/Demos/Vis/FriendlyCulling/src/DeferredRenderer.cpp

    r3233 r3235  
    301301mSunVisiblePixels(0), 
    302302mSavedFrameNumber(-1), 
    303 mSavedFrameSuffix("") 
     303mSavedFrameSuffix(""), 
     304mMaxDistance(1e6f) 
    304305{ 
    305306        /////////// 
     
    488489        //////////////// 
    489490         
    490         string dofParams[] = {"colorsTex", "filterOffs", "sceneRange"}; 
    491  
    492         sCgDOFProgram->AddParameters(dofParams, 0, 3); 
     491        string dofParams[] = {"colorsTex", "filterOffs", "sceneRange", "zFocus"}; 
     492 
     493        sCgDOFProgram->AddParameters(dofParams, 0, 4); 
    493494 
    494495 
     
    520521        // todo matt: it is stupid to put num samples and width of kernel into constructor => change this!!! 
    521522        PoissonDiscSampleGenerator2D poisson3(NUM_DOF_TABS, 1.0f); 
    522         poisson2.Generate((float *)dofSamples); 
     523        poisson3.Generate((float *)dofSamples); 
     524 
     525        for (int i = 0; i < NUM_DOF_TABS; ++ i) 
     526        { 
     527                dofSamples[i].x *= 1.0f / mWidth; 
     528                dofSamples[i].y *= 1.0f / mHeight; 
     529        } 
    523530 
    524531        //float dofWeights[NUM_PCF_TABS]; 
     
    567574        } 
    568575 
     576        /// do depth of field 
     577        DepthOfField(fbo); 
     578 
    569579        if (useToneMapping) 
    570580        { 
     
    574584                ToneMap(fbo, imageKey, whiteLum, middleGrey); 
    575585        } 
    576  
    577  
    578         /// do depth of field 
    579         DepthOfField(fbo); 
    580586 
    581587        /// compute lense flare 
     
    13121318        FlipFbos(fbo); 
    13131319 
    1314         float sceneRange = 1.0f; 
    1315  
    13161320        int i = 0; 
     1321 
     1322        const float zFocus = 7.0f; 
    13171323 
    13181324        sCgDOFProgram->SetTexture(i ++, colorsTex); 
    13191325        sCgDOFProgram->SetArray2f(i ++, (float *)dofSamples, NUM_DOF_TABS); 
    1320         sCgDOFProgram->SetValue1f(i ++, mCamera->GetFar() - mCamera->GetNear()); 
     1326        sCgDOFProgram->SetValue1f(i ++, min(mCamera->GetFar(), mMaxDistance) - mCamera->GetNear()); 
     1327        sCgDOFProgram->SetValue1f(i ++, zFocus); 
    13211328 
    13221329        DrawQuad(sCgDOFProgram); 
     
    14421449 
    14431450 
     1451void DeferredRenderer::SetMaxDistance(float maxDist)  
     1452{ 
     1453        mMaxDistance = maxDist; 
     1454} 
     1455 
     1456 
    14441457void DeferredRenderer::SetSaveFrame(const string &suffix, int frameNumber) 
    14451458{ 
  • GTP/trunk/App/Demos/Vis/FriendlyCulling/src/DeferredRenderer.h

    r3232 r3235  
    8888        void SetSaveFrame(const std::string &suffix, int frameNumber); 
    8989 
     90        void SetMaxDistance(float maxDist); 
     91 
    9092 
    9193        // hack: store the color buffer idx for the currently used flip flop-MRT here 
     
    158160 
    159161 
     162 
    160163        //////////// 
    161164 
     
    205208        int mSavedFrameNumber; 
    206209        std::string mSavedFrameSuffix; 
     210 
     211        float mMaxDistance; 
    207212}; 
    208213 
  • GTP/trunk/App/Demos/Vis/FriendlyCulling/src/SampleGenerator.cpp

    r3234 r3235  
    109109        } 
    110110 
    111         cout << "minDist after= " << (float)minDist / mNumSamples<< " #tries: " << tries << endl; 
     111        //cout << "minDist after= " << (float)minDist / mNumSamples<< " #tries: " << tries << endl; 
    112112} 
    113113 
  • GTP/trunk/App/Demos/Vis/FriendlyCulling/src/chcdemo.cpp

    r3227 r3235  
    541541        //LoadModel("fisch.dem", dynamicObjects); 
    542542        LoadModel("hbuddha.dem", dynamicObjects); 
     543 
    543544        //LoadModel("venusm.dem", dynamicObjects); 
    544545        //LoadModel("camel.dem", dynamicObjects); 
     
    575576                dynamicObjects.push_back(ent); 
    576577        } 
     578 
     579 
     580        //LoadModel("vienna_full_hp.dem", dynamicObjects); 
     581        //dynamicObjects.back()->GetTransform()->SetMatrix(transl); 
    577582 
    578583 
     
    22762281                { 
    22772282                        // usethe maximal visible distance to focus shadow map 
    2278                         float maxVisibleDist = min(camera->GetFar(), traverser->GetMaxVisibleDistance()); 
    2279                         RenderShadowMap(maxVisibleDist); 
     2283                        const float newFar = min(camera->GetFar(), traverser->GetMaxVisibleDistance()); 
     2284                        RenderShadowMap(newFar); 
    22802285                } 
    22812286                // initialize deferred rendering 
  • GTP/trunk/App/Demos/Vis/FriendlyCulling/src/irradiance.cpp

    r3216 r3235  
    11531153                deferredShader->SetUseTemporalCoherence(useTemporalCoherence); 
    11541154                deferredShader->SetSortSamples(sortSamples); 
     1155                deferredShader->SetMaxDistance(traverser->GetMaxVisibleDistance()); 
    11551156 
    11561157                ShadowMap *sm = showShadowMap ? shadowMap : NULL; 
     
    20172018                { 
    20182019                        // usethe maximal visible distance to focus shadow map 
    2019                         float maxVisibleDist = min(camera->GetFar(), traverser->GetMaxVisibleDistance()); 
     2020                        const float maxVisibleDist = min(camera->GetFar(), traverser->GetMaxVisibleDistance()); 
    20202021                        RenderShadowMap(maxVisibleDist); 
    20212022                } 
  • GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/depthOfField.cg

    r3232 r3235  
    77 
    88 
    9 float2 ComputeBlurFactors(float4 color, float sceneRange) 
     9float2 ComputeBlurFactors(float4 color, float sceneRange, float zFocus) 
    1010{ 
    1111        float2 result; 
    1212 
     13        const float m = 1.0f; 
     14        const float focalLen = 1.0f; 
     15 
     16        const float scale = 5e4f; 
     17 
    1318        // Compute blur factor based on the CoC size scaled and normalized to 0..1 range 
    14         //float pixCoC = abs(Dlens * focalLen * (Zfocus - v.fDepth) / (Zfocus * (v.fDepth - focalLen))); 
     19        //float blur = abs(dLens * focalLen * (zFocus - color.w)) / (1e-6f + abs(zFocus * (color.w - focalLen))); 
     20 
     21        const float dist = abs(zFocus - color.w); 
     22        const float blur = scale * m * focalLen * dist / (sceneRange * (1e-6f + abs(color.w + dist * step(0, zFocus - color.w)))); 
    1523         
    16         //float blur = saturate(pixCoC * scale / maxCoC); 
     24        //float blur = saturate(blur * scale / maxCoC); 
    1725         
    1826        // Depth/blurriness value scaled to 0..1 range 
    19         //result = float2(v.fDepth / sceneRange, blur); 
     27        result = float2(color.w / sceneRange, blur); 
    2028 
    2129        return result; 
     
    3038                        ) 
    3139{ 
    32         float average = .0f; 
     40        float4 avgCol = float4(.0f); 
    3341        float total_w = .0f; 
    3442 
    3543        for (int i = 0; i < NUM_DOF_TABS; ++ i) 
    3644        { 
    37                 float2 sampleTexCoord = texCoords + filterOffs[i]; 
     45                float2 sampleTexCoord = texCoords + filterOffs[i] * blurFactors.y; 
    3846                float4 sampleCol = tex2Dlod(colorsTex, float4(sampleTexCoord, 0, 0)); 
    3947                 
     
    4149                float w = 1; 
    4250                 
    43                 average += sampleCol * w; 
     51                avgCol += sampleCol * w; 
    4452                total_w += w; 
    4553        } 
    4654 
    47         average /= max(total_w, 1e-6f); 
     55        avgCol /= max(total_w, 1e-6f); 
    4856 
    49         return saturate(average); 
     57        return avgCol; 
    5058} 
    5159 
     
    5462                                        uniform sampler2D colorsTex, 
    5563                                        uniform float2 filterOffs[NUM_DOF_TABS], 
    56                                         uniform float sceneRange 
     64                                        uniform float sceneRange, 
     65                                        uniform float zFocus 
    5766                                         ): COLOR 
    5867{ 
     
    6069        const float4 color = tex2Dlod(colorsTex, float4(IN.texCoords, 0, 0)); 
    6170         
    62         float2 blurFactors = ComputeBlurFactors(color, sceneRange); 
     71        zFocus = min(zFocus, sceneRange * 0.5f); 
     72        float2 blurFactors = ComputeBlurFactors(color, sceneRange, zFocus); 
     73 
     74        //float4 outCol = float4(color.x, color.y, blurFactors.y, color.w); 
    6375        float4 outCol = Blur(color, IN.texCoords, blurFactors, colorsTex, filterOffs); 
    6476         
Note: See TracChangeset for help on using the changeset viewer.