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

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