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

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