Changeset 2808


Ignore:
Timestamp:
06/29/08 23:48:17 (16 years ago)
Author:
mattausch
Message:
 
Location:
GTP/trunk/App/Demos/Vis/FriendlyCulling
Files:
2 added
4 edited

Legend:

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

    r2806 r2808  
    4141                                Name="VCCLCompilerTool" 
    4242                                Optimization="0" 
    43                                 AdditionalIncludeDirectories="libs;libs/GL;libs/Devil/include;src;libs/Zlib/include" 
     43                                AdditionalIncludeDirectories="libs;libs/GL;libs/Devil/include;src;libs/Zlib/include;"$(CG_INC_PATH)"" 
    4444                                PreprocessorDefinitions="WIN32;_DEBUG;_CONSOLE;_CRT_SECURE_NO_WARNINGS" 
    4545                                MinimalRebuild="true" 
     
    6363                        <Tool 
    6464                                Name="VCLinkerTool" 
    65                                 AdditionalDependencies="glut32.lib glew32s.lib glew32.lib DevIL.lib ILUT.lib zlib.lib" 
     65                                AdditionalDependencies="glut32.lib glew32s.lib glew32.lib DevIL.lib ILUT.lib zlib.lib cg.lib cgGL.lib" 
    6666                                LinkIncremental="2" 
    67                                 AdditionalLibraryDirectories="libs/GL;libs/Devil/lib;libs/Zlib/lib" 
     67                                AdditionalLibraryDirectories="libs/GL;libs/Devil/lib;libs/Zlib/lib;&quot;$(CG_LIB_PATH)&quot;" 
     68                                IgnoreDefaultLibraryNames="LIBCMT" 
    6869                                GenerateDebugInformation="true" 
    6970                                SubSystem="1" 
     
    126127                                OmitFramePointers="true" 
    127128                                EnableFiberSafeOptimizations="true" 
    128                                 AdditionalIncludeDirectories="libs;libs/GL;libs/Devil/include;src;libs/Zlib/include" 
     129                                AdditionalIncludeDirectories="libs;libs/GL;libs/Devil/include;src;libs/Zlib/include;&quot;$(CG_INC_PATH)&quot;" 
    129130                                PreprocessorDefinitions="WIN32;NDEBUG;_CONSOLE; _CRT_SECURE_NO_WARNINGS" 
    130131                                StringPooling="true" 
     
    243244                        </File> 
    244245                        <File 
     246                                RelativePath=".\src\RenderTexture.cpp" 
     247                                > 
     248                        </File> 
     249                        <File 
     250                                RelativePath=".\src\RenderTexture.h" 
     251                                > 
     252                        </File> 
     253                        <File 
    245254                                RelativePath=".\src\SceneQuery.cpp" 
    246255                                > 
  • GTP/trunk/App/Demos/Vis/FriendlyCulling/src/SceneQuery.cpp

    r2800 r2808  
    77#include <IL/il.h> 
    88#include <assert.h> 
     9#include "RenderTexture.h" 
     10 
     11 
     12using namespace std; 
     13 
     14int texWidth = 2048; 
     15int texHeight = 2048; 
     16 
     17/*static void cgErrorCallback() 
     18{ 
     19        CGerror lastError = cgGetError(); 
     20 
     21        if(lastError) 
     22        { 
     23                printf("%s\n\n", cgGetErrorString(lastError)); 
     24                printf("%s\n", cgGetLastListing(sCgContext)); 
     25                printf("Cg error, exiting...\n"); 
     26 
     27                exit(0); 
     28        } 
     29}*/ 
     30 
     31 
     32static void PrintGLerror(char *msg) 
     33{ 
     34        GLenum errCode; 
     35        const GLubyte *errStr; 
     36         
     37        if ((errCode = glGetError()) != GL_NO_ERROR)  
     38        { 
     39                errStr = gluErrorString(errCode); 
     40                fprintf(stderr,"OpenGL ERROR: %s: %s\n", errStr, msg); 
     41        } 
     42} 
    943 
    1044 
     
    2660{ 
    2761 
    28 using namespace std; 
    29  
    30  
    31 //const static int viewport[4] = {0, 0, 512, 512}; 
    32 const static int viewport[4] = {0, 0, 1024, 768}; 
    33 //const static int viewport[4] = {0, 0, 2048, 2048}; 
    34  
    35  
    36  
    37 SceneQuery::SceneQuery(const AxisAlignedBox3 &sceneBox, RenderTraverser *renderer):  
    38 mSceneBox(sceneBox), mDepth(NULL) 
    39 { 
    40         Prepare(renderer); 
    41 } 
    42  
    43  
    44 bool SceneQuery::CalcIntersection(Vector3 &pt) 
    45 { 
    46         int px = (pt.x - mSceneBox.Min(0)) * (viewport[2] - 1) / mSceneBox.Size(0); 
    47         int py = (pt.y - mSceneBox.Min(1)) * (viewport[3] - 1) / mSceneBox.Size(1); 
    48  
    49         unsigned char d = mDepth[px + py * viewport[2]]; 
    50  
    51         const float offs = mSceneBox.Size(2) * 1e-1f; 
    52  
    53         static float depth = (float)d; 
    54  
    55         if (d > 0) 
    56         { 
    57                 const float x = 0.1f; 
    58                 depth = depth * x + d * (1.0f - x); 
    59                 pt.z =  mSceneBox.Max().z - mSceneBox.Size().z * depth / 255.0f + offs; 
    60                 //cout << "new depth " << pt.z << " (" << d << ")" << endl; 
    61  
    62                 return true; 
    63         } 
    64         //cout << "invalid depth: " << d << endl; 
    65  
    66         return false; 
    67 } 
    68  
    69  
    70 void SceneQuery::Prepare(RenderTraverser *renderer) 
    71 { 
    72         cout << "Preparing scene queries" << endl; 
    73  
    74         const float xlen = mSceneBox.Size().x * 0.5f; 
    75         const float ylen = mSceneBox.Size().y * 0.5f; 
    76          
    77         Camera *orthoCam = new Camera(xlen, ylen); 
    78         orthoCam->SetOrtho(true); 
    79  
    80         orthoCam->SetNear(0.0f); 
    81         orthoCam->Yaw(M_PI * 0.5f); 
    82  
    83         Vector3 pos = Vector3(mSceneBox.Center().x, mSceneBox.Center().y, mSceneBox.Max().z); 
    84         orthoCam->SetPosition(pos); 
    85  
    86         //glPixelStorei(GL_PACK_ROW_LENGTH, viewport[2]); 
    87         glPixelStorei(GL_PACK_ALIGNMENT, 1); 
    88         glReadBuffer(GL_BACK); 
    89  
    90         // hack: should create offscreen buffer for this 
    91         glViewport(0, 0, viewport[2], viewport[3]); 
    92  
    93         glMatrixMode(GL_PROJECTION); 
    94         glLoadIdentity(); 
    95          
    96         glOrtho(-xlen, xlen, -ylen, ylen, 0.0f, mSceneBox.Size().z);  
    97          
    98         glMatrixMode(GL_MODELVIEW); 
    99          
    100         orthoCam->SetupCameraView(); 
    101  
    102         glClear(GL_DEPTH_BUFFER_BIT); 
    103  
    104         mDepth = new unsigned char[viewport[2] * viewport[3]]; 
    105         //mDepth = new float[viewport[2] * viewport[3]]; 
    106  
    107         //renderer->SetCamera(orthoCam); 
    108  
    109         renderer->RenderScene(); 
    110  
    111         glReadPixels(0, 0, viewport[2], viewport[3], GL_DEPTH_COMPONENT, GL_UNSIGNED_BYTE, mDepth); 
    112         //glReadPixels(0, 0, viewport[2], viewport[3], GL_DEPTH_COMPONENT, GL_FLOAT, mDepth); 
    113 /* 
     62 
     63void GrabDepthBuffer(float *data, RenderTexture *rt) 
     64{ 
     65        rt->BindDepth(); 
     66        rt->EnableTextureTarget(); 
     67 
     68        const int texFormat = GL_DEPTH_COMPONENT; 
     69        glGetTexImage(rt->GetTextureTarget(), 0, texFormat, GL_FLOAT, data); 
     70 
     71        rt->DisableTextureTarget(); 
     72} 
     73 
     74 
     75void ExportDepthBuffer(float *data) 
     76{ 
    11477        startil(); 
    11578 
    116         if (!ilTexImage(viewport[2], viewport[3], 1, 1, IL_LUMINANCE, IL_UNSIGNED_BYTE, mDepth)) 
     79        ILstring filename = ILstring("depth.tga"); 
     80        ilRegisterType(IL_FLOAT); 
     81 
     82        const int depth = 1; 
     83        const int bpp = 1; 
     84 
     85        if (!ilTexImage(texWidth, texHeight, depth, bpp, IL_LUMINANCE, IL_FLOAT, data)) 
    11786        { 
    11887                cerr << "IL error " << ilGetError() << endl; 
     
    12190        } 
    12291 
    123         ILstring writename = ILstring("out.tga"); 
    124  
    125         ilSetInteger(IL_TGA_RLE, 1); 
    126         if (!ilSaveImage(writename)) 
     92        if (!ilSaveImage(filename)) 
    12793        { 
    12894                cerr << "TGA write error " << ilGetError() << endl; 
     
    13096 
    13197        stopil(); 
    132 */ 
     98 
     99        cout << "exported depth buffer" << endl; 
     100} 
     101 
     102 
     103SceneQuery::SceneQuery(const AxisAlignedBox3 &sceneBox, RenderTraverser *renderer):  
     104mSceneBox(sceneBox), mDepth(NULL) 
     105{ 
     106        Prepare(renderer); 
     107} 
     108 
     109 
     110bool SceneQuery::CalcIntersection(Vector3 &pt) 
     111{ 
     112        const int px = (pt.x - mSceneBox.Min(0)) * (texWidth - 1) / mSceneBox.Size(0); 
     113        const int py = (pt.y - mSceneBox.Min(1)) * (texHeight - 1) / mSceneBox.Size(1); 
     114 
     115        float d = mDepth[px + py * texHeight]; 
     116 
     117        static float depth = d; 
     118 
     119        if (d > 0) 
     120        { 
     121                // temporal smoothing of depth values 
     122                const float x = 0.5f; 
     123                depth = d * x + depth * (1.0f - x); 
     124 
     125                const float offs = mSceneBox.Size(2) * 5e-2f; 
     126                pt.z =  mSceneBox.Max().z - mSceneBox.Size().z * depth + offs; 
     127 
     128                return true; 
     129        } 
     130 
     131        return false; 
     132} 
     133 
     134 
     135void SceneQuery::Prepare(RenderTraverser *renderer) 
     136{ 
     137        cout << "Preparing scene queries" << endl; 
     138 
     139        RenderTexture *depthTexture = new RenderTexture(texWidth, texHeight, true, true); 
     140 
     141#ifdef ATI 
     142        depthTexture->Initialize(true, true, false, false, false, 8, 8, 8, 8, RenderTexture::RT_COPY_TO_TEXTURE); 
     143#else 
     144        depthTexture->Initialize(true, true, false, false, false, 8, 8, 8, 8);//, RenderTexture::RT_COPY_TO_TEXTURE); 
     145#endif 
     146 
     147        PrintGLerror("Init"); 
     148 
     149 
     150        const float xlen = mSceneBox.Size().x * 0.5f; 
     151        const float ylen = mSceneBox.Size().y * 0.5f; 
     152         
     153        Camera *orthoCam = new Camera(xlen, ylen); 
     154        orthoCam->SetOrtho(true); 
     155 
     156        orthoCam->SetNear(0.0f); 
     157        orthoCam->Yaw(M_PI * 0.5f); 
     158 
     159        Vector3 pos = Vector3(mSceneBox.Center().x, mSceneBox.Center().y, mSceneBox.Max().z); 
     160        orthoCam->SetPosition(pos); 
     161 
     162        depthTexture->BeginCapture(); 
     163 
     164        glViewport(0, 0, texWidth, texHeight); 
     165 
     166        glClearColor(1, 1, 1, 1); 
     167        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 
     168 
     169        glFrontFace(GL_CCW); 
     170        glCullFace(GL_BACK); 
     171 
     172        glDisable(GL_CULL_FACE); 
     173        //glEnable(GL_CULL_FACE); 
     174 
     175        glShadeModel(GL_FLAT); 
     176        glEnable(GL_DEPTH_TEST); 
     177 
     178        glMatrixMode(GL_PROJECTION); 
     179        glLoadIdentity(); 
     180         
     181        glOrtho(-xlen, xlen, -ylen, ylen, 0.0f, mSceneBox.Size().z);  
     182         
     183        glMatrixMode(GL_MODELVIEW); 
     184         
     185        orthoCam->SetupCameraView(); 
     186 
     187        mDepth = new float[texHeight * texWidth]; 
     188 
     189        //renderer->SetCamera(orthoCam); 
     190        renderer->RenderScene(); 
     191 
     192        depthTexture->EndCapture(); 
     193 
     194        GrabDepthBuffer(mDepth, depthTexture); 
     195        //ExportDepthBuffer(mDepth); 
     196        //PrintGLerror("grab"); 
     197 
     198        DEL_PTR(depthTexture); 
    133199        DEL_PTR(orthoCam); 
    134200} 
    135201 
    136 } 
     202 
     203 
     204 
     205} 
  • GTP/trunk/App/Demos/Vis/FriendlyCulling/src/SceneQuery.h

    r2806 r2808  
    1010 
    1111class RenderTraverser; 
    12  
    1312 
    1413/** A simple class that computes the first intersection of a horizontal ray with 
     
    3332        void Prepare(RenderTraverser *traverser); 
    3433 
    35  
    3634        /////////// 
    3735 
    3836        AxisAlignedBox3 mSceneBox; 
    3937 
    40         unsigned char *mDepth; 
     38        //unsigned char *mDepth; 
     39        float *mDepth; 
    4140}; 
    4241 
  • GTP/trunk/App/Demos/Vis/FriendlyCulling/src/chcdemo.cpp

    r2807 r2808  
    2323#include "SceneQuery.h" 
    2424#include "RenderQueue.h" 
     25#include <Cg/cg.h> 
     26#include <Cg/cgGL.h> 
    2527 
    2628 
     
    156158 
    157159 
     160// cg stuff 
     161static CGcontext sCgContext = NULL; 
     162static CGprogram sCgDepthPeelingProgram = NULL; 
     163static CGprogram sCgPassThroughProgram = NULL; 
     164 
     165static CGprofile sCgFragmentProfile; 
     166static CGparameter sTextureParam; 
     167static CGparameter sTexWidthParam;  
     168static CGparameter sStepSizeParam; 
     169 
     170 
    158171 
    159172int main(int argc, char* argv[]) 
     
    306319                "'F3'           - shows/hides bounds (boxes or tight bounds)", 
    307320                "'F4'           - shows/hides statistics", 
    308                 "'F5',          - toggles between fly / walkmode", 
    309                 "'F6',          - shows/hides parameters", 
     321                "'F5',          - shows/hides parameters", 
     322                "'F6',          - toggles between fly / walkmode", 
    310323                "'F7',          - depth pass", 
    311324                "'F8',          - enable/disable glFinish for more accurate timings", 
     
    10901103void DisplayStats() 
    10911104{ 
    1092         char *msg[] = {"Frustum Culling", "Stop and Wait", 
    1093                                     "CHC", "CHC ++"}; 
    1094  
    1095         char msg2[400]; 
    1096         char msg3[200]; 
    1097         char msg4[200]; 
    1098         char msg5[200]; 
    1099         char msg6[200]; 
    1100         char msg7[200]; 
    1101         char msg8[200]; 
    1102  
     1105        static char msg[7][300]; 
    11031106 
    11041107        static double renderTime = algTime; 
    1105         const float expFactor = 0.3f; 
    1106  
    1107         //if (useRenderQueue) cout << "rq overhead: " << 1e6f * rqTimer.TotalTime() << " ms" << endl; 
     1108        const float expFactor = 0.5f; 
    11081109 
    11091110        // if some strange render time spike happened in this frame => don't count 
    1110         if (algTime < 1000) renderTime = algTime * expFactor + (1.0f - expFactor) * algTime; 
    1111         //renderTime = 1e3f * (elapsedTime * expFactor + (1.0f - expFactor) * elapsedTime); 
     1111        if (algTime < 500) renderTime = algTime * expFactor + (1.0f - expFactor) * algTime; 
    11121112 
    11131113        accumulatedTime += elapsedTime * 1e3f; 
     
    11301130        } 
    11311131 
    1132         sprintf(msg2, "assumed visible frames: %4d, max batch size: %4d",  
    1133                       assumedVisibleFrames, maxBatchSize); 
    1134  
    1135         sprintf(msg3, "multiqueries: %d, tight bounds: %d, render queue: %d, depth pass: %d, glFinish: %d",  
    1136                       useMultiQueries, useTightBounds, useRenderQueue, depthPass, useGlFinish); 
    1137  
    11381132        string str; 
    11391133        string str2; 
    11401134 
     1135        int i = 0; 
     1136 
    11411137        CalcDecimalPoint(str, renderedTriangles); 
    11421138        CalcDecimalPoint(str2, bvh->GetBvhStats().mTriangles); 
    11431139 
    1144         sprintf(msg4, "rendered nodes: %6d (of %6d), rendered triangles: %s (of %s)",  
    1145                           renderedNodes, bvh->GetNumVirtualNodes(), str.c_str(), str2.c_str());  
    1146  
    1147         sprintf(msg5, "traversed: %5d, frustum culled: %5d, query culled: %5d", 
    1148                           traversedNodes, frustumCulledNodes, queryCulledNodes); 
    1149  
    1150         sprintf(msg6, "issued queries: %5d, state changes: %5d, render batches: %5d", issuedQueries, stateChanges, numBatches); 
    1151  
    1152         sprintf(msg8, "triangles per virtual leaf: %5d", trianglesPerVirtualLeaf); 
    1153  
    1154         sprintf(msg7, "fps: %6.1f", fps); 
     1140        sprintf(msg[i ++], "rendered nodes: %6d (of %6d), rendered triangles: %s (of %s)",  
     1141                        renderedNodes, bvh->GetNumVirtualNodes(), str.c_str(), str2.c_str());  
     1142 
     1143        sprintf(msg[i ++], "traversed: %5d, frustum culled: %5d, query culled: %5d", 
     1144                        traversedNodes, frustumCulledNodes, queryCulledNodes); 
     1145 
     1146        sprintf(msg[i ++], "issued queries: %5d, state changes: %5d, render batches: %5d",  
     1147                    issuedQueries, stateChanges, numBatches); 
     1148 
     1149        sprintf(msg[i ++], "fps: %6.1f", fps); 
     1150 
     1151 
     1152        sprintf(msg[i ++], "assumed visible frames: %4d, max batch size: %4d",  
     1153                    assumedVisibleFrames, maxBatchSize); 
     1154 
     1155        sprintf(msg[i ++], "multiqueries: %d, tight bounds: %d, render queue: %d, depth pass: %d, glFinish: %d",  
     1156                    useMultiQueries, useTightBounds, useRenderQueue, depthPass, useGlFinish); 
     1157 
     1158        sprintf(msg[i ++], "triangles per virtual leaf: %5d", trianglesPerVirtualLeaf); 
    11551159 
    11561160 
     
    11631167        else 
    11641168        { 
     1169                static char *alg_str[] = {"Frustum Culling", "Stop and Wait", "CHC", "CHC ++"}; 
     1170 
    11651171                glColor3f(1.0f, 1.0f, 1.0f); 
    1166                 //glColor3f(1.0f, 0.0f, 0.0f); 
    1167                 Output(850, 30, msg[renderMode]); 
    1168  
    1169                  
     1172                Output(850, 30, alg_str[renderMode]); 
     1173 
    11701174                if (showStatistics) 
    11711175                { 
    1172                         Output(20, 30, msg4); 
    1173                         Output(20, 60, msg5); 
    1174                         Output(20, 90, msg6); 
    1175                         Output(20, 120, msg7); 
     1176                        for (int i = 0; i < 4; ++ i) 
     1177                                Output(20, (i + 1) * 30, msg[i]); 
    11761178                } 
    11771179 
    11781180                if (showOptions) 
    11791181                { 
    1180                         Output(20, 150, msg2); 
    1181                         Output(20, 180, msg3); 
    1182                         Output(20, 210, msg8); 
     1182                        for (int i = 4; i < 8; ++ i) 
     1183                                Output(20, (i + 1) * 30, msg[i]); 
    11831184                } 
    11841185        } 
Note: See TracChangeset for help on using the changeset viewer.