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

Revision 2720, 71.2 KB checked in by mattausch, 16 years ago (diff)

dynamic objects problem!!

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