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

Revision 1145, 11.7 KB checked in by mattausch, 18 years ago (diff)

vsposp debug version

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