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

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

samplign preprocessor updates, merge

Line 
1#include "Mesh.h"
2#include "glInterface.h"
3#include "OcclusionQuery.h"
4#include "QtGlRenderer.h"
5#include "ViewCellsManager.h"
6#include "SceneGraph.h"
7#include "Pvs.h"
8#include "Viewcell.h"
9#include "Beam.h"
10#include "KdTree.h"
11#include "Environment.h"
12#include "RssPreprocessor.h"
13#include "RssTree.h"
14#include "Trackball.h"
15#include "QtPreprocessorThread.h"
16
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        QTextStream ts(&text);
708        ts>>mViewPoint.x>>mViewPoint.y>>mViewPoint.z;
709        mViewCellsManager->GetViewPoint(mViewPoint);
710        updateGL();
711        break;
712  }
713  default:
714        e->ignore();
715        break;
716  }
717}
718
719 
720
721QtGlRendererWidget::QtGlRendererWidget(
722                                                                           SceneGraph *sceneGraph,
723                                                                           ViewCellsManager *viewcells,
724                                                                           KdTree *tree,
725                                                                           QWidget * parent,
726                                                                           const QGLWidget * shareWidget,
727                                                                           Qt::WFlags f
728                                                                           )
729  :
730  GlRendererWidget(sceneGraph, viewcells, tree), QGLWidget(parent, shareWidget, f)
731{
732  mPreprocessorThread = NULL;
733  mTopView = false;
734  mRenderViewCells = false;
735  mTopDistance = 1.0f;
736  mCutViewCells = false;
737  mCutScene = false;
738  mRenderErrors = false;
739  mRenderBoxes = false;
740  mRenderFilter = true;
741  mRenderVisibilityEstimates = false;
742  mTransferFunction = 0.2f;
743
744  bool tmp;
745
746  Environment::GetSingleton()->GetBoolValue("Preprocessor.applyVisibilityFilter", tmp );
747  mUseFilter = tmp;
748 
749  Environment::GetSingleton()->GetBoolValue("Preprocessor.applyVisibilitySpatialFilter",
750                                                        tmp );
751  mUseSpatialFilter = tmp;
752
753  mShowRenderCost = false;
754  mShowPvsSizes = false;
755  mSpatialFilterSize = 0.01;
756  mPvsSize = 0;
757  mRenderError = 0.0f;
758  mShowRays = false;
759 
760  mControlWidget = new QtRendererControlWidget(NULL);
761 
762  connect(mControlWidget, SIGNAL(SetViewCellGranularity(int)), this, SLOT(SetViewCellGranularity(int)));
763  connect(mControlWidget,
764                  SIGNAL(SetTransferFunction(int)),
765                  this,
766                  SLOT(SetTransferFunction(int)));
767  connect(mControlWidget, SIGNAL(UpdateAllPvs()), this, SLOT(UpdateAllPvs()));
768  connect(mControlWidget, SIGNAL(ComputeVisibility()), this, SLOT(ComputeVisibility()));
769  connect(mControlWidget, SIGNAL(StopComputation()), this, SLOT(StopComputation()));
770  connect(mControlWidget, SIGNAL(SetRandomViewPoint()), this,
771                  SLOT(SetRandomViewPoint()));
772
773  connect(mControlWidget, SIGNAL(SetSceneCut(int)), this, SLOT(SetSceneCut(int)));
774  connect(mControlWidget, SIGNAL(SetTopDistance(int)), this, SLOT(SetTopDistance(int)));
775
776  connect(mControlWidget, SIGNAL(SetVisibilityFilterSize(int)), this, SLOT(SetVisibilityFilterSize(int)));
777  connect(mControlWidget, SIGNAL(SetSpatialFilterSize(int)), this, SLOT(SetSpatialFilterSize(int)));
778
779  connect(mControlWidget, SIGNAL(SetShowViewCells(bool)), this, SLOT(SetShowViewCells(bool)));
780  connect(mControlWidget, SIGNAL(SetShowRenderCost(bool)), this, SLOT(SetShowRenderCost(bool)));
781  connect(mControlWidget, SIGNAL(SetShowPvsSizes(bool)), this, SLOT(SetShowPvsSizes(bool)));
782  connect(mControlWidget, SIGNAL(SetTopView(bool)), this, SLOT(SetTopView(bool)));
783  connect(mControlWidget, SIGNAL(SetCutViewCells(bool)), this, SLOT(SetCutViewCells(bool)));
784  connect(mControlWidget, SIGNAL(SetCutScene(bool)), this, SLOT(SetCutScene(bool)));
785  connect(mControlWidget, SIGNAL(SetRenderErrors(bool)), this, SLOT(SetRenderErrors(bool)));
786  connect(mControlWidget, SIGNAL(SetRenderBoxes(bool)), this, SLOT(SetRenderBoxes(bool)));
787  connect(mControlWidget, SIGNAL(SetRenderFilter(bool)), this, SLOT(SetRenderFilter(bool)));
788  connect(mControlWidget, SIGNAL(SetRenderVisibilityEstimates(bool)),
789                  this, SLOT(SetRenderVisibilityEstimates(bool)));
790  connect(mControlWidget, SIGNAL(SetUseFilter(bool)), this, SLOT(SetUseFilter(bool)));
791  connect(mControlWidget, SIGNAL(SetUseSpatialFilter(bool)),
792                  this, SLOT(SetUseSpatialFilter(bool)));
793
794  connect(mControlWidget,
795                  SIGNAL(SetShowRays(bool)),
796                  this,
797                  SLOT(SetShowRays(bool)));
798
799  resize(1000, 500);
800  mControlWidget->show();
801}
802
803void
804QtGlRendererWidget::UpdateAllPvs()
805{
806  // $$ does not work so far:(
807  mViewCellsManager->UpdatePvsForEvaluation();
808  //    mViewCellsManager->FinalizeViewCells(false);
809}
810
811void
812QtGlRendererWidget::ComputeVisibility()
813{
814  cerr<<"Compute Visibility called!\n"<<endl;
815  if (!mPreprocessorThread->isRunning())
816        mPreprocessorThread->RunThread();
817}
818
819void
820QtGlRendererWidget::StopComputation()
821{
822  cerr<<"stop computation called!\n"<<endl;
823  mViewCellsManager->GetPreprocessor()->mStopComputation = true;
824}
825
826void
827QtGlRendererWidget::SetRandomViewPoint()
828{
829  cerr<<"stop computation called!\n"<<endl;
830  mViewCellsManager->GetViewPoint(mViewPoint);
831  updateGL();
832}
833
834void
835QtGlRendererWidget::RenderRenderCost()
836{
837        static vector<float> costFunction;
838        static float maxCost = -1;
839        if (costFunction.size()==0) {
840          ViewCellsTree *tree = mViewCellsManager->GetViewCellsTree();
841          if (tree) {
842                tree->GetCostFunction(costFunction);
843                maxCost = -1;
844                for (int i=0;  i < costFunction.size(); i++) {
845                  //              cout<<i<<":"<<costFunction[i]<<" ";
846                  // update cost function to an absolute value based on the total geometry count
847                  costFunction[i]*=mSceneGraph->GetRoot()->mGeometry.size();
848                  if (costFunction[i] > maxCost)
849                        maxCost = costFunction[i];
850                }
851          }
852        }
853
854       
855        int currentPos = (int)mViewCellsManager->GetViewCells().size();
856        float currentCost= -1;
857
858        if (currentPos < costFunction.size())
859          currentCost = costFunction[currentPos];
860#if 1   
861        cout<<"costFunction.size()="<<costFunction.size()<<endl;
862        cout<<"CP="<<currentPos<<endl;
863        cout<<"MC="<<maxCost<<endl;
864        cout<<"CC="<<currentCost<<endl;
865#endif
866        if (costFunction.size()) {
867          float scaley = 1.0f/log10(maxCost);
868          float scalex = 1.0f/(float)costFunction.size();
869
870          glDisable(GL_DEPTH_TEST);
871          // init ortographic projection
872          glMatrixMode(GL_PROJECTION);
873         
874          glPushMatrix();
875         
876          glLoadIdentity();
877          gluOrtho2D(0, 1.0f, 0, 1.0f);
878         
879          glTranslatef(0.1f, 0.1f, 0.0f);
880          glScalef(0.8f, 0.8f, 1.0f);
881          glMatrixMode(GL_MODELVIEW);
882          glLoadIdentity();
883         
884          glColor3f(1.0f,0,0);
885          glBegin(GL_LINE_STRIP);
886          //      glVertex3f(0,0,0);
887         
888          for (int i=0;  i < costFunction.size(); i++) {
889                float x =  i*scalex;
890                float y = log10(costFunction[i])*scaley;
891                glVertex3f(x,y,0.0f);
892          }
893          glEnd();
894         
895          glColor3f(1.0f,0,0);
896          glBegin(GL_LINES);
897          float x =  currentPos*scalex;
898          glVertex3f(x,0.0,0.0f);
899          glVertex3f(x,1.0f,0.0f);
900          glEnd();
901         
902          glColor3f(0.0f,0,0);
903          // show a grid
904          glBegin(GL_LINE_LOOP);
905          glVertex3f(0,0,0.0f);
906          glVertex3f(1,0,0.0f);
907          glVertex3f(1,1,0.0f);
908          glVertex3f(0,1,0.0f);
909          glEnd();
910
911          glBegin(GL_LINES);
912          for (int i=0;  i < costFunction.size(); i += 1000) {
913                float x =  i*scalex;
914                glVertex3f(x,0.0,0.0f);
915                glVertex3f(x,1.0f,0.0f);
916          }
917
918          for (int i=0;  pow(10.0f, i) < maxCost; i+=1) {
919                float y = i*scaley;
920                //              QString s;
921                //              s.sprintf("%d", (int)pow(10,i));
922                //              renderText(width()/2+5, y*height(), s);
923                glVertex3f(0.0f, y, 0.0f);
924                glVertex3f(1.0f, y, 0.0f);
925          }
926
927          glEnd();
928
929         
930          // restore the projection matrix
931          glMatrixMode(GL_PROJECTION);
932          glPopMatrix();
933          glMatrixMode(GL_MODELVIEW);
934          glEnable(GL_DEPTH_TEST);
935
936        }
937 
938
939
940}
941
942void
943QtGlRendererWidget::RenderInfo()
944{
945 
946  QString s;
947  int vc = 0;
948  if (mViewCellsManager)
949        vc = mViewCellsManager->GetViewCells().size();
950  int filter = 0;
951  if (mViewCellsManager)
952        filter = mViewCellsManager->GetMaxFilterSize();
953
954  glColor3f(1.0f,1.0f,1.0f);
955
956  s.sprintf("frame:%04d viewpoint:(%4.1f,%4.1f,%4.1f) dir:(%4.1f,%4.1f,%4.1f)",
957                        mFrame,
958                        mViewPoint.x,
959                        mViewPoint.y,
960                        mViewPoint.z,
961                        mViewDirection.x,
962                        mViewDirection.y,
963                        mViewDirection.z
964                        );
965
966  renderText(20,20,s);
967
968  s.sprintf("viewcells:%04d filter:%04d pvs:%04d error:%5.5f\%",
969                        vc,
970                        filter,
971                        mPvsSize,
972                        mRenderError*100.0f);
973
974
975  renderText(20,40,s);
976
977 
978
979 
980}
981
982
983void
984QtGlRendererWidget::SetViewCellGranularity(int number)
985{
986  if (mViewCellsManager) {
987        //      mViewCellsManager->SetMaxFilterSize(number);
988    mViewCellsManager->CollectViewCells(number);
989
990        // $$ does not work so far:(
991        //      mViewCellsManager->UpdatePvsForEvaluation();
992        //      mViewCellsManager->FinalizeViewCells(false);
993  }
994  updateGL();
995}
996
997void
998QtGlRendererWidget::SetVisibilityFilterSize(int number)
999{
1000  if (mViewCellsManager)
1001        mViewCellsManager->SetMaxFilterSize(number);
1002  mPvsCache.Reset();
1003  updateGL();
1004}
1005
1006void
1007QtGlRendererWidget::SetSpatialFilterSize(int number)
1008{
1009  mSpatialFilterSize = 1e-3*number;
1010  mPvsCache.Reset();
1011  updateGL();
1012}
1013
1014void
1015QtGlRendererWidget::SetSceneCut(int number)
1016{
1017  // assume the cut plane can only be aligned with xz plane
1018  // shift it along y according to number, which is percentage of the bounding
1019  // box position
1020  if (mViewCellsManager) {
1021        AxisAlignedBox3 box = mViewCellsManager->GetViewSpaceBox();
1022        Vector3 p = box.Min() + (number/1000.0f)*box.Max();
1023        mSceneCutPlane.mNormal = Vector3(0,-1,0);
1024        mSceneCutPlane.mD = -DotProd(mSceneCutPlane.mNormal, p);
1025        updateGL();
1026  }
1027}
1028
1029void
1030QtGlRendererWidget::SetTopDistance(int number)
1031{
1032  mTopDistance = number/1000.0f;
1033  updateGL();
1034}
1035
1036void
1037QtGlRendererWidget::RenderViewCells()
1038{
1039  mUseFalseColors = true;
1040
1041  glPushAttrib(GL_CURRENT_BIT | GL_ENABLE_BIT | GL_POLYGON_BIT);
1042 
1043  glEnable(GL_CULL_FACE);
1044  //glDisable(GL_CULL_FACE);
1045  glCullFace(GL_FRONT);
1046  double eq[4];
1047  eq[0] = mSceneCutPlane.mNormal.x;
1048  eq[1] = mSceneCutPlane.mNormal.y;
1049  eq[2] = mSceneCutPlane.mNormal.z;
1050  eq[3] = mSceneCutPlane.mD;
1051
1052  if (mCutViewCells) {
1053        glClipPlane(GL_CLIP_PLANE0, eq);
1054        glEnable(GL_CLIP_PLANE0);
1055  }
1056 
1057  int i;
1058  ViewCellContainer &viewcells = mViewCellsManager->GetViewCells();
1059  int maxPvs = -1;
1060  for (i=0; i < viewcells.size(); i++)
1061  {
1062        ViewCell *vc = viewcells[i];
1063
1064        //const int p = vc->GetPvs().CountObjectsInPvs();
1065        const int p = vc->GetPvs().GetSize();
1066        if (p > maxPvs)
1067          maxPvs = p;
1068  }
1069
1070
1071  for (i=0; i < viewcells.size(); i++) {
1072        ViewCell *vc = viewcells[i];
1073        //      Mesh *m = vc->GetMesh();
1074
1075
1076        RgbColor c;
1077
1078        if (!mShowPvsSizes) {
1079          mWireFrame = true;
1080          c = vc->GetColor();
1081        } else {
1082          //      const float importance = 5.0f*mTransferFunction * ((float)vc->GetPvs().CountObjectsInPvs() / (float)maxPvs);
1083          const float importance = 5.0f*mTransferFunction * ((float)vc->GetPvs().GetSize() / (float)maxPvs);
1084          c = RgbColor(importance, 1.0f - importance, 0.0f);
1085        }
1086        glColor3f(c.r, c.g, c.b);
1087       
1088        RenderViewCell(vc);
1089  }
1090
1091  mUseFalseColors = false;
1092  mWireFrame = false;
1093
1094  glPopAttrib();
1095 
1096}
1097
1098
1099
1100
1101
1102/***********************************************************************/
1103/*                     QtGlDebuggerWidget implementation                                   */
1104/***********************************************************************/
1105
1106
1107QtGlDebuggerWidget::QtGlDebuggerWidget(QtGlRendererBuffer *buf, QWidget *parent)
1108      : QGLWidget(QGLFormat(QGL::SampleBuffers), parent), mRenderBuffer(buf)
1109{
1110        // create the pbuffer
1111    //pbuffer = new QGLPixelBuffer(QSize(512, 512), format(), this);
1112    timerId = startTimer(20);
1113    setWindowTitle(("OpenGL pbuffers"));
1114}
1115
1116
1117QtGlDebuggerWidget::~QtGlDebuggerWidget()
1118{
1119 mRenderBuffer->releaseFromDynamicTexture();
1120   glDeleteTextures(1, &dynamicTexture);
1121         
1122         DEL_PTR(mRenderBuffer);
1123}
1124
1125
1126
1127QtRendererControlWidget::QtRendererControlWidget(QWidget * parent, Qt::WFlags f):
1128  QWidget(parent, f)
1129{
1130
1131  QVBoxLayout *vl = new QVBoxLayout;
1132  setLayout(vl);
1133 
1134  QWidget *vbox = new QGroupBox("ViewCells", this);
1135  layout()->addWidget(vbox);
1136 
1137  vl = new QVBoxLayout;
1138  vbox->setLayout(vl);
1139
1140  QLabel *label = new QLabel("Granularity");
1141  vbox->layout()->addWidget(label);
1142 
1143  QSlider *slider = new QSlider(Qt::Horizontal, vbox);
1144  vl->addWidget(slider);
1145  slider->show();
1146  slider->setRange(1, 10000);
1147  slider->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
1148  slider->setValue(200);
1149
1150 
1151  connect(slider, SIGNAL(valueChanged(int)), SIGNAL(SetViewCellGranularity(int)));
1152
1153  label = new QLabel("Transfer function");
1154  vbox->layout()->addWidget(label);
1155 
1156  slider = new QSlider(Qt::Horizontal, vbox);
1157  vl->addWidget(slider);
1158  slider->show();
1159  slider->setRange(1, 1000);
1160  slider->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
1161  slider->setValue(100);
1162
1163 
1164  connect(slider, SIGNAL(valueChanged(int)), SIGNAL(SetTransferFunction(int)));
1165
1166  {
1167        QPushButton *button = new QPushButton("Update all PVSs", vbox);
1168        vbox->layout()->addWidget(button);
1169        connect(button, SIGNAL(clicked()), SLOT(UpdateAllPvs()));
1170  }
1171 
1172 
1173  label = new QLabel("Filter size");
1174  vbox->layout()->addWidget(label);
1175 
1176  slider = new QSlider(Qt::Horizontal, vbox);
1177  vbox->layout()->addWidget(slider);
1178  slider->show();
1179  slider->setRange(1, 100);
1180  slider->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
1181  slider->setValue(3);
1182 
1183  connect(slider, SIGNAL(valueChanged(int)), SIGNAL(SetVisibilityFilterSize(int)));
1184
1185
1186  label = new QLabel("Spatial Filter size");
1187  vbox->layout()->addWidget(label);
1188 
1189  slider = new QSlider(Qt::Horizontal, vbox);
1190  vbox->layout()->addWidget(slider);
1191  slider->show();
1192  slider->setRange(1, 100);
1193  slider->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
1194  slider->setValue(10);
1195 
1196  connect(slider, SIGNAL(valueChanged(int)), SIGNAL(SetSpatialFilterSize(int)));
1197
1198
1199
1200  QWidget *hbox = new QWidget(vbox);
1201  vl->addWidget(hbox);
1202  QHBoxLayout *hlayout = new QHBoxLayout;
1203  hbox->setLayout(hlayout);
1204 
1205  QCheckBox *cb = new QCheckBox("Show viewcells", hbox);
1206  hlayout->addWidget(cb);
1207  cb->setChecked(false);
1208  connect(cb, SIGNAL(toggled(bool)), SIGNAL(SetShowViewCells(bool)));
1209
1210  cb = new QCheckBox("Render cost curve", hbox);
1211  hlayout->addWidget(cb);
1212  cb->setChecked(false);
1213  connect(cb, SIGNAL(toggled(bool)), SIGNAL(SetShowRenderCost(bool)));
1214
1215  cb = new QCheckBox("Show render cost", hbox);
1216  hlayout->addWidget(cb);
1217  cb->setChecked(false);
1218  connect(cb, SIGNAL(toggled(bool)), SIGNAL(SetShowPvsSizes(bool)));
1219
1220  cb = new QCheckBox("Show rays", hbox);
1221  hlayout->addWidget(cb);
1222  cb->setChecked(false);
1223  connect(cb, SIGNAL(toggled(bool)), SIGNAL(SetShowRays(bool)));
1224
1225  vbox->resize(800,100);
1226
1227 
1228  vbox = new QGroupBox("Rendering", this);
1229  layout()->addWidget(vbox);
1230 
1231  vl = new QVBoxLayout;
1232  vbox->setLayout(vl);
1233
1234
1235
1236  cb = new QCheckBox("Cut view cells", vbox);
1237  vbox->layout()->addWidget(cb);
1238  cb->setChecked(false);
1239  connect(cb, SIGNAL(toggled(bool)), SIGNAL(SetCutViewCells(bool)));
1240
1241
1242  slider = new QSlider(Qt::Horizontal, vbox);
1243  vbox->layout()->addWidget(slider);
1244  slider->show();
1245  slider->setRange(0, 1000);
1246  slider->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
1247  slider->setValue(1000);
1248
1249  connect(slider, SIGNAL(valueChanged(int)), SIGNAL(SetSceneCut(int)));
1250
1251
1252  cb = new QCheckBox("Cut scene", vbox);
1253  vbox->layout()->addWidget(cb);
1254  cb->setChecked(false);
1255  connect(cb, SIGNAL(toggled(bool)), SIGNAL(SetCutScene(bool)));
1256
1257  cb = new QCheckBox("Render boxes", vbox);
1258  vbox->layout()->addWidget(cb);
1259  cb->setChecked(false);
1260  connect(cb, SIGNAL(toggled(bool)), SIGNAL(SetRenderBoxes(bool)));
1261
1262  cb = new QCheckBox("Render visibility estimates", vbox);
1263  vbox->layout()->addWidget(cb);
1264  cb->setChecked(false);
1265  connect(cb, SIGNAL(toggled(bool)), SIGNAL(SetRenderVisibilityEstimates(bool)));
1266
1267 
1268  cb = new QCheckBox("Render errors", vbox);
1269  vbox->layout()->addWidget(cb);
1270  cb->setChecked(false);
1271  connect(cb, SIGNAL(toggled(bool)), SIGNAL(SetRenderErrors(bool)));
1272
1273 
1274  bool tmp;
1275
1276  cb = new QCheckBox("Use filter", vbox);
1277  vbox->layout()->addWidget(cb);
1278  Environment::GetSingleton()->GetBoolValue("Preprocessor.applyVisibilityFilter", tmp );
1279  cb->setChecked(tmp);
1280  connect(cb, SIGNAL(toggled(bool)), SIGNAL(SetUseFilter(bool)));
1281
1282 
1283  cb = new QCheckBox("Use spatial filter", vbox);
1284  vbox->layout()->addWidget(cb);
1285  Environment::GetSingleton()->GetBoolValue("Preprocessor.applyVisibilitySpatialFilter",
1286                                                                                        tmp );
1287  cb->setChecked(tmp);
1288  connect(cb, SIGNAL(toggled(bool)), SIGNAL(SetUseSpatialFilter(bool)));
1289
1290  cb = new QCheckBox("Render filter", vbox);
1291  vbox->layout()->addWidget(cb);
1292  cb->setChecked(true);
1293  connect(cb, SIGNAL(toggled(bool)), SIGNAL(SetRenderFilter(bool)));
1294
1295
1296
1297 
1298  slider = new QSlider(Qt::Horizontal, vbox);
1299  vbox->layout()->addWidget(slider);
1300  slider->show();
1301  slider->setRange(1, 1000);
1302  slider->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
1303  slider->setValue(500);
1304 
1305  connect(slider, SIGNAL(valueChanged(int)), SIGNAL(SetTopDistance(int)));
1306 
1307  cb = new QCheckBox("Top View", vbox);
1308  vbox->layout()->addWidget(cb);
1309  cb->setChecked(false);
1310  connect(cb, SIGNAL(toggled(bool)), SIGNAL(SetTopView(bool)));
1311
1312
1313
1314  vbox = new QGroupBox("PVS Errors", this);
1315  layout()->addWidget(vbox);
1316 
1317  vl = new QVBoxLayout;
1318  vbox->setLayout(vl);
1319
1320  QPushButton *button = new QPushButton("Compute Visibility", vbox);
1321  vbox->layout()->addWidget(button);
1322  connect(button, SIGNAL(clicked()), SLOT(ComputeVisibility()));
1323
1324  button = new QPushButton("Stop Computation", vbox);
1325  vbox->layout()->addWidget(button);
1326  connect(button, SIGNAL(clicked()), SLOT(StopComputation()));
1327
1328  button = new QPushButton("Set Random View Point", vbox);
1329  vbox->layout()->addWidget(button);
1330  connect(button, SIGNAL(clicked()), SLOT(SetRandomViewPoint()));
1331
1332
1333  if (0) {
1334        vbox = new QGroupBox("PVS Errors", this);
1335        layout()->addWidget(vbox);
1336       
1337        vl = new QVBoxLayout;
1338        vbox->setLayout(vl);
1339       
1340        mPvsErrorWidget = new QListWidget(vbox);
1341        vbox->layout()->addWidget(mPvsErrorWidget);
1342       
1343        connect(mPvsErrorWidget,
1344                        SIGNAL(doubleClicked(const QModelIndex &)),
1345                        this,
1346                        SLOT(PvsErrorClicked(const QModelIndex &)));
1347       
1348        QPushButton *button = new QPushButton("Next Error Frame", vbox);
1349        vbox->layout()->addWidget(button);
1350        connect(button, SIGNAL(clicked(void)), SLOT(FocusNextPvsErrorFrame(void)));
1351  }
1352 
1353  setWindowTitle("Preprocessor Control Widget");
1354  adjustSize();
1355}
1356
1357
1358
1359
1360void
1361QtRendererControlWidget::FocusNextPvsErrorFrame(void)
1362{
1363 
1364 
1365}
1366
1367void
1368QtRendererControlWidget::UpdatePvsErrorItem(int row,
1369                                                                                  GlRendererBuffer::PvsErrorEntry &pvsErrorEntry)
1370{
1371
1372  QListWidgetItem *i = mPvsErrorWidget->item(row);
1373  QString s;
1374  s.sprintf("%5.5f", pvsErrorEntry.mError);
1375  if (i) {
1376        i->setText(s);
1377  } else {
1378        new QListWidgetItem(s, mPvsErrorWidget);
1379  }
1380  mPvsErrorWidget->update();
1381}
1382
1383
1384void QtGlDebuggerWidget::initializeGL()
1385{
1386        glMatrixMode(GL_PROJECTION);
1387        glLoadIdentity();
1388
1389        glFrustum(-1, 1, -1, 1, 10, 100);
1390        glTranslatef(-0.5f, -0.5f, -0.5f);
1391        glTranslatef(0.0f, 0.0f, -15.0f);
1392        glMatrixMode(GL_MODELVIEW);
1393
1394        glEnable(GL_CULL_FACE);
1395        initCommon();
1396        initPbuffer();
1397
1398}
1399
1400
1401void QtGlDebuggerWidget::resizeGL(int w, int h)
1402{
1403        glViewport(0, 0, w, h);
1404}
1405
1406
1407void QtGlDebuggerWidget::paintGL()
1408{
1409        // draw a spinning cube into the pbuffer..
1410        mRenderBuffer->makeCurrent();
1411       
1412        BeamSampleStatistics stats;
1413        mRenderBuffer->SampleBeamContributions(mSourceObject, mBeam, mSamples, stats);
1414
1415        glFlush();
1416
1417        // rendering directly to a texture is not supported on X11, unfortunately
1418    mRenderBuffer->updateDynamicTexture(dynamicTexture);
1419   
1420    // and use the pbuffer contents as a texture when rendering the
1421    // background and the bouncing cubes
1422    makeCurrent();
1423    glBindTexture(GL_TEXTURE_2D, dynamicTexture);
1424    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
1425
1426    // draw the background
1427    glMatrixMode(GL_MODELVIEW);
1428    glPushMatrix();
1429    glLoadIdentity();
1430    glMatrixMode(GL_PROJECTION);
1431    glPushMatrix();
1432    glLoadIdentity();
1433
1434        glPopMatrix();
1435        glMatrixMode(GL_MODELVIEW);
1436        glPopMatrix();
1437}
1438
1439
1440void QtGlDebuggerWidget::initPbuffer()
1441{
1442        // set up the pbuffer context
1443    mRenderBuffer->makeCurrent();
1444        /*mRenderBuffer->InitGL();
1445
1446        glViewport(0, 0, mRenderBuffer->size().width(), mRenderBuffer->size().height());
1447        glMatrixMode(GL_PROJECTION);
1448        glLoadIdentity();
1449        glOrtho(-1, 1, -1, 1, -99, 99);
1450        glTranslatef(-0.5f, -0.5f, 0.0f);
1451        glMatrixMode(GL_MODELVIEW);
1452        glLoadIdentity();
1453
1454        glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);*/
1455       
1456        // generate a texture that has the same size/format as the pbuffer
1457    dynamicTexture = mRenderBuffer->generateDynamicTexture();
1458
1459        // bind the dynamic texture to the pbuffer - this is a no-op under X11
1460        mRenderBuffer->bindToDynamicTexture(dynamicTexture);
1461        makeCurrent();
1462}
1463
1464void QtGlDebuggerWidget::initCommon()
1465{
1466        glEnable(GL_TEXTURE_2D);
1467        glEnable(GL_DEPTH_TEST);
1468
1469        glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
1470}
1471
1472
1473
1474}
Note: See TracBrowser for help on using the repository browser.