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

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