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

Revision 2002, 36.0 KB checked in by bittner, 17 years ago (diff)

renderer code updates for pixel error measurements

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