Ignore:
Timestamp:
01/15/06 04:23:51 (18 years ago)
Author:
mattausch
Message:
 
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/VUT/GtpVisibilityPreprocessor/src/GlRenderer.cpp

    r538 r540  
    1616static CGprofile sCgFragmentProfile; 
    1717 
    18 GLuint depthMap; 
     18GLuint frontDepthMap; 
     19GLuint backDepthMap; 
     20 
    1921const int depthMapSize = 512; 
    2022static vector<int> sQueries; 
     
    99101} 
    100102 
     103 
     104int GlRenderer::GetId(int r, int g, int b) const 
     105{ 
     106        return r + (g << 8) + (b << 16); 
     107} 
     108 
    101109void 
    102110GlRenderer::SetupMaterial(Material *m) 
     
    152160  glGetOcclusionQueryuivNV = (PFNGLGETOCCLUSIONQUERYUIVNVPROC) 
    153161        wglGetProcAddress("glGetOcclusionQueryuivNV"); 
    154  
    155   // initialise second depth buffer texture 
    156   glGenTextures(1, &depthMap); 
    157   glBindTexture(GL_TEXTURE_2D, depthMap); 
    158    
    159   glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, depthMapSize,  
    160                            depthMapSize, 0, GL_DEPTH_COMPONENT, GL_UNSIGNED_BYTE, NULL); 
    161   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); 
    162   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); 
    163   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP); 
    164   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP); 
    165  
    166   // cg initialization 
    167   cgSetErrorCallback(handleCgError); 
    168   sCgContext = cgCreateContext(); 
    169  
    170   if (cgGLIsProfileSupported(CG_PROFILE_ARBFP1)) 
    171           sCgFragmentProfile = CG_PROFILE_ARBFP1; 
    172   else { 
    173           // try FP30 
    174           if (cgGLIsProfileSupported(CG_PROFILE_FP30)) 
    175             sCgFragmentProfile = CG_PROFILE_FP30; 
    176           else { 
    177                   fprintf(stderr, "Neither arbfp1 or fp30 fragment profiles supported on this system.\n"); 
    178                   exit(1); 
    179           } 
    180   } 
    181  
    182   sCgFragmentProgram = cgCreateProgramFromFile(sCgContext,               
    183                                                                                            CG_SOURCE, "../src/dual_depth.cg", 
    184                                                                                            sCgFragmentProfile, 
    185                                                                                            NULL,  
    186                                                                                            NULL); 
    187  
    188   if (!cgIsProgramCompiled(sCgFragmentProgram)) 
    189           cgCompileProgram(sCgFragmentProgram); 
    190  
    191   cgGLLoadProgram(sCgFragmentProgram); 
    192   cgGLBindProgram(sCgFragmentProgram); 
    193  // Debug << "LAST LISTING----" << cgGetLastListing(sCgContext) << "----\n"; 
    194  
    195   Debug << "---- PROGRAM BEGIN ----\n" <<  
    196           cgGetProgramString(sCgFragmentProgram, CG_COMPILED_PROGRAM) << "---- PROGRAM END ----\n"; 
    197162} 
    198163 
     
    617582                                                                                           BeamSampleStatistics &stat) 
    618583{ 
     584        // create beam mesh if not already doen 
     585        //TODO: should this be done here? 
     586         
     587        beam.CreateMesh(2.0f * Magnitude(mSceneGraph->GetBox().Diagonal())); 
     588 
    619589        // TODO: should not be done every time here 
    620590        // only back faces are interesting for the depth pass 
     
    636606        // would distribute the 'efective viewpoints' of the object surface and thus 
    637607        // with a few viewpoints better sample the vipoint space.... 
    638    
    639         int viewPointSamples = sqrt((float)desiredSamples); 
    640  
     608  //TODO: remove 
     609        //int viewPointSamples = sqrt((float)desiredSamples); 
     610        int viewPointSamples = max(desiredSamples / (GetWidth() * GetHeight()), 1); 
     611         
    641612        // the number of direction samples per pass is given by the number of viewpoints 
    642         int directionalSamples = desiredSamples/viewPointSamples; 
    643          
     613        int directionalSamples = desiredSamples / viewPointSamples; 
     614         
     615        Debug << "directional samples: " << directionalSamples << endl; 
    644616        for (int i = 0; i < viewPointSamples; ++ i)  
    645617        { 
     
    674646                                                                                                        const Vector3 viewPoint, 
    675647                                                                                                        Beam &beam, 
    676                                                                                                         const int desiredSamples, 
     648                                                                                                        const int samples, 
    677649                                                    BeamSampleStatistics &stat) 
    678650{ 
    679         // 1. setup the view port to match the desired samples 
    680         glViewport(0, 0, desiredSamples, desiredSamples); 
     651    // 1. setup the view port to match the desired samples 
     652        glViewport(0, 0, GetWidth(), GetHeight()); 
    681653 
    682654        // 2. setup the projection matrix and view matrix to match the viewpoint + beam.mDirBox 
    683655        SetupProjectionForViewPoint(viewPoint, beam, sourceObject); 
    684          
     656 
    685657        // 3. reset z-buffer to 0 and render the source object for the beam 
    686658        //    with glCullFace(Enabled) and glFrontFace(GL_CW) 
    687659        //    save result to depth map 
     660 
     661        // front depth buffer must bé initialised to 0 
     662        float clearDepth; 
     663        glGetFloatv(GL_DEPTH_CLEAR_VALUE, &clearDepth); 
     664        glClearDepth(0.0f); 
     665         
     666 
    688667        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); 
     668        //glFrontFace(GL_CW); 
    689669        glEnable(GL_CULL_FACE); 
    690         glEnable(GL_STENCIL_TEST); 
    691670        glCullFace(GL_FRONT); 
    692671        glColorMask(0, 0, 0, 0); 
    693672         
     673 
    694674        // stencil is increased where the source object is located 
     675        glEnable(GL_STENCIL_TEST);       
    695676        glStencilFunc(GL_ALWAYS, 0x1, 0x1); 
    696         glStencilOp(GL_INCR, GL_INCR, GL_INCR); 
     677        glStencilOp(GL_REPLACE, GL_REPLACE, GL_REPLACE); 
    697678 
    698679#if 0 
     
    713694 
    714695#endif   
     696        // reset clear function 
     697        glClearDepth(clearDepth); 
     698 
     699        // copy contents of depth buffer into depth texture 
     700        // depth buffer holds ray origins 
     701        glBindTexture(GL_TEXTURE_2D, frontDepthMap);     
     702        glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, 0, depthMapSize, depthMapSize); 
     703 
     704 
     705#if 0 
     706        // only the samples which are inside the beam AND on the source object 
     707        // should be considered => only where stencil == 2 
     708        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 
     709        glStencilFunc(GL_ALWAYS, 0x2, 0x2); 
     710        glStencilOp(GL_INCR, GL_INCR, GL_INCR); 
     711 
     712        // render back face of frustum beam 
     713        RenderMesh(beam.mMesh); 
     714#endif 
     715 
    715716 
    716717        // 4. set back to normal rendering and clear the ray termination depth buffer 
    717718        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 
    718719        glColorMask(1, 1, 1, 1); 
    719          
     720        glDepthMask(1); 
     721        glEnable(GL_DEPTH_TEST); 
     722        glDisable(GL_STENCIL_TEST);      
     723        glEnable(GL_CULL_FACE); 
     724        glCullFace(GL_BACK); 
     725 
    720726        // 5. render all objects inside the beam using id based false color 
    721727        //    (objects can be compiled to a gl list now so that subsequent rendering for 
     
    743749                ObjectContainer::const_iterator it, it_end = beam.mObjects.end(); 
    744750                for (it = beam.mObjects.begin(); it != it_end; ++ it) 
    745                         RenderIntersectable(*it); 
     751                { 
     752                        if (*it != sourceObject) 
     753                                RenderIntersectable(*it); 
     754                } 
    746755                 
    747756                glEndList(); 
     
    750759#else 
    751760        ObjectContainer::const_iterator it, it_end = beam.mObjects.end(); 
    752                 for (it = beam.mObjects.begin(); it != it_end; ++ it) 
     761        for (it = beam.mObjects.begin(); it != it_end; ++ it) 
     762        {        
     763                if (*it != sourceObject) 
    753764                        RenderIntersectable(*it); 
     765        } 
    754766#endif 
    755767 
    756         // remove objects again 
    757         if (beam.mFlags & !Beam::STORE_OBJECTS) 
    758                 beam.mObjects.clear(); 
    759  
    760         // bind ray origin depth buffer 
    761         glBindTexture(GL_TEXTURE_2D, depthMap); 
    762     glEnable(GL_TEXTURE_2D); 
    763          
    764  
    765         //Read the depth buffer into the shadow map texture 
    766         glBindTexture(GL_TEXTURE_2D, depthMap); 
    767         glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, 0, depthMapSize, depthMapSize); 
    768  
    769          
    770         // 6. Now we have a two depth buffers defining the ray origin and termination 
     768         
     769    // 6. Now we have a two depth buffers defining the ray origin and termination 
    771770        //    only rays which have non-zero entry in the origin buffer are valid since 
    772771        //    they realy start on the object surface (this can also be tagged by setting a 
    773772        //    stencil buffer bit at step 3) 
     773         
     774        glDisable(GL_STENCIL_TEST); 
     775        //glEnable(GL_STENCIL_TEST); 
    774776        glStencilFunc(GL_EQUAL, 0x1, 0x1); 
    775   
    776         //cgGLBindProgram(sCgFragmentProgram); 
     777        //glStencilFunc(GL_EQUAL, 0x2, 0x2); 
     778        glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP); 
     779 
     780        // bind depth texture 
     781        glEnable(GL_TEXTURE_2D); 
     782         
     783        // bind depth cull pixel shader 
     784        cgGLBindProgram(sCgFragmentProgram); 
    777785        cgGLEnableProfile(sCgFragmentProfile); 
    778786 
     
    785793 
    786794        // now check whether any backfacing polygon would pass the depth test? 
     795        //matt: should check both back /front facing because of dual depth buffer 
     796        //and danger of cutting the near plane with front facing polys. 
    787797        glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE); 
    788798        glDepthMask(GL_FALSE); 
     
    817827                                                                 GL_PIXEL_COUNT_NV, 
    818828                                                                 &pixelCount); 
    819  
    820                 Debug << "visble pixels: " << pixelCount << endl; 
     829                if (pixelCount) 
     830                        Debug << "view cell " << (*vit)->GetId() << " visible pixels: " << pixelCount << endl; 
    821831        } 
    822832         
     
    828838        // evaluate the contribution entropy for example) 
    829839        // However might be an option to extract/store only those the rays which made a contribution 
    830         // (new viewcell has been discovered) or relative contribution greater than a threashold ...  
    831  
     840        // (new viewcell has been discovered) or relative contribution greater than a threshold ...  
     841 
     842        ObjectContainer pvsObj; 
     843        stat.pvsSize = ComputePvs(beam.mObjects, pvsObj); 
     844         
     845         
     846#if 0 
     847        // copy contents of back depth buffer into depth texture 
     848        // depth buffer holds ray origins 
     849        glBindTexture(GL_TEXTURE_2D, backDepthMap);      
     850        glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, 0, depthMapSize, depthMapSize); 
     851#endif 
     852        // reset state 
    832853        glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE); 
    833854        glDepthMask(GL_TRUE); 
    834855        glEnable(GL_CULL_FACE); 
    835  
    836  
     856        glDisable(GL_STENCIL_TEST); 
    837857        cgGLDisableProfile(sCgFragmentProfile); 
    838858        glDisable(GL_TEXTURE_2D); 
     859 
     860        // remove objects again 
     861        if (beam.mFlags & !Beam::STORE_OBJECTS) 
     862                beam.mObjects.clear(); 
    839863} 
    840864 
     
    845869        { 
    846870                const int n = numQueries - (int)sQueries.size(); 
    847                 int *newQueries = new int[n]; 
    848  
    849                 glGenOcclusionQueriesNV(n, (unsigned int *)&newQueries); 
     871                unsigned int *newQueries = new unsigned int[n]; 
     872 
     873                glGenOcclusionQueriesNV(n, (unsigned int *)newQueries); 
    850874 
    851875                for (int i = 0; i < n; ++ i) 
     
    854878                } 
    855879 
    856                 delete newQueries; 
     880                delete [] newQueries; 
    857881        } 
    858882} 
     
    860884 
    861885void GlRendererBuffer::SetupProjectionForViewPoint(const Vector3 &viewPoint,  
    862                                                                                                  const Beam &beam,  
    863                                                                                                  Intersectable *sourceObject) 
     886                                                                                                   const Beam &beam,  
     887                                                                                                   Intersectable *sourceObject) 
    864888{ 
    865889        float left, right, bottom, top, znear, zfar; 
    866890 
    867         beam.ComputeFrustum(left, right, bottom, top, znear, zfar, 
    868                                                 mSceneGraph->GetBox()); 
    869  
     891        beam.ComputePerspectiveFrustum(left, right, bottom, top, znear, zfar, 
     892                                                                   mSceneGraph->GetBox()); 
     893 
     894        //Debug << left << " " << right << " " << bottom << " " << top << " " << znear << " " << zfar << endl; 
     895        glMatrixMode(GL_PROJECTION); 
     896        glLoadIdentity(); 
    870897        glFrustum(left, right, bottom, top, znear, zfar); 
    871  
    872     const Vector3 eye = viewPoint + beam.GetMainDirection(); 
    873         const Vector3 up;//AbitraryNormal(eye); 
    874  
    875         gluLookAt(eye.x, eye.y, eye.z,  
    876                           viewPoint.x, viewPoint.y, viewPoint.z,  
     898        //glFrustum(-1, 1, -1, 1, 1, 20000); 
     899 
     900    const Vector3 center = viewPoint + beam.GetMainDirection() * (zfar - znear) * 0.3; 
     901        const Vector3 up =  
     902                Normalize(CrossProd(beam.mPlanes[0].mNormal, beam.mPlanes[4].mNormal)); 
     903 
     904#ifdef _DEBUG 
     905        Debug << "view point: " << viewPoint << endl; 
     906        Debug << "eye: " << center << endl; 
     907        Debug << "up: " << up << endl; 
     908#endif 
     909 
     910        glMatrixMode(GL_MODELVIEW); 
     911        glLoadIdentity(); 
     912        gluLookAt(viewPoint.x, viewPoint.y, viewPoint.z,  
     913                          center.x, center.y, center.z,                    
    877914                          up.x, up.y, up.z); 
    878915}                
    879916 
    880917   
     918void GlRendererBuffer::InitGL() 
     919{ 
     920        GlRenderer::InitGL(); 
     921        // initialise dual depth buffer textures 
     922        glGenTextures(1, &frontDepthMap); 
     923        glBindTexture(GL_TEXTURE_2D, frontDepthMap); 
     924         
     925        glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, depthMapSize,  
     926                depthMapSize, 0, GL_DEPTH_COMPONENT, GL_UNSIGNED_BYTE, NULL); 
     927        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); 
     928        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); 
     929        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP); 
     930        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP); 
     931 
     932        glGenTextures(1, &backDepthMap); 
     933        glBindTexture(GL_TEXTURE_2D, backDepthMap); 
     934         
     935        glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, depthMapSize,  
     936                depthMapSize, 0, GL_DEPTH_COMPONENT, GL_UNSIGNED_BYTE, NULL); 
     937        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); 
     938        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); 
     939        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP); 
     940        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP); 
     941 
     942        // cg initialization 
     943        cgSetErrorCallback(handleCgError); 
     944        sCgContext = cgCreateContext(); 
     945         
     946        if (cgGLIsProfileSupported(CG_PROFILE_ARBFP1)) 
     947                sCgFragmentProfile = CG_PROFILE_ARBFP1; 
     948        else  
     949        { 
     950          // try FP30 
     951          if (cgGLIsProfileSupported(CG_PROFILE_FP30)) 
     952            sCgFragmentProfile = CG_PROFILE_FP30; 
     953          else  
     954          { 
     955                  Debug << "Neither arbfp1 or fp30 fragment profiles supported on this system" << endl; 
     956                  exit(1); 
     957          } 
     958  } 
     959 
     960  sCgFragmentProgram = cgCreateProgramFromFile(sCgContext,               
     961                                                                                           CG_SOURCE, "../src/dual_depth.cg", 
     962                                                                                           sCgFragmentProfile, 
     963                                                                                           NULL,  
     964                                                                                           NULL); 
     965 
     966  if (!cgIsProgramCompiled(sCgFragmentProgram)) 
     967          cgCompileProgram(sCgFragmentProgram); 
     968 
     969  cgGLLoadProgram(sCgFragmentProgram); 
     970  cgGLBindProgram(sCgFragmentProgram); 
    881971  
    882  
    883 /***************************************************************/ 
    884 /*             GlDebuggerWidget implementation                             */ 
    885 /***************************************************************/ 
    886  
    887  
    888 GlDebuggerWidget::GlDebuggerWidget(QWidget *parent, GlRendererBuffer *buf) 
     972  Debug << "---- PROGRAM BEGIN ----\n" <<  
     973          cgGetProgramString(sCgFragmentProgram, CG_COMPILED_PROGRAM) << "---- PROGRAM END ----\n"; 
     974} 
     975 
     976void GlRendererBuffer::ComputeRays(Intersectable *sourceObj, VssRayContainer &rays) 
     977{ 
     978        for (int i = 0; i < depthMapSize * depthMapSize; ++ i) 
     979        { 
     980                //todo glGetTexImage() 
     981        } 
     982} 
     983 
     984 
     985 
     986inline bool ilt(Intersectable *obj1, Intersectable *obj2) 
     987{ 
     988        return obj1->mId < obj2->mId; 
     989} 
     990 
     991 
     992int GlRendererBuffer::ComputePvs(ObjectContainer &objects,  
     993                                                                 ObjectContainer &pvs) const 
     994{ 
     995        int pvsSize = 0; 
     996        QImage image = toImage(); 
     997        Intersectable::NewMail(); 
     998 
     999        std::stable_sort(objects.begin(), objects.end(), ilt); 
     1000 
     1001        MeshInstance dummy(NULL); 
     1002 
     1003        Intersectable *obj = NULL; 
     1004                         
     1005        for (int x = 0; x < image.width(); ++ x) 
     1006        { 
     1007                for (int y = 0; y < image.height(); ++ y) 
     1008                { 
     1009                        QRgb pix = image.pixel(x, y); 
     1010                        const int id = GetId(qRed(pix), qGreen(pix), qBlue(pix)); 
     1011 
     1012                        dummy.SetId(id); 
     1013 
     1014                        ObjectContainer::iterator oit = 
     1015                                lower_bound(objects.begin(), objects.end(), &dummy, ilt); 
     1016                        obj = *oit; 
     1017                        if (!obj->Mailed()) 
     1018                        { 
     1019                                obj->Mail(); 
     1020                                ++ pvsSize; 
     1021                                pvs.push_back(obj); 
     1022                        } 
     1023                } 
     1024        } 
     1025 
     1026        return pvsSize; 
     1027} 
     1028 
     1029/***********************************************************************/ 
     1030/*                     GlDebuggerWidget implementation                             */ 
     1031/***********************************************************************/ 
     1032 
     1033 
     1034GlDebuggerWidget::GlDebuggerWidget(GlRendererBuffer *buf, QWidget *parent) 
    8891035      : QGLWidget(QGLFormat(QGL::SampleBuffers), parent), mRenderBuffer(buf) 
    8901036{ 
     
    9321078        // draw a spinning cube into the pbuffer.. 
    9331079        mRenderBuffer->makeCurrent(); 
    934         //mRenderBuffer->SampleBeamContributions(); 
     1080         
     1081        BeamSampleStatistics stats; 
     1082        mRenderBuffer->SampleBeamContributions(mSourceObject, mBeam, mSamples, stats); 
     1083 
    9351084        glFlush(); 
    9361085 
    937     // rendering directly to a texture is not supported on X11, unfortunately 
     1086        // rendering directly to a texture is not supported on X11, unfortunately 
    9381087    mRenderBuffer->updateDynamicTexture(dynamicTexture); 
    9391088    
    940     // ..and use the pbuffer contents as a texture when rendering the 
     1089    // and use the pbuffer contents as a texture when rendering the 
    9411090    // background and the bouncing cubes 
    9421091    makeCurrent(); 
     
    9621111        // set up the pbuffer context 
    9631112    mRenderBuffer->makeCurrent(); 
    964         initCommon(); 
     1113        /*mRenderBuffer->InitGL(); 
    9651114 
    9661115        glViewport(0, 0, mRenderBuffer->size().width(), mRenderBuffer->size().height()); 
     
    9721121        glLoadIdentity(); 
    9731122 
    974         glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT); 
    975  
     1123        glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);*/ 
     1124         
    9761125        // generate a texture that has the same size/format as the pbuffer 
    9771126    dynamicTexture = mRenderBuffer->generateDynamicTexture(); 
Note: See TracChangeset for help on using the changeset viewer.