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

Revision 2617, 59.6 KB checked in by bittner, 16 years ago (diff)

change to USE_BIT_PVS for timing tests: Warning visualization will probably not work correctly

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