Ignore:
Timestamp:
01/15/06 04:23:51 (18 years ago)
Author:
mattausch
Message:
 
Location:
trunk/VUT/GtpVisibilityPreprocessor/src
Files:
11 edited

Legend:

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

    r538 r540  
    17151715        } 
    17161716 
    1717         // order intersectioins  
     1717        // order intersections   
    17181718        if (planePoly->mVertices.size() > 3) 
    17191719        { 
  • trunk/VUT/GtpVisibilityPreprocessor/src/Beam.cpp

    r535 r540  
    11#include "VssRay.h" 
    22#include "Beam.h" 
    3  
     3#include "Mesh.h" 
     4#include "Polygon3.h" 
    45 
    56 
    67void 
    7 Beam::Construct(const  AxisAlignedBox3 &box, const AxisAlignedBox3 &dBox) 
     8Beam::Construct(const AxisAlignedBox3 &box, const AxisAlignedBox3 &dBox) 
    89{ 
    910  // the frustum is defined by set of negative halfspace described by mPlanes 
     
    7374 
    7475 
    75 void Beam::ComputeFrustum(float &left, float &right, 
    76                                                   float &bottom, float &top, 
    77                                                   float &near, float &far, 
    78                                                   const AxisAlignedBox3 &sceneBBox) const 
     76void Beam::ComputePerspectiveFrustum(float &left, float &right, 
     77                                                                        float &bottom, float &top, 
     78                                                                        float &near, float &far, 
     79                                                                        const AxisAlignedBox3 &sceneBBox) const 
    7980{ 
    8081        const float xDirRange = mDirBox.Max().x - mDirBox.Min().x; 
    8182        const float yDirRange = mDirBox.Max().y - mDirBox.Min().y; 
     83        //Debug << "xdir range: " << xDirRange << endl; 
     84        //Debug << "ydir range: " << yDirRange << endl; 
    8285 
    8386        near = 0.1f; // NOTE: what is the best value for near and far plane? 
    8487        far = 2.0f * Magnitude(sceneBBox.Diagonal()); 
    85  
    86         left = near / tan(xDirRange * 0.5f); 
    87         right = near / tan(xDirRange * 0.5f); 
    88  
    89         bottom = near / tan(yDirRange * 0.5f); 
    90         top = near / tan(yDirRange * 0.5f); 
     88         
     89        right = fabs(near * tan(xDirRange * 0.5f)); 
     90        left = -right; 
     91 
     92        top = fabs(near * tan(yDirRange * 0.5f)); 
     93        bottom = -top; 
     94} 
     95 
     96 
     97void Beam::ComputeOrthoFrustum(float &left, float &right, 
     98                                                           float &bottom, float &top, 
     99                                                           float &near, float &far, 
     100                                                           const AxisAlignedBox3 &sceneBBox) const 
     101{ 
     102        const int vAxis = GetMainDirection().DrivingAxis(); 
     103        const int axis2 = (vAxis + 1) % 3; 
     104        const int axis3 = (vAxis + 2) % 3; 
     105 
     106        const float xDirRange = mBox.Max()[axis2] - mDirBox.Min()[axis2]; 
     107        const float yDirRange = mDirBox.Max()[axis3] - mDirBox.Min()[axis3]; 
     108 
     109        near = 0.1f; // NOTE: what is the best value for near and far plane? 
     110        far = 2.0f * Magnitude(sceneBBox.Diagonal()); 
     111 
     112        right = xDirRange * 0.5f; 
     113        left = -right; 
     114 
     115        top = yDirRange * 0.5; 
     116        bottom = -top; 
    91117} 
    92118 
     
    94120Vector3 Beam::GetMainDirection() const 
    95121{ 
    96         const Vector3 dCenter = mDirBox.Center(); 
    97         return VssRay::GetDirection(dCenter.x, dCenter.y); 
     122        return - mPlanes[0].mNormal; 
    98123} 
    99124 
     
    123148} 
    124149 
     150 
     151void Beam::CreateMesh(const float zfar) 
     152{ 
     153        if (mMesh) 
     154                return; 
     155 
     156        mMesh = new Mesh(); 
     157         
     158        // -- compute far plane 
     159 
     160        // box should not never remove part of beam polygons 
     161        Vector3 bmin = mBox.Min() - Vector3(zfar * 2.0); 
     162        Vector3 bmax = mBox.Max() + Vector3(zfar * 2.0); 
     163 
     164        AxisAlignedBox3 bbox(bmin, bmax); 
     165        Plane3 fplane; 
     166        fplane.mNormal = -mPlanes[0].mNormal; 
     167 
     168        // NOTE: beam far plane must not not be culled by gl far plane 
     169        fplane.mD = mPlanes[0].mD - zfar - 1.0f;  
     170        mPlanes.push_back(fplane); 
     171 
     172        for (int i = 0; i < mPlanes.size(); ++ i) 
     173        { 
     174                Polygon3 *poly = bbox.CrossSection(mPlanes[i]); 
     175                 
     176                if (!poly->Valid(Limits::Small)) 
     177                        DEL_PTR(poly); 
     178                 
     179                for (int j = 0; (j < mPlanes.size()) && poly; ++ j) 
     180                { 
     181                        if (j != i) 
     182                        { 
     183                                Polygon3 *front = new Polygon3(); 
     184                                Polygon3 *back = new Polygon3(); 
     185                                 
     186                                poly->Split(mPlanes[j], *front, *back, Limits::Small); 
     187                                DEL_PTR(poly); 
     188                                DEL_PTR(front); 
     189 
     190                                if (!back->Valid(Limits::Small)) 
     191                                        DEL_PTR(back); 
     192                                poly = back; 
     193                        } 
     194                } 
     195         
     196                if (poly) 
     197                { 
     198                        poly->AddToMesh(*mMesh); 
     199                } 
     200        } 
     201 
     202        // remove far plane 
     203        mPlanes.pop_back(); 
     204} 
  • trunk/VUT/GtpVisibilityPreprocessor/src/Beam.h

    r532 r540  
    1010class KdNode; 
    1111class Intersectable; 
     12class Mesh; 
    1213 
    1314// the values need for rss tree update computed already inside the glrendererbuffer 
     
    7475  vector<Plane3> mPlanes; 
    7576 
     77  Mesh *mMesh; 
    7678     
     79  /** Constructs a beam from a spatial and a directional box. 
     80  */ 
    7781  void Construct(const AxisAlignedBox3 &box, 
    78           const AxisAlignedBox3 &dBox); 
     82                             const AxisAlignedBox3 &dBox); 
    7983   
    80   void ComputeFrustum(float &left, float &right, 
    81                                           float &bottom, float &top, 
    82                                           float &near, float &far, 
    83                                           const AxisAlignedBox3 &sceneBBox) const; 
     84  /** Computes parameters for glFrustum. 
     85  */ 
     86  void ComputePerspectiveFrustum(float &left, float &right, 
     87                                                                 float &bottom, float &top, 
     88                                                                 float &near, float &far, 
     89                                                                 const AxisAlignedBox3 &sceneBBox) const; 
     90 
     91  /* Computes parameters for glOrtho. 
     92  */ 
     93  void ComputeOrthoFrustum(float &left, float &right, 
     94                                                   float &bottom, float &top, 
     95                                                   float &near, float &far, 
     96                                                   const AxisAlignedBox3 &sceneBBox) const; 
    8497 
    8598  int ComputeIntersection(const AxisAlignedBox3 &box); 
     
    90103  bool SetValid() { return mFlags |= VALID; } 
    91104 
    92   Beam():mFlags(STORE_KD_NODES+STORE_OBJECTS), mKdNodes(0), mObjects(0), mViewCells(0) 
     105  Beam():mFlags(STORE_KD_NODES+STORE_OBJECTS), mKdNodes(0), mObjects(0), mViewCells(0), mMesh(NULL) 
    93106  { 
    94107  } 
     108 
     109  void CreateMesh(const float zfar); 
    95110}; 
    96111 
  • 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(); 
  • trunk/VUT/GtpVisibilityPreprocessor/src/GlRenderer.h

    r538 r540  
    1010#include "Halton.h" 
    1111#include "Renderer.h" 
     12#include "Beam.h" 
    1213 
    1314class SceneGraph; 
     
    2324class BeamSampleStatistics; 
    2425 
     26struct VssRayContainer; 
     27 
    2528struct PvsRenderStatistics { 
    2629   
     
    104107  virtual int GetWidth() const = 0; 
    105108  virtual int GetHeight() const = 0; 
     109 
     110  int GetId(int r, int g, int b) const; 
    106111}; 
    107112 
     
    154159                                                           ); 
    155160 
    156    PvsRenderStatistics mPvsStat; 
     161  void InitGL(); 
     162 
     163  /** Computes rays from information gained with hw sampling- 
     164  */ 
     165  void ComputeRays(Intersectable *sourceObj, VssRayContainer &rays); 
     166 
     167  int ComputePvs() const; 
     168 
     169 
     170  int ComputePvs(ObjectContainer &objects, ObjectContainer &pvs) const; 
     171 
     172  PvsRenderStatistics mPvsStat; 
    157173    
    158    int mPvsStatFrames; 
    159    vector<float> mPvsErrorBuffer; 
     174  int mPvsStatFrames; 
     175  vector<float> mPvsErrorBuffer; 
    160176 
    161177private: 
     178         
    162179        static void GenQueries(const int numQueries); 
     180         
    163181        void SetupProjectionForViewPoint(const Vector3 &viewPoint,  
    164182                                                                         const Beam &beam,  
     
    217235class GlDebuggerWidget : public QGLWidget 
    218236{ 
    219 public: 
    220     GlDebuggerWidget(QWidget *parent, GlRendererBuffer *buf); 
     237        Q_OBJECT 
     238public: 
     239    GlDebuggerWidget(GlRendererBuffer *buf, QWidget *parent = NULL); 
    221240    ~GlDebuggerWidget(); 
    222241    void initializeGL(); 
     
    232251         GlRendererBuffer *mRenderBuffer; 
    233252 
     253         Beam mBeam; 
     254         int mSamples; 
     255         Intersectable *mSourceObject; 
    234256private: 
    235257    GLuint dynamicTexture; 
  • trunk/VUT/GtpVisibilityPreprocessor/src/Preprocessor.cpp

    r538 r540  
    259259        } 
    260260        if (mUseGlRenderer || mUseGlDebugger) 
    261           renderer = new GlRendererBuffer(1024, 768, mSceneGraph, mViewCellsManager, mKdTree); 
     261        { 
     262                // NOTE: render texture should be power of 2 and square 
     263                // renderer must be initialised 
     264                renderer = new GlRendererBuffer(1024, 768, mSceneGraph, mViewCellsManager, mKdTree); 
     265                renderer->makeCurrent(); 
     266        } 
    262267 
    263268 
  • trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellsManager.cpp

    r535 r540  
    1313#include "ViewCellsParser.h" 
    1414#include "Beam.h" 
    15  
     15#include "VssPreprocessor.h" 
     16#include "RssPreprocessor.h" 
    1617 
    1718 
     
    7677int ViewCellsManager::Construct(VssRayContainer &rays) 
    7778{ 
     79        VssPreprocessor preprocessor; 
     80 
     81 
     82 
    7883        return 0; 
    7984} 
  • trunk/VUT/GtpVisibilityPreprocessor/src/VssPreprocessor.cpp

    r538 r540  
    360360                                                                          const ObjectContainer &objects) 
    361361{ 
     362        //debuggerWidget = new GlDebuggerWidget(renderer); 
     363        //  renderer->resize(640, 480); 
     364        //debuggerWidget->resize(640, 480); 
     365 
    362366        vector<VssTreeLeaf *> leaves; 
    363367        tree->CollectLeaves(leaves); 
     
    366370 
    367371        exporter->SetWireframe(); 
    368         //exporter->ExportGeometry(objects); 
     372        exporter->ExportGeometry(objects); 
    369373        exporter->SetFilled(); 
    370374        //Randomize(); 
    371         for (int i = 0; i < 1; ++ i) 
     375        debuggerWidget = new GlDebuggerWidget(renderer); 
     376 
     377        /*debuggerWidget->mBeam = beam; 
     378        debuggerWidget->mSourceObject = sourceObj; 
     379        debuggerWidget->mSamples = 10000; 
     380         
     381        Debug << "showing window" << endl; 
     382        debuggerWidget->show();*/ 
     383         
     384        renderer->makeCurrent(); 
     385 
     386        for (int i = 0; i < 10; ++ i) 
    372387        { 
     388                Beam beam; 
     389                Intersectable *sourceObj = mObjects[5]; 
     390 
    373391                const int index = (int)RandomValue(0, (Real)((int)leaves.size() - 1)); 
    374392                VssTreeLeaf *leaf = leaves[index]; 
    375393 
    376                 Beam beam; 
    377394                AxisAlignedBox3 dirBox = tree->GetDirBBox(leaf); 
    378395                AxisAlignedBox3 box = tree->GetBBox(leaf); 
     
    387404                          << beam.mKdNodes.size() << " kd nodes" << endl; 
    388405 
    389                 Intersectable *sourceObj = mObjects[5]; 
    390406                BeamSampleStatistics stats; 
    391407 
    392408                renderer->SampleBeamContributions(sourceObj, 
    393409                                                                                  beam, 
    394                                                                                   10000, 
     410                                                                                  200000, 
    395411                                                                                  stats); 
    396412 
     413                char s[64]; sprintf(s, "shaft%04d.png", i); 
     414 
     415                QImage image = renderer->toImage(); 
     416                image.save(s, "PNG"); 
    397417                Debug << "beam statistics: " << stats << endl << endl; 
     418 
     419                if (1) 
     420                { 
     421                        AxisAlignedBox3 sbox = mSceneGraph->GetBox(); 
     422                        Vector3 bmin = sbox.Min() - 150.0f; 
     423                        Vector3 bmax = sbox.Max() + 150.0f; 
     424                        AxisAlignedBox3 vbox(bmin, bmax); 
    398425                 
    399                 AxisAlignedBox3 sbox = mSceneGraph->GetBox(); 
    400                 Vector3 bmin = sbox.Min() - 150.0f; 
    401                 Vector3 bmax = sbox.Max() + 150.0f; 
    402                 AxisAlignedBox3 vbox(bmin, bmax); 
    403                  
    404                 exporter->ExportBeam(beam, vbox); 
    405  
    406                 bool exportViewCells = true; 
     426                        exporter->ExportBeam(beam, vbox); 
     427                } 
     428 
     429                bool exportViewCells = false; 
    407430                 
    408431                if (exportViewCells) 
     
    431454                } 
    432455        } 
     456        /*while (1) 
     457        { debuggerWidget->repaint(); 
     458        };*/ 
    433459        delete exporter; 
    434460} 
     461 
    435462 
    436463float 
     
    591618 
    592619   
    593   int numExportRays = 5000; 
    594   //int numExportRays = 0; 
     620  //int numExportRays = 5000; 
     621  int numExportRays = 0; 
    595622 
    596623  if (numExportRays) { 
     
    723750  mViewCellsManager->PostProcess(mObjects, viewCellRays); 
    724751 
    725   if (mTestBeamSampling) 
     752  if (mTestBeamSampling && mUseGlRenderer) 
     753  {      
    726754          TestBeamCasting(vssTree, mViewCellsManager, mObjects); 
     755  } 
    727756 
    728757  //-- several visualizations and statistics 
  • trunk/VUT/GtpVisibilityPreprocessor/src/X3dExporter.cpp

    r535 r540  
    12421242void X3dExporter::ExportBeam(const Beam &beam, const AxisAlignedBox3 &box) 
    12431243{ 
     1244        if (beam.mMesh) 
     1245        { 
     1246                ExportMesh(beam.mMesh); 
     1247                return; 
     1248        } 
     1249 
    12441250        PolygonContainer polys; 
    12451251        //ExportBox(beam.mBox); 
    12461252 
    1247         for (int i = 0; i < beam.mPlanes.size(); ++ i) 
    1248         { 
    1249                 Polygon3 *poly = box.CrossSection(beam.mPlanes[i]); 
     1253        const float zfar = 2.0f * Magnitude(box.Diagonal()); 
     1254 
     1255        // box should not never remove part of beam polygons 
     1256        Vector3 bmin = beam.mBox.Min() - Vector3(zfar * 2.0); 
     1257        Vector3 bmax = beam.mBox.Max() + Vector3(zfar * 2.0); 
     1258 
     1259        AxisAlignedBox3 bbox(bmin, bmax); 
     1260        Plane3 fplane; 
     1261        fplane.mNormal = -beam.mPlanes[0].mNormal; 
     1262 
     1263        fplane.mD = beam.mPlanes[0].mD - zfar - 1.0f;  
     1264         
     1265        vector<Plane3> planes = beam.mPlanes; 
     1266        planes.push_back(fplane); 
     1267 
     1268        for (int i = 0; i < planes.size(); ++ i) 
     1269        { 
     1270                Polygon3 *poly = bbox.CrossSection(planes[i]); 
     1271                if (!poly->Valid(Limits::Small)) 
     1272                        DEL_PTR(poly); 
    12501273                 
    1251                 for (int j = 0; (j < beam.mPlanes.size()) && poly; ++ j) 
     1274                for (int j = 0; (j < planes.size()) && poly; ++ j) 
    12521275                { 
    12531276                        if (j != i) 
     
    12561279                                Polygon3 *back = new Polygon3(); 
    12571280                                 
    1258                                 poly->Split(beam.mPlanes[j], *front, *back, Limits::Small); 
     1281                                poly->Split(planes[j], *front, *back, Limits::Small); 
    12591282                                DEL_PTR(poly); 
    12601283                                DEL_PTR(front); 
     
    12691292        } 
    12701293 
    1271  
    12721294        ExportPolygons(polys); 
    12731295        CLEAR_CONTAINER(polys); 
  • trunk/VUT/GtpVisibilityPreprocessor/src/dual_depth.cg

    r532 r540  
    1515{ 
    1616        pixel OUT; 
    17  
     17        OUT.color = IN.color0; 
     18         
    1819        // the depth buffer     has to be compared to the current depth 
    1920        float4 depth = tex2D(depthMap, IN.depthTexCoord.xy); 
     
    2223        // reject by alpha test 
    2324        if (test < depth.x) 
     25        { 
    2426                OUT.color.w = 0; 
    25  
     27        //      OUT.color.y += 0.5; 
     28        } 
     29         
    2630        return OUT; 
    2731} 
  • trunk/VUT/GtpVisibilityPreprocessor/src/main.cpp

    r538 r540  
    100100 
    101101        pt->start(QThread::LowPriority); 
    102          
    103   } else if (0 /*p->UseGlDebugger*/) { 
    104             //debuggerWidget = new GlDebuggerWidget(*, (p->mSceneGraph, p->mViewCellsManager, p->mKdTree); 
    105                 //  renderer->resize(640, 480); 
    106         //      debuggerWidget->resize(640, 480); 
    107         //      debuggerWidget->show(); 
    108  
    109         pt->start(QThread::LowPriority); 
    110102  } else 
    111103  { 
Note: See TracChangeset for help on using the changeset viewer.