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

Revision 2679, 64.4 KB checked in by mattausch, 16 years ago (diff)

found error with qglpixelbuffer

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