Changeset 1785 for GTP/trunk/Lib


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

merge, filter update, renderebuffer made functional

Location:
GTP/trunk/Lib/Vis/Preprocessing/src
Files:
20 edited

Legend:

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

    r1784 r1785  
    118118BvhLeaf::BvhLeaf(const AxisAlignedBox3 &bbox): 
    119119BvhNode(bbox),  
    120 mSubdivisionCandidate(NULL) 
    121 { 
    122         mActiveNode = this; 
     120mSubdivisionCandidate(NULL), 
     121mGlList(0) 
     122{ 
     123  mActiveNode = this; 
    123124} 
    124125 
    125126 
    126127BvhLeaf::BvhLeaf(const AxisAlignedBox3 &bbox, BvhInterior *parent): 
    127 BvhNode(bbox, parent) 
     128  BvhNode(bbox, parent), 
     129  mGlList(0) 
     130   
    128131{ 
    129132        mActiveNode = this; 
     
    134137                                 BvhInterior *parent,  
    135138                                 const int numObjects): 
    136 BvhNode(bbox, parent) 
     139  BvhNode(bbox, parent), 
     140  mGlList(0) 
     141 
    137142{ 
    138143        mObjects.reserve(numObjects); 
     
    295300        Environment::GetSingleton()->GetIntValue("BvHierarchy.minRaysForVisibility", mMinRaysForVisibility); 
    296301        Environment::GetSingleton()->GetIntValue("BvHierarchy.maxTests", mMaxTests); 
    297         Environment::GetSingleton()->GetBoolValue("BvHierarchy.Construction.useInitialSubdivision", mApplyInitialPartition); 
     302        //      Environment::GetSingleton()->GetBoolValue("BvHierarchy.Construction.useInitialSubdivision", mApplyInitialPartition); 
    298303         
    299304        mInitialObjectsSize = 50; 
  • GTP/trunk/Lib/Vis/Preprocessing/src/BvHierarchy.h

    r1784 r1785  
    327327        */ 
    328328        BvhLeaf(const AxisAlignedBox3 &bbox); 
    329         BvhLeaf(const AxisAlignedBox3 &bbox, BvhInterior *parent); 
     329  BvhLeaf(const AxisAlignedBox3 &bbox, BvhInterior *parent); 
    330330        BvhLeaf(const AxisAlignedBox3 &bbox, BvhInterior *parent, const int numObjects); 
    331331 
     
    363363 
    364364public: 
    365  
    366         /// objects 
    367         ObjectContainer mObjects; 
    368  
     365  // gl list use to store the geometry on the gl server 
     366  int mGlList; 
     367   
     368  /// objects 
     369  ObjectContainer mObjects; 
     370   
    369371protected: 
    370  
    371         /// pointer to a split plane candidate splitting this leaf 
    372         SubdivisionCandidate *mSubdivisionCandidate; 
    373  
    374         /// the active node which will be accounted for in the pvs 
    375         BvhNode *mActiveNode; 
     372   
     373  /// pointer to a split plane candidate splitting this leaf 
     374  SubdivisionCandidate *mSubdivisionCandidate; 
     375   
     376  /// the active node which will be accounted for in the pvs 
     377  BvhNode *mActiveNode; 
    376378}; 
    377379 
  • GTP/trunk/Lib/Vis/Preprocessing/src/Environment.cpp

    r1784 r1785  
    12281228                                 "true"); 
    12291229 
     1230   
    12301231   RegisterOption("VssPreprocessor.enlargeViewSpace", 
    12311232                                 optBool, 
     
    17251726                                        "true"); 
    17261727 
     1728        RegisterOption("Preprocessor.evaluateFilter", 
     1729                                   optBool, 
     1730                                   "preprocessor_evaluate_filter", 
     1731                                   "false"); 
     1732 
    17271733        RegisterOption("Preprocessor.delayVisibilityComputation", 
    17281734                                   optBool, 
     
    18911897                                        optBool, 
    18921898                                        "rss_use_importance", 
     1899                                        "true"); 
     1900 
     1901        RegisterOption("RssPreprocessor.useRssTree", 
     1902                                        optBool, 
     1903                                        "rss_use_rss_tree", 
    18931904                                        "true"); 
    18941905 
  • GTP/trunk/Lib/Vis/Preprocessing/src/GlRenderer.cpp

    r1758 r1785  
    2525static bool nvQuerySupport = false; 
    2626 
     27GLuint frontDepthMap; 
     28GLuint backDepthMap; 
     29 
     30const int depthMapSize = 512; 
    2731 
    2832static void InitExtensions()  
     
    8084  mUseForcedColors = false; 
    8185  mRenderBoxes = false; 
    82   mUseGlLists = true; 
    83   //mUseGlLists = false; 
     86  //  mUseGlLists = true; 
     87  mUseGlLists = false; 
    8488} 
    8589 
     
    133137  case Intersectable::BVH_INTERSECTABLE: { 
    134138 
    135           BvhNode *node = dynamic_cast<BvhNode *>(object); 
    136  
     139        BvhNode *node = dynamic_cast<BvhNode *>(object); 
     140         
    137141        if (mRenderBoxes) 
    138142          RenderBox(node->GetBoundingBox()); 
     
    143147  case Intersectable::KD_INTERSECTABLE: { 
    144148        KdNode *node = (dynamic_cast<KdIntersectable *>(object))->GetItem(); 
    145  
     149         
    146150        if (mRenderBoxes) 
    147151          RenderBox(mKdTree->GetBox(node)); 
     
    293297  glLoadIdentity(); 
    294298 
     299  glFrontFace(GL_CCW); 
     300  glCullFace(GL_BACK); 
    295301  glEnable(GL_CULL_FACE); 
    296302  glShadeModel(GL_FLAT); 
     
    402408GlRenderer::RenderScene() 
    403409{ 
     410  Intersectable::NewMail(); 
    404411  static int glList = -1; 
    405412  if (mUseGlLists) { 
     
    459466GlRenderer(sceneGraph, viewcells, tree)   
    460467{ 
    461         mPixelBuffer = NULL; 
    462  
    463         // implement width and height in subclasses 
     468  Environment::GetSingleton()->GetIntValue("Preprocessor.pvsRenderErrorSamples", mPvsStatFrames); 
     469  mPvsErrorBuffer.resize(mPvsStatFrames); 
     470  ClearErrorBuffer(); 
     471   
     472  mPixelBuffer = NULL; 
     473   
     474  // implement width and height in subclasses 
    464475} 
    465476 
     
    647658  if (node->IsLeaf()) { 
    648659        BvhLeaf *leaf = (BvhLeaf *) node; 
    649         for (int i=0; i < leaf->mObjects.size(); i++) 
    650           RenderIntersectable(leaf->mObjects[i]); 
     660 
     661#if 0 
     662        if (leaf->mGlList == 0) { 
     663          leaf->mGlList = glGenLists(1); 
     664          if (leaf->mGlList != 0) 
     665                glNewList(leaf->mGlList, GL_COMPILE); 
     666           
     667          for (int i=0; i < leaf->mObjects.size(); i++) 
     668                RenderIntersectable(leaf->mObjects[i]); 
     669           
     670          if (leaf->mGlList != 0) 
     671                glEndList(); 
     672        } 
     673         
     674        if (leaf->mGlList != 0) 
     675          glCallList(leaf->mGlList); 
     676#else 
     677          for (int i=0; i < leaf->mObjects.size(); i++) 
     678                RenderIntersectable(leaf->mObjects[i]); 
     679#endif 
    651680  } else { 
    652681        BvhInterior *in = (BvhInterior *)node; 
     
    675704} 
    676705 
    677 } 
     706 
     707 
     708 
     709 
     710 
     711 
     712void 
     713GlRendererBuffer::EvalRenderCostSample(RenderCostSample &sample, 
     714                                                                           const bool useOcclusionQueries, 
     715                                                                           const int threshold 
     716                                                                           ) 
     717{ 
     718  // choose a random view point 
     719  mViewCellsManager->GetViewPoint(mViewPoint); 
     720  sample.mPosition = mViewPoint; 
     721  //cout << "viewpoint: " << mViewPoint << endl; 
     722   
     723  // take a render cost sample by rendering a cube 
     724  Vector3 directions[6]; 
     725   
     726  directions[0] = Vector3(1,0,0); 
     727  directions[1] = Vector3(0,1,0); 
     728  directions[2] = Vector3(0,0,1); 
     729  directions[3] = Vector3(-1,0,0); 
     730  directions[4] = Vector3(0,-1,0); 
     731  directions[5] = Vector3(0,0,-1); 
     732   
     733  sample.mVisibleObjects = 0; 
     734   
     735  // reset object counters 
     736  ObjectContainer::const_iterator it, it_end = mObjects.end(); 
     737   
     738  for (it = mObjects.begin(); it != it_end; ++ it)  
     739        { 
     740          (*it)->mCounter = 0; 
     741        } 
     742 
     743  ++ mFrame; 
     744   
     745  //glCullFace(GL_FRONT); 
     746  glCullFace(GL_BACK); 
     747  glDisable(GL_CULL_FACE); 
     748   
     749   
     750        // query all 6 directions for a full point sample 
     751  for (int i = 0; i < 6; ++ i)  
     752        { 
     753          mViewDirection = directions[i]; 
     754          SetupCamera(); 
     755           
     756          glClearColor(1.0f, 1.0f, 1.0f, 1.0f); 
     757          glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 
     758          //glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);    glDepthMask(GL_TRUE); 
     759          glDepthFunc(GL_LESS); 
     760           
     761          mUseFalseColors = true; 
     762           
     763          // the actual scene rendering fills the depth (for occlusion queries) 
     764          // and the frame buffer (for item buffer) 
     765          RenderScene(); 
     766           
     767           
     768          if (0)  
     769                { 
     770                  char filename[256]; 
     771                  sprintf(filename, "snap/frame-%04d-%d.png", mFrame, i); 
     772                  //              QImage im = toImage(); 
     773                  //              im.save(filename, "PNG"); 
     774                } 
     775           
     776          // evaluate the sample 
     777          if (useOcclusionQueries)  
     778                { 
     779                  EvalQueryWithOcclusionQueries(); 
     780                } 
     781          else  
     782                { 
     783                  EvalQueryWithItemBuffer(); 
     784                } 
     785        }   
     786   
     787  // now evaluate the statistics over that sample 
     788  // currently only the number of visible objects is taken into account 
     789  sample.Reset(); 
     790   
     791  for (it = mObjects.begin(); it != it_end; ++ it)  
     792        { 
     793          Intersectable *obj = *it; 
     794          if (obj->mCounter >= threshold)  
     795                { 
     796                  ++ sample.mVisibleObjects; 
     797                  sample.mVisiblePixels += obj->mCounter; 
     798                } 
     799        } 
     800   
     801  //cout << "RS=" << sample.mVisibleObjects << " "; 
     802} 
     803 
     804 
     805GlRendererBuffer::~GlRendererBuffer() 
     806{ 
     807#if USE_CG 
     808  if (sCgFragmentProgram) 
     809                cgDestroyProgram(sCgFragmentProgram); 
     810        if (sCgContext) 
     811                cgDestroyContext(sCgContext); 
     812#endif 
     813} 
     814 
     815 
     816void 
     817GlRendererBuffer::SampleRenderCost(const int numSamples, 
     818                                                                   vector<RenderCostSample> &samples, 
     819                                                                   const bool useOcclusionQueries, 
     820                                                                   const int threshold 
     821                                                                   ) 
     822{ 
     823  MakeCurrent(); 
     824 
     825  if (mPixelBuffer == NULL) 
     826          mPixelBuffer = new unsigned int[GetWidth()*GetHeight()]; 
     827   
     828  // using 90 degree projection to capture 360 view with 6 samples 
     829  SetupProjection(GetHeight(), GetHeight(), 90.0f); 
     830 
     831  //samples.resize(numSamples); 
     832  halton.Reset(); 
     833   
     834  // the number of queries queried in batch mode 
     835  const int numQ = 500; 
     836 
     837  //const int numQ = (int)mObjects.size(); 
     838  if (useOcclusionQueries) 
     839  { 
     840          cout << "\ngenerating " << numQ << " queries ... "; 
     841          OcclusionQuery::GenQueries(mOcclusionQueries, numQ); 
     842          cout << "finished" << endl; 
     843  } 
     844 
     845  // sampling queries  
     846  for (int i = 0; i < numSamples; ++ i) 
     847  { 
     848          cout << "."; 
     849          EvalRenderCostSample(samples[i], useOcclusionQueries, threshold); 
     850  } 
     851 
     852  DoneCurrent(); 
     853} 
     854   
     855 
     856 
     857 
     858 
     859void 
     860GlRendererBuffer::ClearErrorBuffer() 
     861{ 
     862  for (int i=0; i < mPvsStatFrames; i++) { 
     863        mPvsErrorBuffer[i].mError = 1.0f; 
     864  } 
     865} 
     866 
     867 
     868void 
     869GlRendererBuffer::EvalPvsStat() 
     870{ 
     871  mPvsStat.Reset(); 
     872  halton.Reset(); 
     873 
     874  MakeCurrent(); 
     875 
     876  SetupProjection(GetWidth(), GetHeight()); 
     877 
     878  cout<<"mPvsStatFrames="<<mPvsStatFrames<<endl; 
     879   
     880  for (int i=0; i < mPvsStatFrames; i++) { 
     881        float err; 
     882        // set frame id for saving the error buffer 
     883        mFrame = i; 
     884        RandomViewPoint(); 
     885 
     886        // atlanta problematic frames: 325 525 691 1543 
     887#if 0 
     888        if (mFrame != 325 && 
     889                mFrame != 525 && 
     890                mFrame != 691 && 
     891                mFrame != 1543) 
     892          mPvsErrorBuffer[i] = -1; 
     893        else { 
     894          Debug<<"frame ="<<mFrame<<" vp="<<mViewPoint<<" vd="<<mViewDirection<<endl; 
     895        } 
     896#endif 
     897         
     898        if (mPvsErrorBuffer[i].mError > 0.0f) { 
     899          int pvsSize; 
     900 
     901 
     902          float error = GetPixelError(pvsSize); 
     903          mPvsErrorBuffer[i].mError = error; 
     904          mPvsErrorBuffer[i].mPvsSize = pvsSize; 
     905 
     906          //      emit UpdatePvsErrorItem(i, 
     907          //                                                      mPvsErrorBuffer[i]); 
     908 
     909          cout<<"("<<i<<","<<mPvsErrorBuffer[i].mError<<")"; 
     910          //      swapBuffers(); 
     911        } 
     912         
     913        err = mPvsErrorBuffer[i].mError; 
     914         
     915        if (err >= 0.0f) { 
     916          if (err > mPvsStat.maxError) 
     917                mPvsStat.maxError = err; 
     918          mPvsStat.sumError += err; 
     919          mPvsStat.sumPvsSize += mPvsErrorBuffer[i].mPvsSize; 
     920           
     921          if (err == 0.0f) 
     922                mPvsStat.errorFreeFrames++; 
     923          mPvsStat.frames++; 
     924        } 
     925  } 
     926 
     927  glFinish(); 
     928  DoneCurrent(); 
     929 
     930  cout<<endl<<flush; 
     931  //  mRenderingFinished.wakeAll(); 
     932} 
     933 
     934 
     935 
     936 
     937 
     938 
     939 
     940void GlRendererBuffer::SampleBeamContributions(Intersectable *sourceObject, 
     941                                                                                           Beam &beam, 
     942                                                                                           const int desiredSamples, 
     943                                                                                           BeamSampleStatistics &stat) 
     944{ 
     945        // TODO: should be moved out of here (not to be done every time) 
     946        // only back faces are interesting for the depth pass 
     947        glShadeModel(GL_FLAT); 
     948        glDisable(GL_LIGHTING); 
     949 
     950        // needed to kill the fragments for the front buffer 
     951        glEnable(GL_ALPHA_TEST); 
     952        glAlphaFunc(GL_GREATER, 0); 
     953 
     954        // assumes that the beam is constructed and contains kd-tree nodes 
     955        // and viewcells which it intersects 
     956   
     957   
     958        // Get the number of viewpoints to be sampled 
     959        // Now it is a sqrt but in general a wiser decision could be made. 
     960        // The less viewpoints the better for rendering performance, since less passes 
     961        // over the beam is needed. 
     962        // The viewpoints could actually be generated outside of the bounding box which 
     963        // would distribute the 'efective viewpoints' of the object surface and thus 
     964        // with a few viewpoints better sample the viewpoint space.... 
     965 
     966        //TODO: comment in 
     967        //int viewPointSamples = sqrt((float)desiredSamples); 
     968        int viewPointSamples = max(desiredSamples / (GetWidth() * GetHeight()), 1); 
     969         
     970        // the number of direction samples per pass is given by the number of viewpoints 
     971        int directionalSamples = desiredSamples / viewPointSamples; 
     972         
     973        Debug << "directional samples: " << directionalSamples << endl; 
     974        for (int i = 0; i < viewPointSamples; ++ i)  
     975        { 
     976                Vector3 viewPoint = beam.mBox.GetRandomPoint(); 
     977                 
     978                // perhaps the viewpoint should be shifted back a little bit so that it always lies 
     979                // inside the source object 
     980                // 'ideally' the viewpoints would be distributed on the soureObject surface, but this 
     981        // would require more complicated sampling (perhaps hierarchical rejection sampling of 
     982                // the object surface is an option here - only the mesh faces which are inside the box 
     983                // are considered as candidates)  
     984                 
     985                SampleViewpointContributions(sourceObject, 
     986                                                                         viewPoint, 
     987                                                                         beam, 
     988                                                                         directionalSamples, 
     989                                                                         stat); 
     990        } 
     991 
     992 
     993        // note: 
     994        // this routine would be called only if the number of desired samples is sufficiently 
     995        // large - for other rss tree cells the cpu based sampling is perhaps more efficient 
     996        // distributing the work between cpu and gpu would also allow us to place more sophisticated 
     997        // sample distributions (silhouette ones) using the cpu and the jittered once on the GPU 
     998        // in order that thios scheme is working well the gpu render buffer should run in a separate 
     999        // thread than the cpu sampler, which would not be such a big problem.... 
     1000 
     1001        // disable alpha test again 
     1002        glDisable(GL_ALPHA_TEST); 
     1003} 
     1004 
     1005 
     1006 
     1007void GlRendererBuffer::SampleViewpointContributions(Intersectable *sourceObject, 
     1008                                                                                                        const Vector3 viewPoint, 
     1009                                                                                                        Beam &beam, 
     1010                                                                                                        const int samples, 
     1011                                                    BeamSampleStatistics &stat) 
     1012{ 
     1013    // 1. setup the view port to match the desired samples 
     1014        glViewport(0, 0, samples, samples); 
     1015 
     1016        // 2. setup the projection matrix and view matrix to match the viewpoint + beam.mDirBox 
     1017        SetupProjectionForViewPoint(viewPoint, beam, sourceObject); 
     1018 
     1019 
     1020        // 3. reset z-buffer to 0 and render the source object for the beam 
     1021        //    with glCullFace(Enabled) and glFrontFace(GL_CW) 
     1022        //    save result to the front depth map 
     1023        //    the front depth map holds ray origins 
     1024 
     1025 
     1026        // front depth buffer must be initialised to 0 
     1027        float clearDepth; 
     1028         
     1029        glGetFloatv(GL_DEPTH_CLEAR_VALUE, &clearDepth); 
     1030        glClearDepth(0.0f); 
     1031        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); 
     1032 
     1033 
     1034        // glFrontFace(GL_CCW); 
     1035        glEnable(GL_CULL_FACE); 
     1036        glCullFace(GL_FRONT); 
     1037        glColorMask(0, 0, 0, 0); 
     1038         
     1039 
     1040        // stencil is increased where the source object is located 
     1041        glEnable(GL_STENCIL_TEST);       
     1042        glStencilFunc(GL_ALWAYS, 0x1, 0x1); 
     1043        glStencilOp(GL_REPLACE, GL_REPLACE, GL_REPLACE); 
     1044 
     1045 
     1046#if 0 
     1047        static int glSourceObjList = -1;          
     1048        if (glSourceObjList != -1)  
     1049        { 
     1050                glSourceObjList = glGenLists(1); 
     1051                glNewList(glSourceObjList, GL_COMPILE); 
     1052 
     1053                RenderIntersectable(sourceObject); 
     1054         
     1055                glEndList(); 
     1056        } 
     1057        glCallList(glSourceObjList); 
     1058 
     1059#else 
     1060        RenderIntersectable(sourceObject); 
     1061 
     1062#endif   
     1063 
     1064         // copy contents of the front depth buffer into depth texture 
     1065        glBindTexture(GL_TEXTURE_2D, frontDepthMap);     
     1066        glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, 0, depthMapSize, depthMapSize); 
     1067 
     1068 
     1069        // reset clear function 
     1070        glClearDepth(clearDepth); 
     1071 
     1072         
     1073         
     1074        // 4. set up the termination depth buffer (= standard depth buffer) 
     1075        //    only rays which have non-zero entry in the origin buffer are valid since 
     1076        //    they realy start on the object surface (this is tagged by setting a 
     1077        //    stencil buffer bit at step 3). 
     1078         
     1079        glStencilFunc(GL_EQUAL, 0x1, 0x1); 
     1080        glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP); 
     1081 
     1082        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 
     1083        glDepthMask(1); 
     1084 
     1085        glEnable(GL_DEPTH_TEST); 
     1086                 
     1087        glEnable(GL_CULL_FACE); 
     1088        glCullFace(GL_BACK); 
     1089 
     1090        // setup front depth buffer 
     1091        glEnable(GL_TEXTURE_2D); 
     1092         
     1093#if USE_CG 
     1094        // bind pixel shader implementing the front depth buffer functionality 
     1095        cgGLBindProgram(sCgFragmentProgram); 
     1096        cgGLEnableProfile(sCgFragmentProfile); 
     1097#endif 
     1098 
     1099        // 5. render all objects inside the beam  
     1100        //    we can use id based false color to read them back for gaining the pvs 
     1101 
     1102        glColorMask(1, 1, 1, 1); 
     1103 
     1104         
     1105        // if objects not stored in beam => extract objects 
     1106        if (beam.mFlags & !Beam::STORE_OBJECTS) 
     1107        { 
     1108                vector<KdNode *>::const_iterator it, it_end = beam.mKdNodes.end(); 
     1109 
     1110                Intersectable::NewMail(); 
     1111                for (it = beam.mKdNodes.begin(); it != it_end; ++ it) 
     1112                { 
     1113                        mKdTree->CollectObjects(*it, beam.mObjects); 
     1114                } 
     1115        } 
     1116 
     1117 
     1118        //    (objects can be compiled to a gl list now so that subsequent rendering for 
     1119        //    this beam is fast - the same hold for step 3) 
     1120        //    Afterwards we have two depth buffers defining the ray origin and termination 
     1121         
     1122 
     1123#if 0 
     1124        static int glObjList = -1;  
     1125        if (glObjList != -1)  
     1126        { 
     1127                glObjList = glGenLists(1); 
     1128                glNewList(glObjList, GL_COMPILE); 
     1129         
     1130                ObjectContainer::const_iterator it, it_end = beam.mObjects.end(); 
     1131                for (it = beam.mObjects.begin(); it != it_end; ++ it) 
     1132                { 
     1133                        // render all objects except the source object 
     1134                        if (*it != sourceObject) 
     1135                                RenderIntersectable(*it); 
     1136                } 
     1137                 
     1138                glEndList(); 
     1139        } 
     1140 
     1141        glCallList(glObjList); 
     1142#else 
     1143        ObjectContainer::const_iterator it, it_end = beam.mObjects.end(); 
     1144        for (it = beam.mObjects.begin(); it != it_end; ++ it) 
     1145        {        
     1146                // render all objects except the source object 
     1147                if (*it != sourceObject) 
     1148                        RenderIntersectable(*it); 
     1149        } 
     1150#endif 
     1151         
     1152    
     1153 
     1154        // 6. Use occlusion queries for all viewcell meshes associated with the beam -> 
     1155        //     a fragment passes if the corresponding stencil fragment is set and its depth is 
     1156        //     between origin and termination buffer 
     1157 
     1158        // create new queries if necessary 
     1159        OcclusionQuery::GenQueries(mOcclusionQueries, (int)beam.mViewCells.size()); 
     1160 
     1161        // check whether any backfacing polygon would pass the depth test? 
     1162        // matt: should check both back /front facing because of dual depth buffer 
     1163        // and danger of cutting the near plane with front facing polys. 
     1164         
     1165        glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE); 
     1166        glDepthMask(GL_FALSE); 
     1167        glDisable(GL_CULL_FACE); 
     1168 
     1169   
     1170        ViewCellContainer::const_iterator vit, vit_end = beam.mViewCells.end(); 
     1171 
     1172        int queryIdx = 0; 
     1173 
     1174        for (vit = beam.mViewCells.begin(); vit != vit_end; ++ vit) 
     1175        { 
     1176                mOcclusionQueries[queryIdx ++]->BeginQuery(); 
     1177 
     1178                RenderIntersectable(*vit); 
     1179                 
     1180                mOcclusionQueries[queryIdx]->EndQuery(); 
     1181 
     1182                ++ queryIdx; 
     1183        } 
     1184 
     1185 
     1186 
     1187        // at this point, if possible, go and do some other computation 
     1188 
     1189 
     1190         
     1191        // 7. The number of visible pixels is the number of sample rays which see the source 
     1192        //    object from the corresponding viewcell -> remember these values for later update 
     1193        //   of the viewcell pvs - or update immediately? 
     1194 
     1195        queryIdx = 0; 
     1196 
     1197        for (vit = beam.mViewCells.begin(); vit != vit_end; ++ vit) 
     1198        { 
     1199                // fetch queries 
     1200                unsigned int pixelCount = mOcclusionQueries[queryIdx ++]->GetQueryResult(); 
     1201 
     1202                if (pixelCount) 
     1203                        Debug << "view cell " << (*vit)->GetId() << " visible pixels: " << pixelCount << endl; 
     1204        } 
     1205         
     1206 
     1207        // 8. Copmpute rendering statistics 
     1208        // In general it is not neccessary to remember to extract all the rays cast. I hope it 
     1209        // would be sufficient to gain only the intergral statistics about the new contributions 
     1210        // and so the rss tree would actually store no new rays (only the initial ones) 
     1211        // the subdivision of the tree would only be driven by the statistics (the glrender could 
     1212        // evaluate the contribution entropy for example) 
     1213        // However might be an option to extract/store only those the rays which made a contribution 
     1214        // (new viewcell has been discovered) or relative contribution greater than a threshold ...  
     1215 
     1216        ObjectContainer pvsObj; 
     1217        stat.pvsSize = ComputePvs(beam.mObjects, pvsObj); 
     1218         
     1219        // to gain ray source and termination 
     1220        // copy contents of ray termination buffer into depth texture 
     1221        // and compare with ray source buffer 
     1222#if 0 
     1223        VssRayContainer rays; 
     1224 
     1225        glBindTexture(GL_TEXTURE_2D, backDepthMap);      
     1226        glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, 0, depthMapSize, depthMapSize); 
     1227 
     1228        ComputeRays(Intersectable *sourceObj, rays); 
     1229 
     1230#endif 
     1231 
     1232 
     1233 
     1234        //-- cleanup 
     1235 
     1236 
     1237        // reset gl state 
     1238        glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE); 
     1239        glDepthMask(GL_TRUE); 
     1240        glEnable(GL_CULL_FACE); 
     1241        glDisable(GL_STENCIL_TEST); 
     1242#if USE_CG 
     1243        cgGLDisableProfile(sCgFragmentProfile); 
     1244#endif 
     1245        glDisable(GL_TEXTURE_2D); 
     1246 
     1247        // remove objects from beam 
     1248        if (beam.mFlags & !Beam::STORE_OBJECTS) 
     1249                beam.mObjects.clear(); 
     1250} 
     1251 
     1252 
     1253void GlRendererBuffer::SetupProjectionForViewPoint(const Vector3 &viewPoint,  
     1254                                                                                                   const Beam &beam,  
     1255                                                                                                   Intersectable *sourceObject) 
     1256{ 
     1257        float left, right, bottom, top, znear, zfar; 
     1258 
     1259        beam.ComputePerspectiveFrustum(left, right, bottom, top, znear, zfar, 
     1260                                                                   mSceneGraph->GetBox()); 
     1261 
     1262        //Debug << left << " " << right << " " << bottom << " " << top << " " << znear << " " << zfar << endl; 
     1263        glMatrixMode(GL_PROJECTION); 
     1264        glLoadIdentity(); 
     1265        glFrustum(left, right, bottom, top, znear, zfar); 
     1266        //glFrustum(-1, 1, -1, 1, 1, 20000); 
     1267 
     1268    const Vector3 center = viewPoint + beam.GetMainDirection() * (zfar - znear) * 0.3f; 
     1269        const Vector3 up =  
     1270                Normalize(CrossProd(beam.mPlanes[0].mNormal, beam.mPlanes[4].mNormal)); 
     1271 
     1272#ifdef GTP_DEBUG 
     1273        Debug << "view point: " << viewPoint << endl; 
     1274        Debug << "eye: " << center << endl; 
     1275        Debug << "up: " << up << endl; 
     1276#endif 
     1277 
     1278        glMatrixMode(GL_MODELVIEW); 
     1279        glLoadIdentity(); 
     1280        gluLookAt(viewPoint.x, viewPoint.y, viewPoint.z,  
     1281                          center.x, center.y, center.z,                    
     1282                          up.x, up.y, up.z); 
     1283}                
     1284 
     1285   
     1286void GlRendererBuffer::InitGL() 
     1287{ 
     1288  MakeCurrent();  
     1289  GlRenderer::InitGL(); 
     1290 
     1291#if 1 
     1292        // initialise dual depth buffer textures 
     1293        glGenTextures(1, &frontDepthMap); 
     1294        glBindTexture(GL_TEXTURE_2D, frontDepthMap); 
     1295         
     1296        glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, depthMapSize,  
     1297                                 depthMapSize, 0, GL_DEPTH_COMPONENT, GL_UNSIGNED_BYTE, NULL); 
     1298 
     1299        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); 
     1300        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); 
     1301        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP); 
     1302        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP); 
     1303 
     1304        glGenTextures(1, &backDepthMap); 
     1305        glBindTexture(GL_TEXTURE_2D, backDepthMap); 
     1306         
     1307        glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, depthMapSize,  
     1308                                 depthMapSize, 0, GL_DEPTH_COMPONENT, GL_UNSIGNED_BYTE, NULL); 
     1309        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); 
     1310        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); 
     1311        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP); 
     1312        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP); 
     1313 
     1314         
     1315#if USE_CG 
     1316        // cg initialization 
     1317        cgSetErrorCallback(handleCgError); 
     1318        sCgContext = cgCreateContext(); 
     1319         
     1320        if (cgGLIsProfileSupported(CG_PROFILE_ARBFP1)) 
     1321                sCgFragmentProfile = CG_PROFILE_ARBFP1; 
     1322        else  
     1323        { 
     1324          // try FP30 
     1325          if (cgGLIsProfileSupported(CG_PROFILE_FP30)) 
     1326            sCgFragmentProfile = CG_PROFILE_FP30; 
     1327          else  
     1328          { 
     1329                  Debug << "Neither arbfp1 or fp30 fragment profiles supported on this system" << endl; 
     1330                  exit(1); 
     1331          } 
     1332  } 
     1333 
     1334 sCgFragmentProgram = cgCreateProgramFromFile(sCgContext, 
     1335                                                                                           CG_SOURCE, "../src/dual_depth.cg", 
     1336                                                                                           sCgFragmentProfile, 
     1337                                                                                           NULL,  
     1338                                                                                           NULL); 
     1339 
     1340  if (!cgIsProgramCompiled(sCgFragmentProgram)) 
     1341          cgCompileProgram(sCgFragmentProgram); 
     1342 
     1343  cgGLLoadProgram(sCgFragmentProgram); 
     1344  cgGLBindProgram(sCgFragmentProgram); 
     1345 
     1346  Debug << "---- PROGRAM BEGIN ----\n" << 
     1347          cgGetProgramString(sCgFragmentProgram, CG_COMPILED_PROGRAM) << "---- PROGRAM END ----\n"; 
     1348 
     1349#endif 
     1350 
     1351#endif 
     1352  DoneCurrent(); 
     1353} 
     1354 
     1355 
     1356void GlRendererBuffer::ComputeRays(Intersectable *sourceObj, VssRayContainer &rays) 
     1357{ 
     1358        for (int i = 0; i < depthMapSize * depthMapSize; ++ i) 
     1359        { 
     1360                //todo glGetTexImage() 
     1361        } 
     1362} 
     1363 
     1364 
     1365 
     1366 
     1367 
     1368 
     1369 
     1370 
     1371 
     1372 
     1373 
     1374 
     1375} 
  • GTP/trunk/Lib/Vis/Preprocessing/src/GlRenderer.h

    r1608 r1785  
    190190                                 KdTree *tree); 
    191191 
     192  virtual ~GlRendererBuffer(); 
    192193 
    193194        /** Evaluates render cost of a point sample. 
     
    196197                @param threshold number of pixels / samples from where an object is considered visible. 
    197198        */ 
    198         void EvalRenderCostSample(RenderCostSample &sample, 
     199  virtual void EvalRenderCostSample(RenderCostSample &sample, 
    199200                                                          const bool useOcclusionQueries, 
    200201                                                          const int threshold); 
     
    208209                @param threshold number of pixels / samples from where an object is considered visible. 
    209210        */ 
    210         void SampleRenderCost(const int numSamples,  
    211                                                   vector<RenderCostSample> &samples, 
    212                                                   const bool useOcclusionQueries, 
    213                                                   const int threshold = 0) 
    214         {} 
     211  virtual void SampleRenderCost(const int numSamples,  
     212                                                                vector<RenderCostSample> &samples, 
     213                                                                const bool useOcclusionQueries, 
     214                                                                const int threshold = 0); 
     215   
    215216 
    216217        /** Implerment in subclasses. 
    217218        */ 
    218         void EvalPvsStat() {}; 
    219  
    220         void ClearErrorBuffer(); 
    221    
    222  
    223   virtual int GetWidth() const { return 0; } 
    224   virtual int GetHeight() const { return 0; } 
    225  
    226  
    227   void RandomViewPoint(); 
    228   void SampleBeamContributions( 
     219  virtual void EvalPvsStat(); 
     220 
     221  virtual void ClearErrorBuffer(); 
     222   
     223 
     224  virtual int GetWidth() const = 0; 
     225  virtual int GetHeight() const  = 0;  
     226 
     227  virtual void MakeCurrent() = 0; 
     228  virtual void DoneCurrent() = 0; 
     229   
     230 
     231  virtual void RandomViewPoint(); 
     232  virtual void SampleBeamContributions( 
    229233                                                           Intersectable *sourceObject, 
    230234                                                           Beam &beam, 
     
    233237                                                           ); 
    234238 
    235   void 
     239  virtual void 
    236240  SampleViewpointContributions( 
    237241                                                           Intersectable *sourceObject, 
     
    242246                                                           ); 
    243247 
    244   void InitGL(); 
     248  virtual void InitGL(); 
    245249 
    246250  /** Computes rays from information gained with hw sampling- 
    247251  */ 
    248   void ComputeRays(Intersectable *sourceObj, VssRayContainer &rays); 
    249  
    250   int ComputePvs() const; 
    251  
    252   float 
    253   GetPixelError(int &pvsSize); 
    254  
    255   int ComputePvs(ObjectContainer &objects, ObjectContainer &pvs) const; 
    256  
     252  virtual void ComputeRays(Intersectable *sourceObj, VssRayContainer &rays); 
     253 
     254  virtual int ComputePvs() const = 0; 
     255 
     256  virtual float 
     257  GetPixelError(int &pvsSize) = 0; 
     258   
     259  virtual int ComputePvs(ObjectContainer &objects, ObjectContainer &pvs) const = 0; 
     260   
    257261  PvsRenderStatistics mPvsStat; 
    258262    
     
    270274   
    271275protected: 
    272         unsigned int *mPixelBuffer; 
    273    
    274         static void GenQueries(const int numQueries); 
     276  unsigned int *mPixelBuffer; 
     277   
     278  static void GenQueries(const int numQueries); 
    275279         
    276         void SetupProjectionForViewPoint(const Vector3 &viewPoint,  
     280  virtual void SetupProjectionForViewPoint(const Vector3 &viewPoint,  
    277281                                                                         const Beam &beam,  
    278282 
     
    281285        /** Evaluates query for one direction using item buffer. 
    282286        */ 
    283         void EvalQueryWithItemBuffer(); 
     287  virtual void EvalQueryWithItemBuffer(); 
    284288 
    285289        /** Evaluates query for one direction using occlusion queries. 
    286290        */ 
    287         void EvalQueryWithOcclusionQueries(); 
     291  virtual void EvalQueryWithOcclusionQueries(); 
    288292 
    289293public: 
  • GTP/trunk/Lib/Vis/Preprocessing/src/Makefile

    r1761 r1785  
    11############################################################################# 
    22# Makefile for building: preprocessor 
    3 # Generated by qmake (2.00a) (Qt 4.1.2) on: Thu Nov 16 14:37:00 2006 
     3# Generated by qmake (2.00a) (Qt 4.1.2) on: ?t 23. XI 14:54:07 2006 
    44# Project:  preprocessor.pro 
    55# Template: app 
  • GTP/trunk/Lib/Vis/Preprocessing/src/OcclusionQuery.cpp

    r1145 r1785  
    88namespace GtpVisibilityPreprocessor { 
    99 
    10 bool OcclusionQuery::sUseArbQueries = true; 
     10bool OcclusionQuery::sUseArbQueries = false; 
    1111 
    1212 
  • GTP/trunk/Lib/Vis/Preprocessing/src/Preprocessor.cpp

    r1779 r1785  
    138138        // renderer will be constructed when the scene graph and viewcell manager will be known 
    139139        renderer = NULL; 
    140    
     140         
    141141        Environment::GetSingleton()->GetBoolValue("Preprocessor.delayVisibilityComputation", 
    142142                                                                                          mDelayVisibilityComputation); 
     
    705705        mViewCellsManager->SetRenderer(mRenderSimulator); 
    706706 
    707         if (mUseGlRenderer || mUseGlDebugger) 
    708         { 
    709                 // NOTE: render texture should be power of 2 and square 
    710                 // renderer must be initialised 
    711                 // $$matt 
    712                 //              renderer = new GlRendererBuffer(1024, 768, mSceneGraph, mViewCellsManager, mKdTree); 
    713                 //              renderer->makeCurrent(); 
    714  
    715         } 
    716  
     707         
    717708        mViewCellsManager->SetPreprocessor(this); 
    718709        return true; 
  • GTP/trunk/Lib/Vis/Preprocessing/src/Preprocessor.h

    r1771 r1785  
    204204  ofstream mStats; 
    205205 
     206  GlRendererBuffer *renderer; 
     207 
    206208protected: 
    207209 
     
    233235        vector<FaceParentInfo> mFaceParents; 
    234236 
    235         GlRendererBuffer *renderer; 
    236237 
    237238        /// if box around view space should be used 
  • 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} 
  • GTP/trunk/Lib/Vis/Preprocessing/src/QtGlRenderer.h

    r1757 r1785  
    4848                KdTree *tree); 
    4949 
    50         ~QtGlRendererBuffer(); 
    51  
    52         /** Evaluates render cost of a point sample. 
    53         @param sample the render cost sample to be evaluated 
    54         @param useOcclusionQueries if occlusion queries should be used or item buffer 
    55         @param threshold number of pixels / samples from where an object is considered visible. 
    56         */ 
    57         void EvalRenderCostSample(RenderCostSample &sample, 
    58                 const bool useOcclusionQueries, 
    59                 const int threshold); 
    60  
    61         /** Evaluates render cost of a number of point samples. The point samples 
    62         are distributed uniformly over the defined view space. 
    63  
    64         @param numSamples the number of point samples taken 
    65         @param samples stores the taken point samples in a container 
    66         @param useOcclusionQueries if occlusion queries should be used or item buffer 
    67         @param threshold number of pixels / samples from where an object is considered visible. 
    68         */ 
    69         void SampleRenderCost(const int numSamples,  
    70                 vector<RenderCostSample> &samples, 
    71                 const bool useOcclusionQueries, 
    72                 const int threshold = 0); 
    73  
    74  
    75   void 
    76   EvalPvsStat(); 
    77  
    78   void 
    79   ClearErrorBuffer(); 
    80    
     50  ~QtGlRendererBuffer() {} 
     51 
     52  virtual void MakeCurrent(); 
     53  virtual void DoneCurrent(); 
    8154 
    8255  virtual int GetWidth() const { return width(); } 
    8356  virtual int GetHeight() const { return height(); } 
    8457 
    85  
    86   
    87   void SampleBeamContributions( 
    88                                                            Intersectable *sourceObject, 
    89                                                            Beam &beam, 
    90                                                            const int samples, 
    91                                                            BeamSampleStatistics &stat 
    92                                                            ); 
    93  
    94   void 
    95   SampleViewpointContributions( 
    96                                                            Intersectable *sourceObject, 
    97                                                            const Vector3 viewPoint, 
    98                                                            Beam &beam, 
    99                                                            const int desiredSamples, 
    100                                                            BeamSampleStatistics &stat 
    101                                                            ); 
    102  
    103   void InitGL(); 
    104  
    105   /** Computes rays from information gained with hw sampling- 
    106   */ 
    107   void ComputeRays(Intersectable *sourceObj, VssRayContainer &rays); 
    108  
    109   int ComputePvs() const; 
    110  
     58  int ComputePvs() const { return 0; } 
     59   
    11160  float 
    11261  GetPixelError(int &pvsSize); 
    113  
     62   
    11463  int ComputePvs(ObjectContainer &objects, ObjectContainer &pvs) const; 
    11564      
    116     
    117 private: 
    118          
    119         void SetupProjectionForViewPoint(const Vector3 &viewPoint,  
    120                                                                          const Beam &beam,  
    121                                                                          Intersectable *sourceObject); 
    122  
    123  
     65   
     66   
     67   
    12468public: 
    12569  signals: 
    126         void UpdatePvsErrorItem(int i, GlRendererBuffer::PvsErrorEntry &); 
     70  void UpdatePvsErrorItem(int i, GlRendererBuffer::PvsErrorEntry &); 
    12771}; 
    12872 
  • GTP/trunk/Lib/Vis/Preprocessing/src/RssPreprocessor.cpp

    r1771 r1785  
    1515 
    1616 
    17 #define USE_RSS_TREE 1 
    1817 
    1918namespace GtpVisibilityPreprocessor { 
     
    3837  Environment::GetSingleton()->GetIntValue("RssPreprocessor.vssSamplesPerPass", mRssSamplesPerPass); 
    3938  Environment::GetSingleton()->GetBoolValue("RssPreprocessor.useImportanceSampling", mUseImportanceSampling); 
     39 
     40  Environment::GetSingleton()->GetBoolValue("RssPreprocessor.useRssTree", 
     41                                                                                        mUseRssTree); 
    4042 
    4143  Environment::GetSingleton()->GetBoolValue("RssPreprocessor.Export.pvs", mExportPvs); 
     
    4850 
    4951  Environment::GetSingleton()->GetBoolValue("RssPreprocessor.loadInitialSamples", mLoadInitialSamples); 
     52 
    5053  Environment::GetSingleton()->GetBoolValue("RssPreprocessor.storeInitialSamples", mStoreInitialSamples); 
     54 
    5155  Environment::GetSingleton()->GetBoolValue("RssPreprocessor.updateSubdivision", mUpdateSubdivision); 
    5256   
     
    353357        //      renderer->mRenderingFinished.wait(&mutex); 
    354358        //      mutex.unlock(); 
    355  
     359         
    356360        renderer->EvalPvsStat(); 
     361 
    357362        mStats << 
    358363          "#AvgPvsRenderError\n" <<renderer->mPvsStat.GetAvgError()<<endl<< 
     
    364369 
    365370void 
    366 NormalizeRatios(vector<SamplingStrategy *> distributions) 
     371NormalizeRatios(vector<SamplingStrategy *> &distributions) 
    367372{ 
    368373  int i; 
     
    397402        distributions[i]->mRatio/=sumRatios; 
    398403   
    399  
     404   
     405  cout<<"New ratios: "; 
     406  for (i=0 ; i < distributions.size(); i++) 
     407        cout<<distributions[i]->mRatio<<" "; 
     408  cout<<endl; 
    400409} 
    401410 
    402411void 
    403 NormalizeRatios(float ratios[4]) 
    404 { 
    405   int i; 
    406   float sumRatios; 
    407   sumRatios = ratios[0] + ratios[1] + ratios[2] + ratios[3]; 
    408   if (sumRatios == 0.0f) { 
    409         ratios[0] = ratios[1] = ratios[2] = 1.0f; 
    410         sumRatios = 4.0f; 
    411   } 
    412    
    413   for (i=0 ; i < 4; i++) 
    414         ratios[i]/=sumRatios; 
    415  
    416 #define MIN_RATIO 0.1f 
    417  
    418 #if USE_RSS_TREE 
    419   i = 0; 
    420 #else 
    421   i = 1; 
     412EvaluateRatios(vector<SamplingStrategy *> &distributions) 
     413{ 
     414  // now evaluate the ratios for the next pass 
     415#define TIME_WEIGHT 0.5f 
     416  for (int i=0; i < distributions.size(); i++) { 
     417        distributions[i]->mRatio = distributions[i]->mContribution/ 
     418          (TIME_WEIGHT*distributions[i]->mTime + 
     419           (1 - TIME_WEIGHT)*distributions[i]->mRays); 
     420 
     421#if 1 
     422        distributions[i]->mRatio = sqr(distributions[i]->mRatio); 
    422423#endif 
    423    
    424   for (; i < 4; i++) 
    425         if (ratios[i] < MIN_RATIO) 
    426           ratios[i] = MIN_RATIO; 
    427  
    428   sumRatios = ratios[0] + ratios[1] + ratios[2] + ratios[3]; 
    429   for (i=0 ; i < 4; i++) 
    430         ratios[i]/=sumRatios; 
    431    
    432 } 
     424  } 
     425} 
     426 
     427 
    433428 
    434429bool 
     
    443438  Randomize(0); 
    444439   
     440  // use ray buffer to export ray animation 
    445441  const bool useRayBuffer = false; 
    446442   
     
    468464  int rssSamples = 0; 
    469465 
    470   mDistributions.push_back(new RssBasedDistribution(*this)); 
    471   mDistributions.push_back(new SpatialBoxBasedDistribution(*this)); 
    472   mDistributions.push_back(new DirectionBasedDistribution(*this)); 
    473   mDistributions.push_back(new ReverseObjectBasedDistribution(*this)); 
     466  if (mUseRssTree) 
     467        mDistributions.push_back(new RssBasedDistribution(*this)); 
     468 
     469  //  mDistributions.push_back(new SpatialBoxBasedDistribution(*this)); 
     470  //  mDistributions.push_back(new DirectionBasedDistribution(*this)); 
     471  mDistributions.push_back(new ObjectDirectionBasedDistribution(*this)); 
     472  //  mDistributions.push_back(new ReverseObjectBasedDistribution(*this)); 
     473  mDistributions.push_back(new ObjectBasedDistribution(*this)); 
     474  mDistributions.push_back(new ReverseViewSpaceBorderBasedDistribution(*this)); 
    474475   
    475476  if (mLoadInitialSamples) { 
     
    483484         
    484485        if (mUseImportanceSampling) { 
    485           GenerateRays(mInitialSamples/3, SamplingStrategy::SPATIAL_BOX_BASED_DISTRIBUTION, rays); 
    486           //      GenerateRays(mInitialSamples/3, SamplingStrategy::OBJECT_BASED_DISTRIBUTION, rays); 
    487           GenerateRays(mInitialSamples/3, SamplingStrategy::DIRECTION_BASED_DISTRIBUTION, rays); 
    488           GenerateRays(mInitialSamples/3, SamplingStrategy::REVERSE_OBJECT_BASED_DISTRIBUTION, rays); 
    489           //    GenerateRays(mInitialSamples/4, OBJECT_DIRECTION_BASED_DISTRIBUTION, rays); 
    490            
     486          int count = 0; 
     487          int i; 
     488 
     489          // Generate initial samples 
     490          for (i=0; i < mDistributions.size(); i++) 
     491                if (mDistributions[i]->mType != SamplingStrategy::RSS_BASED_DISTRIBUTION) 
     492                  count++; 
     493           
     494          for (i=0; i < mDistributions.size(); i++) 
     495                if (mDistributions[i]->mType != SamplingStrategy::RSS_BASED_DISTRIBUTION) 
     496                  GenerateRays(mInitialSamples/count, 
     497                                           mDistributions[i]->mType, rays); 
    491498 
    492499        } else { 
     
    584591  if (mUseImportanceSampling) { 
    585592 
    586 #if USE_RSS_TREE         
    587         mRssTree->Construct(mObjects, mVssRays); 
    588 #endif 
    589          
    590         mRssTree->stat.Print(mStats); 
    591         cout<<"RssTree root PVS size = "<<mRssTree->GetRootPvsSize()<<endl; 
    592          
    593         if (mExportRssTree) { 
    594           ExportRssTree("rss-tree-100.x3d", mRssTree, Vector3(1,0,0)); 
    595           ExportRssTree("rss-tree-001.x3d", mRssTree, Vector3(0,0,1)); 
    596           ExportRssTree("rss-tree-101.x3d", mRssTree, Vector3(1,0,1)); 
    597           ExportRssTree("rss-tree-101m.x3d", mRssTree, Vector3(-1,0,-1)); 
    598           ExportRssTreeLeaves(mRssTree, 10); 
    599         } 
    600          
     593        if (mUseRssTree) { 
     594          mRssTree->Construct(mObjects, mVssRays); 
     595         
     596          mRssTree->stat.Print(mStats); 
     597          cout<<"RssTree root PVS size = "<<mRssTree->GetRootPvsSize()<<endl; 
     598           
     599          if (mExportRssTree) { 
     600                ExportRssTree("rss-tree-100.x3d", mRssTree, Vector3(1,0,0)); 
     601                ExportRssTree("rss-tree-001.x3d", mRssTree, Vector3(0,0,1)); 
     602                ExportRssTree("rss-tree-101.x3d", mRssTree, Vector3(1,0,1)); 
     603                ExportRssTree("rss-tree-101m.x3d", mRssTree, Vector3(-1,0,-1)); 
     604                ExportRssTreeLeaves(mRssTree, 10); 
     605          } 
     606        } 
     607 
    601608        if (mExportPvs) { 
    602609          ExportPvs("rss-pvs-initial.x3d", mRssTree); 
     
    621628        if (mUseImportanceSampling) { 
    622629 
    623 #if USE_RSS_TREE         
    624           static float ratios[] = {0.9f,0.05f,0.05f,0.05f}; 
    625 #else 
    626           static float ratios[] = {0.0f,0.05f,0.05f,0.05f}; 
    627 #endif 
    628           NormalizeRatios(ratios); 
    629                    
    630           cout<<"New ratios: "<<ratios[0]<<" "<<ratios[1]<<" "<<ratios[2]<<" "<<ratios[3]<<endl; 
    631  
    632           //float ratios[] = {1.0f,0.0f,0.0f}; 
    633            
    634           int nrays[4]; 
    635           float contributions[4]; 
    636           float times[4]; 
     630          NormalizeRatios(mDistributions); 
    637631           
    638632          long t1; 
    639633 
    640           t1 = GetTime(); 
    641            
    642           GenerateRays(int(mRssSamplesPerPass*ratios[0]), 
    643                                    SamplingStrategy::RSS_BASED_DISTRIBUTION, 
    644                                    rays); 
    645            
    646           rays.NormalizePdf((float)rays.size()); 
    647            
    648           CastRays(rays, tmpVssRays, true); 
    649           castRays += (int)rays.size(); 
    650 #if ADD_RAYS_IN_PLACE  
    651           contributions[0] = mViewCellsManager->ComputeSampleContributions(tmpVssRays, true, false); 
    652 #else 
    653           contributions[0] = mViewCellsManager->ComputeSampleContributions(tmpVssRays, false, true); 
    654 #endif 
    655           times[0] = TimeDiff(t1, GetTime()); 
    656           nrays[0] = (int)rays.size(); 
    657            
    658           mStats<<"#RssRelContrib"<<endl<<contributions[0]/nrays[0]<<endl; 
    659            
    660           vssRays.insert(vssRays.end(), tmpVssRays.begin(), tmpVssRays.end() ); 
    661           rays.clear(); 
    662           tmpVssRays.clear(); 
    663           if (ratios[1]!=0.0f) { 
     634          for (int i=0; i < mDistributions.size(); i++) { 
    664635                t1 = GetTime(); 
    665                 GenerateRays(int(mRssSamplesPerPass*ratios[1]), SamplingStrategy::SPATIAL_BOX_BASED_DISTRIBUTION, rays); 
    666                 CastRays(rays, tmpVssRays, true); 
    667                 castRays += (int)rays.size(); 
    668 #if ADD_RAYS_IN_PLACE  
    669                 contributions[1] = mViewCellsManager->ComputeSampleContributions(tmpVssRays, true, false); 
    670 #else 
    671                 contributions[1] = mViewCellsManager->ComputeSampleContributions(tmpVssRays, false, true); 
    672 #endif 
    673           times[1] = TimeDiff(t1, GetTime()); 
    674           nrays[1] = (int)rays.size(); 
    675  
    676           mStats<<"#SpatialRelContrib"<<endl<<contributions[1]/nrays[1]<<endl; 
    677                  
    678                 vssRays.insert(vssRays.end(), tmpVssRays.begin(), tmpVssRays.end() ); 
    679                  
    680636                rays.clear(); 
    681637                tmpVssRays.clear(); 
     638                if (mDistributions[i]->mRatio != 0) { 
     639                  GenerateRays(int(mRssSamplesPerPass*mDistributions[i]->mRatio), 
     640                                           mDistributions[i]->mType, 
     641                                           rays); 
     642                   
     643                  rays.NormalizePdf((float)rays.size()); 
     644                   
     645                  CastRays(rays, tmpVssRays, true); 
     646                  castRays += (int)rays.size(); 
     647                   
     648#if ADD_RAYS_IN_PLACE  
     649                  mDistributions[i]->mContribution = 
     650                        mViewCellsManager->ComputeSampleContributions(tmpVssRays, true, false); 
     651#else 
     652                  mDistributions[i]->mContribution = 
     653                        mViewCellsManager->ComputeSampleContributions(tmpVssRays, false, true); 
     654#endif 
     655                } 
     656 
     657                mDistributions[i]->mTime = TimeDiff(t1, GetTime()); 
     658                mDistributions[i]->mRays = (int)rays.size(); 
     659                //              mStats<<"#RssRelContrib"<<endl<<contributions[0]/nrays[0]<<endl; 
     660                vssRays.insert(vssRays.end(), tmpVssRays.begin(), tmpVssRays.end() ); 
    682661          } 
    683            
    684            
    685           if (ratios[2]!=0.0f) { 
    686                 t1 = GetTime(); 
    687                 GenerateRays(int(mRssSamplesPerPass*ratios[2]), SamplingStrategy::DIRECTION_BASED_DISTRIBUTION, rays); 
    688                 CastRays(rays, tmpVssRays, true); 
    689                 castRays += (int)rays.size(); 
    690 #if ADD_RAYS_IN_PLACE  
    691                 contributions[2] = mViewCellsManager->ComputeSampleContributions(tmpVssRays, true, false); 
    692 #else 
    693                 contributions[2] = mViewCellsManager->ComputeSampleContributions(tmpVssRays, false, true); 
    694 #endif 
    695                 times[2] = TimeDiff(t1, GetTime()); 
    696                 nrays[2] = (int)rays.size(); 
    697                  
    698                 mStats<<"#DirectionalRelContrib"<<endl<<contributions[2]/nrays[2]<<endl; 
    699                  
    700                 vssRays.insert(vssRays.end(), tmpVssRays.begin(), tmpVssRays.end() ); 
    701                  
    702                 rays.clear(); 
    703                 tmpVssRays.clear(); 
    704           } 
    705            
    706           if (ratios[3]!=0.0f) { 
    707                 t1 = GetTime(); 
    708                 GenerateRays(int(mRssSamplesPerPass*ratios[3]), 
    709                                          SamplingStrategy::REVERSE_OBJECT_BASED_DISTRIBUTION, rays); 
    710                 CastRays(rays, tmpVssRays, true); 
    711                 castRays += (int)rays.size(); 
    712 #if ADD_RAYS_IN_PLACE  
    713                 contributions[3] = mViewCellsManager->ComputeSampleContributions(tmpVssRays, true, false); 
    714 #else 
    715                 contributions[3] = mViewCellsManager->ComputeSampleContributions(tmpVssRays, false, true); 
    716 #endif 
    717                 times[3] = TimeDiff(t1, GetTime()); 
    718                 nrays[3] = (int)rays.size(); 
    719                  
    720                 mStats<<"#DirectionalRelContrib"<<endl<<contributions[3]/nrays[3]<<endl; 
    721                  
    722                 vssRays.insert(vssRays.end(), tmpVssRays.begin(), tmpVssRays.end() ); 
    723                  
    724                 rays.clear(); 
    725                 tmpVssRays.clear(); 
    726           } 
    727  
    728            
    729           // now evaluate the ratios for the next pass 
    730 #define TIME_WEIGHT 0.5f 
    731  
    732 #if 1 
    733           ratios[0] = sqr(contributions[0]/(TIME_WEIGHT*times[0] + (1 - TIME_WEIGHT)*nrays[0])); 
    734           ratios[1] = sqr(contributions[1]/(TIME_WEIGHT*times[1] + (1 - TIME_WEIGHT)*nrays[1])); 
    735           ratios[2] = sqr(contributions[2]/(TIME_WEIGHT*times[2] + (1 - TIME_WEIGHT)*nrays[2])); 
    736           ratios[3] = sqr(contributions[3]/(TIME_WEIGHT*times[3] + (1 - TIME_WEIGHT)*nrays[3])); 
    737 #else 
    738           ratios[0] = contributions[0]/(TIME_WEIGHT*times[0] + (1 - TIME_WEIGHT)*nrays[0]); 
    739           ratios[1] = contributions[1]/(TIME_WEIGHT*times[1] + (1 - TIME_WEIGHT)*nrays[1]); 
    740           ratios[2] = contributions[2]/(TIME_WEIGHT*times[2] + (1 - TIME_WEIGHT)*nrays[2]); 
    741           ratios[3] = contributions[3]/(TIME_WEIGHT*times[3] + (1 - TIME_WEIGHT)*nrays[3]); 
    742            
    743 #endif     
    744  
    745 #if !USE_RSS_TREE 
    746           ratios[0] = 0; 
    747 #endif 
     662 
     663          EvaluateRatios(mDistributions); 
    748664           
    749665          // add contributions of all rays at once... 
     
    803719        } 
    804720 
    805         if (0 && mPass > 0) { 
    806                 char buf[100]; 
     721 
     722        if (renderer && mPass > 0) { 
     723          char buf[100]; 
    807724          if (mUseImportanceSampling) 
    808           { 
    809                 sprintf(buf, "snap/i-%02d-", mPass); 
    810  
    811                 renderer->SetSnapPrefix(buf); 
    812           } 
     725                { 
     726                  sprintf(buf, "snap/i-%02d-", mPass); 
     727                   
     728                  renderer->SetSnapPrefix(buf); 
     729                } 
    813730          else 
    814           { 
    815                 sprintf(buf, "snap/r-%02d-", mPass); 
    816  
    817                 renderer->SetSnapPrefix(buf); 
    818           } 
    819  
     731                { 
     732                  sprintf(buf, "snap/r-%02d-", mPass); 
     733                   
     734                  renderer->SetSnapPrefix(buf); 
     735                } 
     736           
    820737          renderer->SetSnapErrorFrames(true); 
    821738        } 
    822  
     739         
    823740        ComputeRenderError(); 
    824741         
     
    845762          sprintf(filename, "rss-crays-%04d.x3d", rssPass); 
    846763          ExportRays(filename, contributingRays, mExportNumRays); 
    847            
    848764        } 
    849765 
     
    853769        // do not add those rays which have too low or no contribution.... 
    854770 
    855 #if USE_RSS_TREE         
    856         if (mUseImportanceSampling) { 
     771         
     772        if (mUseRssTree && mUseImportanceSampling) { 
    857773          Debug<<"Adding rays...\n"<<flush; 
    858774          mRssTree->AddRays(vssRays); 
     
    869785          } 
    870786        } 
    871 #endif 
    872787         
    873788        if (mExportPvs) { 
     
    878793         
    879794         
    880         if (!mUseImportanceSampling) 
     795        if (!mUseImportanceSampling || !mUseRssTree) 
    881796          CLEAR_CONTAINER(vssRays); 
    882797        // otherwise the rays get deleted by the rss tree update according to RssTree.maxRays .... 
    883  
     798         
    884799        if (totalSamples >= mRssSamples + mInitialSamples) 
    885800          break; 
     
    889804        mPass++; 
    890805        mRssTree->SetPass(mPass); 
    891  
    892          
    893806  } 
    894807   
    895808  if (mUseViewcells) { 
    896809 
    897         if(0) 
    898           { 
    899                 VssRayContainer selectedRays; 
    900                 int desired = mViewCellsManager->GetVisualizationSamples(); 
    901                  
    902                 mVssRays.SelectRays(desired, selectedRays); 
    903                  
    904                 mViewCellsManager->Visualize(mObjects, selectedRays); 
     810        if(0) { 
     811          VssRayContainer selectedRays; 
     812          int desired = mViewCellsManager->GetVisualizationSamples(); 
     813           
     814          mVssRays.SelectRays(desired, selectedRays); 
     815           
     816          mViewCellsManager->Visualize(mObjects, selectedRays); 
    905817          } 
    906818         
    907819        // view cells after sampling 
    908820        mViewCellsManager->PrintStatistics(Debug); 
    909  
     821         
    910822        EvalViewCellHistogram(); 
    911  
     823         
    912824        //-- render simulation after merge 
    913825        cout << "\nevaluating bsp view cells render time after sampling ... "; 
     
    920832        cout << ss << endl; 
    921833        Debug << ss << endl; 
    922          
    923834  } 
    924835 
  • GTP/trunk/Lib/Vis/Preprocessing/src/RssPreprocessor.h

    r1771 r1785  
    2727  int mRssSamples; 
    2828  bool mUseImportanceSampling; 
     29  bool mUseRssTree; 
    2930 
    3031  bool mExportPvs; 
  • GTP/trunk/Lib/Vis/Preprocessing/src/RssTree.cpp

    r1757 r1785  
    21792179  int startIndex = (int)rays.size(); 
    21802180 
    2181   Debug<<"B"<<flush; 
     2181  //  Debug<<"B"<<flush; 
    21822182 
    21832183  AxisAlignedBox3 box = GetBBox(leaf); 
     
    21922192  } 
    21932193 
    2194   Debug<<"R"<<flush; 
     2194  //  Debug<<"R"<<flush; 
    21952195   
    21962196  for (int i=0; i < numberOfRays; i++) { 
     
    22292229        origin = box.GetPoint(pVector); 
    22302230        dirVector = dirBox.GetPoint(dVector); 
    2231  
     2231         
    22322232        direction = Vector3(sin(dirVector.x), sin(dirVector.y), cos(dirVector.x)); 
    2233  
    22342233         
    22352234         
     
    22532252#if 0 
    22542253        static int counter = 0; 
    2255         Debug<<counter++<<flush; 
     2254        //      Debug<<counter++<<flush; 
    22562255 
    22572256        if (counter > 10374968) { 
     
    22682267  } 
    22692268 
    2270   Debug<<"D"<<flush; 
     2269  //  Debug<<"D"<<flush; 
    22712270   
    22722271  float density = 1.0f; 
     
    26922691   
    26932692  // always generate at leat n ray per leaf 
    2694   int fixedPerLeaf = 0; 
     2693  int fixedPerLeaf = 1; 
    26952694  // int fixedPerLeaf = 0; 
    26962695  int fixed = fixedPerLeaf*(int)leaves.size(); 
    2697   int iGenerated = numberOfRays; 
     2696 
     2697  // check if the number of fixed is not too big 
     2698 
     2699  if (fixed >= numberOfRays/2) { 
     2700        fixedPerLeaf = 0; 
     2701        fixed = 0; 
     2702  } 
     2703   
     2704  int iGenerated = numberOfRays - fixed; 
    26982705  float ratioPerLeaf = iGenerated /(avgContrib*numberOfLeaves); 
    2699  
     2706   
    27002707  k = 0; 
    27012708 
     
    27492756} 
    27502757 
    2751 float weightAbsContributions = 0.0f; 
     2758float weightAbsContributions = ABS_CONTRIBUTION_WEIGHT; 
    27522759// if small very high importance of the last sample 
    27532760// if 1.0f then weighs = 1 1/2 1/3 1/4 
    27542761float passSampleWeightDecay = 1.0f; 
    27552762//float passSampleWeightDecay = 0.0001f; 
    2756  
    27572763 
    27582764float 
  • GTP/trunk/Lib/Vis/Preprocessing/src/SamplingStrategy.cpp

    r1772 r1785  
    1111 
    1212SamplingStrategy::SamplingStrategy(const Preprocessor &preprocessor):  
    13 mPreprocessor(preprocessor) 
     13  mPreprocessor(preprocessor), mRatio(1.0f) 
    1414{ 
    1515} 
  • GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.cpp

    r1784 r1785  
    2626 
    2727 
    28 #define ABS_CONTRIBUTION_WEIGHT 1.0f 
    2928 
    3029// warning: Should not count origin object for sampling  
     
    3332 
    3433// $$JB HACK 
    35 #define USE_KD_PVS 0 
     34#define USE_KD_PVS 1 
    3635#define KD_PVS_AREA (1e-5f) 
    3736 
     
    18261825                 
    18271826                stat.avgPvs += pvsCost; 
    1828                  
    1829                 const bool evaluateFilter = true; 
     1827 
     1828                bool evaluateFilter; 
     1829                Environment::GetSingleton()->GetBoolValue("Preprocessor.evaluateFilter", evaluateFilter); 
     1830                 
    18301831                 
    18311832                if (evaluateFilter) { 
    18321833                  ObjectPvs filteredPvs = viewcell->GetPvs(); 
    1833                   PvsFilterStatistics fstat = ApplyFilter2(viewcell, false, 1.0f, filteredPvs); 
     1834                  PvsFilterStatistics fstat = ApplyFilter2(viewcell, false, 
     1835                                                                                                   1.0f, filteredPvs); 
    18341836                   
    18351837                  float filteredCost = filteredPvs.EvalPvsCost(); 
     
    20322034        ViewCell::NewMail(); 
    20332035 
     2036        //      if (ray.mPdf!=1.0f) 
     2037        //        cout<<ray.mPdf<<" "; 
     2038         
     2039         
    20342040        // traverse the view space subdivision 
    20352041        CastLineSegment(origin, termination, viewcells); 
     
    58525858        //Ray hray(ray); 
    58535859 
     5860        //      if (ray.mPdf!=1.0f) 
     5861        //        cout<<ray.mPdf<<" "; 
     5862 
    58545863        float tmin = 0, tmax = 1.0; 
    58555864 
     5865         
    58565866        if (!GetViewSpaceBox().GetRaySegment(hray, tmin, tmax) || (tmin > tmax)) 
    58575867                return 0; 
     
    58675877        if (storeViewCells) 
    58685878        {        
    5869                 // copy viewcells memory efficiently 
    5870                 ray.mViewCells.reserve(viewcells.size()); 
    5871                 ray.mViewCells = viewcells; 
     5879          // copy viewcells memory efficiently 
     5880          ray.mViewCells.reserve(viewcells.size()); 
     5881          ray.mViewCells = viewcells; 
    58725882        } 
    58735883 
     
    58775887        Intersectable *originObj = GetIntersectable(ray, false); 
    58785888           
    5879         for (; it != viewcells.end(); ++ it)  
    5880         { 
    5881                 ViewCell *viewcell = *it; 
    5882  
    5883                 if (viewcell->GetValid()) 
     5889        for (; it != viewcells.end(); ++ it) { 
     5890          ViewCell *viewcell = *it; 
     5891           
     5892          if (viewcell->GetValid()) 
    58845893                {       // if ray not outside of view space 
    5885                         float contribution; 
    5886  
    5887                         if (terminationObj)  
     5894                  float contribution; 
     5895                   
     5896                  if (terminationObj)  
    58885897                        { 
    5889                                 // todo: maybe not correct for kd node pvs 
    5890                                 if (viewcell->GetPvs().GetSampleContribution( 
    5891                                                 terminationObj, ray.mPdf, contribution)) 
     5898                          // todo: maybe not correct for kd node pvs 
     5899                          if (viewcell->GetPvs().GetSampleContribution( 
     5900                                                                                                                   terminationObj, ray.mPdf, contribution)) 
    58925901                                { 
    5893                                         ++ ray.mPvsContribution; 
     5902                                  ++ ray.mPvsContribution; 
    58945903                                } 
    5895  
    5896                                 ray.mRelativePvsContribution += contribution; 
     5904                           
     5905                          ray.mRelativePvsContribution += contribution; 
    58975906                        } 
    58985907                         
    5899                         //////////////// 
    5900                         //-- for directional sampling it is important to count  
    5901                         //-- only contributions made in one direction! 
    5902                         //-- the other contributions of this sample will be counted for the opposite ray! 
    5903  
     5908                  //////////////// 
     5909                  //-- for directional sampling it is important to count  
     5910                  //-- only contributions made in one direction! 
     5911                  //-- the other contributions of this sample will be counted for the opposite ray! 
     5912                   
    59045913#if SAMPLE_ORIGIN_OBJECTS 
    5905  
    5906                         if (originObj &&  
    5907                                 viewcell->GetPvs().GetSampleContribution(originObj, 
    5908                                                                                                                 ray.mPdf, 
    5909                                                                                                                 contribution)) 
    5910                           { 
    5911                                 ++ ray.mPvsContribution; 
    5912                                 ray.mRelativePvsContribution += contribution; 
    5913                           } 
     5914                   
     5915                  if (originObj &&  
     5916                          viewcell->GetPvs().GetSampleContribution(originObj, 
     5917                                                                                                          ray.mPdf, 
     5918                                                                                                          contribution)) 
     5919                        { 
     5920                          ++ ray.mPvsContribution; 
     5921                          ray.mRelativePvsContribution += contribution; 
     5922                        } 
    59145923#endif 
    59155924                } 
    59165925        } 
    5917  
     5926         
    59185927        if (!addRays) 
    59195928        { 
  • GTP/trunk/Lib/Vis/Preprocessing/src/VssRay.h

    r1771 r1785  
    1414class KdNode; 
    1515 
     16#define ABS_CONTRIBUTION_WEIGHT 0.8f 
    1617 
    1718class VssRay { 
  • GTP/trunk/Lib/Vis/Preprocessing/src/default.env

    r1771 r1785  
    77 
    88#filename ../data/Arena/arena-low-lods.obj 
    9 # filename ../data/Arena/arena-high-lods.obj 
     9 filename ../data/Arena/arena-high-lods.obj 
    1010# filename ../data/City4M/City4M.obj 
    1111# filename ../data/CityModel/CityModel.obj 
     
    5555        quitOnFinish true 
    5656        computeVisibility true 
     57 
     58        evaluateFilter false 
     59 
    5760        applyVisibilityFilter false 
    5861        applyVisibilitySpatialFilter false 
     
    8992        initialSamples 3000000 
    9093        vssSamples 50000000 
    91 #       vssSamples 1000000 
    9294        vssSamplesPerPass 5000000 
    9395        useImportanceSampling true 
     
    107109        loadInitialSamples false 
    108110        storeInitialSamples false 
     111 
     112        useRssTree false 
    109113} 
    110114 
     
    158162                minCost 1 
    159163                maxDepth 30 
    160                 maxCostRatio 0.95 
     164                maxCostRatio 0.98 
    161165                ct_div_ci 0.5 
    162                 maxNodes 200000 
     166                maxNodes 500000 
    163167#500000  
    164168        } 
     
    271275#       filename ../data/soda/soda5-viewcells.xml 
    272276#       filename ../scripts/viewcells_atlanta.xml 
    273 #       filename ../data/soda/soda-viewcells-5000.xml 
     277#       filename ../data/soda/soda-viewcells.xml.gz 
    274278#       filename ../data/test1/test-viewcells.xml 
    275279 
  • GTP/trunk/Lib/Vis/Preprocessing/src/main.cpp

    r1766 r1785  
    313313                } 
    314314 
    315                 qApp->exec();    
     315                int frames; 
     316                Environment::GetSingleton()->GetIntValue("Preprocessor.pvsRenderErrorSamples", frames); 
     317                if (frames) { 
     318                  // NOTE: render texture should be power of 2 and square 
     319                  // renderer must be initialised 
     320                  // $$matt 
     321                  preprocessor->renderer =  
     322                        new QtGlRendererBuffer(512, 512, 
     323                                                                   preprocessor->mSceneGraph, 
     324                                                                   preprocessor->mViewCellsManager, 
     325                                                                   preprocessor->mKdTree); 
     326                  //            preprocessor->Getrenderer->makeCurrent(); 
     327                } 
     328                qApp->exec(); 
    316329#endif 
    317330          } 
    318          
     331 
    319332        if (!guiSupported) { 
    320333          preprocessor->mUseGlRenderer = false; 
  • GTP/trunk/Lib/Vis/Preprocessing/src/run_test2

    r1771 r1785  
    22 
    33#COMMAND="./release/preprocessor.exe -preprocessor_quit_on_finish+" 
    4 COMMAND="../scripts/preprocessor.sh -preprocessor_quit_on_finish+ -preprocessor_use_gl_renderer-" 
     4COMMAND="../scripts/preprocessor.sh -preprocessor_quit_on_finish+ -preprocessor_use_gl_renderer- -preprocessor_evaluate_filter+" 
    55 
    66#SCENE="../data/vienna/vienna-buildings.x3d;../data/vienna/vienna-roofs.x3d;../data/vienna/vienna-roads.x3d" 
     
    2929VIEWCELLS=../data/vienna/vienna_cropped-gradient-viewcells.xml.gz 
    3030 
    31 PREFIX=../work/plots/osp-rss-1e5 
     31PREFIX=../work/plots/osp-rss-1e5-n 
    3232 
    3333#SCENE=../data/atlanta/atlanta2.x3d 
     
    3535 
    3636 
    37 $COMMAND -preprocessor=sampling -scene_filename=$SCENE -view_cells_filename=$VIEWCELLS \ 
    38 -view_cells_filter_max_size=1 -preprocessor_stats=$PREFIX-r-reference.log \ 
    39 -preprocessor_histogram_file=$PREFIX-r-reference.hlog 
     37# $COMMAND -preprocessor=sampling -scene_filename=$SCENE -view_cells_filename=$VIEWCELLS \ 
     38# -view_cells_filter_max_size=1 -preprocessor_stats=$PREFIX-r-reference.log \ 
     39# -preprocessor_histogram_file=$PREFIX-r-reference.hlog 
    4040 
    4141 
     
    4747 
    4848 
    49 ####### 
    50 ####### 
    51 $COMMAND -scene_filename=$SCENE -view_cells_filename=$VIEWCELLS \ 
    52 -rss_use_importance- -rss_object_based_sampling- -rss_directional_sampling+ \ 
    53 -view_cells_filter_max_size=1 -preprocessor_stats=$PREFIX-r-directional-based-b3.log \ 
    54 -preprocessor_histogram_file=$PREFIX-r-directional-based-b3.hlog 
     49# ####### 
     50# ####### 
     51# $COMMAND -scene_filename=$SCENE -view_cells_filename=$VIEWCELLS \ 
     52# -rss_use_importance- -rss_object_based_sampling- -rss_directional_sampling+ \ 
     53# -view_cells_filter_max_size=1 -preprocessor_stats=$PREFIX-r-directional-based-b3.log \ 
     54# -preprocessor_histogram_file=$PREFIX-r-directional-based-b3.hlog 
     55 
     56# $COMMAND -scene_filename=$SCENE -view_cells_filename=$VIEWCELLS \ 
     57# -rss_use_importance- -rss_object_based_sampling- -rss_directional_sampling- \ 
     58# -view_cells_filter_max_size=1 -preprocessor_stats=$PREFIX-r-spatial-based-bb3.log \ 
     59# -preprocessor_histogram_file=$PREFIX-r-spatial-based-bb3.hlog 
     60 
    5561 
    5662$COMMAND -scene_filename=$SCENE -view_cells_filename=$VIEWCELLS \ 
    57 -rss_use_importance- -rss_object_based_sampling- -rss_directional_sampling- \ 
    58 -view_cells_filter_max_size=1 -preprocessor_stats=$PREFIX-r-spatial-based-bb3.log \ 
    59 -preprocessor_histogram_file=$PREFIX-r-spatial-based-bb3.hlog 
     63 -rss_use_importance+ -rss_use_rss_tree- -view_cells_filter_max_size=1 \ 
     64 -preprocessor_stats=$PREFIX-i-mixed-b1.log \ 
     65 -preprocessor_histogram_file=$PREFIX-i-mixed-b1.hlog 
    6066 
    61  
    62  
Note: See TracChangeset for help on using the changeset viewer.