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

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