source: GTP/trunk/Lib/Vis/Preprocessing/src/QtInterface/QtGlRenderer.cpp @ 2173

Revision 2173, 35.5 KB checked in by bittner, 17 years ago (diff)

merge

Line 
1#include "Mesh.h"
2#include "glInterface.h"
3#include "OcclusionQuery.h"
4#include "QtGlRenderer.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 "RssPreprocessor.h"
13#include "RssTree.h"
14#include "Trackball.h"
15#include "QtPreprocessorThread.h"
16#include "Material.h"
17
18#define USE_CG 1
19
20
21#if USE_CG
22#include <Cg/cg.h>
23#include <Cg/cgGL.h>
24#endif
25
26#include <QVBoxLayout>
27
28namespace GtpVisibilityPreprocessor {
29
30
31 
32class ViewCellsManager;
33
34static CGcontext sCgContext = NULL;
35static CGprogram sCgFragmentProgram = NULL;
36static CGprofile sCgFragmentProfile;
37
38
39//static vector<OcclusionQuery *> sQueries;
40
41QtGlRendererWidget *rendererWidget = NULL;
42QtGlDebuggerWidget *debuggerWidget = NULL;
43
44
45static inline bool ilt(Intersectable *obj1, Intersectable *obj2)
46{
47        return obj1->mId < obj2->mId;
48}
49
50#if USE_CG
51static void handleCgError()
52{
53    Debug << "Cg error: " << cgGetErrorString(cgGetError()) << endl;
54    exit(1);
55}
56#endif
57
58void
59QtGlRendererBuffer::MakeCurrent()
60{
61  makeCurrent();
62}
63
64void
65QtGlRendererBuffer::DoneCurrent()
66{
67  doneCurrent();
68}
69 
70
71QtGlRendererBuffer::QtGlRendererBuffer(const int w,
72                                                                           const int h,
73                                                                           SceneGraph *sceneGraph,
74                                                                           ViewCellsManager *viewcells,
75                                                                           KdTree *tree):
76  QGLPixelBuffer(QSize(w, h)),
77  //QGLWidget(NULL, w, h),
78  GlRendererBuffer(sceneGraph, viewcells, tree)
79{
80  MakeCurrent();
81  InitGL();
82  DoneCurrent();
83}
84
85
86// reimplemented here so that we can snap the error windows
87float
88QtGlRendererBuffer::GetPixelError(int &pvsSize)
89{
90  float pErrorPixels = -1.0f;
91 
92 
93  mUseFalseColors = false;
94  unsigned int pixelCount;
95 
96 
97  ViewCell *viewcell = mViewCellsManager->GetViewCell(mViewPoint);
98  //  cout<<"View cell"<<viewcell<<endl;
99  if (viewcell == NULL)
100        return 0.0f;
101 
102  //  ViewCell *viewcell = NULL;
103 
104  //  PrVs prvs;
105 
106  //  mViewCellsManager->SetMaxFilterSize(0);
107  //  mViewCellsManager->GetPrVS(mViewPoint, prvs, mViewCellsManager->GetFilterWidth());
108  //  viewcell = prvs.mViewCell;
109 
110  ObjectPvs pvs;
111  if (1) {
112        pvs = viewcell->GetPvs();
113  } else {
114        mViewCellsManager->ApplyFilter2(viewcell,
115                                                                        false,
116                                                                        1.0f,
117                                                                        pvs);
118  }
119 
120  //  cout<<"pvs size"<<pvs.GetSize()<<endl<<flush;
121
122  SetupCamera();
123  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
124  glColorMask(GL_FALSE, GL_TRUE, GL_FALSE, GL_FALSE);
125
126
127  glStencilFunc(GL_EQUAL, 0x0, 0x1);
128  glStencilOp(GL_KEEP, GL_KEEP, GL_INCR);
129 
130  //    // Render PVS
131  ObjectPvsIterator it = pvs.GetIterator();
132 
133  pvsSize = pvs.GetSize();
134  Intersectable::NewMail();
135  for (; it.HasMoreEntries(); ) {
136        ObjectPvsEntry entry = it.Next();
137        Intersectable *object = entry.mObject;
138        RenderIntersectable(object);
139  }
140
141
142  //    glColorMask(GL_TRUE, GL_FALSE, GL_FALSE, GL_FALSE);
143  glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
144  glEnable(GL_STENCIL_TEST);
145 
146  mUseFalseColors = true;
147
148  OcclusionQuery *query = mOcclusionQueries[0];
149
150 
151  query->BeginQuery();
152 
153  SetupCamera();
154 
155  RenderScene();
156 
157  query->EndQuery();
158  glDisable(GL_STENCIL_TEST);
159  // reenable other state
160  //  int wait=0;
161  //  while (!query.ResultAvailable()) {
162  //    wait++;
163  //  }
164
165
166  pixelCount = query->GetQueryResult();
167 
168  pErrorPixels = ((float)pixelCount)/(GetWidth()*GetHeight());
169 
170 
171  if (mSnapErrorFrames && pErrorPixels >= 0.01f) {
172        glReadBuffer(GL_BACK);
173
174        char filename[256];
175        sprintf(filename, "error-frame-%04d-%0.5f.png", mFrame, pErrorPixels);
176        QImage im = toImage();
177        string str = mSnapPrefix + filename;
178        QString qstr(str.c_str());
179       
180        im.save(qstr, "PNG");
181        if (1) { //0 && mFrame == 1543) {
182          int x,y;
183          int lastIndex = -1;
184          for (y=0; y < im.height(); y++)
185                for (x=0; x < im.width(); x++) {
186                  QRgb p = im.pixel(x,y);
187                  int index = qRed(p) + (qGreen(p)<<8) + (qBlue(p)<<16);
188                  if (qGreen(p) != 255 && index!=0) {
189                        if (index != lastIndex) {
190                          //                            Debug<<"ei="<<index<<" ";
191                          lastIndex = index;
192                        }
193                  }
194                }
195        }
196       
197       
198        mUseFalseColors = false;
199        glPushAttrib(GL_CURRENT_BIT);
200        glColor3f(0,1,0);
201        glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
202        SetupCamera();
203        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
204       
205        // Render PVS
206        Intersectable::NewMail();
207
208        ObjectPvsIterator it = pvs.GetIterator();
209        for (; it.HasMoreEntries(); ) {
210          ObjectPvsEntry entry = it.Next();
211          Intersectable *object = entry.mObject;
212          RenderIntersectable(object);
213        }
214       
215        im = toImage();
216        sprintf(filename, "error-frame-%04d-%0.5f-pvs.png", mFrame, pErrorPixels);
217        str = mSnapPrefix + filename;
218        qstr = str.c_str();
219        im.save(qstr, "PNG");
220        glPopAttrib();
221  }
222 
223  glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
224 
225  return pErrorPixels;
226}
227
228int
229QtGlRendererBuffer::ComputePvs(ObjectContainer &objects,
230                                                           ObjectContainer &pvs) const
231{
232        int pvsSize = 0;
233        QImage image = toImage();
234        Intersectable::NewMail();
235
236        std::stable_sort(objects.begin(), objects.end(), ilt);
237
238        MeshInstance dummy(NULL);
239
240        Intersectable *obj = NULL;
241                       
242        for (int x = 0; x < image.width(); ++ x)
243        {
244                for (int y = 0; y < image.height(); ++ y)
245                {
246                        QRgb pix = image.pixel(x, y);
247                        const int id = GetId(qRed(pix), qGreen(pix), qBlue(pix));
248
249                        dummy.SetId(id);
250
251                        ObjectContainer::iterator oit =
252                                lower_bound(objects.begin(), objects.end(), &dummy, ilt);
253                       
254                       
255                        if (//(oit != oit.end()) &&
256                                ((*oit)->GetId() == id) &&
257                                !obj->Mailed())
258                        {
259                                obj = *oit;
260                                obj->Mail();
261                                ++ pvsSize;
262                                pvs.push_back(obj);
263                        }
264                }
265        }
266
267        return pvsSize;
268}
269
270///////////////////////////////////////////////////////////////
271///////////////////////////////////////////////////////////////
272///////////////////////////////////////////////////////////////
273///////////////////////////////////////////////////////////////
274
275
276void
277QtGlRendererWidget::InitGL()
278{
279  GlRenderer::InitGL();
280
281  GLfloat mat_ambient[]   = {  0.5, 0.5, 0.5, 1.0  };
282  /*  mat_specular and mat_shininess are NOT default values     */
283  GLfloat mat_diffuse[]   = {  1.0, 1.0, 1.0, 1.0  };
284  GLfloat mat_specular[]  = {  0.3, 0.3, 0.3, 1.0  };
285  GLfloat mat_shininess[] = {  1.0  };
286 
287  GLfloat light_ambient[]  = {  0.2, 0.2, 0.2, 1.0  };
288  GLfloat light_diffuse[]  = {  0.4, 0.4, 0.4, 1.0  };
289  GLfloat light_specular[] = {  0.3, 0.3, 0.3, 1.0  };
290 
291  GLfloat lmodel_ambient[] = {  0.3, 0.3, 0.3, 1.0  };
292 
293 
294  // default Material
295  glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient);
296  glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
297  glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
298  glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
299
300  // a light
301  glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
302  glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
303  glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
304 
305  glLightfv(GL_LIGHT1, GL_AMBIENT, light_ambient);
306  glLightfv(GL_LIGHT1, GL_DIFFUSE, light_diffuse);
307  glLightfv(GL_LIGHT1, GL_SPECULAR, light_specular);
308 
309  glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lmodel_ambient);
310 
311  glEnable(GL_LIGHTING);
312  glEnable(GL_LIGHT0);
313  glEnable(GL_LIGHT1);
314 
315 
316  // set position of the light
317  GLfloat infinite_light[] = {  1.0, 0.8, 1.0, 0.0  };
318  glLightfv (GL_LIGHT0, GL_POSITION, infinite_light);
319 
320  // set position of the light2
321  GLfloat infinite_light2[] = {  -0.3, 1.5, 1.0, 0.0  };
322  glLightfv (GL_LIGHT1, GL_POSITION, infinite_light2);
323 
324  glColorMaterial( GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE);
325  //   glColorMaterial( GL_FRONT_AND_BACK, GL_SPECULAR);
326  glEnable(GL_COLOR_MATERIAL);
327 
328  glShadeModel( GL_FLAT );
329}
330
331
332void
333QtGlRendererWidget::SetupCameraProjection(const int w, const int h, const float angle)
334{
335  if (!mTopView) {
336        int ww = w;
337        int hh = h;
338        glViewport(0, 0, ww, hh);
339        glMatrixMode(GL_PROJECTION);
340        glLoadIdentity();
341        gluPerspective(angle, ww/(float)hh, 0.1, 2.0*Magnitude(mSceneGraph->GetBox().Diagonal()));
342        glMatrixMode(GL_MODELVIEW);
343  } else {
344        int ww = w;
345        int hh = h;
346        glViewport(0, 0, ww, hh);
347        glMatrixMode(GL_PROJECTION);
348        glLoadIdentity();
349        gluPerspective(50.0, ww/(float)hh, 0.1, 20.0*Magnitude(mSceneGraph->GetBox().Diagonal()));
350        glMatrixMode(GL_MODELVIEW);
351  }
352}
353
354
355
356void
357QtGlRendererWidget::RenderPvs()
358{
359       
360  Intersectable::NewMail();
361
362  ViewCell *viewcell = NULL;
363
364  if (mDetectEmptyViewSpace)
365        glEnable( GL_CULL_FACE );
366  else
367        glDisable( GL_CULL_FACE );
368 
369  viewcell = mViewCellsManager->GetViewCell(mViewPoint, true);
370 
371  if (viewcell) {
372        // copy the pvs so that it can be filtered...
373       
374        //      mPvsCache.mPvs;
375       
376        if (mPvsCache.mViewCell != viewcell) {
377          mPvsCache.Reset();
378          mPvsCache.mViewCell = viewcell;
379         
380          if (mUseSpatialFilter) {
381                // 9.11. 2006 -> experiment with new spatial filter
382#if 0
383                mViewCellsManager->ApplySpatialFilter(mKdTree,
384                                                                                          mSpatialFilterSize*
385                                                                                          Magnitude(mViewCellsManager->GetViewSpaceBox().Size()),
386                                                                                          mPvsCache.mPvs);
387#else
388                // mSpatialFilter size is in range 0.001 - 0.1
389                mViewCellsManager->ApplyFilter2(viewcell,
390                                                                                mUseFilter,
391                                                                                100*mSpatialFilterSize,
392                                                                                mPvsCache.mPvs,
393                                                                                &mPvsCache.filteredBoxes
394                                                                                );
395#endif
396          } else
397                mPvsCache.mPvs = viewcell->GetPvs();
398         
399          emit PvsUpdated();
400        }
401
402        Intersectable::NewMail();
403
404        // Render PVS
405        if (mUseSpatialFilter && mRenderBoxes) {
406          for (int i=0; i < mPvsCache.filteredBoxes.size(); i++)
407                RenderBox(mPvsCache.filteredBoxes[i]);
408        } else {
409          ObjectPvsIterator it = mPvsCache.mPvs.GetIterator();
410         
411          mPvsSize = mPvsCache.mPvs.GetSize();
412          for (; it.HasMoreEntries(); ) {
413                ObjectPvsEntry entry = it.Next();
414                Intersectable *object = entry.mObject;
415               
416                if (mRenderVisibilityEstimates) {
417                  //float visibility = mTransferFunction*log10(entry.mData.mSumPdf + 1); // /5.0f
418                  //              glColor3f(visibility, 0.0f, 0.0f);
419
420                  RgbColor color = RainbowColorMapping(mTransferFunction*log10(entry.mData.mSumPdf + 1));
421                  glColor3f(color.r, color.g, color.b);
422                 
423                  mUseForcedColors = true;
424                  RenderIntersectable(object);
425                  mUseForcedColors = false;
426                } else {
427                  mUseForcedColors = false;
428                  RenderIntersectable(object);
429                }
430          }
431        }
432       
433        if (mRenderFilter) {
434          mWireFrame = true;
435          RenderIntersectable(viewcell);
436          glPushMatrix();
437          glTranslatef(mViewPoint.x, mViewPoint.y, mViewPoint.z);
438          glScalef(5.0f,5.0f,5.0f);
439          glPushAttrib(GL_CURRENT_BIT);
440          glColor3f(1.0f, 0.0f, 0.0f);
441          //      gluSphere((::GLUquadric *)mSphere,
442          //                            1e-3*Magnitude(mViewCellsManager->GetViewSpaceBox().Size()), 6, 6);
443          glPopAttrib();
444          glPopMatrix();
445          mWireFrame = false;
446        }
447  } else {
448        ObjectContainer::const_iterator oi = mObjects.begin();
449        for (; oi != mObjects.end(); oi++)
450          RenderIntersectable(*oi);
451  }
452}
453
454float
455QtGlRendererWidget::RenderErrors()
456{
457  float pErrorPixels = -1.0f;
458 
459  SetupCamera();
460  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
461
462  glPushAttrib(GL_ENABLE_BIT);
463
464  glStencilFunc(GL_EQUAL, 0x0, 0x1);
465  glStencilOp(GL_KEEP, GL_KEEP, GL_INCR);
466
467  mUseForcedColors = true;
468
469  glColor3f(0.0f, 0.8f, 0.2f);
470
471  //    // Render PVS
472  RenderPvs();
473 
474  glEnable(GL_STENCIL_TEST);
475 
476  //  mUseFalseColors = true;
477 
478  glDisable(GL_LIGHTING);
479 
480  OcclusionQuery *query = mOcclusionQueries[0];
481  query->BeginQuery();
482 
483  SetupCamera();
484
485  glColor3f(1.0f, 0.0f, 0.0f);
486 
487  RenderScene();
488
489  mUseForcedColors = false;
490
491  query->EndQuery();
492 
493  glDisable(GL_STENCIL_TEST);
494  glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
495
496  glPopAttrib();
497
498  // reenable other state
499  //  int wait=0;
500  //  while (!query.ResultAvailable()) {
501  //    wait++;
502  //  }
503
504  int pixelCount = query->GetQueryResult();
505  pErrorPixels = ((float)pixelCount)/(GetWidth()*GetHeight());
506  cout<<"error pixels="<<pixelCount<<endl;
507
508  mRenderError = pErrorPixels;
509
510  return pErrorPixels;
511}
512
513
514void
515QtGlRendererWidget::mousePressEvent(QMouseEvent *e)
516{
517  int x = e->pos().x();
518  int y = e->pos().y();
519
520  mousePoint.x = x;
521  mousePoint.y = y;
522 
523}
524
525void
526QtGlRendererWidget::mouseMoveEvent(QMouseEvent *e)
527{
528  float MOVE_SENSITIVITY = Magnitude(mSceneGraph->GetBox().Diagonal())*1e-3;
529  float TURN_SENSITIVITY=0.1f;
530  float TILT_SENSITIVITY=32.0 ;
531  float TURN_ANGLE= M_PI/36.0 ;
532
533  int x = e->pos().x();
534  int y = e->pos().y();
535
536  int diffx = -(mousePoint.x - x);
537  int diffy = -(mousePoint.y - y);
538 
539  if (e->modifiers() & Qt::ControlModifier) {
540        mViewPoint.y += (y-mousePoint.y)*MOVE_SENSITIVITY/2.0;
541        mViewPoint.x += (x-mousePoint.x)*MOVE_SENSITIVITY/2.0;
542  } else {
543        mViewPoint += mViewDirection*((mousePoint.y - y)*MOVE_SENSITIVITY);
544        float adiff = TURN_ANGLE*(x - mousePoint.x)*-TURN_SENSITIVITY;
545        float angle = atan2(mViewDirection.x, mViewDirection.z);
546        mViewDirection.x = sin(angle+adiff);
547        mViewDirection.z = cos(angle+adiff);
548  }
549 
550  mousePoint.x = x;
551  mousePoint.y = y;
552 
553  updateGL();
554}
555
556void
557QtGlRendererWidget::mouseReleaseEvent(QMouseEvent *)
558{
559
560
561}
562
563void
564QtGlRendererWidget::resizeGL(int w, int h)
565{
566  SetupCameraProjection(w, h);
567  updateGL();
568}
569
570void
571QtGlRendererWidget::paintGL()
572{
573  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
574
575  if (1) {
576        SetupCameraProjection(width(), height());
577        SetupCamera();
578       
579        if (mRenderErrors) {
580          RenderErrors();
581        } else {
582          glColor3f(0.6f, 0.6f, 0.6f);
583          RenderPvs();
584        }
585  }
586
587  RenderInfo();
588
589  mFrame++;
590 
591}
592
593
594void
595QtGlRendererWidget::SetupCamera()
596{
597  if (!mTopView)
598        GlRenderer::SetupCamera();
599  else {
600        if (0) {
601          float dist = Magnitude(mSceneGraph->GetBox().Diagonal())*0.05;
602          Vector3 pos = mViewPoint - dist*Vector3(mViewDirection.x,
603                                                                                          -1,
604                                                                                          mViewDirection.y);
605         
606          Vector3 target = mViewPoint + dist*mViewDirection;
607          Vector3 up(0,1,0);
608         
609          glLoadIdentity();
610          gluLookAt(pos.x, pos.y, pos.z,
611                                target.x, target.y, target.z,
612                                up.x, up.y, up.z);
613        } else {
614          float dist = Magnitude(mSceneGraph->GetBox().Diagonal())*mTopDistance;
615          Vector3 pos = mViewPoint  + dist*Vector3(0,
616                                                                                           1,
617                                                                                           0);
618         
619          Vector3 target = mViewPoint;
620          Vector3 up(mViewDirection.x, 0, mViewDirection.z);
621         
622          glLoadIdentity();
623          gluLookAt(pos.x, pos.y, pos.z,
624                                target.x, target.y, target.z,
625                                up.x, up.y, up.z);
626         
627        }
628  }
629
630}
631
632
633void
634QtGlRendererWidget::keyPressEvent ( QKeyEvent * e )
635{
636  switch (e->key()) {
637  case Qt::Key_T:
638        mTopView = !mTopView;
639        SetupCameraProjection(width(), height());
640        updateGL();
641        break;
642  case Qt::Key_V:
643        mRenderViewCells = !mRenderViewCells;
644        updateGL();
645        break;
646  case Qt::Key_P:
647        // set random viewpoint
648        mViewCellsManager->GetViewPoint(mViewPoint);
649        updateGL();
650        break;
651  case Qt::Key_S: {
652        // set view poitn and direction
653        QString text;
654        bool ok;
655        text.sprintf("%f %f %f", mViewPoint.x, mViewPoint.y, mViewPoint.z);
656        text = QInputDialog::getText(this,
657                                                                 "Enter a view point",
658                                                                 "",
659                                                                 QLineEdit::Normal,
660                                                                 text,
661                                                                 &ok);
662        if (!ok)
663          break;
664       
665        if (sscanf(text.toAscii(), "%f %f %f", &mViewPoint.x, &mViewPoint.y, &mViewPoint.z) == 3) {
666          text.sprintf("%f %f %f", mViewDirection.x, mViewDirection.y, mViewDirection.z);
667          text = QInputDialog::getText(this,
668                                                                   "Enter a direction",
669                                                                   "",
670                                                                   QLineEdit::Normal,
671                                                                   text,
672                                                                   &ok);
673          if (!ok)
674                break;
675          if (sscanf(text.toAscii(), "%f %f %f", &mViewDirection.x,
676                                 &mViewDirection.y, &mViewDirection.z) == 3) {
677                updateGL();
678          }
679          break;
680        }
681  }
682  default:
683        e->ignore();
684        break;
685  }
686}
687
688 
689
690QtGlRendererWidget::QtGlRendererWidget(
691                                                                           SceneGraph *sceneGraph,
692                                                                           ViewCellsManager *viewcells,
693                                                                           KdTree *tree,
694                                                                           QWidget * parent,
695                                                                           const QGLWidget * shareWidget,
696                                                                           Qt::WFlags f
697                                                                           )
698  :
699  GlRendererWidget(sceneGraph, viewcells, tree), QGLWidget(parent, shareWidget, f)
700{
701  mPreprocessorThread = NULL;
702  mTopView = false;
703  mRenderViewCells = false;
704  mTopDistance = 1.0f;
705  mCutViewCells = false;
706  mCutScene = false;
707  mRenderErrors = false;
708  mRenderBoxes = false;
709  mRenderFilter = true;
710  mRenderVisibilityEstimates = false;
711  mTransferFunction = 0.2f;
712
713  bool tmp;
714
715  Environment::GetSingleton()->GetBoolValue("Preprocessor.applyVisibilityFilter", tmp );
716  mUseFilter = tmp;
717 
718  Environment::GetSingleton()->GetBoolValue("Preprocessor.applyVisibilitySpatialFilter",
719                                                        tmp );
720  mUseSpatialFilter = tmp;
721
722  mShowRenderCost = false;
723  mShowPvsSizes = false;
724  mSpatialFilterSize = 0.01;
725  mPvsSize = 0;
726  mRenderError = 0.0f;
727  mShowRays = false;
728 
729  mControlWidget = new QtRendererControlWidget(NULL);
730 
731  connect(mControlWidget, SIGNAL(SetViewCellGranularity(int)), this, SLOT(SetViewCellGranularity(int)));
732  connect(mControlWidget,
733                  SIGNAL(SetTransferFunction(int)),
734                  this,
735                  SLOT(SetTransferFunction(int)));
736  connect(mControlWidget, SIGNAL(UpdateAllPvs()), this, SLOT(UpdateAllPvs()));
737  connect(mControlWidget, SIGNAL(ComputeVisibility()), this, SLOT(ComputeVisibility()));
738  connect(mControlWidget, SIGNAL(StopComputation()), this, SLOT(StopComputation()));
739  connect(mControlWidget, SIGNAL(SetRandomViewPoint()), this,
740                  SLOT(SetRandomViewPoint()));
741
742  connect(mControlWidget, SIGNAL(SetSceneCut(int)), this, SLOT(SetSceneCut(int)));
743  connect(mControlWidget, SIGNAL(SetTopDistance(int)), this, SLOT(SetTopDistance(int)));
744
745  connect(mControlWidget, SIGNAL(SetVisibilityFilterSize(int)), this, SLOT(SetVisibilityFilterSize(int)));
746  connect(mControlWidget, SIGNAL(SetSpatialFilterSize(int)), this, SLOT(SetSpatialFilterSize(int)));
747
748  connect(mControlWidget, SIGNAL(SetShowViewCells(bool)), this, SLOT(SetShowViewCells(bool)));
749  connect(mControlWidget, SIGNAL(SetShowRenderCost(bool)), this, SLOT(SetShowRenderCost(bool)));
750  connect(mControlWidget, SIGNAL(SetShowPvsSizes(bool)), this, SLOT(SetShowPvsSizes(bool)));
751  connect(mControlWidget, SIGNAL(SetTopView(bool)), this, SLOT(SetTopView(bool)));
752  connect(mControlWidget, SIGNAL(SetCutViewCells(bool)), this, SLOT(SetCutViewCells(bool)));
753  connect(mControlWidget, SIGNAL(SetCutScene(bool)), this, SLOT(SetCutScene(bool)));
754  connect(mControlWidget, SIGNAL(SetRenderErrors(bool)), this, SLOT(SetRenderErrors(bool)));
755  connect(mControlWidget, SIGNAL(SetRenderBoxes(bool)), this, SLOT(SetRenderBoxes(bool)));
756  connect(mControlWidget, SIGNAL(SetRenderFilter(bool)), this, SLOT(SetRenderFilter(bool)));
757  connect(mControlWidget, SIGNAL(SetRenderVisibilityEstimates(bool)),
758                  this, SLOT(SetRenderVisibilityEstimates(bool)));
759  connect(mControlWidget, SIGNAL(SetUseFilter(bool)), this, SLOT(SetUseFilter(bool)));
760  connect(mControlWidget, SIGNAL(SetUseSpatialFilter(bool)),
761                  this, SLOT(SetUseSpatialFilter(bool)));
762
763  connect(mControlWidget,
764                  SIGNAL(SetShowRays(bool)),
765                  this,
766                  SLOT(SetShowRays(bool)));
767
768  resize(1000, 500);
769  mControlWidget->show();
770}
771
772void
773QtGlRendererWidget::UpdateAllPvs()
774{
775  // $$ does not work so far:(
776  mViewCellsManager->UpdatePvsForEvaluation();
777  //    mViewCellsManager->FinalizeViewCells(false);
778}
779
780void
781QtGlRendererWidget::ComputeVisibility()
782{
783  cerr<<"Compute Visibility called!\n"<<endl;
784  if (!mPreprocessorThread->isRunning())
785        mPreprocessorThread->RunThread();
786}
787
788void
789QtGlRendererWidget::StopComputation()
790{
791  cerr<<"stop computation called!\n"<<endl;
792  mViewCellsManager->GetPreprocessor()->mStopComputation = true;
793}
794
795void
796QtGlRendererWidget::SetRandomViewPoint()
797{
798  cerr<<"stop computation called!\n"<<endl;
799  mViewCellsManager->GetViewPoint(mViewPoint);
800  updateGL();
801}
802
803void
804QtGlRendererWidget::RenderRenderCost()
805{
806        static vector<float> costFunction;
807        static float maxCost = -1;
808        if (costFunction.size()==0) {
809          ViewCellsTree *tree = mViewCellsManager->GetViewCellsTree();
810          if (tree) {
811                tree->GetCostFunction(costFunction);
812                maxCost = -1;
813                for (int i=0;  i < costFunction.size(); i++) {
814                  //              cout<<i<<":"<<costFunction[i]<<" ";
815                  // update cost function to an absolute value based on the total geometry count
816                  costFunction[i]*=mSceneGraph->GetRoot()->mGeometry.size();
817                  if (costFunction[i] > maxCost)
818                        maxCost = costFunction[i];
819                }
820          }
821        }
822
823       
824        int currentPos = (int)mViewCellsManager->GetViewCells().size();
825        float currentCost= -1;
826
827        if (currentPos < costFunction.size())
828          currentCost = costFunction[currentPos];
829#if 1   
830        cout<<"costFunction.size()="<<costFunction.size()<<endl;
831        cout<<"CP="<<currentPos<<endl;
832        cout<<"MC="<<maxCost<<endl;
833        cout<<"CC="<<currentCost<<endl;
834#endif
835        if (costFunction.size()) {
836          float scaley = 1.0f/log10(maxCost);
837          float scalex = 1.0f/(float)costFunction.size();
838
839          glDisable(GL_DEPTH_TEST);
840          // init ortographic projection
841          glMatrixMode(GL_PROJECTION);
842         
843          glPushMatrix();
844         
845          glLoadIdentity();
846          gluOrtho2D(0, 1.0f, 0, 1.0f);
847         
848          glTranslatef(0.1f, 0.1f, 0.0f);
849          glScalef(0.8f, 0.8f, 1.0f);
850          glMatrixMode(GL_MODELVIEW);
851          glLoadIdentity();
852         
853          glColor3f(1.0f,0,0);
854          glBegin(GL_LINE_STRIP);
855          //      glVertex3f(0,0,0);
856         
857          for (int i=0;  i < costFunction.size(); i++) {
858                float x =  i*scalex;
859                float y = log10(costFunction[i])*scaley;
860                glVertex3f(x,y,0.0f);
861          }
862          glEnd();
863         
864          glColor3f(1.0f,0,0);
865          glBegin(GL_LINES);
866          float x =  currentPos*scalex;
867          glVertex3f(x,0.0,0.0f);
868          glVertex3f(x,1.0f,0.0f);
869          glEnd();
870         
871          glColor3f(0.0f,0,0);
872          // show a grid
873          glBegin(GL_LINE_LOOP);
874          glVertex3f(0,0,0.0f);
875          glVertex3f(1,0,0.0f);
876          glVertex3f(1,1,0.0f);
877          glVertex3f(0,1,0.0f);
878          glEnd();
879
880          glBegin(GL_LINES);
881          for (int i=0;  i < costFunction.size(); i += 1000) {
882                float x =  i*scalex;
883                glVertex3f(x,0.0,0.0f);
884                glVertex3f(x,1.0f,0.0f);
885          }
886
887          for (int i=0;  pow(10.0f, i) < maxCost; i+=1) {
888                float y = i*scaley;
889                //              QString s;
890                //              s.sprintf("%d", (int)pow(10,i));
891                //              renderText(width()/2+5, y*height(), s);
892                glVertex3f(0.0f, y, 0.0f);
893                glVertex3f(1.0f, y, 0.0f);
894          }
895
896          glEnd();
897
898         
899          // restore the projection matrix
900          glMatrixMode(GL_PROJECTION);
901          glPopMatrix();
902          glMatrixMode(GL_MODELVIEW);
903          glEnable(GL_DEPTH_TEST);
904
905        }
906 
907
908
909}
910
911void
912QtGlRendererWidget::RenderInfo()
913{
914 
915  QString s;
916  int vc = 0;
917  if (mViewCellsManager)
918        vc = mViewCellsManager->GetViewCells().size();
919
920  int filter = 0;
921  if (mViewCellsManager)
922        filter = mViewCellsManager->GetMaxFilterSize();
923
924  glColor3f(1.0f,1.0f,1.0f);
925
926  s.sprintf("frame:%04d viewpoint:(%4.1f,%4.1f,%4.1f) dir:(%4.1f,%4.1f,%4.1f)",
927                        mFrame,
928                        mViewPoint.x,
929                        mViewPoint.y,
930                        mViewPoint.z,
931                        mViewDirection.x,
932                        mViewDirection.y,
933                        mViewDirection.z
934                        );
935
936  renderText(20,20,s);
937
938  s.sprintf("viewcells:%04d filter:%04d pvs:%04d error:%5.5f\%",
939                        vc,
940                        filter,
941                        mPvsSize,
942                        mRenderError*100.0f);
943
944
945  renderText(20,40,s);
946
947 
948
949 
950}
951
952
953void
954QtGlRendererWidget::SetViewCellGranularity(int number)
955{
956  if (mViewCellsManager) {
957        //      mViewCellsManager->SetMaxFilterSize(number);
958
959        // $$ tmp off
960        mViewCellsManager->CollectViewCells(number);
961
962        // $$ does not work so far:(
963        //      mViewCellsManager->UpdatePvsForEvaluation();
964        //      mViewCellsManager->FinalizeViewCells(false);
965  }
966  updateGL();
967}
968
969void
970QtGlRendererWidget::SetVisibilityFilterSize(int number)
971{
972  if (mViewCellsManager)
973        mViewCellsManager->SetMaxFilterSize(number);
974  mPvsCache.Reset();
975  updateGL();
976}
977
978void
979QtGlRendererWidget::SetSpatialFilterSize(int number)
980{
981  mSpatialFilterSize = 1e-3*number;
982  mPvsCache.Reset();
983  updateGL();
984}
985
986void
987QtGlRendererWidget::SetSceneCut(int number)
988{
989  // assume the cut plane can only be aligned with xz plane
990  // shift it along y according to number, which is percentage of the bounding
991  // box position
992  if (mViewCellsManager) {
993        AxisAlignedBox3 box = mViewCellsManager->GetViewSpaceBox();
994        Vector3 p = box.Min() + (number/1000.0f)*box.Max();
995        mSceneCutPlane.mNormal = Vector3(0,-1,0);
996        mSceneCutPlane.mD = -DotProd(mSceneCutPlane.mNormal, p);
997        updateGL();
998  }
999}
1000
1001void
1002QtGlRendererWidget::SetTopDistance(int number)
1003{
1004  mTopDistance = number/1000.0f;
1005  updateGL();
1006}
1007
1008void
1009QtGlRendererWidget::RenderViewCells()
1010{
1011  mUseFalseColors = true;
1012
1013  glPushAttrib(GL_CURRENT_BIT | GL_ENABLE_BIT | GL_POLYGON_BIT);
1014 
1015  glEnable(GL_CULL_FACE);
1016  glCullFace(GL_FRONT);
1017
1018  glDisable(GL_CULL_FACE);
1019  double eq[4];
1020  eq[0] = mSceneCutPlane.mNormal.x;
1021  eq[1] = mSceneCutPlane.mNormal.y;
1022  eq[2] = mSceneCutPlane.mNormal.z;
1023  eq[3] = mSceneCutPlane.mD;
1024
1025  if (mCutViewCells) {
1026        glClipPlane(GL_CLIP_PLANE0, eq);
1027        glEnable(GL_CLIP_PLANE0);
1028  }
1029 
1030  int i;
1031  ViewCellContainer &viewcells = mViewCellsManager->GetViewCells();
1032  int maxPvs = -1;
1033  for (i=0; i < viewcells.size(); i++)
1034  {
1035        ViewCell *vc = viewcells[i];
1036
1037        //const int p = vc->GetPvs().CountObjectsInPvs();
1038        const int p = vc->GetPvs().GetSize();
1039        if (p > maxPvs)
1040          maxPvs = p;
1041  }
1042
1043
1044  for (i=0; i < viewcells.size(); i++) {
1045        ViewCell *vc = viewcells[i];
1046        //      Mesh *m = vc->GetMesh();
1047
1048        RgbColor c;
1049
1050        if (!mShowPvsSizes) {
1051          mWireFrame = true;
1052          c = vc->GetColor();
1053        } else {
1054          //      const float importance = 5.0f*mTransferFunction * ((float)vc->GetPvs().CountObjectsInPvs() / (float)maxPvs);
1055          const float importance = 5.0f*mTransferFunction *
1056                ((float)vc->GetPvs().GetSize() / (float)maxPvs);
1057          //      c = RgbColor(importance, 1.0f - importance, 0.0f);
1058          c = RainbowColorMapping(importance);
1059         
1060        }
1061        glColor3f(c.r, c.g, c.b);
1062       
1063        RenderViewCell(vc);
1064  }
1065
1066  mUseFalseColors = false;
1067  mWireFrame = false;
1068
1069  glPopAttrib();
1070 
1071}
1072
1073
1074
1075
1076
1077/***********************************************************************/
1078/*                     QtGlDebuggerWidget implementation                                   */
1079/***********************************************************************/
1080
1081
1082QtGlDebuggerWidget::QtGlDebuggerWidget(QtGlRendererBuffer *buf, QWidget *parent)
1083      : QGLWidget(QGLFormat(QGL::SampleBuffers), parent), mRenderBuffer(buf)
1084{
1085        // create the pbuffer
1086    //pbuffer = new QGLPixelBuffer(QSize(512, 512), format(), this);
1087    timerId = startTimer(20);
1088    setWindowTitle(("OpenGL pbuffers"));
1089}
1090
1091
1092QtGlDebuggerWidget::~QtGlDebuggerWidget()
1093{
1094 mRenderBuffer->releaseFromDynamicTexture();
1095   glDeleteTextures(1, &dynamicTexture);
1096         
1097         DEL_PTR(mRenderBuffer);
1098}
1099
1100
1101
1102QtRendererControlWidget::QtRendererControlWidget(QWidget * parent, Qt::WFlags f):
1103  QWidget(parent, f)
1104{
1105
1106  QVBoxLayout *vl = new QVBoxLayout;
1107  setLayout(vl);
1108 
1109  QWidget *vbox = new QGroupBox("ViewCells", this);
1110  layout()->addWidget(vbox);
1111 
1112  vl = new QVBoxLayout;
1113  vbox->setLayout(vl);
1114
1115  QLabel *label = new QLabel("Granularity");
1116  vbox->layout()->addWidget(label);
1117 
1118  QSlider *slider = new QSlider(Qt::Horizontal, vbox);
1119  vl->addWidget(slider);
1120  slider->show();
1121  slider->setRange(1, 10000);
1122  slider->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
1123  slider->setValue(200);
1124
1125 
1126  connect(slider, SIGNAL(valueChanged(int)), SIGNAL(SetViewCellGranularity(int)));
1127
1128  label = new QLabel("Transfer function");
1129  vbox->layout()->addWidget(label);
1130 
1131  slider = new QSlider(Qt::Horizontal, vbox);
1132  vl->addWidget(slider);
1133  slider->show();
1134  slider->setRange(1, 10000);
1135  slider->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
1136  slider->setValue(100);
1137
1138 
1139  connect(slider, SIGNAL(valueChanged(int)), SIGNAL(SetTransferFunction(int)));
1140
1141  {
1142        QPushButton *button = new QPushButton("Update all PVSs", vbox);
1143        vbox->layout()->addWidget(button);
1144        connect(button, SIGNAL(clicked()), SLOT(UpdateAllPvs()));
1145  }
1146 
1147 
1148  label = new QLabel("Filter size");
1149  vbox->layout()->addWidget(label);
1150 
1151  slider = new QSlider(Qt::Horizontal, vbox);
1152  vbox->layout()->addWidget(slider);
1153  slider->show();
1154  slider->setRange(1, 100);
1155  slider->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
1156  slider->setValue(3);
1157 
1158  connect(slider, SIGNAL(valueChanged(int)), SIGNAL(SetVisibilityFilterSize(int)));
1159
1160
1161  label = new QLabel("Spatial Filter size");
1162  vbox->layout()->addWidget(label);
1163 
1164  slider = new QSlider(Qt::Horizontal, vbox);
1165  vbox->layout()->addWidget(slider);
1166  slider->show();
1167  slider->setRange(1, 100);
1168  slider->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
1169  slider->setValue(10);
1170 
1171  connect(slider, SIGNAL(valueChanged(int)), SIGNAL(SetSpatialFilterSize(int)));
1172
1173
1174
1175  QWidget *hbox = new QWidget(vbox);
1176  vl->addWidget(hbox);
1177  QHBoxLayout *hlayout = new QHBoxLayout;
1178  hbox->setLayout(hlayout);
1179 
1180  QCheckBox *cb = new QCheckBox("Show viewcells", hbox);
1181  hlayout->addWidget(cb);
1182  cb->setChecked(false);
1183  connect(cb, SIGNAL(toggled(bool)), SIGNAL(SetShowViewCells(bool)));
1184
1185  cb = new QCheckBox("Render cost curve", hbox);
1186  hlayout->addWidget(cb);
1187  cb->setChecked(false);
1188  connect(cb, SIGNAL(toggled(bool)), SIGNAL(SetShowRenderCost(bool)));
1189
1190  cb = new QCheckBox("Show render cost", hbox);
1191  hlayout->addWidget(cb);
1192  cb->setChecked(false);
1193  connect(cb, SIGNAL(toggled(bool)), SIGNAL(SetShowPvsSizes(bool)));
1194
1195  cb = new QCheckBox("Show rays", hbox);
1196  hlayout->addWidget(cb);
1197  cb->setChecked(false);
1198  connect(cb, SIGNAL(toggled(bool)), SIGNAL(SetShowRays(bool)));
1199
1200  vbox->resize(800,100);
1201
1202 
1203  vbox = new QGroupBox("Rendering", this);
1204  layout()->addWidget(vbox);
1205 
1206  vl = new QVBoxLayout;
1207  vbox->setLayout(vl);
1208
1209
1210
1211  cb = new QCheckBox("Cut view cells", vbox);
1212  vbox->layout()->addWidget(cb);
1213  cb->setChecked(false);
1214  connect(cb, SIGNAL(toggled(bool)), SIGNAL(SetCutViewCells(bool)));
1215
1216
1217  slider = new QSlider(Qt::Horizontal, vbox);
1218  vbox->layout()->addWidget(slider);
1219  slider->show();
1220  slider->setRange(0, 1000);
1221  slider->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
1222  slider->setValue(1000);
1223
1224  connect(slider, SIGNAL(valueChanged(int)), SIGNAL(SetSceneCut(int)));
1225
1226
1227  cb = new QCheckBox("Cut scene", vbox);
1228  vbox->layout()->addWidget(cb);
1229  cb->setChecked(false);
1230  connect(cb, SIGNAL(toggled(bool)), SIGNAL(SetCutScene(bool)));
1231
1232  cb = new QCheckBox("Render boxes", vbox);
1233  vbox->layout()->addWidget(cb);
1234  cb->setChecked(false);
1235  connect(cb, SIGNAL(toggled(bool)), SIGNAL(SetRenderBoxes(bool)));
1236
1237  cb = new QCheckBox("Render visibility estimates", vbox);
1238  vbox->layout()->addWidget(cb);
1239  cb->setChecked(false);
1240  connect(cb, SIGNAL(toggled(bool)), SIGNAL(SetRenderVisibilityEstimates(bool)));
1241
1242 
1243  cb = new QCheckBox("Render errors", vbox);
1244  vbox->layout()->addWidget(cb);
1245  cb->setChecked(false);
1246  connect(cb, SIGNAL(toggled(bool)), SIGNAL(SetRenderErrors(bool)));
1247
1248 
1249  bool tmp;
1250
1251  cb = new QCheckBox("Use filter", vbox);
1252  vbox->layout()->addWidget(cb);
1253  Environment::GetSingleton()->GetBoolValue("Preprocessor.applyVisibilityFilter", tmp );
1254  cb->setChecked(tmp);
1255  connect(cb, SIGNAL(toggled(bool)), SIGNAL(SetUseFilter(bool)));
1256
1257 
1258  cb = new QCheckBox("Use spatial filter", vbox);
1259  vbox->layout()->addWidget(cb);
1260  Environment::GetSingleton()->GetBoolValue("Preprocessor.applyVisibilitySpatialFilter",
1261                                                                                        tmp );
1262  cb->setChecked(tmp);
1263  connect(cb, SIGNAL(toggled(bool)), SIGNAL(SetUseSpatialFilter(bool)));
1264
1265  cb = new QCheckBox("Render filter", vbox);
1266  vbox->layout()->addWidget(cb);
1267  cb->setChecked(true);
1268  connect(cb, SIGNAL(toggled(bool)), SIGNAL(SetRenderFilter(bool)));
1269
1270
1271
1272 
1273  slider = new QSlider(Qt::Horizontal, vbox);
1274  vbox->layout()->addWidget(slider);
1275  slider->show();
1276  slider->setRange(1, 1000);
1277  slider->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
1278  slider->setValue(500);
1279 
1280  connect(slider, SIGNAL(valueChanged(int)), SIGNAL(SetTopDistance(int)));
1281 
1282  cb = new QCheckBox("Top View", vbox);
1283  vbox->layout()->addWidget(cb);
1284  cb->setChecked(false);
1285  connect(cb, SIGNAL(toggled(bool)), SIGNAL(SetTopView(bool)));
1286
1287
1288
1289  vbox = new QGroupBox("PVS Errors", this);
1290  layout()->addWidget(vbox);
1291 
1292  vl = new QVBoxLayout;
1293  vbox->setLayout(vl);
1294
1295  QPushButton *button = new QPushButton("Compute Visibility", vbox);
1296  vbox->layout()->addWidget(button);
1297  connect(button, SIGNAL(clicked()), SLOT(ComputeVisibility()));
1298
1299  button = new QPushButton("Stop Computation", vbox);
1300  vbox->layout()->addWidget(button);
1301  connect(button, SIGNAL(clicked()), SLOT(StopComputation()));
1302
1303  button = new QPushButton("Set Random View Point", vbox);
1304  vbox->layout()->addWidget(button);
1305  connect(button, SIGNAL(clicked()), SLOT(SetRandomViewPoint()));
1306
1307
1308  if (0) {
1309        vbox = new QGroupBox("PVS Errors", this);
1310        layout()->addWidget(vbox);
1311       
1312        vl = new QVBoxLayout;
1313        vbox->setLayout(vl);
1314       
1315        mPvsErrorWidget = new QListWidget(vbox);
1316        vbox->layout()->addWidget(mPvsErrorWidget);
1317       
1318        connect(mPvsErrorWidget,
1319                        SIGNAL(doubleClicked(const QModelIndex &)),
1320                        this,
1321                        SLOT(PvsErrorClicked(const QModelIndex &)));
1322       
1323        QPushButton *button = new QPushButton("Next Error Frame", vbox);
1324        vbox->layout()->addWidget(button);
1325        connect(button, SIGNAL(clicked(void)), SLOT(FocusNextPvsErrorFrame(void)));
1326  }
1327 
1328  setWindowTitle("Preprocessor Control Widget");
1329  adjustSize();
1330}
1331
1332
1333
1334
1335void
1336QtRendererControlWidget::FocusNextPvsErrorFrame(void)
1337{
1338 
1339 
1340}
1341
1342void
1343QtRendererControlWidget::UpdatePvsErrorItem(int row,
1344                                                                                  GlRendererBuffer::PvsErrorEntry &pvsErrorEntry)
1345{
1346
1347  QListWidgetItem *i = mPvsErrorWidget->item(row);
1348  QString s;
1349  s.sprintf("%5.5f", pvsErrorEntry.mError);
1350  if (i) {
1351        i->setText(s);
1352  } else {
1353        new QListWidgetItem(s, mPvsErrorWidget);
1354  }
1355  mPvsErrorWidget->update();
1356}
1357
1358
1359void QtGlDebuggerWidget::initializeGL()
1360{
1361        glMatrixMode(GL_PROJECTION);
1362        glLoadIdentity();
1363
1364        glFrustum(-1, 1, -1, 1, 10, 100);
1365        glTranslatef(-0.5f, -0.5f, -0.5f);
1366        glTranslatef(0.0f, 0.0f, -15.0f);
1367        glMatrixMode(GL_MODELVIEW);
1368
1369        glEnable(GL_CULL_FACE);
1370        initCommon();
1371        initPbuffer();
1372
1373}
1374
1375
1376void QtGlDebuggerWidget::resizeGL(int w, int h)
1377{
1378        glViewport(0, 0, w, h);
1379}
1380
1381
1382void QtGlDebuggerWidget::paintGL()
1383{
1384        // draw a spinning cube into the pbuffer..
1385        mRenderBuffer->makeCurrent();
1386       
1387        BeamSampleStatistics stats;
1388        mRenderBuffer->SampleBeamContributions(mSourceObject, mBeam, mSamples, stats);
1389
1390        glFlush();
1391
1392        // rendering directly to a texture is not supported on X11, unfortunately
1393    mRenderBuffer->updateDynamicTexture(dynamicTexture);
1394   
1395    // and use the pbuffer contents as a texture when rendering the
1396    // background and the bouncing cubes
1397    makeCurrent();
1398    glBindTexture(GL_TEXTURE_2D, dynamicTexture);
1399    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
1400
1401    // draw the background
1402    glMatrixMode(GL_MODELVIEW);
1403    glPushMatrix();
1404    glLoadIdentity();
1405    glMatrixMode(GL_PROJECTION);
1406    glPushMatrix();
1407    glLoadIdentity();
1408
1409        glPopMatrix();
1410        glMatrixMode(GL_MODELVIEW);
1411        glPopMatrix();
1412}
1413
1414
1415void QtGlDebuggerWidget::initPbuffer()
1416{
1417        // set up the pbuffer context
1418    mRenderBuffer->makeCurrent();
1419        /*mRenderBuffer->InitGL();
1420
1421        glViewport(0, 0, mRenderBuffer->size().width(), mRenderBuffer->size().height());
1422        glMatrixMode(GL_PROJECTION);
1423        glLoadIdentity();
1424        glOrtho(-1, 1, -1, 1, -99, 99);
1425        glTranslatef(-0.5f, -0.5f, 0.0f);
1426        glMatrixMode(GL_MODELVIEW);
1427        glLoadIdentity();
1428
1429        glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);*/
1430       
1431        // generate a texture that has the same size/format as the pbuffer
1432    dynamicTexture = mRenderBuffer->generateDynamicTexture();
1433
1434        // bind the dynamic texture to the pbuffer - this is a no-op under X11
1435        mRenderBuffer->bindToDynamicTexture(dynamicTexture);
1436        makeCurrent();
1437}
1438
1439void QtGlDebuggerWidget::initCommon()
1440{
1441        glEnable(GL_TEXTURE_2D);
1442        glEnable(GL_DEPTH_TEST);
1443
1444        glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
1445}
1446
1447
1448
1449}
Note: See TracBrowser for help on using the repository browser.