source: GTP/trunk/Lib/Vis/Preprocessing/src/GlRenderer.cpp @ 1737

Revision 1737, 15.4 KB checked in by bittner, 18 years ago (diff)

visibility filter updates

  • Property svn:executable set to *
RevLine 
[589]1#include "Mesh.h"
[1001]2#include "glInterface.h"
3#include "OcclusionQuery.h"
[589]4#include "GlRenderer.h"
5#include "ViewCellsManager.h"
6#include "SceneGraph.h"
7#include "Pvs.h"
8#include "Viewcell.h"
9#include "Beam.h"
[532]10#include "KdTree.h"
[589]11#include "Environment.h"
[1581]12#include "Triangle3.h"
13#include "IntersectableWrapper.h"
[1585]14#include "BvHierarchy.h"
[1594]15#include "KdTree.h"
[513]16
[1146]17//#include <Cg/cg.h>
18//#include <Cg/cgGL.h>
[1001]19
20
[863]21namespace GtpVisibilityPreprocessor {
[860]22
[516]23
[1112]24static bool arbQuerySupport = false;
25static bool nvQuerySupport = false;
26
27
28static void InitExtensions()
29{
30        GLenum err = glewInit();
31
32        if (GLEW_OK != err)
33        {
34                // problem: glewInit failed, something is seriously wrong
35                cerr << "Error: " << glewGetErrorString(err) << endl;
36                exit(1);
37        }
38
39        if (GLEW_ARB_occlusion_query)
40                arbQuerySupport = true;
41       
42        if (GLEW_NV_occlusion_query)
43                nvQuerySupport = true;
44       
45
46        if  (!arbQuerySupport && !nvQuerySupport)
47        {
48                cout << "I require the GL_ARB_occlusion_query or the GL_NV_occlusion_query OpenGL extension to work.\n";
49                exit(1);
50        }
[1001]51}
[589]52
[811]53
[589]54GlRenderer::GlRenderer(SceneGraph *sceneGraph,
55                                           ViewCellsManager *viewCellsManager,
56                                           KdTree *tree):
57  Renderer(sceneGraph, viewCellsManager),
58  mKdTree(tree)
59{
60  mSceneGraph->CollectObjects(&mObjects);
[1112]61
62  //  mViewCellsManager->GetViewPoint(mViewPoint);
63
[589]64  mViewPoint = mSceneGraph->GetBox().Center();
65  mViewDirection = Vector3(0,0,1);
[608]66
[1112]67  //  mViewPoint = Vector3(991.7, 187.8, -271);
68  //  mViewDirection = Vector3(0.9, 0, -0.4);
[878]69
[589]70  //  timerId = startTimer(10);
71  // debug coords for atlanta
[608]72  //  mViewPoint = Vector3(3473, 6.778, -1699);
73  //  mViewDirection = Vector3(-0.2432, 0, 0.97);
[589]74 
75  mFrame = 0;
76  mWireFrame = false;
[1004]77  Environment::GetSingleton()->GetBoolValue("Preprocessor.detectEmptyViewSpace", mDetectEmptyViewSpace);
[746]78  mSnapErrorFrames = true;
79  mSnapPrefix = "snap/";
80  mUseForcedColors = false;
[1594]81  mRenderBoxes = false;
[811]82  mUseGlLists = true;
[1001]83  //mUseGlLists = false;
[589]84}
85
86GlRenderer::~GlRenderer()
87{
88  cerr<<"gl renderer destructor..\n";
[1145]89 
90  //CLEAR_CONTAINER(sQueries);
[1001]91  CLEAR_CONTAINER(mOcclusionQueries);
[589]92
[1145]93  cerr<<"done."<<endl;
[589]94}
95
[1145]96
[589]97void
[1581]98GlRenderer::RenderTriangle(TriangleIntersectable *object)
99{
100  Triangle3 &t = object->GetItem();
101  glBegin(GL_TRIANGLES);
102  glVertex3f(t.mVertices[0].x, t.mVertices[0].y, t.mVertices[0].z);
103  glVertex3f(t.mVertices[1].x, t.mVertices[1].y, t.mVertices[1].z);
104  glVertex3f(t.mVertices[2].x, t.mVertices[2].y, t.mVertices[2].z);
105  glEnd();
106}
107
108void
[589]109GlRenderer::RenderIntersectable(Intersectable *object)
110{
[1594]111  if (object->Mailed())
112        return;
113  object->Mail();
114 
[589]115  glPushAttrib(GL_CURRENT_BIT);
116  if (mUseFalseColors)
117        SetupFalseColor(object->mId);
118 
119
120  switch (object->Type()) {
121  case Intersectable::MESH_INSTANCE:
122        RenderMeshInstance((MeshInstance *)object);
123        break;
124  case Intersectable::VIEW_CELL:
[1581]125        RenderViewCell(dynamic_cast<ViewCell *>(object));
126        break;
[1001]127  case Intersectable::TRANSFORMED_MESH_INSTANCE:
[1581]128        RenderTransformedMeshInstance(dynamic_cast<TransformedMeshInstance *>(object));
129        break;
130  case Intersectable::TRIANGLE_INTERSECTABLE:
131        RenderTriangle(dynamic_cast<TriangleIntersectable *>(object));
132        break;
[1585]133  case Intersectable::BVH_INTERSECTABLE: {
134
[1594]135       
[1585]136        BvhNode *node = (dynamic_cast<BvhIntersectable *>(object))->GetItem();
[1594]137
138        if (mRenderBoxes)
139          RenderBox(node->GetBoundingBox());
140        else
141          RenderBvhNode(node);
[1585]142        break;
143  }
[1594]144  case Intersectable::KD_INTERSECTABLE: {
145        KdNode *node = (dynamic_cast<KdIntersectable *>(object))->GetItem();
146
147        if (mRenderBoxes)
148          RenderBox(mKdTree->GetBox(node));
149        else
150          RenderKdNode(node);
151        break;
152  }
[1585]153       
[589]154  default:
155        cerr<<"Rendering this object not yet implemented\n";
156        break;
157  }
158
159  glPopAttrib();
160}
161
[1581]162void
163GlRenderer::RenderRays(const VssRayContainer &rays)
164{
165  VssRayContainer::const_iterator it = rays.begin(), it_end = rays.end();
[589]166
[1581]167  glBegin(GL_LINES);
168  for (; it != it_end; ++it) {
169        VssRay *ray = *it;
170        float importance = log10(1e3*ray->mWeightedPvsContribution)/3.0f;
171        //      cout<<"w="<<ray->mWeightedPvsContribution<<" r="<<ray->mWeightedPvsContribution;
172        glColor3f(importance, importance, importance);
173        glVertex3fv(&ray->mOrigin.x);
174        glVertex3fv(&ray->mTermination.x);
175  }
176  glEnd();
177}
178
[1737]179
180
[589]181void
182GlRenderer::RenderViewCell(ViewCell *vc)
183{
184  if (vc->GetMesh()) {
185
[599]186        if (!mUseFalseColors) {
187          if (vc->GetValid())
188                glColor3f(0,1,0);
189          else
190                glColor3f(0,0,1);
191        }
[589]192       
193        RenderMesh(vc->GetMesh());
[599]194  } else {
195        // render viewcells in the subtree
196        if (!vc->IsLeaf()) {
197          ViewCellInterior *vci = (ViewCellInterior *) vc;
198
199          ViewCellContainer::iterator it = vci->mChildren.begin();
200          for (; it != vci->mChildren.end(); ++it) {
201                RenderViewCell(*it);
202          }
[685]203        } else {
204          //      cerr<<"Empty viewcell mesh\n";
[599]205        }
[589]206  }
207}
208
[599]209
[589]210void
211GlRenderer::RenderMeshInstance(MeshInstance *mi)
212{
213  RenderMesh(mi->GetMesh());
214}
215
[1001]216
[589]217void
[1001]218GlRenderer::RenderTransformedMeshInstance(TransformedMeshInstance *mi)
219{
220        // apply world transform before rendering
221        Matrix4x4 m;
222        mi->GetWorldTransform(m);
223
224        glPushMatrix();
[1002]225/* cout << "\n";
[1001]226        for (int i = 0; i < 4; ++ i)
227                for (int j = 0; j < 4; ++ j)
[1002]228                        cout << m.x[i][j] << " "; cout << "\n"*/
[1001]229
230        glMultMatrixf((float *)m.x);
231
[1002]232        /*GLfloat dummy[16];
233        glGetFloatv(GL_MODELVIEW_MATRIX, dummy);
[1001]234        for (int i = 0; i < 16; ++ i)
235                cout << dummy[i] << " ";
236        cout << endl;*/
237        RenderMesh(mi->GetMesh());
238       
239        glPopMatrix();
240}
241
242
243void
[589]244GlRenderer::SetupFalseColor(const int id)
245{
246  // swap bits of the color
247  glColor3ub(id&255, (id>>8)&255, (id>>16)&255);
248}
249
250
251int GlRenderer::GetId(int r, int g, int b) const
252{
253        return r + (g << 8) + (b << 16);
254}
255
256void
257GlRenderer::SetupMaterial(Material *m)
258{
259  if (m)
260        glColor3fv(&(m->mDiffuseColor.r));
261}
262
263void
264GlRenderer::RenderMesh(Mesh *mesh)
265{
266  int i = 0;
267
[746]268  if (!mUseFalseColors && !mUseForcedColors)
[589]269        SetupMaterial(mesh->mMaterial);
270 
271  for (i=0; i < mesh->mFaces.size(); i++) {
272        if (mWireFrame)
273          glBegin(GL_LINE_LOOP);
274        else
275          glBegin(GL_POLYGON);
276
277        Face *face = mesh->mFaces[i];
278        for (int j = 0; j < face->mVertexIndices.size(); j++) {
279          glVertex3fv(&mesh->mVertices[face->mVertexIndices[j]].x);
280        }
281        glEnd();
282  }
283}
284       
285void
286GlRenderer::InitGL()
287{
[1608]288  mSphere = (GLUquadric *)gluNewQuadric();
289 
[589]290  glMatrixMode(GL_PROJECTION);
291  glLoadIdentity();
292 
293  glMatrixMode(GL_MODELVIEW);
294  glLoadIdentity();
295
296  glEnable(GL_CULL_FACE);
297  glShadeModel(GL_FLAT);
298  glEnable(GL_DEPTH_TEST);
299  glEnable(GL_CULL_FACE);
300 
[1001]301  InitExtensions();
[811]302 
[746]303#if 0
304  GLfloat mat_ambient[]   = {  0.5, 0.5, 0.5, 1.0  };
305  /*  mat_specular and mat_shininess are NOT default values     */
306  GLfloat mat_diffuse[]   = {  1.0, 1.0, 1.0, 1.0  };
307  GLfloat mat_specular[]  = {  0.3, 0.3, 0.3, 1.0  };
308  GLfloat mat_shininess[] = {  1.0  };
309 
310  GLfloat light_ambient[]  = {  0.2, 0.2, 0.2, 1.0  };
311  GLfloat light_diffuse[]  = {  0.4, 0.4, 0.4, 1.0  };
312  GLfloat light_specular[] = {  0.3, 0.3, 0.3, 1.0  };
313 
314  GLfloat lmodel_ambient[] = {  0.3, 0.3, 0.3, 1.0  };
315 
316 
317  // default Material
318  glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient);
319  glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
320  glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
321  glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
322
323  // a light
324  glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
325  glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
326  glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
327 
328  glLightfv(GL_LIGHT1, GL_AMBIENT, light_ambient);
329  glLightfv(GL_LIGHT1, GL_DIFFUSE, light_diffuse);
330  glLightfv(GL_LIGHT1, GL_SPECULAR, light_specular);
331 
332  glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lmodel_ambient);
333 
334  glEnable(GL_LIGHTING);
335  glEnable(GL_LIGHT0);
336  glEnable(GL_LIGHT1);
337 
338 
339  // set position of the light
340  GLfloat infinite_light[] = {  1.0, 0.8, 1.0, 0.0  };
341  glLightfv (GL_LIGHT0, GL_POSITION, infinite_light);
342 
343  // set position of the light2
344  GLfloat infinite_light2[] = {  -0.3, 1.5, 1.0, 0.0  };
345  glLightfv (GL_LIGHT1, GL_POSITION, infinite_light2);
346 
347  glColorMaterial( GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE);
348  //   glColorMaterial( GL_FRONT_AND_BACK, GL_SPECULAR);
349  glEnable(GL_COLOR_MATERIAL);
350 
351  glShadeModel( GL_FLAT );
352 
353  glDepthFunc( GL_LESS );
354  glEnable( GL_DEPTH_TEST );
355#endif
356
357  glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE );
358
359  glEnable( GL_NORMALIZE );
360 
361  glClearColor(0.0f, 0.0f, 1.0f, 1.0f);
[589]362}
363
[746]364
[589]365void
[811]366GlRenderer::SetupProjection(const int w, const int h, const float angle)
[589]367{
368  glViewport(0, 0, w, h);
369  glMatrixMode(GL_PROJECTION);
370  glLoadIdentity();
[811]371  gluPerspective(angle, 1.0, 0.1, 2.0*Magnitude(mSceneGraph->GetBox().Diagonal()));
[589]372  glMatrixMode(GL_MODELVIEW);
373}
374
[1581]375
376
[589]377void
378GlRenderer::SetupCamera()
379{
380  Vector3 target = mViewPoint + mViewDirection;
[811]381
[589]382  Vector3 up(0,1,0);
383 
[811]384  if (abs(DotProd(mViewDirection, up)) > 0.99f)
385        up = Vector3(1, 0, 0);
386 
[589]387  glLoadIdentity();
388  gluLookAt(mViewPoint.x, mViewPoint.y, mViewPoint.z,
389                        target.x, target.y, target.z,
390                        up.x, up.y, up.z);
391}
392
[1145]393void
394GlRenderer::_RenderScene()
395{
396  ObjectContainer::const_iterator oi = mObjects.begin();
[811]397
[1145]398  for (; oi != mObjects.end(); oi++)
399        RenderIntersectable(*oi);
400}
401
402bool
403GlRenderer::RenderScene()
404{
405  static int glList = -1;
406  if (mUseGlLists) {
407        if (glList == -1) {
408          glList = glGenLists(1);
409          glNewList(glList, GL_COMPILE);
410          _RenderScene();
411          glEndList();
412        }
413        glCallList(glList);
414  } else
415        _RenderScene();
416       
417 
418  return true;
419}
420
421
[589]422void
[997]423GlRendererBuffer::EvalQueryWithItemBuffer(
[1001]424                                                                                  //RenderCostSample &sample
[997]425                                                                           )
426{
427        // read back the texture
428        glReadPixels(0, 0,
429                                GetWidth(), GetHeight(),
430                                GL_RGBA,
431                                GL_UNSIGNED_BYTE,
432                                mPixelBuffer);
433               
434                       
435        unsigned int *p = mPixelBuffer;
436                       
437        for (int y = 0; y < GetHeight(); y++)
438        {
439                for (int x = 0; x < GetWidth(); x++, p++)
440                {
441                        unsigned int id = (*p) & 0xFFFFFF;
442
443                        if (id != 0xFFFFFF)
444                                ++ mObjects[id]->mCounter;
445                }
446        }
447}
448
[1145]449
450
451/****************************************************************/
452/*               GlRendererBuffer implementation                */
453/****************************************************************/
454
455
456
457GlRendererBuffer::GlRendererBuffer(SceneGraph *sceneGraph,
458                                                                   ViewCellsManager *viewcells,
459                                                                   KdTree *tree):
460GlRenderer(sceneGraph, viewcells, tree) 
461{
462        mPixelBuffer = NULL;
463
464        // implement width and height in subclasses
465}
466
467
[997]468void
469GlRendererBuffer::EvalQueryWithOcclusionQueries(
470                                                                           //RenderCostSample &sample
471                                                                           )
472{
473        glDepthFunc(GL_LEQUAL);
474               
475        glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
476        glDepthMask(GL_FALSE);
[1000]477
478
479        // simulate detectemptyviewspace using backface culling
[1001]480        if (mDetectEmptyViewSpace)
[997]481        {
[1000]482                glEnable(GL_CULL_FACE);
[1001]483                //cout << "culling" << endl;
[1000]484        }
485        else
486        {
[1001]487                //cout << "not culling" << endl;
[1000]488                glDisable(GL_CULL_FACE);
[1001]489        }
[1000]490
491       
[1001]492        //const int numQ = 1;
493        const int numQ = (int)mOcclusionQueries.size();
[1000]494       
[1001]495        //glFinish();
496#if 0
[1000]497        //-- now issue queries for all objects
498        for (int j = 0; j < (int)mObjects.size(); ++ j)
[1001]499        {
500                mOcclusionQueries[j]->BeginQuery();
501                RenderIntersectable(mObjects[j]);
502                mOcclusionQueries[j]->EndQuery();
503
504                unsigned int pixelCount;
505
506                pixelCount = mOcclusionQueries[j]->GetQueryResult();
507                mObjects[j]->mCounter += pixelCount;
508        }
509#else
510
511        int q = 0;
512
513        //-- now issue queries for all objects
514        for (int j = 0; j < (int)mObjects.size(); j += q)
[1000]515        {       
[1001]516                for (q = 0; ((j + q) < (int)mObjects.size()) && (q < numQ); ++ q)
[997]517                {
[1000]518                        //glFinish();
[1001]519                        mOcclusionQueries[q]->BeginQuery();
[1000]520                       
[997]521                        RenderIntersectable(mObjects[j + q]);
[1000]522               
[1001]523                        mOcclusionQueries[q]->EndQuery();
[1000]524                        //glFinish();
[997]525                }
[1001]526                //cout << "q: " << q << endl;
[997]527                // collect results of the queries
[1001]528                for (int t = 0; t < q; ++ t)
[997]529                {
530                        unsigned int pixelCount;
[1001]531               
[997]532                        //-- reenable other state
[1001]533#if 0
534                        bool available;
535
[997]536                        do
537                        {
[1001]538                                available = mOcclusionQueries[t]->ResultAvailable();
539                               
540                                if (!available) cout << "W";
[997]541                        }
[1001]542                        while (!available);
[997]543#endif
544
[1001]545                        pixelCount = mOcclusionQueries[t]->GetQueryResult();
[997]546
[1001]547                        //if (pixelCount > 0)
548                        //      cout <<"o="<<j+q<<" q="<<mOcclusionQueries[q]->GetQueryId()<<" pc="<<pixelCount<<" ";
549                        mObjects[j + t]->mCounter += pixelCount;
[997]550                }
551
[1001]552                //j += q;
[997]553        }
[1001]554#endif
555        //glFinish();
[997]556        glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
557        glDepthMask(GL_TRUE);
[1000]558       
559        glEnable(GL_CULL_FACE);
[997]560}
561
562
563void
[589]564GlRendererBuffer::RandomViewPoint()
[1001]565{
[589]566  // do not use this function since it could return different viewpoints for
567  // different executions of the algorithm
568
[1112]569  //  mViewCellsManager->GetViewPoint(mViewPoint);
[589]570
571  while (1) {
572        Vector3 pVector = Vector3(halton.GetNumber(1),
573                                                          halton.GetNumber(2),
574                                                          halton.GetNumber(3));
575       
[1112]576        mViewPoint =  mViewCellsManager->GetViewSpaceBox().GetPoint(pVector);
[589]577        ViewCell *v = mViewCellsManager->GetViewCell(mViewPoint);
578        if (v && v->GetValid())
579          break;
580        // generate a new vector
581        halton.GenerateNext();
582  }
583 
584  Vector3 dVector = Vector3(2*M_PI*halton.GetNumber(4),
585                                                        M_PI*halton.GetNumber(5),
586                                                        0.0f);
587 
588  mViewDirection = Normalize(Vector3(sin(dVector.x),
589                                                                         //                                                                      cos(dVector.y),
590                                                                         0.0f,
591                                                                         cos(dVector.x)));
592  halton.GenerateNext();
593}
594
595
[1585]596void
597GlRenderer::RenderBox(const AxisAlignedBox3 &box)
598{
599
600  glBegin(GL_LINE_LOOP);
601  glVertex3d(box.Min().x, box.Max().y, box.Min().z );
602  glVertex3d(box.Max().x, box.Max().y, box.Min().z );
603  glVertex3d(box.Max().x, box.Min().y, box.Min().z );
604  glVertex3d(box.Min().x, box.Min().y, box.Min().z );
605  glEnd();
606
607  glBegin(GL_LINE_LOOP);
608  glVertex3d(box.Min().x, box.Min().y, box.Max().z );
609  glVertex3d(box.Max().x, box.Min().y, box.Max().z );
610  glVertex3d(box.Max().x, box.Max().y, box.Max().z );
611  glVertex3d(box.Min().x, box.Max().y, box.Max().z );
612  glEnd();
613
614  glBegin(GL_LINE_LOOP);
615  glVertex3d(box.Max().x, box.Min().y, box.Min().z );
616  glVertex3d(box.Max().x, box.Min().y, box.Max().z );
617  glVertex3d(box.Max().x, box.Max().y, box.Max().z );
618  glVertex3d(box.Max().x, box.Max().y, box.Min().z );
619  glEnd();
620
621  glBegin(GL_LINE_LOOP);
622  glVertex3d(box.Min().x, box.Min().y, box.Min().z );
623  glVertex3d(box.Min().x, box.Min().y, box.Max().z );
624  glVertex3d(box.Min().x, box.Max().y, box.Max().z );
625  glVertex3d(box.Min().x, box.Max().y, box.Min().z );
626  glEnd();
627
628  glBegin(GL_LINE_LOOP);
629  glVertex3d(box.Min().x, box.Min().y, box.Min().z );
630  glVertex3d(box.Max().x, box.Min().y, box.Min().z );
631  glVertex3d(box.Max().x, box.Min().y, box.Max().z );
632  glVertex3d(box.Min().x, box.Min().y, box.Max().z );
633  glEnd();
634
635  glBegin(GL_LINE_LOOP);
636  glVertex3d(box.Min().x, box.Max().y, box.Min().z );
637  glVertex3d(box.Max().x, box.Max().y, box.Min().z );
638  glVertex3d(box.Max().x, box.Max().y, box.Max().z );
639  glVertex3d(box.Min().x, box.Max().y, box.Max().z );
640
641  glEnd();
642
[811]643}
[1585]644
645void
646GlRenderer::RenderBvhNode(BvhNode *node)
647{
648  if (node->IsLeaf()) {
649        BvhLeaf *leaf = (BvhLeaf *) node;
[1589]650        for (int i=0; i < leaf->mObjects.size(); i++)
[1594]651          RenderIntersectable(leaf->mObjects[i]);
[1585]652  } else {
653        BvhInterior *in = (BvhInterior *)node;
654        RenderBvhNode(in->GetBack());
655        RenderBvhNode(in->GetFront());
656  }
[1594]657
658  //cout << "leaf obj " << i << endl;
659
660}
661
662void
663GlRenderer::RenderKdNode(KdNode *node)
664{
665  if (node->IsLeaf()) {
666        KdLeaf *leaf = (KdLeaf *) node;
667        for (int i=0; i < leaf->mObjects.size(); i++) {
668          RenderIntersectable(leaf->mObjects[i]);
669        }
670  } else {
671        KdInterior *in = (KdInterior *)node;
672        RenderKdNode(in->mBack);
673        RenderKdNode(in->mFront);
674  }
[1585]675 
676}
677
678}
Note: See TracBrowser for help on using the repository browser.