Ignore:
Timestamp:
11/23/06 21:38:45 (18 years ago)
Author:
bittner
Message:

merge, filter update, renderebuffer made functional

File:
1 edited

Legend:

Unmodified
Added
Removed
  • GTP/trunk/Lib/Vis/Preprocessing/src/QtGlRenderer.cpp

    r1757 r1785  
    3535static CGprofile sCgFragmentProfile; 
    3636 
    37 GLuint frontDepthMap; 
    38 GLuint backDepthMap; 
    39  
    40 const int depthMapSize = 512; 
    4137 
    4238//static vector<OcclusionQuery *> sQueries; 
     
    6056 
    6157void 
    62 QtGlRendererBuffer::EvalRenderCostSample(RenderCostSample &sample, 
    63                                                                                  const bool useOcclusionQueries, 
    64                                                                                  const int threshold 
    65                                                                                  ) 
    66 { 
    67         // choose a random view point 
    68         mViewCellsManager->GetViewPoint(mViewPoint); 
    69         sample.mPosition = mViewPoint; 
    70         //cout << "viewpoint: " << mViewPoint << endl; 
    71  
    72         // take a render cost sample by rendering a cube 
    73         Vector3 directions[6]; 
    74  
    75         directions[0] = Vector3(1,0,0); 
    76         directions[1] = Vector3(0,1,0); 
    77         directions[2] = Vector3(0,0,1); 
    78         directions[3] = Vector3(-1,0,0); 
    79         directions[4] = Vector3(0,-1,0); 
    80         directions[5] = Vector3(0,0,-1); 
    81  
    82         sample.mVisibleObjects = 0; 
    83  
    84         // reset object counters 
    85         ObjectContainer::const_iterator it, it_end = mObjects.end(); 
    86  
    87         for (it = mObjects.begin(); it != it_end; ++ it)  
    88         { 
    89                 (*it)->mCounter = 0; 
    90         } 
    91  
    92         ++ mFrame; 
    93  
    94         //glCullFace(GL_FRONT); 
    95         glCullFace(GL_BACK); 
    96         glDisable(GL_CULL_FACE); 
    97          
    98  
    99         // query all 6 directions for a full point sample 
    100         for (int i = 0; i < 6; ++ i)  
    101         { 
    102                 mViewDirection = directions[i]; 
    103                 SetupCamera(); 
    104  
    105                 glClearColor(1.0f, 1.0f, 1.0f, 1.0f); 
    106                 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 
    107                 //glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);      glDepthMask(GL_TRUE); 
    108                 glDepthFunc(GL_LESS); 
    109  
    110                 mUseFalseColors = true; 
    111  
    112                 // the actual scene rendering fills the depth (for occlusion queries) 
    113                 // and the frame buffer (for item buffer) 
    114                 RenderScene(); 
    115  
    116  
    117                 if (0)  
    118                 { 
    119                         char filename[256]; 
    120                         sprintf(filename, "snap/frame-%04d-%d.png", mFrame, i); 
    121                         QImage im = toImage(); 
    122                         im.save(filename, "PNG"); 
    123                 } 
    124           
    125                 // evaluate the sample 
    126                 if (useOcclusionQueries)  
    127                 { 
    128                         EvalQueryWithOcclusionQueries(); 
    129                 } 
    130                 else  
    131                 { 
    132                         EvalQueryWithItemBuffer(); 
    133                 } 
    134         }   
    135  
    136         // now evaluate the statistics over that sample 
    137         // currently only the number of visible objects is taken into account 
    138         sample.Reset(); 
    139  
    140         for (it = mObjects.begin(); it != it_end; ++ it)  
    141         { 
    142                 Intersectable *obj = *it; 
    143                 if (obj->mCounter >= threshold)  
    144                 { 
    145                         ++ sample.mVisibleObjects; 
    146                         sample.mVisiblePixels += obj->mCounter; 
    147                 } 
    148         } 
    149  
    150         //cout << "RS=" << sample.mVisibleObjects << " "; 
    151 } 
    152  
    153  
    154 QtGlRendererBuffer::~QtGlRendererBuffer() 
    155 { 
    156 #if USE_CG 
    157   if (sCgFragmentProgram) 
    158                 cgDestroyProgram(sCgFragmentProgram); 
    159         if (sCgContext) 
    160                 cgDestroyContext(sCgContext); 
    161 #endif 
    162 } 
    163  
    164  
    165 void 
    166 QtGlRendererBuffer::SampleRenderCost(const int numSamples, 
    167                                                                          vector<RenderCostSample> &samples, 
    168                                                                          const bool useOcclusionQueries, 
    169                                                                          const int threshold 
    170                                                                          ) 
     58QtGlRendererBuffer::MakeCurrent() 
    17159{ 
    17260  makeCurrent(); 
    173  
    174   if (mPixelBuffer == NULL) 
    175           mPixelBuffer = new unsigned int[GetWidth()*GetHeight()]; 
    176    
    177   // using 90 degree projection to capture 360 view with 6 samples 
    178   SetupProjection(GetHeight(), GetHeight(), 90.0f); 
    179  
    180   //samples.resize(numSamples); 
    181   halton.Reset(); 
    182    
    183   // the number of queries queried in batch mode 
    184   const int numQ = 500; 
    185  
    186   //const int numQ = (int)mObjects.size(); 
    187   if (useOcclusionQueries) 
    188   { 
    189           cout << "\ngenerating " << numQ << " queries ... "; 
    190           OcclusionQuery::GenQueries(mOcclusionQueries, numQ); 
    191           cout << "finished" << endl; 
    192   } 
    193  
    194   // sampling queries  
    195   for (int i = 0; i < numSamples; ++ i) 
    196   { 
    197           cout << "."; 
    198           EvalRenderCostSample(samples[i], useOcclusionQueries, threshold); 
    199   } 
    200  
     61} 
     62 
     63void 
     64QtGlRendererBuffer::DoneCurrent() 
     65{ 
    20166  doneCurrent(); 
    202  
    203 } 
    204    
    205  
     67} 
     68   
    20669 
    20770QtGlRendererBuffer::QtGlRendererBuffer(const int w, 
     
    21073                                                                           ViewCellsManager *viewcells, 
    21174                                                                           KdTree *tree): 
    212   QGLPixelBuffer(QSize(w, h)), GlRendererBuffer(sceneGraph, viewcells, tree) { 
    213    
    214   Environment::GetSingleton()->GetIntValue("Preprocessor.pvsRenderErrorSamples", mPvsStatFrames); 
    215   mPvsErrorBuffer.resize(mPvsStatFrames); 
    216   ClearErrorBuffer(); 
    217  
    218   mPixelBuffer = NULL; 
    219    
    220   makeCurrent(); 
     75  QGLPixelBuffer(QSize(w, h)), 
     76  //QGLWidget(NULL, w, h), 
     77  GlRendererBuffer(sceneGraph, viewcells, tree) 
     78{ 
     79  MakeCurrent(); 
    22180  InitGL(); 
    222   doneCurrent(); 
    223    
    224 } 
     81  DoneCurrent(); 
     82} 
     83 
     84 
    22585 
    22686float 
     
    23898  //static int query = -1; 
    23999  //if (query == -1) 
    240 //        glGenOcclusionQueriesNV(1, (unsigned int *)&query); 
     100  //      glGenOcclusionQueriesNV(1, (unsigned int *)&query); 
    241101 
    242102  OcclusionQuery query; 
     
    247107        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 
    248108        glEnable( GL_CULL_FACE ); 
     109        glCullFace(GL_BACK); 
    249110         
    250111        RenderScene(); 
     
    254115        glDisable( GL_CULL_FACE ); 
    255116 
    256          
    257117        query.BeginQuery(); 
    258118         
     
    271131        if (pixelCount > 0) 
    272132          return -1.0f; // backfacing polygon found -> not a valid viewspace sample 
     133 
    273134  } else 
    274135        glDisable( GL_CULL_FACE ); 
    275136         
    276137 
    277   ViewCell *viewcell = NULL; 
    278    
    279   PrVs prvs; 
    280    
    281   mViewCellsManager->SetMaxFilterSize(0); 
    282   mViewCellsManager->GetPrVS(mViewPoint, prvs, mViewCellsManager->GetFilterWidth()); 
    283   viewcell = prvs.mViewCell; 
    284    
    285   //  ViewCell *viewcell = mViewCellsManager->GetViewCell(mViewPoint); 
    286   pvsSize = 0; 
     138  //  ViewCell *viewcell = NULL; 
     139   
     140  //  PrVs prvs; 
     141   
     142  //  mViewCellsManager->SetMaxFilterSize(0); 
     143  //  mViewCellsManager->GetPrVS(mViewPoint, prvs, mViewCellsManager->GetFilterWidth()); 
     144  //  viewcell = prvs.mViewCell; 
     145   
     146  ObjectPvs pvs; 
     147  ViewCell *viewcell = mViewCellsManager->GetViewCell(mViewPoint); 
     148         
    287149  if (viewcell) { 
     150        mViewCellsManager->ApplyFilter2(viewcell, 
     151                                                                        false, 
     152                                                                        1.0f, 
     153                                                                        pvs); 
     154         
     155        pvsSize = 0; 
    288156        SetupCamera(); 
    289157        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 
    290  
    291158        glColorMask(GL_FALSE, GL_TRUE, GL_FALSE, GL_FALSE); 
    292159         
    293         // Render PVS 
    294         ObjectPvsIterator it = viewcell->GetPvs().GetIterator(); 
    295  
    296         pvsSize = viewcell->GetPvs().GetSize(); 
    297          
     160           
     161        //      // Render PVS 
     162        ObjectPvsIterator it = pvs.GetIterator(); 
     163         
     164        pvsSize = pvs.GetSize(); 
     165        Intersectable::NewMail(); 
    298166        for (; it.HasMoreEntries(); ) { 
    299167          ObjectPvsEntry entry = it.Next(); 
     
    301169          RenderIntersectable(object); 
    302170        } 
    303  
     171         
    304172        //      glColorMask(GL_TRUE, GL_FALSE, GL_FALSE, GL_FALSE); 
    305173        glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE); 
     174         
     175         
    306176        mUseFalseColors = true; 
    307  
     177         
    308178        query.BeginQuery(); 
    309  
     179         
    310180        SetupCamera(); 
    311  
     181         
    312182        RenderScene(); 
    313183         
    314184        query.EndQuery(); 
    315185         
    316  
     186         
    317187        unsigned int pixelCount; 
    318188        // reenable other state 
     
    321191         
    322192        pErrorPixels = ((float)pixelCount)/(GetWidth()*GetHeight()); 
    323         if (mSnapErrorFrames && pErrorPixels > 0.01) { 
    324            
    325           char filename[256]; 
    326           sprintf(filename, "error-frame-%04d-%0.5f.png", mFrame, pErrorPixels); 
    327           QImage im = toImage(); 
    328           string str = mSnapPrefix + filename; 
    329           QString qstr(str.c_str()); 
    330  
    331           im.save(qstr, "PNG"); 
    332           if (1) { //0 && mFrame == 1543) { 
    333                 int x,y; 
    334                 int lastIndex = -1; 
    335                 for (y=0; y < im.height(); y++) 
    336                   for (x=0; x < im.width(); x++) { 
    337                         QRgb p = im.pixel(x,y); 
    338                         int index = qRed(p) + (qGreen(p)<<8) + (qBlue(p)<<16); 
    339                         if (qGreen(p) != 255 && index!=0) { 
    340                           if (index != lastIndex) { 
    341                                 //                              Debug<<"ei="<<index<<" "; 
    342                                 lastIndex = index; 
    343                           } 
     193  } else 
     194        pErrorPixels = 0.0f; 
     195   
     196  if (mSnapErrorFrames && pErrorPixels > 0.001f) { 
     197         
     198        char filename[256]; 
     199        sprintf(filename, "error-frame-%04d-%0.5f.png", mFrame, pErrorPixels); 
     200        QImage im = toImage(); 
     201        string str = mSnapPrefix + filename; 
     202        QString qstr(str.c_str()); 
     203         
     204        im.save(qstr, "PNG"); 
     205        if (1) { //0 && mFrame == 1543) { 
     206          int x,y; 
     207          int lastIndex = -1; 
     208          for (y=0; y < im.height(); y++) 
     209                for (x=0; x < im.width(); x++) { 
     210                  QRgb p = im.pixel(x,y); 
     211                  int index = qRed(p) + (qGreen(p)<<8) + (qBlue(p)<<16); 
     212                  if (qGreen(p) != 255 && index!=0) { 
     213                        if (index != lastIndex) { 
     214                          //                            Debug<<"ei="<<index<<" "; 
     215                          lastIndex = index; 
    344216                        } 
    345217                  } 
    346           } 
    347  
    348  
    349           mUseFalseColors = false; 
    350           glPushAttrib(GL_CURRENT_BIT); 
    351           glColor3f(0,1,0); 
    352           glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE); 
    353           SetupCamera(); 
    354           glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 
    355            
    356           // Render PVS 
    357           ObjectPvsIterator it = viewcell->GetPvs().GetIterator(); 
    358           for (; it.HasMoreEntries(); ) { 
    359                 ObjectPvsEntry entry = it.Next(); 
    360                 Intersectable *object = entry.mObject; 
    361                 RenderIntersectable(object); 
    362           } 
    363  
    364           im = toImage(); 
    365           sprintf(filename, "error-frame-%04d-%0.5f-pvs.png", mFrame, pErrorPixels); 
    366           str = mSnapPrefix + filename; 
    367           qstr = str.c_str(); 
    368           im.save(qstr, "PNG"); 
    369           glPopAttrib(); 
    370         } 
     218                } 
     219        } 
     220         
     221         
     222        mUseFalseColors = false; 
     223        glPushAttrib(GL_CURRENT_BIT); 
     224        glColor3f(0,1,0); 
    371225        glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE); 
    372   } 
    373  
    374   if (viewcell && mViewCellsManager->GetMaxFilterSize() > 0) 
    375         mViewCellsManager->DeleteLocalMergeTree(viewcell); 
     226        SetupCamera(); 
     227        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 
     228         
     229        // Render PVS 
     230        Intersectable::NewMail(); 
     231 
     232        ObjectPvsIterator it = pvs.GetIterator(); 
     233        for (; it.HasMoreEntries(); ) { 
     234          ObjectPvsEntry entry = it.Next(); 
     235          Intersectable *object = entry.mObject; 
     236          RenderIntersectable(object); 
     237        } 
     238         
     239        im = toImage(); 
     240        sprintf(filename, "error-frame-%04d-%0.5f-pvs.png", mFrame, pErrorPixels); 
     241        str = mSnapPrefix + filename; 
     242        qstr = str.c_str(); 
     243        im.save(qstr, "PNG"); 
     244        glPopAttrib(); 
     245  } 
     246   
     247  glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE); 
    376248   
    377249  return pErrorPixels; 
    378250} 
    379251 
    380  
    381 void 
    382 QtGlRendererBuffer::ClearErrorBuffer() 
    383 { 
    384   for (int i=0; i < mPvsStatFrames; i++) { 
    385         mPvsErrorBuffer[i].mError = 1.0f; 
    386   } 
    387 } 
    388  
    389  
    390 void 
    391 QtGlRendererBuffer::EvalPvsStat() 
    392 { 
    393   mPvsStat.Reset(); 
    394   halton.Reset(); 
    395  
    396   makeCurrent(); 
    397  
    398   SetupProjection(GetWidth(), GetHeight()); 
    399    
    400   for (int i=0; i < mPvsStatFrames; i++) { 
    401         float err; 
    402         // set frame id for saving the error buffer 
    403         mFrame = i; 
    404         RandomViewPoint(); 
    405  
    406         // atlanta problematic frames: 325 525 691 1543 
    407 #if 0 
    408         if (mFrame != 325 && 
    409                 mFrame != 525 && 
    410                 mFrame != 691 && 
    411                 mFrame != 1543) 
    412           mPvsErrorBuffer[i] = -1; 
    413         else { 
    414           Debug<<"frame ="<<mFrame<<" vp="<<mViewPoint<<" vd="<<mViewDirection<<endl; 
    415         } 
    416 #endif 
    417         if (mPvsErrorBuffer[i].mError > 0.0f) { 
    418           int pvsSize; 
    419  
    420  
    421           float error = GetPixelError(pvsSize); 
    422           mPvsErrorBuffer[i].mError = error; 
    423           mPvsErrorBuffer[i].mPvsSize = pvsSize; 
    424  
    425           emit UpdatePvsErrorItem(i, 
    426                                                           mPvsErrorBuffer[i]); 
    427            
    428           cout<<"("<<i<<","<<mPvsErrorBuffer[i].mError<<")"; 
    429           //      swapBuffers(); 
    430         } 
    431          
    432         err = mPvsErrorBuffer[i].mError; 
    433          
    434         if (err >= 0.0f) { 
    435           if (err > mPvsStat.maxError) 
    436                 mPvsStat.maxError = err; 
    437           mPvsStat.sumError += err; 
    438           mPvsStat.sumPvsSize += mPvsErrorBuffer[i].mPvsSize; 
    439            
    440           if (err == 0.0f) 
    441                 mPvsStat.errorFreeFrames++; 
    442           mPvsStat.frames++; 
    443         } 
    444   } 
    445  
    446   glFinish(); 
    447   doneCurrent(); 
    448  
    449   cout<<endl<<flush; 
    450   //  mRenderingFinished.wakeAll(); 
    451 } 
    452  
    453  
    454  
    455  
    456  
    457  
    458  
    459 void QtGlRendererBuffer::SampleBeamContributions(Intersectable *sourceObject, 
    460                                                                                            Beam &beam, 
    461                                                                                            const int desiredSamples, 
    462                                                                                            BeamSampleStatistics &stat) 
    463 { 
    464         // TODO: should be moved out of here (not to be done every time) 
    465         // only back faces are interesting for the depth pass 
    466         glShadeModel(GL_FLAT); 
    467         glDisable(GL_LIGHTING); 
    468  
    469         // needed to kill the fragments for the front buffer 
    470         glEnable(GL_ALPHA_TEST); 
    471         glAlphaFunc(GL_GREATER, 0); 
    472  
    473         // assumes that the beam is constructed and contains kd-tree nodes 
    474         // and viewcells which it intersects 
    475    
    476    
    477         // Get the number of viewpoints to be sampled 
    478         // Now it is a sqrt but in general a wiser decision could be made. 
    479         // The less viewpoints the better for rendering performance, since less passes 
    480         // over the beam is needed. 
    481         // The viewpoints could actually be generated outside of the bounding box which 
    482         // would distribute the 'efective viewpoints' of the object surface and thus 
    483         // with a few viewpoints better sample the viewpoint space.... 
    484  
    485         //TODO: comment in 
    486         //int viewPointSamples = sqrt((float)desiredSamples); 
    487         int viewPointSamples = max(desiredSamples / (GetWidth() * GetHeight()), 1); 
    488          
    489         // the number of direction samples per pass is given by the number of viewpoints 
    490         int directionalSamples = desiredSamples / viewPointSamples; 
    491          
    492         Debug << "directional samples: " << directionalSamples << endl; 
    493         for (int i = 0; i < viewPointSamples; ++ i)  
    494         { 
    495                 Vector3 viewPoint = beam.mBox.GetRandomPoint(); 
    496                  
    497                 // perhaps the viewpoint should be shifted back a little bit so that it always lies 
    498                 // inside the source object 
    499                 // 'ideally' the viewpoints would be distributed on the soureObject surface, but this 
    500         // would require more complicated sampling (perhaps hierarchical rejection sampling of 
    501                 // the object surface is an option here - only the mesh faces which are inside the box 
    502                 // are considered as candidates)  
    503                  
    504                 SampleViewpointContributions(sourceObject, 
    505                                                                          viewPoint, 
    506                                                                          beam, 
    507                                                                          directionalSamples, 
    508                                                                          stat); 
    509         } 
    510  
    511  
    512         // note: 
    513         // this routine would be called only if the number of desired samples is sufficiently 
    514         // large - for other rss tree cells the cpu based sampling is perhaps more efficient 
    515         // distributing the work between cpu and gpu would also allow us to place more sophisticated 
    516         // sample distributions (silhouette ones) using the cpu and the jittered once on the GPU 
    517         // in order that thios scheme is working well the gpu render buffer should run in a separate 
    518         // thread than the cpu sampler, which would not be such a big problem.... 
    519  
    520         // disable alpha test again 
    521         glDisable(GL_ALPHA_TEST); 
    522 } 
    523  
    524  
    525  
    526 void QtGlRendererBuffer::SampleViewpointContributions(Intersectable *sourceObject, 
    527                                                                                                         const Vector3 viewPoint, 
    528                                                                                                         Beam &beam, 
    529                                                                                                         const int samples, 
    530                                                     BeamSampleStatistics &stat) 
    531 { 
    532     // 1. setup the view port to match the desired samples 
    533         glViewport(0, 0, samples, samples); 
    534  
    535         // 2. setup the projection matrix and view matrix to match the viewpoint + beam.mDirBox 
    536         SetupProjectionForViewPoint(viewPoint, beam, sourceObject); 
    537  
    538  
    539         // 3. reset z-buffer to 0 and render the source object for the beam 
    540         //    with glCullFace(Enabled) and glFrontFace(GL_CW) 
    541         //    save result to the front depth map 
    542         //    the front depth map holds ray origins 
    543  
    544  
    545         // front depth buffer must be initialised to 0 
    546         float clearDepth; 
    547          
    548         glGetFloatv(GL_DEPTH_CLEAR_VALUE, &clearDepth); 
    549         glClearDepth(0.0f); 
    550         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); 
    551  
    552  
    553         //glFrontFace(GL_CW); 
    554         glEnable(GL_CULL_FACE); 
    555         glCullFace(GL_FRONT); 
    556         glColorMask(0, 0, 0, 0); 
    557          
    558  
    559         // stencil is increased where the source object is located 
    560         glEnable(GL_STENCIL_TEST);       
    561         glStencilFunc(GL_ALWAYS, 0x1, 0x1); 
    562         glStencilOp(GL_REPLACE, GL_REPLACE, GL_REPLACE); 
    563  
    564  
    565 #if 0 
    566         static int glSourceObjList = -1;          
    567         if (glSourceObjList != -1)  
    568         { 
    569                 glSourceObjList = glGenLists(1); 
    570                 glNewList(glSourceObjList, GL_COMPILE); 
    571  
    572                 RenderIntersectable(sourceObject); 
    573          
    574                 glEndList(); 
    575         } 
    576         glCallList(glSourceObjList); 
    577  
    578 #else 
    579         RenderIntersectable(sourceObject); 
    580  
    581 #endif   
    582  
    583          // copy contents of the front depth buffer into depth texture 
    584         glBindTexture(GL_TEXTURE_2D, frontDepthMap);     
    585         glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, 0, depthMapSize, depthMapSize); 
    586  
    587  
    588         // reset clear function 
    589         glClearDepth(clearDepth); 
    590  
    591          
    592          
    593         // 4. set up the termination depth buffer (= standard depth buffer) 
    594         //    only rays which have non-zero entry in the origin buffer are valid since 
    595         //    they realy start on the object surface (this is tagged by setting a 
    596         //    stencil buffer bit at step 3). 
    597          
    598         glStencilFunc(GL_EQUAL, 0x1, 0x1); 
    599         glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP); 
    600  
    601         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 
    602         glDepthMask(1); 
    603  
    604         glEnable(GL_DEPTH_TEST); 
    605                  
    606         glEnable(GL_CULL_FACE); 
    607         glCullFace(GL_BACK); 
    608  
    609         // setup front depth buffer 
    610         glEnable(GL_TEXTURE_2D); 
    611          
    612 #if USE_CG 
    613         // bind pixel shader implementing the front depth buffer functionality 
    614         cgGLBindProgram(sCgFragmentProgram); 
    615         cgGLEnableProfile(sCgFragmentProfile); 
    616 #endif 
    617  
    618         // 5. render all objects inside the beam  
    619         //    we can use id based false color to read them back for gaining the pvs 
    620  
    621         glColorMask(1, 1, 1, 1); 
    622  
    623          
    624         // if objects not stored in beam => extract objects 
    625         if (beam.mFlags & !Beam::STORE_OBJECTS) 
    626         { 
    627                 vector<KdNode *>::const_iterator it, it_end = beam.mKdNodes.end(); 
    628  
    629                 Intersectable::NewMail(); 
    630                 for (it = beam.mKdNodes.begin(); it != it_end; ++ it) 
    631                 { 
    632                         mKdTree->CollectObjects(*it, beam.mObjects); 
    633                 } 
    634         } 
    635  
    636  
    637         //    (objects can be compiled to a gl list now so that subsequent rendering for 
    638         //    this beam is fast - the same hold for step 3) 
    639         //    Afterwards we have two depth buffers defining the ray origin and termination 
    640          
    641  
    642 #if 0 
    643         static int glObjList = -1;  
    644         if (glObjList != -1)  
    645         { 
    646                 glObjList = glGenLists(1); 
    647                 glNewList(glObjList, GL_COMPILE); 
    648          
    649                 ObjectContainer::const_iterator it, it_end = beam.mObjects.end(); 
    650                 for (it = beam.mObjects.begin(); it != it_end; ++ it) 
    651                 { 
    652                         // render all objects except the source object 
    653                         if (*it != sourceObject) 
    654                                 RenderIntersectable(*it); 
    655                 } 
    656                  
    657                 glEndList(); 
    658         } 
    659  
    660         glCallList(glObjList); 
    661 #else 
    662         ObjectContainer::const_iterator it, it_end = beam.mObjects.end(); 
    663         for (it = beam.mObjects.begin(); it != it_end; ++ it) 
    664         {        
    665                 // render all objects except the source object 
    666                 if (*it != sourceObject) 
    667                         RenderIntersectable(*it); 
    668         } 
    669 #endif 
    670          
    671     
    672  
    673         // 6. Use occlusion queries for all viewcell meshes associated with the beam -> 
    674         //     a fragment passes if the corresponding stencil fragment is set and its depth is 
    675         //     between origin and termination buffer 
    676  
    677         // create new queries if necessary 
    678         OcclusionQuery::GenQueries(mOcclusionQueries, (int)beam.mViewCells.size()); 
    679  
    680         // check whether any backfacing polygon would pass the depth test? 
    681         // matt: should check both back /front facing because of dual depth buffer 
    682         // and danger of cutting the near plane with front facing polys. 
    683          
    684         glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE); 
    685         glDepthMask(GL_FALSE); 
    686         glDisable(GL_CULL_FACE); 
    687  
    688    
    689         ViewCellContainer::const_iterator vit, vit_end = beam.mViewCells.end(); 
    690  
    691         int queryIdx = 0; 
    692  
    693         for (vit = beam.mViewCells.begin(); vit != vit_end; ++ vit) 
    694         { 
    695                 mOcclusionQueries[queryIdx ++]->BeginQuery(); 
    696  
    697                 RenderIntersectable(*vit); 
    698                  
    699                 mOcclusionQueries[queryIdx]->EndQuery(); 
    700  
    701                 ++ queryIdx; 
    702         } 
    703  
    704  
    705  
    706         // at this point, if possible, go and do some other computation 
    707  
    708  
    709          
    710         // 7. The number of visible pixels is the number of sample rays which see the source 
    711         //    object from the corresponding viewcell -> remember these values for later update 
    712         //   of the viewcell pvs - or update immediately? 
    713  
    714         queryIdx = 0; 
    715  
    716         for (vit = beam.mViewCells.begin(); vit != vit_end; ++ vit) 
    717         { 
    718                 // fetch queries 
    719                 unsigned int pixelCount = mOcclusionQueries[queryIdx ++]->GetQueryResult(); 
    720  
    721                 if (pixelCount) 
    722                         Debug << "view cell " << (*vit)->GetId() << " visible pixels: " << pixelCount << endl; 
    723         } 
    724          
    725  
    726         // 8. Copmpute rendering statistics 
    727         // In general it is not neccessary to remember to extract all the rays cast. I hope it 
    728         // would be sufficient to gain only the intergral statistics about the new contributions 
    729         // and so the rss tree would actually store no new rays (only the initial ones) 
    730         // the subdivision of the tree would only be driven by the statistics (the glrender could 
    731         // evaluate the contribution entropy for example) 
    732         // However might be an option to extract/store only those the rays which made a contribution 
    733         // (new viewcell has been discovered) or relative contribution greater than a threshold ...  
    734  
    735         ObjectContainer pvsObj; 
    736         stat.pvsSize = ComputePvs(beam.mObjects, pvsObj); 
    737          
    738         // to gain ray source and termination 
    739         // copy contents of ray termination buffer into depth texture 
    740         // and compare with ray source buffer 
    741 #if 0 
    742         VssRayContainer rays; 
    743  
    744         glBindTexture(GL_TEXTURE_2D, backDepthMap);      
    745         glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, 0, depthMapSize, depthMapSize); 
    746  
    747         ComputeRays(Intersectable *sourceObj, rays); 
    748  
    749 #endif 
    750  
    751  
    752  
    753         //-- cleanup 
    754  
    755  
    756         // reset gl state 
    757         glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE); 
    758         glDepthMask(GL_TRUE); 
    759         glEnable(GL_CULL_FACE); 
    760         glDisable(GL_STENCIL_TEST); 
    761 #if USE_CG 
    762         cgGLDisableProfile(sCgFragmentProfile); 
    763 #endif 
    764         glDisable(GL_TEXTURE_2D); 
    765  
    766         // remove objects from beam 
    767         if (beam.mFlags & !Beam::STORE_OBJECTS) 
    768                 beam.mObjects.clear(); 
    769 } 
    770  
    771  
    772 void QtGlRendererBuffer::SetupProjectionForViewPoint(const Vector3 &viewPoint,  
    773                                                                                                    const Beam &beam,  
    774                                                                                                    Intersectable *sourceObject) 
    775 { 
    776         float left, right, bottom, top, znear, zfar; 
    777  
    778         beam.ComputePerspectiveFrustum(left, right, bottom, top, znear, zfar, 
    779                                                                    mSceneGraph->GetBox()); 
    780  
    781         //Debug << left << " " << right << " " << bottom << " " << top << " " << znear << " " << zfar << endl; 
    782         glMatrixMode(GL_PROJECTION); 
    783         glLoadIdentity(); 
    784         glFrustum(left, right, bottom, top, znear, zfar); 
    785         //glFrustum(-1, 1, -1, 1, 1, 20000); 
    786  
    787     const Vector3 center = viewPoint + beam.GetMainDirection() * (zfar - znear) * 0.3f; 
    788         const Vector3 up =  
    789                 Normalize(CrossProd(beam.mPlanes[0].mNormal, beam.mPlanes[4].mNormal)); 
    790  
    791 #ifdef GTP_DEBUG 
    792         Debug << "view point: " << viewPoint << endl; 
    793         Debug << "eye: " << center << endl; 
    794         Debug << "up: " << up << endl; 
    795 #endif 
    796  
    797         glMatrixMode(GL_MODELVIEW); 
    798         glLoadIdentity(); 
    799         gluLookAt(viewPoint.x, viewPoint.y, viewPoint.z,  
    800                           center.x, center.y, center.z,                    
    801                           up.x, up.y, up.z); 
    802 }                
    803  
    804    
    805 void QtGlRendererBuffer::InitGL() 
    806 { 
    807  makeCurrent();  
    808  GlRenderer::InitGL(); 
    809  
    810 #if 1 
    811         // initialise dual depth buffer textures 
    812         glGenTextures(1, &frontDepthMap); 
    813         glBindTexture(GL_TEXTURE_2D, frontDepthMap); 
    814          
    815         glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, depthMapSize,  
    816                                  depthMapSize, 0, GL_DEPTH_COMPONENT, GL_UNSIGNED_BYTE, NULL); 
    817  
    818         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); 
    819         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); 
    820         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP); 
    821         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP); 
    822  
    823         glGenTextures(1, &backDepthMap); 
    824         glBindTexture(GL_TEXTURE_2D, backDepthMap); 
    825          
    826         glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, depthMapSize,  
    827                 depthMapSize, 0, GL_DEPTH_COMPONENT, GL_UNSIGNED_BYTE, NULL); 
    828         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); 
    829         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); 
    830         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP); 
    831         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP); 
    832  
    833 #if USE_CG 
    834         // cg initialization 
    835         cgSetErrorCallback(handleCgError); 
    836         sCgContext = cgCreateContext(); 
    837          
    838         if (cgGLIsProfileSupported(CG_PROFILE_ARBFP1)) 
    839                 sCgFragmentProfile = CG_PROFILE_ARBFP1; 
    840         else  
    841         { 
    842           // try FP30 
    843           if (cgGLIsProfileSupported(CG_PROFILE_FP30)) 
    844             sCgFragmentProfile = CG_PROFILE_FP30; 
    845           else  
    846           { 
    847                   Debug << "Neither arbfp1 or fp30 fragment profiles supported on this system" << endl; 
    848                   exit(1); 
    849           } 
    850   } 
    851  
    852  sCgFragmentProgram = cgCreateProgramFromFile(sCgContext, 
    853                                                                                            CG_SOURCE, "../src/dual_depth.cg", 
    854                                                                                            sCgFragmentProfile, 
    855                                                                                            NULL,  
    856                                                                                            NULL); 
    857  
    858   if (!cgIsProgramCompiled(sCgFragmentProgram)) 
    859           cgCompileProgram(sCgFragmentProgram); 
    860  
    861   cgGLLoadProgram(sCgFragmentProgram); 
    862   cgGLBindProgram(sCgFragmentProgram); 
    863  
    864   Debug << "---- PROGRAM BEGIN ----\n" << 
    865           cgGetProgramString(sCgFragmentProgram, CG_COMPILED_PROGRAM) << "---- PROGRAM END ----\n"; 
    866  
    867 #endif 
    868  
    869 #endif 
    870   doneCurrent(); 
    871 } 
    872  
    873  
    874 void QtGlRendererBuffer::ComputeRays(Intersectable *sourceObj, VssRayContainer &rays) 
    875 { 
    876         for (int i = 0; i < depthMapSize * depthMapSize; ++ i) 
    877         { 
    878                 //todo glGetTexImage() 
    879         } 
    880 } 
    881  
    882  
    883  
    884 int QtGlRendererBuffer::ComputePvs(ObjectContainer &objects,  
    885                                                                    ObjectContainer &pvs) const 
     252int 
     253QtGlRendererBuffer::ComputePvs(ObjectContainer &objects,  
     254                                                           ObjectContainer &pvs) const 
    886255{ 
    887256        int pvsSize = 0; 
     
    922291        return pvsSize; 
    923292} 
     293 
     294/////////////////////////////////////////////////////////////// 
     295/////////////////////////////////////////////////////////////// 
     296/////////////////////////////////////////////////////////////// 
     297/////////////////////////////////////////////////////////////// 
     298 
     299 
    924300 
    925301 
     
    17021078  if (mViewCellsManager)  
    17031079        mViewCellsManager->SetMaxFilterSize(number); 
     1080  mPvsCache.Reset(); 
    17041081  updateGL(); 
    17051082} 
     
    17091086{ 
    17101087  mSpatialFilterSize = 1e-3*number; 
     1088  mPvsCache.Reset(); 
    17111089  updateGL(); 
    17121090} 
Note: See TracChangeset for help on using the changeset viewer.