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

merge, filter update, renderebuffer made functional

File:
1 edited

Legend:

Unmodified
Added
Removed
  • GTP/trunk/Lib/Vis/Preprocessing/src/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} 
Note: See TracChangeset for help on using the changeset viewer.