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

Revision 1824, 37.0 KB checked in by bittner, 18 years ago (diff)

global lines support

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