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

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