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

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