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

Revision 2049, 35.2 KB checked in by bittner, 17 years ago (diff)

glrenderer updates

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