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

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