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

Revision 2613, 56.4 KB checked in by bittner, 16 years ago (diff)

evaluation of pvs error still does not work

Line 
1#include "glInterface.h"
2#include "QtGlRenderer.h"
3#include "Mesh.h"
4#include "OcclusionQuery.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#include "Material.h"
17#include "IntersectableWrapper.h"
18#include "LogWriter.h"
19
20
21#define USE_CG 1
22
23
24#if USE_CG
25#include <Cg/cg.h>
26#include <Cg/cgGL.h>
27#endif
28
29#include <QVBoxLayout>
30
31namespace GtpVisibilityPreprocessor {
32
33
34 
35class ViewCellsManager;
36
37static CGcontext sCgContext = NULL;
38static CGprogram sCgFragmentProgram = NULL;
39static CGprofile sCgFragmentProfile;
40
41
42//static vector<OcclusionQuery *> sQueries;
43
44QtGlRendererWidget *rendererWidget = NULL;
45QtGlDebuggerWidget *debuggerWidget = NULL;
46
47const static int SAMPLES_INCR = 2000000;
48
49
50
51static inline bool ilt(Intersectable *obj1, Intersectable *obj2)
52{
53        return obj1->mId < obj2->mId;
54}
55
56
57inline static bool nearerThan(ViewCell *vc1, ViewCell *vc2)
58{
59        return vc1->GetDistance() > vc2->GetDistance();
60}
61
62
63#if USE_CG
64static void handleCgError()
65{
66    Debug << "Cg error: " << cgGetErrorString(cgGetError()) << endl;
67    exit(1);
68}
69#endif
70
71void
72QtGlRendererBuffer::MakeCurrent()
73{
74  makeCurrent();
75}
76
77void
78QtGlRendererBuffer::DoneCurrent()
79{
80  doneCurrent();
81}
82 
83
84QtGlRendererBuffer::QtGlRendererBuffer(const int w,
85                                                                           const int h,
86                                                                           SceneGraph *sceneGraph,
87                                                                           ViewCellsManager *viewcells,
88                                                                           KdTree *tree):
89  QGLPixelBuffer(QSize(w, h)),
90  //QGLWidget(NULL, w, h),
91  GlRendererBuffer(sceneGraph, viewcells, tree)
92{
93  MakeCurrent();
94  InitGL();
95  DoneCurrent();
96}
97
98
99// reimplemented here so that we can snap the error windows
100float
101QtGlRendererBuffer::GetPixelError(int &pvsSize)
102{
103
104  MakeCurrent();
105
106  float pErrorPixels = -1.0f;
107
108
109        mUseFalseColors = false;
110        unsigned int pixelCount;
111
112
113        ViewCell *viewcell = mViewCellsManager->GetViewCell(mViewPoint);
114        if (viewcell == NULL)
115                return 0.0f;
116
117        ObjectPvs pvs;
118       
119        if (1)
120                pvs = viewcell->GetPvs();
121        else
122                mViewCellsManager->ApplyFilter2(viewcell, false, 1.0f, pvs);
123
124        mUseForcedColors = true;
125
126        SetupCamera();
127        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
128        glColorMask(GL_FALSE, GL_TRUE, GL_FALSE, GL_FALSE);
129        glColor3f(0,1,0);
130
131
132        glStencilFunc(GL_EQUAL, 0x0, 0x1);
133        glStencilOp(GL_KEEP, GL_KEEP, GL_INCR);
134
135        // Render PVS
136        ObjectPvsIterator it = pvs.GetIterator();
137
138        pvsSize = pvs.GetSize();
139        cout<<pvsSize<<endl;
140
141        Intersectable::NewMail();
142        while (it.HasMoreEntries())
143          {
144                RenderIntersectable(it.Next());
145          }
146
147        //glColorMask(GL_TRUE, GL_FALSE, GL_FALSE, GL_FALSE);
148        glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
149        glEnable(GL_STENCIL_TEST);
150        glColor3f(1,0,0);
151
152       
153        OcclusionQuery *query = mOcclusionQueries[0];
154
155        //      SetupCamera();
156
157        Intersectable::NewMail();
158
159        query->BeginQuery();
160
161        RenderScene();
162       
163        query->EndQuery();
164        glDisable(GL_STENCIL_TEST);
165
166        // reenable other state
167        //  int wait=0;
168        //  while (!query.ResultAvailable()) {
169        //      wait++;
170        //  }
171       
172
173        pixelCount = query->GetQueryResult();
174        //      cout<<"pc="<<pixelCount<<endl;
175       
176        pErrorPixels = ((float)pixelCount)/(GetWidth()*GetHeight());
177
178
179        if (mSnapErrorFrames && (0 && pErrorPixels >= 0.01f)) {
180          glReadBuffer(GL_BACK);
181          //glReadBuffer(GL_FRONT);
182         
183          char filename[256];
184          sprintf(filename, "error-frame-%04d-%0.5f.png", mFrame, pErrorPixels);
185          QImage im = toImage();
186          string str = mSnapPrefix + filename;
187          QString qstr(str.c_str());
188         
189          im.save(qstr, "PNG");
190          if (0) { //0 && mFrame == 1543) {
191                int x,y;
192                int lastIndex = -1;
193                for (y=0; y < im.height(); y++)
194                  for (x=0; x < im.width(); x++) {
195                        QRgb p = im.pixel(x,y);
196                        int index = qRed(p) + (qGreen(p)<<8) + (qBlue(p)<<16);
197                        if (qGreen(p) != 255 && index!=0) {
198                          if (index != lastIndex) {
199                                //                              Debug<<"ei="<<index<<" ";
200                                lastIndex = index;
201                          }
202                        }
203                  }
204          }
205         
206          mUseFalseColors = false;
207          glPushAttrib(GL_CURRENT_BIT);
208          glColor3f(0,1,0);
209          glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
210          glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
211         
212          // Render PVS
213          Intersectable::NewMail();
214         
215         
216          ObjectPvsIterator it = pvs.GetIterator();
217          for (; it.HasMoreEntries(); )
218                {
219                  RenderIntersectable(it.Next());
220                }
221
222          mUseForcedColors = false;
223         
224          im = toImage();
225          sprintf(filename, "error-frame-%04d-%0.5f-pvs.png", mFrame, pErrorPixels);
226          str = mSnapPrefix + filename;
227          qstr = str.c_str();
228          im.save(qstr, "PNG");
229          glPopAttrib();
230        }
231       
232        glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
233
234        DoneCurrent();
235
236        return pErrorPixels;
237}
238
239int
240QtGlRendererBuffer::ComputePvs(ObjectContainer &objects,
241                                                           ObjectContainer &pvs) const
242{
243        int pvsSize = 0;
244        QImage image = toImage();
245        Intersectable::NewMail();
246
247        std::stable_sort(objects.begin(), objects.end(), ilt);
248
249        MeshInstance dummy(NULL);
250
251        Intersectable *obj = NULL;
252
253        for (int x = 0; x < image.width(); ++ x)
254        {
255                for (int y = 0; y < image.height(); ++ y)
256                {
257                        QRgb pix = image.pixel(x, y);
258                        const int id = GetId(qRed(pix), qGreen(pix), qBlue(pix));
259
260                        dummy.SetId(id);
261
262                        ObjectContainer::iterator oit =
263                                lower_bound(objects.begin(), objects.end(), &dummy, ilt);
264
265
266                        if (//(oit != oit.end()) &&
267                                ((*oit)->GetId() == id) &&
268                                !obj->Mailed())
269                        {
270                                obj = *oit;
271                                obj->Mail();
272                                ++ pvsSize;
273                                pvs.push_back(obj);
274                        }
275                }
276        }
277
278        return pvsSize;
279}
280
281
282void QtGlRendererWidget::InitGL()
283{
284        GlRenderer::InitGL();
285
286        GLfloat mat_ambient[] = {0.5f, 0.5f, 0.5f, 1.0f};
287       
288        // mat_specular and mat_shininess are NOT default values
289        GLfloat mat_diffuse[] = {1.0f, 1.0f, 1.0f, 1.0f};
290        GLfloat mat_specular[] = {0.3f, 0.3f, 0.3f, 1.0f};
291        GLfloat mat_shininess[] = {1.0f};
292
293        GLfloat light_ambient[] = {0.2f, 0.2f, 0.2f, 1.0f};
294        GLfloat light_diffuse[] = {0.4f, 0.4f, 0.4f, 1.0f};
295        GLfloat light_specular[] = {0.3f, 0.3f, 0.3f, 1.0f};
296
297        GLfloat lmodel_ambient[] = {0.3f, 0.3f, 0.3f, 1.0f};
298
299
300        // default Material
301        glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient);
302        glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
303        glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
304        glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
305
306        // a light
307        glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
308        glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
309        glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
310
311        glLightfv(GL_LIGHT1, GL_AMBIENT, light_ambient);
312        glLightfv(GL_LIGHT1, GL_DIFFUSE, light_diffuse);
313        glLightfv(GL_LIGHT1, GL_SPECULAR, light_specular);
314
315        glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lmodel_ambient);
316
317        glEnable(GL_LIGHTING);
318        glEnable(GL_LIGHT0);
319        glEnable(GL_LIGHT1);
320
321        // set position of the light
322        GLfloat infinite_light[] = {  1.0, 0.8, 1.0, 0.0  };
323        glLightfv (GL_LIGHT0, GL_POSITION, infinite_light);
324
325        // set position of the light2
326        GLfloat infinite_light2[] = {  -0.3, 1.5, 1.0, 0.0  };
327        glLightfv (GL_LIGHT1, GL_POSITION, infinite_light2);
328
329        glColorMaterial( GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE);
330        // glColorMaterial( GL_FRONT_AND_BACK, GL_SPECULAR);
331        glEnable(GL_COLOR_MATERIAL);
332
333        glShadeModel(GL_FLAT);
334}
335
336
337void
338QtGlRendererWidget::SetupCameraProjection(const int w, const int h, const float angle)
339{
340        if (!mTopView) {
341                int ww = w;
342                int hh = h;
343                glViewport(0, 0, ww, hh);
344                glMatrixMode(GL_PROJECTION);
345                glLoadIdentity();
346                gluPerspective(angle, ww/(float)hh, 0.1, 2.0 * Magnitude(mSceneGraph->GetBox().Diagonal()));
347                glMatrixMode(GL_MODELVIEW);
348        } else {
349                int ww = w;
350                int hh = h;
351                glViewport(0, 0, ww, hh);
352                glMatrixMode(GL_PROJECTION);
353                glLoadIdentity();
354                gluPerspective(50.0, ww / (float)hh, 0.1, 20.0 * Magnitude(mSceneGraph->GetBox().Diagonal()));
355                glMatrixMode(GL_MODELVIEW);
356        }
357}
358
359
360bool QtGlRendererWidget::PvsChanged(ViewCell *viewCell) const
361{
362        if (viewCell != mPvsCache.mViewCell)
363                return true;
364
365        if (viewCell->GetPvs().GetSize() != mPvsCache.mUnfilteredPvsSize)
366                return true;
367
368        return false;
369}
370
371
372void QtGlRendererWidget::_RenderPvs()
373{
374        mUseFalseColors = false;
375
376        int offset = (int)mObjects.size() * 3;
377        char *arrayPtr = mUseVbos ? NULL : (char *)mData;
378
379        glVertexPointer(3, GL_FLOAT, 0, (char *)arrayPtr);
380        glNormalPointer(GL_FLOAT, 0, (char *)arrayPtr + offset * sizeof(Vector3));
381        glDrawElements(GL_TRIANGLES, mIndexBufferSize, GL_UNSIGNED_INT, mIndices);
382
383        // handle dynamic objects
384        DynamicObjectsContainer::const_iterator dit, dit_end = mDynamicObjects.end();
385
386        for (dit = mDynamicObjects.begin(); dit != dit_end; ++ dit)
387        {
388                RenderIntersectable(*dit);
389        }
390}
391
392
393void QtGlRendererWidget::PreparePvs()
394{
395        int indexBufferSize = 0;
396       
397        KdNode::NewMail();
398        //Intersectable::NewMail();
399
400        mPvsSize = mPvsCache.mPvs.GetSize();
401
402        mDynamicObjects.clear();
403
404        ObjectPvsIterator it = mPvsCache.mPvs.GetIterator();
405
406        while (it.HasMoreEntries())
407        {
408                Intersectable *obj = it.Next();
409                switch (obj->Type())
410                {
411                case Intersectable::KD_INTERSECTABLE:
412                        {
413                                KdNode *node = static_cast<KdIntersectable *>(obj)->GetItem();
414                                _UpdatePvsIndices(node, indexBufferSize);
415                        }
416                        break;
417                case Intersectable::TRANSFORMED_MESH_INSTANCE:
418                        mDynamicObjects.push_back(static_cast<TransformedMeshInstance *>(obj));
419                        break;
420                default:
421                        cerr << "PreparePvs: type " << Intersectable::GetTypeName(obj) << " not handled yet" << endl;
422                }
423        }
424
425        mIndexBufferSize = indexBufferSize;
426}
427
428
429void QtGlRendererWidget::_UpdatePvsIndices(KdNode *node, int &indexBufferSize)
430{
431        if (node->Mailed())
432                return;
433
434        node->Mail();
435
436        // if (mObjects.size() * 3 < indexBufferSize) cerr << "problem: " << mObjects.size() * 3 << " < " << indexBufferSize << endl;
437        if (!node->IsLeaf())
438        {
439                KdInterior *kdInterior = static_cast<KdInterior *>(node);
440
441                _UpdatePvsIndices(kdInterior->mBack, indexBufferSize);
442                _UpdatePvsIndices(kdInterior->mFront, indexBufferSize);
443        }
444        else
445        {
446                KdLeaf *leaf = static_cast<KdLeaf *>(node);
447
448                leaf->mIndexBufferStart = indexBufferSize;
449
450                for (size_t i = 0; i < leaf->mObjects.size(); ++ i)
451                {
452                        TriangleIntersectable *obj =
453                                static_cast<TriangleIntersectable *>(leaf->mObjects[i]);
454
455                        if (obj->mRenderedFrame != mCurrentFrame)
456                        {
457                                obj->mRenderedFrame = mCurrentFrame;
458
459                                mIndices[indexBufferSize + 0] = (obj->GetId() - 1) * 3 + 0;
460                                mIndices[indexBufferSize + 1] = (obj->GetId() - 1) * 3 + 1;
461                                mIndices[indexBufferSize + 2] = (obj->GetId() - 1) * 3 + 2;
462
463                                indexBufferSize += 3;
464                        }
465                }
466
467                leaf->mIndexBufferSize = indexBufferSize - leaf->mIndexBufferStart;
468        }
469
470        //cout << "id: " << indexBufferSize << endl;
471}
472
473
474void QtGlRendererWidget::RenderPvs()
475{
476        if (mUseVbos)
477                glBindBufferARB(GL_ARRAY_BUFFER_ARB, mVboId);
478
479        ++ mCurrentFrame;
480
481        EnableDrawArrays();
482
483        //Intersectable::NewMail();
484       
485        if (mDetectEmptyViewSpace)
486                glEnable(GL_CULL_FACE);
487        else
488                glDisable(GL_CULL_FACE);
489
490        ViewCell *viewcell = NULL;
491        viewcell = mViewCellsManager->GetViewCell(mViewPoint, true);
492
493        if (viewcell)
494        {
495                // copy the pvs so that it can be filtered ...
496                if (PvsChanged(viewcell))
497                {
498                        mPvsCache.Reset();
499                        mPvsCache.mViewCell = viewcell;
500                        mPvsCache.mUnfilteredPvsSize = viewcell->GetPvs().GetSize();
501
502                        if (mUseSpatialFilter)
503                        {
504                                //mMutex.lock();
505                                // mSpatialFilter size is in range 0.001 - 0.1
506                                mViewCellsManager->ApplyFilter2(viewcell,
507                                                                    mUseFilter,
508                                                                    100.0f * mSpatialFilterSize,
509                                                                    //pvs,
510                                                                                                mPvsCache.mPvs,         
511                                                                    &mPvsCache.filteredBoxes);
512                                //mPvsCache.mPvs = pvs;
513                                //mMutex.unlock();
514                        }
515                        else
516                        {
517                                mPvsCache.mPvs = viewcell->GetPvs();
518                        }
519
520                        /// update the indices for rendering
521                        PreparePvs();
522
523                        emit PvsUpdated();
524                }
525
526
527                // Render PVS
528                if (mUseSpatialFilter && mRenderBoxes)
529                {
530                        for (int i=0; i < mPvsCache.filteredBoxes.size(); ++ i)
531                        {
532                                RenderBox(mPvsCache.filteredBoxes[i]);
533                        }
534                }
535                else
536                {
537                        if (!mRenderVisibilityEstimates && !mUseRandomColorPerPvsObject)
538                                _RenderPvs();
539                        else
540                                _RenderColoredPvs();
541                }
542
543                if (mRenderFilter)
544                {
545                        mWireFrame = true;
546                        RenderIntersectable(viewcell);
547                       
548                        /*glPushMatrix();
549                        glTranslatef(mViewPoint.x, mViewPoint.y, mViewPoint.z);
550                        glScalef(5.0f,5.0f,5.0f);
551                        glPushAttrib(GL_CURRENT_BIT);
552                        glColor3f(1.0f, 0.0f, 0.0f);
553                        //        gluSphere((::GLUquadric *)mSphere,
554                        //                              1e-3*Magnitude(mViewCellsManager->GetViewSpaceBox().Size()), 6, 6);
555                        glPopAttrib();
556                        glPopMatrix();
557                        */
558                        mWireFrame = false;
559                }
560        }
561        else
562        {
563                //ObjectContainer::const_iterator oi = mObjects.begin();
564                //for (; oi != mObjects.end(); oi++)
565                //      RenderIntersectable(*oi);
566                RenderScene();
567        }
568}
569
570float
571QtGlRendererWidget::RenderErrors()
572{
573        float pErrorPixels = -1.0f;
574
575        SetupCamera();
576        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
577
578        glPushAttrib(GL_ENABLE_BIT);
579
580        glStencilFunc(GL_EQUAL, 0x0, 0x1);
581        glStencilOp(GL_KEEP, GL_KEEP, GL_INCR);
582
583        mUseForcedColors = true;
584
585        glColor3f(0.0f, 0.8f, 0.2f);
586
587        // Render PVS
588        RenderPvs();
589
590        glEnable(GL_STENCIL_TEST);
591
592        //mUseFalseColors = true;
593
594        glDisable(GL_LIGHTING);
595
596        OcclusionQuery *query = mOcclusionQueries[0];
597        query->BeginQuery();
598
599        SetupCamera();
600
601        glColor3f(1.0f, 0.0f, 0.0f);
602
603        RenderScene();
604
605        mUseForcedColors = false;
606
607        query->EndQuery();
608
609        glDisable(GL_STENCIL_TEST);
610        glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
611
612        glPopAttrib();
613
614        // reenable other state
615        //  int wait=0;
616        //  while (!query.ResultAvailable()) {
617        //      wait++;
618        //  }
619
620        int pixelCount = query->GetQueryResult();
621        pErrorPixels = ((float)pixelCount)/(GetWidth()*GetHeight());
622        if (0) cout<<"error pixels="<<pixelCount<<endl;
623
624        mRenderError = pErrorPixels;
625
626        return pErrorPixels;
627}
628
629
630void QtGlRendererWidget::timerEvent(QTimerEvent *event)
631{
632        //std::cout << "Timer ID:" << event->timerId();
633        update();
634}
635
636
637void QtGlRendererWidget::mousePressEvent(QMouseEvent *e)
638{
639        int x = e->pos().x();
640        int y = e->pos().y();
641
642        mousePoint.x = x;
643        mousePoint.y = y;
644
645}
646
647void
648QtGlRendererWidget::mouseMoveEvent(QMouseEvent *e)
649{
650        float MOVE_SENSITIVITY = Magnitude(mSceneGraph->GetBox().Diagonal())*1e-3;
651        float TURN_SENSITIVITY = 0.1f;
652        float TILT_SENSITIVITY = 32.0 ;
653        float TURN_ANGLE= M_PI  /36.0 ;
654
655        int x = e->pos().x();
656        int y = e->pos().y();
657
658        int diffx = -(mousePoint.x - x);
659        int diffy = -(mousePoint.y - y);
660
661        if (e->modifiers() & Qt::ControlModifier)
662        {
663                mViewPoint.y += (y-mousePoint.y)*MOVE_SENSITIVITY / 2.0;
664                mViewPoint.x += (x-mousePoint.x)*MOVE_SENSITIVITY / 2.0;
665        }
666        else
667        {
668                mViewPoint += mViewDirection*((mousePoint.y - y)*MOVE_SENSITIVITY);
669                float adiff = TURN_ANGLE*(x - mousePoint.x)*-TURN_SENSITIVITY;
670                float angle = atan2(mViewDirection.x, mViewDirection.z);
671                mViewDirection.x = sin(angle + adiff);
672                mViewDirection.z = cos(angle + adiff);
673        }
674
675        mousePoint.x = x;
676        mousePoint.y = y;
677
678        updateGL();
679}
680
681void
682QtGlRendererWidget::mouseReleaseEvent(QMouseEvent *)
683{
684}
685
686void
687QtGlRendererWidget::resizeGL(int w, int h)
688{
689        SetupCameraProjection(w, h);
690        updateGL();
691}
692
693void
694QtGlRendererWidget::paintGL()
695{
696        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
697
698        if (1)
699        {
700                SetupCameraProjection(width(), height());
701                SetupCamera();
702
703                if (mRenderErrors)
704                {
705                        RenderErrors();
706                }
707                else
708                {
709                        glColor3f(0.6f, 0.6f, 0.6f);
710                        RenderPvs();
711                }
712
713                if (1 && mShowRays)
714                {
715                        RenderRays(mViewCellsManager->mVizBuffer.GetRays(), mRayVisualizationMethod, mShowDistribution, 1);
716                }
717        }
718
719        RenderInfo();
720        mFrame ++;
721}
722
723
724void
725QtGlRendererWidget::SetupCamera()
726{
727        if (!mTopView)
728                GlRenderer::SetupCamera();
729        else
730        {
731                if (0)
732                {
733                        float dist = Magnitude(mSceneGraph->GetBox().Diagonal())*0.05;
734                        Vector3 pos = mViewPoint - dist*Vector3(mViewDirection.x,
735                                -1,
736                                mViewDirection.y);
737
738                        Vector3 target = mViewPoint + dist*mViewDirection;
739                        Vector3 up(0,1,0);
740
741                        glLoadIdentity();
742                        gluLookAt(pos.x, pos.y, pos.z,
743                                target.x, target.y, target.z,
744                                up.x, up.y, up.z);
745                }
746                else
747                {
748                        float dist = Magnitude(mSceneGraph->GetBox().Diagonal()) * mTopDistance;
749                        Vector3 pos = mViewPoint  + dist*Vector3(0,
750                                1,
751                                0);
752
753                        Vector3 target = mViewPoint;
754                        Vector3 up(mViewDirection.x, 0, mViewDirection.z);
755
756                        glLoadIdentity();
757                        gluLookAt(pos.x, pos.y, pos.z,
758                                target.x, target.y, target.z,
759                                up.x, up.y, up.z);
760
761                }
762        }
763
764}
765
766
767void
768QtGlRendererWidget::keyPressEvent ( QKeyEvent * e )
769{
770        switch (e->key())
771        {
772case Qt::Key_E:
773        mRenderErrors = !mRenderErrors;
774        updateGL();
775        break;
776case Qt::Key_R:
777        mUseRandomColorPerPvsObject = !mUseRandomColorPerPvsObject;;
778        updateGL();
779        break;
780case Qt::Key_T:
781        mTopView = !mTopView;
782        SetupCameraProjection(width(), height());
783        updateGL();
784        break;
785case Qt::Key_V:
786        mRenderViewCells = !mRenderViewCells;
787        updateGL();
788        break;
789case Qt::Key_P:
790        // set random viewpoint
791        mViewCellsManager->GetViewPoint(mViewPoint);
792        updateGL();
793        break;
794case Qt::Key_S: {
795        // set view poitn and direction
796        QString text;
797        bool ok;
798        text.sprintf("%f %f %f", mViewPoint.x, mViewPoint.y, mViewPoint.z);
799        text = QInputDialog::getText(this,
800                "Enter a view point",
801                "",
802                QLineEdit::Normal,
803                text,
804                &ok);
805        if (!ok)
806                break;
807
808        if (sscanf(text.toAscii(), "%f %f %f", &mViewPoint.x, &mViewPoint.y, &mViewPoint.z) == 3) {
809                text.sprintf("%f %f %f", mViewDirection.x, mViewDirection.y, mViewDirection.z);
810                text = QInputDialog::getText(this,
811                        "Enter a direction",
812                        "",
813                        QLineEdit::Normal,
814                        text,
815                        &ok);
816                if (!ok)
817                        break;
818                if (sscanf(text.toAscii(), "%f %f %f", &mViewDirection.x,
819                        &mViewDirection.y, &mViewDirection.z) == 3) {
820                                updateGL();
821                        }
822                        break;
823        }
824                                }
825default:
826        cerr << "unknown key" << endl;
827        e->ignore();
828        break;
829        }
830}
831
832
833
834QtGlRendererWidget::QtGlRendererWidget(
835                                                                           SceneGraph *sceneGraph,
836                                                                           ViewCellsManager *viewcells,
837                                                                           KdTree *tree,
838                                                                           QWidget * parent,
839                                                                           const QGLWidget * shareWidget,
840                                                                           Qt::WFlags f
841                                                                           )
842                                                                           :
843GlRendererWidget(sceneGraph, viewcells, tree), QGLWidget(parent, shareWidget, f)
844{
845        mPreprocessorThread = NULL;
846        mTopView = false;
847        mRenderViewCells = false;
848        mTopDistance = 1.0f;
849        mCutViewCells = false;
850        mCutScene = false;
851        mRenderErrors = false;
852        mRenderBoxes = false;
853        mRenderFilter = true;
854        mRenderVisibilityEstimates = false;
855
856        mUseRandomColorPerPvsObject = false;
857
858        mHideByCost = false;
859        mUseTransparency = false;
860
861        mTransferFunction = 1.0f;
862        mIndexBufferSize = 0;
863
864        //mCurrentFrame = 0;
865
866        const int delay = 250; // in milliseconds
867        timerId = startTimer(delay);
868
869        bool tmp;
870
871        Environment::GetSingleton()->GetBoolValue("Preprocessor.applyVisibilityFilter", tmp );
872        mUseFilter = tmp;
873
874        Environment::GetSingleton()->GetBoolValue("Preprocessor.applyVisibilitySpatialFilter", tmp );
875        mUseSpatialFilter = tmp;
876
877        //mLogWriter = new LogWriter("myfile.out");
878
879        mShowRenderCost = false;
880        mShowPvsSizes = false;
881        mShowComparison = false;
882        mShowPiercingRays = false;
883        mShowWeightedRays = false;
884        mUseStandardColors = false;
885        mShowWeightedCost = true;
886
887        mShowDistanceWeightedPvs = true;
888        mShowDistanceWeightedTriangles = false;
889        mShowWeightedTriangles = false;
890        mShowDistribution = 15;
891
892        mSpatialFilterSize = 0.01;
893        mPvsSize = 0;
894        mRayVisualizationMethod = 0;
895        mRenderError = 0.0f;
896        mShowRays = false;
897
898        mControlWidget = new QtRendererControlWidget(NULL);
899
900        connect(mControlWidget, SIGNAL(SetViewCellGranularity(int)), this, SLOT(SetViewCellGranularity(int)));
901        connect(mControlWidget, SIGNAL(SetTransferFunction(int)), this, SLOT(SetTransferFunction(int)));
902
903        connect(mControlWidget, SIGNAL(UpdateAllPvs()), this, SLOT(UpdateAllPvs()));
904        connect(mControlWidget, SIGNAL(ComputeVisibility()), this, SLOT(ComputeVisibility()));
905        connect(mControlWidget, SIGNAL(StopComputation()), this, SLOT(StopComputation()));
906        connect(mControlWidget, SIGNAL(SetRandomViewPoint()), this,     SLOT(SetRandomViewPoint()));
907        connect(mControlWidget, SIGNAL(StoreStatistics(void)), this, SLOT(StoreStatistics(void)));
908        connect(mControlWidget, SIGNAL(LoadObject(void)), this, SLOT(LoadObject(void)));
909
910        connect(mControlWidget, SIGNAL(SetSceneCut(int)), this, SLOT(SetSceneCut(int)));
911        connect(mControlWidget, SIGNAL(SetTopDistance(int)), this, SLOT(SetTopDistance(int)));
912        connect(mControlWidget, SIGNAL(SetTransparency(int)), this, SLOT(SetTransparency(int)));
913
914        connect(mControlWidget, SIGNAL(SetVisibilityFilterSize(int)), this, SLOT(SetVisibilityFilterSize(int)));
915        connect(mControlWidget, SIGNAL(SetSpatialFilterSize(int)), this, SLOT(SetSpatialFilterSize(int)));
916        connect(mControlWidget, SIGNAL(SetHidingCost(int)), this, SLOT(SetHidingCost(int)));
917
918        connect(mControlWidget, SIGNAL(SetShowViewCells(bool)), this, SLOT(SetShowViewCells(bool)));
919        connect(mControlWidget, SIGNAL(SetShowRenderCost(bool)), this, SLOT(SetShowRenderCost(bool)));
920        connect(mControlWidget, SIGNAL(SetUseTransparency(bool)), this, SLOT(SetUseTransparency(bool)));
921        connect(mControlWidget, SIGNAL(SetShowPvsSizes(bool)), this, SLOT(SetShowPvsSizes(bool)));
922        connect(mControlWidget, SIGNAL(SetShowComparison(bool)), this, SLOT(SetShowComparison(bool)));
923        connect(mControlWidget, SIGNAL(SetTopView(bool)), this, SLOT(SetTopView(bool)));
924        connect(mControlWidget, SIGNAL(SetCutViewCells(bool)), this, SLOT(SetCutViewCells(bool)));
925        connect(mControlWidget, SIGNAL(SetHideByCost(bool)), this, SLOT(SetHideByCost(bool)));
926        connect(mControlWidget, SIGNAL(SetCutScene(bool)), this, SLOT(SetCutScene(bool)));
927        connect(mControlWidget, SIGNAL(SetRenderErrors(bool)), this, SLOT(SetRenderErrors(bool)));
928        connect(mControlWidget, SIGNAL(SetRenderBoxes(bool)), this, SLOT(SetRenderBoxes(bool)));
929        connect(mControlWidget, SIGNAL(SetRenderFilter(bool)), this, SLOT(SetRenderFilter(bool)));
930        connect(mControlWidget, SIGNAL(SetRenderVisibilityEstimates(bool)), this, SLOT(SetRenderVisibilityEstimates(bool)));
931        connect(mControlWidget, SIGNAL(SetUseFilter(bool)), this, SLOT(SetUseFilter(bool)));
932        connect(mControlWidget, SIGNAL(SetUseSpatialFilter(bool)), this, SLOT(SetUseSpatialFilter(bool)));
933        connect(mControlWidget, SIGNAL(SetShowPiercingRays(bool)), this, SLOT(SetShowPiercingRays(bool)));
934        connect(mControlWidget, SIGNAL(SetShowWireFrame(bool)), this, SLOT(SetShowWireFrame(bool)));
935        connect(mControlWidget, SIGNAL(SetShowWeightedRays(bool)), this, SLOT(SetShowWeightedRays(bool)));
936        connect(mControlWidget, SIGNAL(SetShowWeightedCost(bool)), this, SLOT(SetShowWeightedCost(bool)));
937
938        connect(mControlWidget, SIGNAL(SetShowDistanceWeightedTriangles(bool)), this, SLOT(SetShowDistanceWeightedTriangles(bool)));
939        connect(mControlWidget, SIGNAL(SetShowDistanceWeightedPvs(bool)), this, SLOT(SetShowDistanceWeightedPvs(bool)));
940        connect(mControlWidget, SIGNAL(SetShowWeightedTriangles(bool)), this, SLOT(SetShowWeightedTriangles(bool)));
941
942        connect(mControlWidget, SIGNAL(UseConstColorForRayViz(bool)), this, SLOT(UseConstColorForRayViz(bool)));
943        connect(mControlWidget, SIGNAL(UseRayLengthForRayViz(bool)), this, SLOT(UseRayLengthForRayViz(bool)));
944        connect(mControlWidget, SIGNAL(SetShowContribution(bool)), this, SLOT(SetShowContribution(bool)));
945        connect(mControlWidget, SIGNAL(SetShowDistribution(bool)), this, SLOT(SetShowDistribution(bool)));
946
947        connect(mControlWidget, SIGNAL(SetShowDistribution1(bool)), this, SLOT(SetShowDistribution1(bool)));
948        connect(mControlWidget, SIGNAL(SetShowDistribution2(bool)), this, SLOT(SetShowDistribution2(bool)));
949        connect(mControlWidget, SIGNAL(SetShowDistribution3(bool)), this, SLOT(SetShowDistribution3(bool)));
950        connect(mControlWidget, SIGNAL(SetShowDistribution4(bool)), this, SLOT(SetShowDistribution4(bool)));
951
952
953        connect(mControlWidget, SIGNAL(SetShowRays(bool)), this, SLOT(SetShowRays(bool)));
954
955        // setting the size here
956        resize(800, 600);
957        mControlWidget->show();
958}
959
960void
961QtGlRendererWidget::UpdateAllPvs()
962{
963        // $$ does not work so far:(
964        mViewCellsManager->UpdatePvsForEvaluation();
965        //      mViewCellsManager->FinalizeViewCells(false);
966}
967
968void
969QtGlRendererWidget::ComputeVisibility()
970{
971        cerr<<"Compute Visibility called!\n"<<endl;
972        if (!mPreprocessorThread->isRunning())
973                mPreprocessorThread->RunThread();
974}
975
976void
977QtGlRendererWidget::StopComputation()
978{
979        cerr<<"stop computation called!\n"<<endl;
980        mViewCellsManager->GetPreprocessor()->mStopComputation = true;
981}
982
983void
984QtGlRendererWidget::SetRandomViewPoint()
985{
986        cerr<<"setting random view point!\n"<<endl;
987        mViewCellsManager->GetViewPoint(mViewPoint);
988        updateGL();
989}
990
991
992void QtGlRendererWidget::StoreStatistics()
993{
994        cerr<<"storing statistics!\n"<<endl;
995        const int currentSamples = mViewCellsManager->GetPreprocessor()->mCurrentSamples;
996
997        cout<<"**********************************************" << endl;
998        cout << "reached " << currentSamples << " samples " << " => writing file" << endl;
999               
1000        LogWriter writer;
1001        writer.SetFilename("compare.log");
1002        writer.Write(currentSamples, mViewCellsManager->GetViewCells());
1003        cout << "finished writing file" << endl;
1004        mCompareInfo.clear();
1005        updateGL();
1006}
1007
1008
1009void QtGlRendererWidget::LoadObject()
1010{
1011        string filename("../data/bunny.bn");
1012       
1013        cout << "Loading model << " << filename << endl;
1014
1015        if (mViewCellsManager->GetPreprocessor()->LoadDynamicGeometry(filename))
1016                cout << "Loading finished" << endl;
1017        else
1018                cerr << "Loading failed" << endl;
1019
1020    updateGL();
1021}
1022
1023void
1024QtGlRendererWidget::RenderRenderCost()
1025{
1026        static vector<float> costFunction;
1027        static float maxCost = -1;
1028        if (costFunction.size()==0) {
1029                ViewCellsTree *tree = mViewCellsManager->GetViewCellsTree();
1030                if (tree) {
1031                        tree->GetCostFunction(costFunction);
1032                        maxCost = -1;
1033                        for (int i=0;  i < costFunction.size(); i++) {
1034                                //                cout<<i<<":"<<costFunction[i]<<" ";
1035                                // update cost function to an absolute value based on the total geometry count
1036                                costFunction[i] *= mSceneGraph->GetSize();
1037                                if (costFunction[i] > maxCost)
1038                                        maxCost = costFunction[i];
1039                        }
1040                }
1041        }
1042
1043
1044        int currentPos = (int)mViewCellsManager->GetViewCells().size();
1045        float currentCost= -1;
1046
1047        if (currentPos < costFunction.size())
1048                currentCost = costFunction[currentPos];
1049#if 1   
1050        cout<<"costFunction.size()="<<(int)costFunction.size()<<endl;
1051        cout<<"CP="<<currentPos<<endl;
1052        cout<<"MC="<<maxCost<<endl;
1053        cout<<"CC="<<currentCost<<endl;
1054#endif
1055        if (costFunction.size()) {
1056                float scaley = 1.0f/log10(maxCost);
1057                float scalex = 1.0f/(float)costFunction.size();
1058
1059                glDisable(GL_DEPTH_TEST);
1060                // init ortographic projection
1061                glMatrixMode(GL_PROJECTION);
1062
1063                glPushMatrix();
1064
1065                glLoadIdentity();
1066                gluOrtho2D(0, 1.0f, 0, 1.0f);
1067
1068                glTranslatef(0.1f, 0.1f, 0.0f);
1069                glScalef(0.8f, 0.8f, 1.0f);
1070                glMatrixMode(GL_MODELVIEW);
1071                glLoadIdentity();
1072
1073                glColor3f(1.0f,0,0);
1074                glBegin(GL_LINE_STRIP);
1075                //        glVertex3f(0,0,0);
1076
1077                for (int i=0;  i < costFunction.size(); i++) {
1078                        float x =  i*scalex;
1079                        float y = log10(costFunction[i])*scaley;
1080                        glVertex3f(x,y,0.0f);
1081                }
1082                glEnd();
1083
1084                glColor3f(1.0f,0,0);
1085                glBegin(GL_LINES);
1086                float x =  currentPos*scalex;
1087                glVertex3f(x,0.0,0.0f);
1088                glVertex3f(x,1.0f,0.0f);
1089                glEnd();
1090
1091                glColor3f(0.0f,0,0);
1092                // show a grid
1093                glBegin(GL_LINE_LOOP);
1094                glVertex3f(0,0,0.0f);
1095                glVertex3f(1,0,0.0f);
1096                glVertex3f(1,1,0.0f);
1097                glVertex3f(0,1,0.0f);
1098                glEnd();
1099
1100                glBegin(GL_LINES);
1101                for (int i=0;  i < costFunction.size(); i += 1000) {
1102                        float x =  i*scalex;
1103                        glVertex3f(x,0.0,0.0f);
1104                        glVertex3f(x,1.0f,0.0f);
1105                }
1106
1107                for (int i=0;  pow(10.0f, i) < maxCost; i+=1) {
1108                        float y = i*scaley;
1109                        //              QString s;
1110                        //              s.sprintf("%d", (int)pow(10,i));
1111                        //              renderText(width()/2+5, y*height(), s);
1112                        glVertex3f(0.0f, y, 0.0f);
1113                        glVertex3f(1.0f, y, 0.0f);
1114                }
1115
1116                glEnd();
1117
1118
1119                // restore the projection matrix
1120                glMatrixMode(GL_PROJECTION);
1121                glPopMatrix();
1122                glMatrixMode(GL_MODELVIEW);
1123                glEnable(GL_DEPTH_TEST);
1124
1125        }
1126
1127
1128
1129}
1130
1131void
1132QtGlRendererWidget::RenderInfo()
1133{
1134
1135        QString s;
1136
1137        int vc = 0;
1138        if (mViewCellsManager)
1139                vc = (int)mViewCellsManager->GetViewCells().size();
1140
1141        int filter = 0;
1142        if (mViewCellsManager)
1143                filter = mViewCellsManager->GetMaxFilterSize();
1144
1145        glColor3f(0.0f, 0.0f, 1.0f);
1146
1147#if REMOVE_TEMPORARY
1148        s.sprintf("frame:%04d viewpoint:(%4.1f,%4.1f,%4.1f) dir:(%4.1f,%4.1f,%4.1f)",
1149                mFrame,
1150                mViewPoint.x,
1151                mViewPoint.y,
1152                mViewPoint.z,
1153                mViewDirection.x,
1154                mViewDirection.y,
1155                mViewDirection.z
1156                );
1157
1158        renderText(20, 20, s);
1159
1160        s.sprintf("viewcells:%04d filter:%04d pvs:%04d error:%5.5f %",
1161                vc,
1162                filter,
1163                mPvsSize,
1164                mRenderError*100.0f);
1165
1166        renderText(20, 40, s);
1167#endif
1168       
1169        QFont font40; font40.setPointSize(30);
1170        s.sprintf("PVS: %04d", mPvsSize);
1171        renderText(20, 40, s, font40);
1172}
1173
1174
1175void
1176QtGlRendererWidget::SetViewCellGranularity(int number)
1177{
1178        if (mViewCellsManager) {
1179                //      mViewCellsManager->SetMaxFilterSize(number);
1180
1181                // $$ tmp off
1182                mViewCellsManager->CollectViewCells(number);
1183
1184                // $$ does not work so far:(
1185                //      mViewCellsManager->UpdatePvsForEvaluation();
1186                //      mViewCellsManager->FinalizeViewCells(false);
1187        }
1188        updateGL();
1189}
1190
1191void
1192QtGlRendererWidget::SetVisibilityFilterSize(int number)
1193{
1194        if (mViewCellsManager)
1195                mViewCellsManager->SetMaxFilterSize(number);
1196        mPvsCache.Reset();
1197        updateGL();
1198}
1199
1200void
1201QtGlRendererWidget::SetSpatialFilterSize(int number)
1202{
1203        mSpatialFilterSize = 1e-3*number;
1204        mPvsCache.Reset();
1205        updateGL();
1206}
1207
1208void
1209QtGlRendererWidget::SetSceneCut(int number)
1210{
1211        // assume the cut plane can only be aligned with xz plane
1212        // shift it along y according to number, which is percentage of the bounding
1213        // box position
1214        if (mViewCellsManager)
1215        {
1216                const float f = number / 1000.0f;
1217                AxisAlignedBox3 box = mViewCellsManager->GetViewSpaceBox();
1218                Vector3 p = (1.0f - f) * box.Min() + f * box.Max();
1219                mSceneCutPlane.mNormal = Vector3(0,-1,0);
1220                mSceneCutPlane.mD = -DotProd(mSceneCutPlane.mNormal, p);
1221
1222                updateGL();
1223        }
1224}
1225
1226void
1227QtGlRendererWidget::SetHidingCost(int number)
1228{
1229        mHidingCost = (float)number / 1000.0f;
1230}
1231
1232
1233void
1234QtGlRendererWidget::SetTopDistance(int number)
1235{
1236        mTopDistance = number / 1000.0f;
1237        updateGL();
1238}
1239
1240void QtGlRendererWidget::SetTransparency(int number)
1241{
1242        mTransparency = number / 1000.0f;
1243        updateGL();
1244}
1245
1246#if 0
1247float QtGlRendererWidget::ComputeRenderCost(ViewCell *vc)
1248{
1249        ObjectPvs basePvs;
1250       
1251        basePvs = vc->CopyPvs();
1252        ObjectPvsIterator pit = basePvs.GetIterator();
1253
1254        float renderCost = 0;
1255
1256        //cout << "cost vis: " << mShowDistanceWeightedPvs << " " << " " << mShowDistanceWeightedTriangles << " " << mShowWeightedTriangles << endl;
1257       
1258        // first mark all objects from this pvs
1259        while (pit.HasMoreEntries())   
1260        {
1261                if (mShowDistanceWeightedPvs)
1262                {
1263                        KdIntersectable *kdObj = static_cast<KdIntersectable *>(pit.Next());
1264                        const AxisAlignedBox3 box = kdObj->GetBox();
1265
1266                        const float dist = SqrDistance(vc->GetBox().Center(), box.Center());
1267                        renderCost += 1.0f / dist;
1268                }
1269                else if (mShowDistanceWeightedTriangles)
1270                {
1271                        KdIntersectable *kdObj = static_cast<KdIntersectable *>(pit.Next());
1272                        const AxisAlignedBox3 box = kdObj->GetBox();
1273
1274                        const float dist = SqrDistance(vc->GetBox().Center(), box.Center());
1275                        renderCost += kdObj->ComputeNumTriangles() / dist;
1276                }
1277                else //if (mShowWeightedTriangles)
1278                {
1279                        KdIntersectable *kdObj = static_cast<KdIntersectable *>(pit.Next());
1280                        renderCost += kdObj->ComputeNumTriangles();
1281                }
1282                //if (pit.Next()->Mail();
1283        }
1284
1285        return renderCost;
1286}
1287#else
1288
1289float QtGlRendererWidget::ComputeRenderCost(ViewCell *vc)
1290{
1291        float renderCost = 0;
1292
1293        if (mShowDistanceWeightedPvs)
1294        {
1295                return vc->GetPvs().mStats.mDistanceWeightedPvs;
1296        }
1297        else if (mShowDistanceWeightedTriangles)
1298        {
1299                return vc->GetPvs().mStats.mDistanceWeightedTriangles;
1300        }
1301        else //if (mShowWeightedTriangles)
1302        {
1303                return vc->GetPvs().mStats.mWeightedTriangles;
1304        }
1305}
1306#endif
1307
1308
1309
1310void QtGlRendererWidget::ComputeMaxValues(const ViewCellContainer &viewCells,
1311                                                                                  int &maxPvs,
1312                                                                                  int &maxPiercingRays,
1313                                                                                  float &maxRelativeRays,
1314                                                                                  float &maxRcCost)
1315{
1316        maxPvs = -1;
1317        maxPiercingRays = 1; // not zero for savety
1318        maxRelativeRays = Limits::Small; // not zero for savety
1319        maxRcCost = Limits::Small;
1320
1321        for (size_t i = 0; i < viewCells.size(); ++ i)
1322        {
1323                ViewCell *vc = viewCells[i];
1324
1325                if (mShowPvsSizes) // pvs size
1326                {
1327                        //const int p = vc->GetPvs().CountObjectsInPvs();
1328                        const int p = vc->GetPvs().GetSize();
1329                        if (p > maxPvs)
1330                                maxPvs = p;
1331                }
1332                else if (mShowPiercingRays) // relative number of rays
1333                {
1334                        const int piercingRays = vc->GetNumPiercingRays();
1335
1336                        if (piercingRays > maxPiercingRays)
1337                                maxPiercingRays = piercingRays;
1338                }
1339                else if (mShowWeightedRays)
1340                {
1341                        const int piercingRays = vc->GetNumPiercingRays();
1342
1343                        const float relativeArea =
1344                                vc->GetBox().SurfaceArea() / mViewCellsManager->GetViewSpaceBox().SurfaceArea();
1345
1346                        if ((float)piercingRays / relativeArea > maxRelativeRays)
1347                                maxRelativeRays = (float)piercingRays / relativeArea;
1348                }
1349                else if (mShowWeightedCost)
1350                {
1351                        const float rcCost = ComputeRenderCost(vc);
1352                        mViewCellsManager->UpdateScalarPvsCost(vc, rcCost);
1353
1354                        if (rcCost > maxRcCost)
1355                                maxRcCost = rcCost;
1356                }
1357        }
1358}
1359
1360
1361void QtGlRendererWidget::RenderViewCells()
1362{
1363        mUseFalseColors = true;
1364        //glPushAttrib(GL_CURRENT_BIT | GL_ENABLE_BIT | GL_POLYGON_BIT);
1365
1366        glEnable(GL_CULL_FACE);
1367        glCullFace(GL_FRONT);
1368        //glDisable(GL_CULL_FACE);
1369
1370        if (mCutViewCells)
1371        {
1372                double eq[4];
1373                eq[0] = mSceneCutPlane.mNormal.x;
1374                eq[1] = mSceneCutPlane.mNormal.y;
1375                eq[2] = mSceneCutPlane.mNormal.z;
1376                eq[3] = mSceneCutPlane.mD;
1377
1378                glClipPlane(GL_CLIP_PLANE0, eq);
1379                glEnable(GL_CLIP_PLANE0);
1380        }
1381
1382        ViewCellContainer &viewcells = mViewCellsManager->GetViewCells();
1383       
1384        int maxPvs, maxPiercingRays;
1385        float maxRelativeRays, maxRcCost;
1386
1387        ComputeMaxValues(viewcells, maxPvs, maxPiercingRays, maxRelativeRays, maxRcCost);
1388
1389        int i;
1390
1391        // transparency
1392        if (!mUseTransparency)
1393        {
1394                glEnable(GL_DEPTH_TEST);
1395                glDisable(GL_BLEND);
1396        }
1397        else
1398        {
1399                glDisable(GL_DEPTH_TEST);
1400                glEnable(GL_BLEND);
1401
1402                glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1403                //glBlendFunc(GL_SRC_ALPHA, GL_ONE);
1404
1405                for (i = 0; i < viewcells.size(); ++ i)
1406                {
1407                        ViewCell *vc = viewcells[i];
1408
1409                        const float dist = SqrDistance(mDummyViewPoint, vc->GetBox().Center());
1410                        vc->SetDistance(dist);
1411                }
1412
1413                sort(viewcells.begin(), viewcells.end(), nearerThan);
1414        }
1415
1416        //mWireFrame = true;
1417               
1418        // normal rendering
1419        //if (!mShowPvsSizes && !mShowPiercingRays && !mShowWeightedRays && !mShowWeightedCost && !mShowComparison)
1420        if (mUseStandardColors)
1421        {
1422                for (i = 0; i < viewcells.size(); ++ i)
1423                {
1424                        ViewCell *vc = viewcells[i];
1425                        RgbColor c;
1426
1427                        //if (!mShowPvsSizes && !mShowPiercingRays)
1428                        c = vc->GetColor();
1429                       
1430                        glColor3f(c.r, c.g, c.b);
1431
1432                        if (!mHideByCost || (mHidingCost < (vc->GetNumPiercingRays() / (float)maxPiercingRays)))
1433                        {
1434                RenderViewCell(vc);
1435                        }
1436                }
1437        }
1438        else // using specialised colors
1439        {
1440       
1441
1442                if (!mShowComparison)
1443                        AssignImportanceByRelativeValue(viewcells, maxPvs, maxPiercingRays, maxRelativeRays, maxRcCost);
1444                else
1445                {
1446                        if (mCompareInfo.empty())
1447                        {
1448                                LogReader reader;
1449                                reader.SetFilename("compare.log");
1450                                int samples;
1451                                reader.Read(samples, mCompareInfo);
1452                        }
1453
1454                        AssignColorByComparison(viewcells, mCompareInfo);
1455                }
1456
1457                glEnable(GL_DEPTH_TEST);       
1458        }
1459       
1460        glEnable(GL_CULL_FACE);
1461        glDisable(GL_CULL_FACE);
1462
1463        glDisable(GL_CLIP_PLANE0);
1464
1465        mUseFalseColors = false;
1466        mWireFrame = false;
1467
1468        glPopAttrib();
1469}
1470
1471
1472void QtGlRendererWidget::AssignImportanceByRelativeValue(const ViewCellContainer &viewCells,
1473                                                                                                                 int &maxPvs,
1474                                                                                                                 int &maxPiercingRays,
1475                                                                                                                 float &maxRelativeRays,
1476                                                                                                                 float &maxRcCost)
1477{
1478        for (size_t i = 0; i < viewCells.size(); ++ i)
1479        {
1480                RgbColor c;
1481                ViewCell *vc = viewCells[i];
1482
1483                float importance;
1484
1485                if (mShowPiercingRays)
1486                {
1487                        importance = mTransferFunction *
1488                                ((float)vc->GetNumPiercingRays() / (float)maxPiercingRays);
1489                }
1490                else if (mShowWeightedRays) // relative number of rays
1491                {
1492                        float relativeArea = vc->GetBox().SurfaceArea() / mViewCellsManager->GetViewSpaceBox().SurfaceArea();
1493
1494                        if (relativeArea < Limits::Small)
1495                                relativeArea = Limits::Small;
1496
1497                        importance = mTransferFunction * ((float)vc->GetNumPiercingRays() / relativeArea) / maxRelativeRays;
1498                }
1499                else if (mShowPvsSizes) // pvs size
1500                {
1501                        importance = mTransferFunction *
1502                                ((float)vc->GetPvs().GetSize() / (float)maxPvs);
1503                } // weighted render cost
1504                else if (mShowWeightedCost)
1505                {
1506                        const float rcCost = mTransferFunction * ComputeRenderCost(vc);
1507                        importance = rcCost / maxRcCost;
1508                }
1509
1510                // c = RgbColor(importance, 1.0f - importance, 0.0f);
1511                c = RainbowColorMapping(importance);
1512
1513                glColor4f(c.r, c.g, c.b, 1.0f - mTransparency);
1514
1515                if (!mHideByCost || (mHidingCost < importance))
1516                {
1517                        RenderViewCell(vc);
1518                }
1519        }
1520}
1521
1522
1523void QtGlRendererWidget::AssignColorByComparison(const ViewCellContainer &viewcells,
1524                                                                                                 //const ViewCellInfoContainer &infos1,
1525                                                                                                 const ViewCellInfoContainer &compareInfo)
1526{
1527        //cout << "comparing " << viewcells.size() << " view cells to " << compareInfo.size() << " values" << endl;
1528
1529        if (viewcells.size() > compareInfo.size())
1530        {
1531                cerr << "loaded size (" << (int)compareInfo.size() << ") does not fit to view cells size (" << (int)viewcells.size() << ")" << endl;
1532                return;
1533        }
1534
1535        //const float maxRatio = 1.0f;
1536        const float maxRatio = 2.0f;
1537        const float minRatio = 0.0f;
1538
1539        const float scale = 1.0f / (maxRatio - minRatio);
1540
1541        for (size_t i = 0; i < viewcells.size(); ++ i)
1542        {
1543                RgbColor c;
1544                ViewCell *vc = viewcells[i];
1545
1546                //ViewCellInfo vc1Info = infos1[i];
1547                ViewCellInfo vc2Info = compareInfo[i];
1548
1549                //const float vcRatio = vc->GetNumPiercingRays() / vc2Info.mPiercingRays + Limits::Small;
1550                float vcRatio = 1.0f;//maxRatio;
1551               
1552                if (vc2Info.mPvsSize > Limits::Small)
1553                        vcRatio = (float)vc->GetPvs().GetSize() / vc2Info.mPvsSize;
1554
1555                // truncate here
1556                if (vcRatio > maxRatio) vcRatio = 1.0f;
1557
1558                const float importance = (vcRatio - minRatio) * scale;
1559       
1560                if (0 && (i < 20))
1561                {
1562                        cout << "pvs1: " << vc->GetPvs().GetSize() << " pvs2: " << compareInfo[i].mPvsSize << " importance: " << importance << endl;
1563                }
1564
1565                // c = RgbColor(importance, 1.0f - importance, 0.0f);
1566                c = RainbowColorMapping(importance);
1567
1568                glColor4f(c.r, c.g, c.b, 1.0f);
1569
1570                if (1)//!mHideByCost || (mHidingCost < importance))
1571                {
1572                        RenderViewCell(vc);
1573                }
1574        }
1575}
1576
1577
1578
1579/**********************************************************************/
1580/*              QtRendererControlWidget implementation                */
1581/**********************************************************************/
1582
1583
1584QGroupBox *QtRendererControlWidget::CreateVisualizationPanel(QWidget *parent)
1585{
1586        QRadioButton *rb1, *rb2, *rb3, *rb4, *rb5;
1587       
1588        rb1 = new QRadioButton("random colors", parent);
1589        rb1->setText("wireframe");
1590        connect(rb1, SIGNAL(toggled(bool)), SIGNAL(SetShowWireFrame(bool)));
1591
1592        // Create a check box to be in the group box
1593        rb2 = new QRadioButton("piercing rays", parent);
1594        rb2->setText("piercing rays");
1595        //vl->addWidget(rb1);
1596        connect(rb2, SIGNAL(toggled(bool)), SIGNAL(SetShowPiercingRays(bool)));
1597
1598        rb3 = new QRadioButton("pvs size", parent);
1599        rb3->setText("pvs size");
1600        connect(rb3, SIGNAL(toggled(bool)), SIGNAL(SetShowPvsSizes(bool)));
1601       
1602        rb4 = new QRadioButton("weighted rays", parent);
1603        rb4->setText("weighted rays");
1604        connect(rb4, SIGNAL(toggled(bool)), SIGNAL(SetShowWeightedRays(bool)));
1605       
1606        rb5 = new QRadioButton("pvs cost", parent);
1607        rb5->setText("pvs cost");
1608        connect(rb5, SIGNAL(toggled(bool)), SIGNAL(SetShowWeightedCost(bool)));
1609
1610        QGroupBox *groupBox = new QGroupBox("Visualization options");
1611
1612        QVBoxLayout *vbox2 = new QVBoxLayout;
1613   
1614        vbox2->addWidget(rb1);
1615        vbox2->addWidget(rb2);
1616        vbox2->addWidget(rb3);
1617        vbox2->addWidget(rb4);
1618        vbox2->addWidget(rb5);
1619       
1620        rb5->setChecked(true);
1621
1622        vbox2->addStretch(1);
1623        groupBox->setLayout(vbox2);
1624
1625        return groupBox;
1626}
1627
1628
1629
1630QGroupBox *QtRendererControlWidget::CreateRenderCostPanel(QWidget *parent)
1631{
1632        QRadioButton *rb1, *rb2, *rb3;
1633       
1634        // Create a check box to be in the group box
1635        rb1 = new QRadioButton("triangles", parent);
1636        rb1->setText("triangles");
1637        connect(rb1, SIGNAL(toggled(bool)), SIGNAL(SetShowWeightedTriangles(bool)));
1638
1639        rb2 = new QRadioButton("distance weighted pvs", parent);
1640        rb2->setText("distance weighted");
1641        connect(rb2, SIGNAL(toggled(bool)), SIGNAL(SetShowDistanceWeightedPvs(bool)));
1642
1643        rb3 = new QRadioButton("distance weighted triangles", parent);
1644        rb3->setText("distance weighted triangles");
1645        connect(rb3, SIGNAL(toggled(bool)), SIGNAL(SetShowDistanceWeightedTriangles(bool)));
1646
1647        QGroupBox *groupBox = new QGroupBox("Render cost options");
1648
1649        QVBoxLayout *vbox2 = new QVBoxLayout;
1650   
1651        vbox2->addWidget(rb1);
1652        vbox2->addWidget(rb2);
1653        vbox2->addWidget(rb3);
1654       
1655        rb1->setChecked(true);
1656
1657        vbox2->addStretch(1);
1658        groupBox->setLayout(vbox2);
1659
1660        return groupBox;
1661}
1662
1663
1664QGroupBox *QtRendererControlWidget::CreateRayVisualizationPanel(QWidget *parent)
1665{
1666        QRadioButton *rb1, *rb2, *rb3, *rb4;
1667       
1668        // Create a check box to be in the group box
1669        rb1 = new QRadioButton("const color", parent);
1670        rb1->setText("const color");
1671        //vl->addWidget(rb1);
1672        connect(rb1, SIGNAL(toggled(bool)), SIGNAL(UseConstColorForRayViz(bool)));
1673
1674        rb2 = new QRadioButton("ray length", parent);
1675        rb2->setText("ray length");
1676        connect(rb2, SIGNAL(toggled(bool)), SIGNAL(UseRayLengthForRayViz(bool)));
1677       
1678        rb3 = new QRadioButton("contribution", parent);
1679        rb3->setText("contribution");
1680        connect(rb3, SIGNAL(toggled(bool)), SIGNAL(SetShowContribution(bool)));
1681       
1682        rb4 = new QRadioButton("distribution", parent);
1683        rb4->setText("distribution");
1684        connect(rb4, SIGNAL(toggled(bool)), SIGNAL(SetShowDistribution(bool)));
1685
1686
1687        ///////////////////////////
1688
1689       
1690        QGroupBox *groupBox = new QGroupBox("Ray visualization options");
1691        QVBoxLayout *vbox2 = new QVBoxLayout;
1692   
1693        vbox2->addWidget(rb1);
1694        vbox2->addWidget(rb2);
1695        vbox2->addWidget(rb3);
1696        vbox2->addWidget(rb4);
1697       
1698        rb1->setChecked(true);
1699
1700
1701        QCheckBox *cb = new QCheckBox("Distribution 1", parent);
1702        vbox2->addWidget(cb);
1703        cb->setChecked(true);
1704        connect(cb, SIGNAL(toggled(bool)), SIGNAL(SetShowDistribution1(bool)));
1705
1706        cb = new QCheckBox("Distribution 2", parent);
1707        vbox2->addWidget(cb);
1708        cb->setChecked(true);
1709        connect(cb, SIGNAL(toggled(bool)), SIGNAL(SetShowDistribution2(bool)));
1710
1711        cb = new QCheckBox("Distribution 3", parent);
1712        vbox2->addWidget(cb);
1713        cb->setChecked(true);
1714        connect(cb, SIGNAL(toggled(bool)), SIGNAL(SetShowDistribution3(bool)));
1715       
1716        cb = new QCheckBox("Distribution 4", parent);
1717        vbox2->addWidget(cb);
1718        cb->setChecked(true);
1719        connect(cb, SIGNAL(toggled(bool)), SIGNAL(SetShowDistribution4(bool)));
1720
1721
1722        vbox2->addStretch(1);
1723        groupBox->setLayout(vbox2);
1724
1725        return groupBox;
1726}
1727
1728
1729QtRendererControlWidget::QtRendererControlWidget(QWidget * parent, Qt::WFlags f):
1730QWidget(parent, f)
1731{
1732
1733        QVBoxLayout *vl = new QVBoxLayout;
1734        setLayout(vl);
1735
1736        QWidget *vbox = new QGroupBox("ViewCells", this);
1737        layout()->addWidget(vbox);
1738
1739        vl = new QVBoxLayout;
1740        vbox->setLayout(vl);
1741
1742        QLabel *label;
1743        QSlider *slider;
1744        QPushButton *button;
1745
1746#if REMOVE_TEMPORARY
1747
1748        label = new QLabel("Granularity");
1749        vbox->layout()->addWidget(label);
1750
1751        slider = new QSlider(Qt::Horizontal, vbox);
1752        vl->addWidget(slider);
1753        slider->show();
1754        slider->setRange(1, 10000);
1755        slider->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
1756        slider->setValue(200);
1757
1758        connect(slider, SIGNAL(valueChanged(int)), SIGNAL(SetViewCellGranularity(int)));
1759
1760        ///////////////////////////
1761
1762        label = new QLabel("Transfer function");
1763        vbox->layout()->addWidget(label);
1764
1765        slider = new QSlider(Qt::Horizontal, vbox);
1766        vl->addWidget(slider);
1767        slider->show();
1768        slider->setRange(1, 10000);
1769        slider->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
1770        slider->setValue(100);
1771
1772
1773        connect(slider, SIGNAL(valueChanged(int)), SIGNAL(SetTransferFunction(int)));
1774
1775        ////////////////////////////////////////7
1776
1777        button = new QPushButton("Update all PVSs", vbox);
1778        vbox->layout()->addWidget(button);
1779        connect(button, SIGNAL(clicked()), SLOT(UpdateAllPvs()));
1780
1781        ////////////////////////////////////////77777
1782
1783        label = new QLabel("Filter size");
1784        vbox->layout()->addWidget(label);
1785
1786        slider = new QSlider(Qt::Horizontal, vbox);
1787        vbox->layout()->addWidget(slider);
1788        slider->show();
1789        slider->setRange(1, 100);
1790        slider->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
1791        slider->setValue(3);
1792
1793        connect(slider, SIGNAL(valueChanged(int)), SIGNAL(SetVisibilityFilterSize(int)));
1794
1795#endif
1796
1797
1798        //////////////////////////////////////////
1799
1800        label = new QLabel("Spatial Filter size");
1801        vbox->layout()->addWidget(label);
1802
1803        slider = new QSlider(Qt::Horizontal, vbox);
1804        vbox->layout()->addWidget(slider);
1805        slider->show();
1806        slider->setRange(1, 100);
1807        slider->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
1808        slider->setValue(10);
1809
1810        connect(slider, SIGNAL(valueChanged(int)), SIGNAL(SetSpatialFilterSize(int)));
1811
1812
1813        ///////////////////////////////////
1814
1815
1816        QWidget *hbox = new QWidget(vbox);
1817        vl->addWidget(hbox);
1818        QHBoxLayout *hlayout = new QHBoxLayout;
1819        hbox->setLayout(hlayout);
1820
1821        QCheckBox *cb = new QCheckBox("Show viewcells", hbox);
1822        hlayout->addWidget(cb);
1823        cb->setChecked(false);
1824        connect(cb, SIGNAL(toggled(bool)), SIGNAL(SetShowViewCells(bool)));
1825
1826        cb = new QCheckBox("Render cost curve", hbox);
1827        hlayout->addWidget(cb);
1828        cb->setChecked(false);
1829        connect(cb, SIGNAL(toggled(bool)), SIGNAL(SetShowRenderCost(bool)));
1830
1831        cb = new QCheckBox("Show rays", hbox);
1832        hlayout->addWidget(cb);
1833        cb->setChecked(false);
1834        connect(cb, SIGNAL(toggled(bool)), SIGNAL(SetShowRays(bool)));
1835       
1836        cb = new QCheckBox("Show Comparison", hbox);
1837        hlayout->addWidget(cb);
1838        cb->setChecked(false);
1839        connect(cb, SIGNAL(toggled(bool)), SIGNAL(SetShowComparison(bool)));
1840
1841
1842        //////////////////
1843
1844        QHBoxLayout *vh = new QHBoxLayout;
1845
1846        QGroupBox *myBox = new QGroupBox("Visualization");
1847
1848        myBox->setLayout(vh);
1849        vl->addWidget(myBox, 0, 0);
1850
1851        QGroupBox *groupBox = CreateVisualizationPanel(hbox);
1852        vh->addWidget(groupBox, 0, 0);
1853
1854        QGroupBox *groupBox2 = CreateRenderCostPanel(hbox);
1855        vh->addWidget(groupBox2, 0, 0);
1856       
1857        QGroupBox *groupBox3 = CreateRayVisualizationPanel(hbox);
1858        vh->addWidget(groupBox3, 0, 0);
1859
1860        //////////////////////////////////
1861
1862
1863        /*cb = new QRadiobox("Top View", vbox);
1864        vbox->layout()->addWidget(cb);
1865        cb->setChecked(false);
1866        connect(cb, SIGNAL(toggled(bool)), SIGNAL(SetTopView(bool)));
1867*/
1868
1869        vbox->resize(800,150);
1870
1871        vbox = new QGroupBox("Rendering", this);
1872        layout()->addWidget(vbox);
1873
1874        vl = new QVBoxLayout;
1875        vbox->setLayout(vl);
1876
1877
1878
1879        cb = new QCheckBox("Cut view cells", vbox);
1880        vbox->layout()->addWidget(cb);
1881        cb->setChecked(false);
1882        connect(cb, SIGNAL(toggled(bool)), SIGNAL(SetCutViewCells(bool)));
1883
1884
1885        slider = new QSlider(Qt::Horizontal, vbox);
1886        vbox->layout()->addWidget(slider);
1887        slider->show();
1888        slider->setRange(0, 1000);
1889        slider->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
1890        slider->setValue(1000);
1891
1892        connect(slider, SIGNAL(valueChanged(int)), SIGNAL(SetSceneCut(int)));
1893
1894       
1895        cb = new QCheckBox("Hide view cells by render cost ", vbox);
1896        vbox->layout()->addWidget(cb);
1897        cb->setChecked(false);
1898        connect(cb, SIGNAL(toggled(bool)), SIGNAL(SetHideByCost(bool)));
1899
1900        ////////////////////////////
1901
1902        const int range = 1000;
1903
1904        label = new QLabel("Hide by cost");
1905        vbox->layout()->addWidget(label);
1906
1907        // the render cost visualization
1908        slider = new QSlider(Qt::Horizontal, vbox);
1909        vbox->layout()->addWidget(slider);
1910        slider->show();
1911        slider->setRange(0, range);
1912        slider->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
1913        slider->setValue(0);
1914        connect(slider, SIGNAL(valueChanged(int)), SIGNAL(SetHidingCost(int)));
1915
1916        ///////////////////////////////////////////
1917
1918        cb = new QCheckBox("Top View", vbox);
1919        vbox->layout()->addWidget(cb);
1920        cb->setChecked(false);
1921        connect(cb, SIGNAL(toggled(bool)), SIGNAL(SetTopView(bool)));
1922
1923
1924        label = new QLabel("Top distance");
1925        vbox->layout()->addWidget(label);
1926       
1927        slider = new QSlider(Qt::Horizontal, vbox);
1928        vbox->layout()->addWidget(slider);
1929        slider->show();
1930        slider->setRange(1, 1000);
1931        slider->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
1932        slider->setValue(500);
1933
1934        connect(slider, SIGNAL(valueChanged(int)), SIGNAL(SetTopDistance(int)));
1935       
1936
1937        ///////////////////////////////////////////
1938
1939        cb = new QCheckBox("Transparency", vbox);
1940        vbox->layout()->addWidget(cb);
1941        cb->setChecked(false);
1942        connect(cb, SIGNAL(toggled(bool)), SIGNAL(SetUseTransparency(bool)));
1943
1944        label = new QLabel("Use transparency");
1945        vbox->layout()->addWidget(label);
1946
1947        // the render cost visualization
1948        slider = new QSlider(Qt::Horizontal, vbox);
1949        vbox->layout()->addWidget(slider);
1950        slider->show();
1951        slider->setRange(0, range);
1952        slider->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
1953        slider->setValue(0);
1954
1955        connect(slider, SIGNAL(valueChanged(int)), SIGNAL(SetTransparency(int)));
1956
1957        //////////////////////////////
1958
1959#if REMOVE_TEMPORARY
1960        cb = new QCheckBox("Cut scene", vbox);
1961        vbox->layout()->addWidget(cb);
1962        cb->setChecked(false);
1963        connect(cb, SIGNAL(toggled(bool)), SIGNAL(SetCutScene(bool)));
1964#endif
1965        cb = new QCheckBox("Render boxes", vbox);
1966        vbox->layout()->addWidget(cb);
1967        cb->setChecked(false);
1968        connect(cb, SIGNAL(toggled(bool)), SIGNAL(SetRenderBoxes(bool)));
1969
1970        cb = new QCheckBox("Render visibility estimates", vbox);
1971        vbox->layout()->addWidget(cb);
1972        cb->setChecked(false);
1973        connect(cb, SIGNAL(toggled(bool)), SIGNAL(SetRenderVisibilityEstimates(bool)));
1974
1975
1976        cb = new QCheckBox("Render errors", vbox);
1977        vbox->layout()->addWidget(cb);
1978        cb->setChecked(false);
1979        connect(cb, SIGNAL(toggled(bool)), SIGNAL(SetRenderErrors(bool)));
1980
1981        bool tmp;
1982#if REMOVE_TEMPORARY
1983
1984        cb = new QCheckBox("Use filter", vbox);
1985        vbox->layout()->addWidget(cb);
1986        Environment::GetSingleton()->GetBoolValue("Preprocessor.applyVisibilityFilter", tmp );
1987        cb->setChecked(tmp);
1988        connect(cb, SIGNAL(toggled(bool)), SIGNAL(SetUseFilter(bool)));
1989#endif
1990
1991        cb = new QCheckBox("Use spatial filter", vbox);
1992        vbox->layout()->addWidget(cb);
1993        Environment::GetSingleton()->GetBoolValue("Preprocessor.applyVisibilitySpatialFilter",
1994                tmp );
1995        cb->setChecked(tmp);
1996        connect(cb, SIGNAL(toggled(bool)), SIGNAL(SetUseSpatialFilter(bool)));
1997
1998        cb = new QCheckBox("Render filter", vbox);
1999        vbox->layout()->addWidget(cb);
2000        cb->setChecked(true);
2001        connect(cb, SIGNAL(toggled(bool)), SIGNAL(SetRenderFilter(bool)));
2002
2003
2004        /*vbox = new QGroupBox("PVS Errors", this);
2005        layout()->addWidget(vbox);
2006
2007        vl = new QVBoxLayout;
2008        vbox->setLayout(vl);
2009
2010        button = new QPushButton("Compute Visibility", vbox);
2011        vbox->layout()->addWidget(button);
2012        connect(button, SIGNAL(clicked()), SLOT(ComputeVisibility()));
2013
2014        button = new QPushButton("Stop Computation", vbox);
2015        vbox->layout()->addWidget(button);
2016        connect(button, SIGNAL(clicked()), SLOT(StopComputation()));
2017
2018        button = new QPushButton("Set Random View Point", vbox);
2019        vbox->layout()->addWidget(button);
2020        connect(button, SIGNAL(clicked()), SLOT(SetRandomViewPoint()));*/
2021
2022        button = new QPushButton("Store statistics", vbox);
2023        vbox->layout()->addWidget(button);
2024        connect(button, SIGNAL(clicked()), SIGNAL(StoreStatistics()));
2025
2026        button = new QPushButton("Load object", vbox);
2027        vbox->layout()->addWidget(button);
2028        connect(button, SIGNAL(clicked()), SIGNAL(LoadObject()));
2029
2030        /*cb = new QCheckBox("Stats", vbox);
2031        vbox->layout()->addWidget(cb);
2032        cb->setChecked(false);
2033        connect(cb, SIGNAL(toggled(bool)), SIGNAL(StoreStatistics()));*/
2034        if (0)
2035        {
2036                vbox = new QGroupBox("PVS Errors", this);
2037                layout()->addWidget(vbox);
2038
2039                vl = new QVBoxLayout;
2040                vbox->setLayout(vl);
2041
2042                mPvsErrorWidget = new QListWidget(vbox);
2043                vbox->layout()->addWidget(mPvsErrorWidget);
2044
2045                connect(mPvsErrorWidget,
2046                        SIGNAL(doubleClicked(const QModelIndex &)),
2047                        this,
2048                        SLOT(PvsErrorClicked(const QModelIndex &)));
2049
2050                button = new QPushButton("Next Error Frame", vbox);
2051                vbox->layout()->addWidget(button);
2052                connect(button, SIGNAL(clicked(void)), SLOT(FocusNextPvsErrorFrame(void)));
2053        }
2054       
2055        //connect(button, SIGNAL(clicked(void)), SLOT(StoreStatistics(void)));
2056        //mHidingCost = 0.1f;
2057
2058        setWindowTitle("Preprocessor Control Widget");
2059        adjustSize();
2060}
2061
2062
2063
2064
2065void
2066QtRendererControlWidget::FocusNextPvsErrorFrame(void)
2067{}
2068
2069void
2070QtRendererControlWidget::UpdatePvsErrorItem(int row,
2071                                                                                        GlRendererBuffer::PvsErrorEntry &pvsErrorEntry)
2072{
2073
2074        QListWidgetItem *i = mPvsErrorWidget->item(row);
2075        QString s;
2076        s.sprintf("%5.5f", pvsErrorEntry.mError);
2077        if (i) {
2078                i->setText(s);
2079        } else {
2080                new QListWidgetItem(s, mPvsErrorWidget);
2081        }
2082        mPvsErrorWidget->update();
2083}
2084
2085
2086
2087
2088/*********************************************************************/
2089/*                   QtGlDebuggerWidget implementation               */
2090/*********************************************************************/
2091
2092
2093QtGlDebuggerWidget::QtGlDebuggerWidget(QtGlRendererBuffer *buf, QWidget *parent)
2094: QGLWidget(QGLFormat(QGL::SampleBuffers), parent), mRenderBuffer(buf)
2095{
2096        // create the pbuffer
2097        //pbuffer = new QGLPixelBuffer(QSize(512, 512), format(), this);
2098        timerId = startTimer(20);
2099        setWindowTitle(("OpenGL pbuffers"));
2100}
2101
2102
2103QtGlDebuggerWidget::~QtGlDebuggerWidget()
2104{
2105        mRenderBuffer->releaseFromDynamicTexture();
2106        glDeleteTextures(1, &dynamicTexture);
2107
2108        DEL_PTR(mRenderBuffer);
2109}
2110
2111
2112void QtGlDebuggerWidget::initializeGL()
2113{
2114        glMatrixMode(GL_PROJECTION);
2115        glLoadIdentity();
2116
2117        glFrustum(-1, 1, -1, 1, 10, 100);
2118        glTranslatef(-0.5f, -0.5f, -0.5f);
2119        glTranslatef(0.0f, 0.0f, -15.0f);
2120        glMatrixMode(GL_MODELVIEW);
2121
2122        glEnable(GL_CULL_FACE);
2123        initCommon();
2124        initPbuffer();
2125
2126}
2127
2128
2129void QtGlDebuggerWidget::resizeGL(int w, int h)
2130{
2131        glViewport(0, 0, w, h);
2132}
2133
2134
2135void QtGlDebuggerWidget::paintGL()
2136{
2137        // draw a spinning cube into the pbuffer..
2138        mRenderBuffer->makeCurrent();
2139
2140        BeamSampleStatistics stats;
2141        mRenderBuffer->SampleBeamContributions(mSourceObject, mBeam, mSamples, stats);
2142
2143        glFlush();
2144
2145        // rendering directly to a texture is not supported on X11, unfortunately
2146        mRenderBuffer->updateDynamicTexture(dynamicTexture);
2147
2148        // and use the pbuffer contents as a texture when rendering the
2149        // background and the bouncing cubes
2150        makeCurrent();
2151        glBindTexture(GL_TEXTURE_2D, dynamicTexture);
2152        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
2153
2154        // draw the background
2155        glMatrixMode(GL_MODELVIEW);
2156        glPushMatrix();
2157        glLoadIdentity();
2158        glMatrixMode(GL_PROJECTION);
2159        glPushMatrix();
2160        glLoadIdentity();
2161
2162        glPopMatrix();
2163        glMatrixMode(GL_MODELVIEW);
2164        glPopMatrix();
2165}
2166
2167
2168void QtGlDebuggerWidget::initPbuffer()
2169{
2170        // set up the pbuffer context
2171        mRenderBuffer->makeCurrent();
2172        /*mRenderBuffer->InitGL();
2173
2174        glViewport(0, 0, mRenderBuffer->size().width(), mRenderBuffer->size().height());
2175        glMatrixMode(GL_PROJECTION);
2176        glLoadIdentity();
2177        glOrtho(-1, 1, -1, 1, -99, 99);
2178        glTranslatef(-0.5f, -0.5f, 0.0f);
2179        glMatrixMode(GL_MODELVIEW);
2180        glLoadIdentity();
2181
2182        glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);*/
2183
2184        // generate a texture that has the same size/format as the pbuffer
2185        dynamicTexture = mRenderBuffer->generateDynamicTexture();
2186
2187        // bind the dynamic texture to the pbuffer - this is a no-op under X11
2188        mRenderBuffer->bindToDynamicTexture(dynamicTexture);
2189        makeCurrent();
2190}
2191
2192
2193void QtGlDebuggerWidget::initCommon()
2194{
2195        glEnable(GL_TEXTURE_2D);
2196        glEnable(GL_DEPTH_TEST);
2197
2198        glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
2199}
2200
2201
2202void QtGlRendererWidget::_RenderColoredPvs()
2203{
2204        // note: could be done more efficiently using color buffers
2205        mPvsSize = mPvsCache.mPvs.GetSize();
2206
2207        ObjectPvsIterator it = mPvsCache.mPvs.GetIterator();
2208
2209        PvsData pvsData;
2210
2211        while (it.HasMoreEntries())
2212        {
2213                Intersectable *object = it.Next(pvsData);
2214
2215                RgbColor color;
2216
2217                //float visibility = mTransferFunction*log10(entry.mData.mSumPdf + 1); // /5.0f
2218                // glColor3f(visibility, 0.0f, 0.0f);
2219                //cerr << "sumpdf: " << pvsData.mSumPdf << endl;
2220                if (mUseRandomColorPerPvsObject)
2221                {
2222                        KdIntersectable *kdint = static_cast<KdIntersectable *>(object);
2223
2224                        if (kdint->mGenericIdx == -1)
2225                        {
2226                                kdint->mGenericIdx = (int)mColors.size();
2227                                mColors.push_back(RandomColor());
2228                        }
2229                        color = mColors[kdint->mGenericIdx];
2230                }
2231                else
2232                {
2233                        color = RainbowColorMapping(mTransferFunction * log10(pvsData.mSumPdf + 1));
2234                }
2235
2236                glColor3f(color.r, color.g, color.b);
2237
2238                mUseForcedColors = true;
2239                RenderIntersectable(object);
2240                mUseForcedColors = false;
2241        }
2242}
2243
2244}
Note: See TracBrowser for help on using the repository browser.