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

Revision 2584, 51.5 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                {
719                        RenderRays(mViewCellsManager->mVizBuffer.GetRays());
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();
733        else {
734                if (0) {
735                        float dist = Magnitude(mSceneGraph->GetBox().Diagonal())*0.05;
736                        Vector3 pos = mViewPoint - dist*Vector3(mViewDirection.x,
737                                -1,
738                                mViewDirection.y);
739
740                        Vector3 target = mViewPoint + dist*mViewDirection;
741                        Vector3 up(0,1,0);
742
743                        glLoadIdentity();
744                        gluLookAt(pos.x, pos.y, pos.z,
[1252]745                                target.x, target.y, target.z,
746                                up.x, up.y, up.z);
[2563]747                }
748                else
749                {
750                        float dist = Magnitude(mSceneGraph->GetBox().Diagonal()) * mTopDistance;
[2543]751                        Vector3 pos = mViewPoint  + dist*Vector3(0,
752                                1,
753                                0);
754
755                        Vector3 target = mViewPoint;
756                        Vector3 up(mViewDirection.x, 0, mViewDirection.z);
757
758                        glLoadIdentity();
759                        gluLookAt(pos.x, pos.y, pos.z,
[1252]760                                target.x, target.y, target.z,
761                                up.x, up.y, up.z);
[2543]762
763                }
[1252]764        }
765
766}
767
[1942]768
[1252]769void
770QtGlRendererWidget::keyPressEvent ( QKeyEvent * e )
771{
[2543]772        switch (e->key()) {
773case Qt::Key_T:
[1252]774        mTopView = !mTopView;
[1942]775        SetupCameraProjection(width(), height());
[1252]776        updateGL();
777        break;
[2543]778case Qt::Key_V:
[1252]779        mRenderViewCells = !mRenderViewCells;
780        updateGL();
781        break;
[2543]782case Qt::Key_P:
[1252]783        // set random viewpoint
784        mViewCellsManager->GetViewPoint(mViewPoint);
785        updateGL();
786        break;
[2543]787case Qt::Key_S: {
[1966]788        // set view poitn and direction
789        QString text;
790        bool ok;
791        text.sprintf("%f %f %f", mViewPoint.x, mViewPoint.y, mViewPoint.z);
792        text = QInputDialog::getText(this,
[2543]793                "Enter a view point",
794                "",
795                QLineEdit::Normal,
796                text,
797                &ok);
[1966]798        if (!ok)
[2543]799                break;
800
[1967]801        if (sscanf(text.toAscii(), "%f %f %f", &mViewPoint.x, &mViewPoint.y, &mViewPoint.z) == 3) {
[2543]802                text.sprintf("%f %f %f", mViewDirection.x, mViewDirection.y, mViewDirection.z);
803                text = QInputDialog::getText(this,
804                        "Enter a direction",
805                        "",
806                        QLineEdit::Normal,
807                        text,
808                        &ok);
809                if (!ok)
810                        break;
811                if (sscanf(text.toAscii(), "%f %f %f", &mViewDirection.x,
812                        &mViewDirection.y, &mViewDirection.z) == 3) {
813                                updateGL();
814                        }
815                        break;
[1967]816        }
[2543]817                                }
818default:
[1252]819        e->ignore();
820        break;
[2543]821        }
[1252]822}
823
824
[2543]825
[1942]826QtGlRendererWidget::QtGlRendererWidget(
827                                                                           SceneGraph *sceneGraph,
828                                                                           ViewCellsManager *viewcells,
829                                                                           KdTree *tree,
830                                                                           QWidget * parent,
831                                                                           const QGLWidget * shareWidget,
832                                                                           Qt::WFlags f
833                                                                           )
[2543]834                                                                           :
835GlRendererWidget(sceneGraph, viewcells, tree), QGLWidget(parent, shareWidget, f)
[1252]836{
[2543]837        mPreprocessorThread = NULL;
838        mTopView = false;
839        mRenderViewCells = false;
840        mTopDistance = 1.0f;
841        mCutViewCells = false;
842        mCutScene = false;
843        mRenderErrors = false;
844        mRenderBoxes = false;
845        mRenderFilter = true;
846        mRenderVisibilityEstimates = false;
[2562]847
848        mHideByCost = false;
[2564]849        mUseTransparency = false;
[2562]850
[2569]851        mTransferFunction = 1.0f;
[2543]852        mIndexBufferSize = 0;
[1252]853
[2543]854        //mCurrentFrame = 0;
[1252]855
[2562]856        const int delay = 250; // in milliseconds
[2543]857        timerId = startTimer(delay);
[1252]858
[2543]859        bool tmp;
[1942]860
[2543]861        Environment::GetSingleton()->GetBoolValue("Preprocessor.applyVisibilityFilter", tmp );
862        mUseFilter = tmp;
[1252]863
[2580]864        Environment::GetSingleton()->GetBoolValue("Preprocessor.applyVisibilitySpatialFilter", tmp );
[2543]865        mUseSpatialFilter = tmp;
[1252]866
[2580]867        //mLogWriter = new LogWriter("myfile.out");
868
[2543]869        mShowRenderCost = false;
870        mShowPvsSizes = false;
[2580]871        mShowComparison = false;
[2560]872        mShowPiercingRays = false;
[2566]873        mShowWeightedRays = false;
[2569]874        mShowWeightedCost = false;
[2566]875
[2569]876        mShowDistanceWeightedPvs = true;
877        mShowDistanceWeightedTriangles = false;
878        mShowWeightedTriangles = false;
879
[2543]880        mSpatialFilterSize = 0.01;
881        mPvsSize = 0;
882        mRenderError = 0.0f;
883        mShowRays = false;
[1252]884
[2543]885        mControlWidget = new QtRendererControlWidget(NULL);
[1942]886
[2543]887        connect(mControlWidget, SIGNAL(SetViewCellGranularity(int)), this, SLOT(SetViewCellGranularity(int)));
[2584]888        connect(mControlWidget, SIGNAL(SetTransferFunction(int)), this, SLOT(SetTransferFunction(int)));
889
[2543]890        connect(mControlWidget, SIGNAL(UpdateAllPvs()), this, SLOT(UpdateAllPvs()));
891        connect(mControlWidget, SIGNAL(ComputeVisibility()), this, SLOT(ComputeVisibility()));
892        connect(mControlWidget, SIGNAL(StopComputation()), this, SLOT(StopComputation()));
[2584]893        connect(mControlWidget, SIGNAL(SetRandomViewPoint()), this,     SLOT(SetRandomViewPoint()));
894        connect(mControlWidget, SIGNAL(StoreStatistics(void)), this, SLOT(StoreStatistics(void)));
[2543]895
896        connect(mControlWidget, SIGNAL(SetSceneCut(int)), this, SLOT(SetSceneCut(int)));
897        connect(mControlWidget, SIGNAL(SetTopDistance(int)), this, SLOT(SetTopDistance(int)));
[2563]898        connect(mControlWidget, SIGNAL(SetTransparency(int)), this, SLOT(SetTransparency(int)));
[2543]899
900        connect(mControlWidget, SIGNAL(SetVisibilityFilterSize(int)), this, SLOT(SetVisibilityFilterSize(int)));
901        connect(mControlWidget, SIGNAL(SetSpatialFilterSize(int)), this, SLOT(SetSpatialFilterSize(int)));
[2562]902        connect(mControlWidget, SIGNAL(SetHidingCost(int)), this, SLOT(SetHidingCost(int)));
[2543]903
904        connect(mControlWidget, SIGNAL(SetShowViewCells(bool)), this, SLOT(SetShowViewCells(bool)));
905        connect(mControlWidget, SIGNAL(SetShowRenderCost(bool)), this, SLOT(SetShowRenderCost(bool)));
[2564]906        connect(mControlWidget, SIGNAL(SetUseTransparency(bool)), this, SLOT(SetUseTransparency(bool)));
[2543]907        connect(mControlWidget, SIGNAL(SetShowPvsSizes(bool)), this, SLOT(SetShowPvsSizes(bool)));
[2580]908        connect(mControlWidget, SIGNAL(SetShowComparison(bool)), this, SLOT(SetShowComparison(bool)));
[2543]909        connect(mControlWidget, SIGNAL(SetTopView(bool)), this, SLOT(SetTopView(bool)));
910        connect(mControlWidget, SIGNAL(SetCutViewCells(bool)), this, SLOT(SetCutViewCells(bool)));
[2562]911        connect(mControlWidget, SIGNAL(SetHideByCost(bool)), this, SLOT(SetHideByCost(bool)));
[2543]912        connect(mControlWidget, SIGNAL(SetCutScene(bool)), this, SLOT(SetCutScene(bool)));
913        connect(mControlWidget, SIGNAL(SetRenderErrors(bool)), this, SLOT(SetRenderErrors(bool)));
914        connect(mControlWidget, SIGNAL(SetRenderBoxes(bool)), this, SLOT(SetRenderBoxes(bool)));
915        connect(mControlWidget, SIGNAL(SetRenderFilter(bool)), this, SLOT(SetRenderFilter(bool)));
[2566]916        connect(mControlWidget, SIGNAL(SetRenderVisibilityEstimates(bool)), this, SLOT(SetRenderVisibilityEstimates(bool)));
[2543]917        connect(mControlWidget, SIGNAL(SetUseFilter(bool)), this, SLOT(SetUseFilter(bool)));
[2566]918        connect(mControlWidget, SIGNAL(SetUseSpatialFilter(bool)), this, SLOT(SetUseSpatialFilter(bool)));
[2560]919        connect(mControlWidget, SIGNAL(SetShowPiercingRays(bool)), this, SLOT(SetShowPiercingRays(bool)));
[2566]920        connect(mControlWidget, SIGNAL(SetShowWeightedRays(bool)), this, SLOT(SetShowWeightedRays(bool)));
[2569]921        connect(mControlWidget, SIGNAL(SetShowWeightedCost(bool)), this, SLOT(SetShowWeightedCost(bool)));
[2543]922
[2569]923        connect(mControlWidget, SIGNAL(SetShowDistanceWeightedTriangles(bool)), this, SLOT(SetShowDistanceWeightedTriangles(bool)));
924        connect(mControlWidget, SIGNAL(SetShowDistanceWeightedPvs(bool)), this, SLOT(SetShowDistanceWeightedPvs(bool)));
925        connect(mControlWidget, SIGNAL(SetShowWeightedTriangles(bool)), this, SLOT(SetShowWeightedTriangles(bool)));
926
[2543]927        connect(mControlWidget,
928                SIGNAL(SetShowRays(bool)),
929                this,
930                SLOT(SetShowRays(bool)));
931
932        resize(1000, 500);
933        mControlWidget->show();
[1252]934}
935
[1942]936void
937QtGlRendererWidget::UpdateAllPvs()
938{
[2543]939        // $$ does not work so far:(
940        mViewCellsManager->UpdatePvsForEvaluation();
941        //      mViewCellsManager->FinalizeViewCells(false);
[1942]942}
[1252]943
944void
[1942]945QtGlRendererWidget::ComputeVisibility()
[1252]946{
[2543]947        cerr<<"Compute Visibility called!\n"<<endl;
948        if (!mPreprocessorThread->isRunning())
949                mPreprocessorThread->RunThread();
[1942]950}
[1925]951
[1942]952void
953QtGlRendererWidget::StopComputation()
954{
[2543]955        cerr<<"stop computation called!\n"<<endl;
956        mViewCellsManager->GetPreprocessor()->mStopComputation = true;
[1942]957}
[1252]958
[1942]959void
960QtGlRendererWidget::SetRandomViewPoint()
961{
[2584]962        cerr<<"setting random view point!\n"<<endl;
[2543]963        mViewCellsManager->GetViewPoint(mViewPoint);
964        updateGL();
[1942]965}
[1252]966
[2584]967
968void QtGlRendererWidget::StoreStatistics()
969{
970        cerr<<"storing statistics!\n"<<endl;
971        const int currentSamples = mViewCellsManager->GetPreprocessor()->mCurrentSamples;
972
973        cout<<"**********************************************" << endl;
974        cout << "reached " << currentSamples << " samples " << " => writing file" << endl;
975               
976        LogWriter writer;
977        writer.SetFilename("compare.log");
978        writer.Write(currentSamples, mViewCellsManager->GetViewCells());
979        cout << "finished writing file" << endl;
980        mCompareInfo.clear();
981        updateGL();
982}
983
[1942]984void
985QtGlRendererWidget::RenderRenderCost()
986{
[1252]987        static vector<float> costFunction;
988        static float maxCost = -1;
989        if (costFunction.size()==0) {
[2543]990                ViewCellsTree *tree = mViewCellsManager->GetViewCellsTree();
991                if (tree) {
992                        tree->GetCostFunction(costFunction);
993                        maxCost = -1;
994                        for (int i=0;  i < costFunction.size(); i++) {
995                                //                cout<<i<<":"<<costFunction[i]<<" ";
996                                // update cost function to an absolute value based on the total geometry count
997                                costFunction[i]*=mSceneGraph->GetRoot()->mGeometry.size();
998                                if (costFunction[i] > maxCost)
999                                        maxCost = costFunction[i];
1000                        }
[1252]1001                }
1002        }
1003
[2543]1004
[1252]1005        int currentPos = (int)mViewCellsManager->GetViewCells().size();
1006        float currentCost= -1;
1007
1008        if (currentPos < costFunction.size())
[2543]1009                currentCost = costFunction[currentPos];
[1942]1010#if 1   
[2538]1011        cout<<"costFunction.size()="<<(int)costFunction.size()<<endl;
[1252]1012        cout<<"CP="<<currentPos<<endl;
1013        cout<<"MC="<<maxCost<<endl;
1014        cout<<"CC="<<currentCost<<endl;
1015#endif
1016        if (costFunction.size()) {
[2543]1017                float scaley = 1.0f/log10(maxCost);
1018                float scalex = 1.0f/(float)costFunction.size();
[1942]1019
[2543]1020                glDisable(GL_DEPTH_TEST);
1021                // init ortographic projection
1022                glMatrixMode(GL_PROJECTION);
[1252]1023
[2543]1024                glPushMatrix();
1025
1026                glLoadIdentity();
1027                gluOrtho2D(0, 1.0f, 0, 1.0f);
1028
1029                glTranslatef(0.1f, 0.1f, 0.0f);
1030                glScalef(0.8f, 0.8f, 1.0f);
1031                glMatrixMode(GL_MODELVIEW);
1032                glLoadIdentity();
1033
1034                glColor3f(1.0f,0,0);
1035                glBegin(GL_LINE_STRIP);
1036                //        glVertex3f(0,0,0);
1037
1038                for (int i=0;  i < costFunction.size(); i++) {
1039                        float x =  i*scalex;
1040                        float y = log10(costFunction[i])*scaley;
1041                        glVertex3f(x,y,0.0f);
1042                }
1043                glEnd();
1044
1045                glColor3f(1.0f,0,0);
1046                glBegin(GL_LINES);
1047                float x =  currentPos*scalex;
[1252]1048                glVertex3f(x,0.0,0.0f);
1049                glVertex3f(x,1.0f,0.0f);
[2543]1050                glEnd();
[1252]1051
[2543]1052                glColor3f(0.0f,0,0);
1053                // show a grid
1054                glBegin(GL_LINE_LOOP);
1055                glVertex3f(0,0,0.0f);
1056                glVertex3f(1,0,0.0f);
1057                glVertex3f(1,1,0.0f);
1058                glVertex3f(0,1,0.0f);
1059                glEnd();
[1252]1060
[2543]1061                glBegin(GL_LINES);
1062                for (int i=0;  i < costFunction.size(); i += 1000) {
1063                        float x =  i*scalex;
1064                        glVertex3f(x,0.0,0.0f);
1065                        glVertex3f(x,1.0f,0.0f);
1066                }
[1252]1067
[2543]1068                for (int i=0;  pow(10.0f, i) < maxCost; i+=1) {
1069                        float y = i*scaley;
1070                        //              QString s;
1071                        //              s.sprintf("%d", (int)pow(10,i));
1072                        //              renderText(width()/2+5, y*height(), s);
1073                        glVertex3f(0.0f, y, 0.0f);
1074                        glVertex3f(1.0f, y, 0.0f);
1075                }
[1252]1076
[2543]1077                glEnd();
1078
1079
1080                // restore the projection matrix
1081                glMatrixMode(GL_PROJECTION);
1082                glPopMatrix();
1083                glMatrixMode(GL_MODELVIEW);
1084                glEnable(GL_DEPTH_TEST);
1085
[1252]1086        }
[1942]1087
1088
[2543]1089
[1252]1090}
1091
[1942]1092void
1093QtGlRendererWidget::RenderInfo()
1094{
[1983]1095
[2543]1096        QString s;
1097        int vc = 0;
1098        if (mViewCellsManager)
1099                vc = (int)mViewCellsManager->GetViewCells().size();
[1252]1100
[2543]1101        int filter = 0;
1102        if (mViewCellsManager)
1103                filter = mViewCellsManager->GetMaxFilterSize();
[1942]1104
[2543]1105        glColor3f(1.0f,1.0f,1.0f);
[1942]1106
[2543]1107        s.sprintf("frame:%04d viewpoint:(%4.1f,%4.1f,%4.1f) dir:(%4.1f,%4.1f,%4.1f)",
1108                mFrame,
1109                mViewPoint.x,
1110                mViewPoint.y,
1111                mViewPoint.z,
1112                mViewDirection.x,
1113                mViewDirection.y,
1114                mViewDirection.z
1115                );
[1942]1116
[2543]1117        renderText(20,20,s);
[1942]1118
[2543]1119        s.sprintf("viewcells:%04d filter:%04d pvs:%04d error:%5.5f %",
1120                vc,
1121                filter,
1122                mPvsSize,
1123                mRenderError*100.0f);
[1942]1124
1125
[2543]1126        renderText(20,40,s);
[1942]1127
[2543]1128
1129
1130
[1942]1131}
1132
1133
[1252]1134void
1135QtGlRendererWidget::SetViewCellGranularity(int number)
1136{
[2543]1137        if (mViewCellsManager) {
1138                //      mViewCellsManager->SetMaxFilterSize(number);
[1252]1139
[2543]1140                // $$ tmp off
1141                mViewCellsManager->CollectViewCells(number);
[1983]1142
[2543]1143                // $$ does not work so far:(
1144                //      mViewCellsManager->UpdatePvsForEvaluation();
1145                //      mViewCellsManager->FinalizeViewCells(false);
1146        }
1147        updateGL();
[1252]1148}
1149
1150void
1151QtGlRendererWidget::SetVisibilityFilterSize(int number)
1152{
[2543]1153        if (mViewCellsManager)
1154                mViewCellsManager->SetMaxFilterSize(number);
1155        mPvsCache.Reset();
1156        updateGL();
[1252]1157}
1158
1159void
1160QtGlRendererWidget::SetSpatialFilterSize(int number)
1161{
[2543]1162        mSpatialFilterSize = 1e-3*number;
1163        mPvsCache.Reset();
1164        updateGL();
[1252]1165}
1166
1167void
1168QtGlRendererWidget::SetSceneCut(int number)
1169{
[2543]1170        // assume the cut plane can only be aligned with xz plane
1171        // shift it along y according to number, which is percentage of the bounding
1172        // box position
[2562]1173        if (mViewCellsManager)
1174        {
[2571]1175                const float f = number / 1000.0f;
[2543]1176                AxisAlignedBox3 box = mViewCellsManager->GetViewSpaceBox();
[2571]1177                Vector3 p = (1.0f - f) * box.Min() + f * box.Max();
[2543]1178                mSceneCutPlane.mNormal = Vector3(0,-1,0);
1179                mSceneCutPlane.mD = -DotProd(mSceneCutPlane.mNormal, p);
[2562]1180
[2543]1181                updateGL();
1182        }
[1252]1183}
1184
1185void
[2562]1186QtGlRendererWidget::SetHidingCost(int number)
1187{
1188        mHidingCost = (float)number / 1000.0f;
1189}
1190
1191
1192void
[1252]1193QtGlRendererWidget::SetTopDistance(int number)
1194{
[2569]1195        mTopDistance = number / 1000.0f;
[2543]1196        updateGL();
[1252]1197}
1198
[2569]1199void QtGlRendererWidget::SetTransparency(int number)
[2563]1200{
[2569]1201        mTransparency = number / 1000.0f;
[2563]1202        updateGL();
1203}
1204
[2570]1205#if 0
[2569]1206float QtGlRendererWidget::ComputeRenderCost(ViewCell *vc)
1207{
1208        ObjectPvs basePvs;
1209       
1210        basePvs = vc->CopyPvs();
1211        ObjectPvsIterator pit = basePvs.GetIterator();
1212
1213        float renderCost = 0;
1214
1215        //cout << "cost vis: " << mShowDistanceWeightedPvs << " " << " " << mShowDistanceWeightedTriangles << " " << mShowWeightedTriangles << endl;
1216       
1217        // first mark all objects from this pvs
1218        while (pit.HasMoreEntries())   
1219        {
1220                if (mShowDistanceWeightedPvs)
1221                {
1222                        KdIntersectable *kdObj = static_cast<KdIntersectable *>(pit.Next());
1223                        const AxisAlignedBox3 box = kdObj->GetBox();
1224
1225                        const float dist = SqrDistance(vc->GetBox().Center(), box.Center());
1226                        renderCost += 1.0f / dist;
1227                }
1228                else if (mShowDistanceWeightedTriangles)
1229                {
1230                        KdIntersectable *kdObj = static_cast<KdIntersectable *>(pit.Next());
1231                        const AxisAlignedBox3 box = kdObj->GetBox();
1232
1233                        const float dist = SqrDistance(vc->GetBox().Center(), box.Center());
1234                        renderCost += kdObj->ComputeNumTriangles() / dist;
1235                }
1236                else //if (mShowWeightedTriangles)
1237                {
1238                        KdIntersectable *kdObj = static_cast<KdIntersectable *>(pit.Next());
1239                        renderCost += kdObj->ComputeNumTriangles();
1240                }
1241                //if (pit.Next()->Mail();
1242        }
1243
1244        return renderCost;
1245}
[2570]1246#else
[2569]1247
[2570]1248float QtGlRendererWidget::ComputeRenderCost(ViewCell *vc)
1249{
1250        float renderCost = 0;
[2569]1251
[2570]1252        if (mShowDistanceWeightedPvs)
1253        {
1254                return vc->GetPvs().mStats.mDistanceWeightedPvs;
1255        }
1256        else if (mShowDistanceWeightedTriangles)
1257        {
1258                return vc->GetPvs().mStats.mDistanceWeightedTriangles;
1259        }
1260        else //if (mShowWeightedTriangles)
1261        {
1262                return vc->GetPvs().mStats.mWeightedTriangles;
1263        }
1264}
1265#endif
1266
[1252]1267
[2049]1268
[2576]1269void QtGlRendererWidget::ComputeMaxValues(const ViewCellContainer &viewCells,
1270                                                                                  int &maxPvs,
1271                                                                                  int &maxPiercingRays,
1272                                                                                  float &maxRelativeRays,
1273                                                                                  float &maxRcCost)
1274{
1275        maxPvs = -1;
1276        maxPiercingRays = 1; // not zero for savety
1277        maxRelativeRays = Limits::Small; // not zero for savety
1278        maxRcCost = Limits::Small;
[1252]1279
[2576]1280        for (size_t i = 0; i < viewCells.size(); ++ i)
[2560]1281        {
[2576]1282                ViewCell *vc = viewCells[i];
[1252]1283
[2571]1284                if (mShowPvsSizes) // pvs size
[2569]1285                {
1286                        //const int p = vc->GetPvs().CountObjectsInPvs();
1287                        const int p = vc->GetPvs().GetSize();
1288                        if (p > maxPvs)
1289                                maxPvs = p;
1290                }
1291                else if (mShowPiercingRays) // relative number of rays
1292                {
1293                        const int piercingRays = vc->GetNumPiercingRays();
[2562]1294
[2569]1295                        if (piercingRays > maxPiercingRays)
1296                                maxPiercingRays = piercingRays;
1297                }
1298                else if (mShowWeightedRays)
1299                {
1300                        const int piercingRays = vc->GetNumPiercingRays();
[2566]1301
[2569]1302                        const float relativeArea =
1303                                vc->GetBox().SurfaceArea() / mViewCellsManager->GetViewSpaceBox().SurfaceArea();
[2566]1304
[2569]1305                        if ((float)piercingRays / relativeArea > maxRelativeRays)
1306                                maxRelativeRays = (float)piercingRays / relativeArea;
1307                }
1308                else if (mShowWeightedCost)
1309                {
1310                        const float rcCost = ComputeRenderCost(vc);
1311                        mViewCellsManager->UpdateScalarPvsCost(vc, rcCost);
1312
1313                        if (rcCost > maxRcCost)
1314                                maxRcCost = rcCost;
1315                }
[2543]1316        }
[2576]1317}
[1252]1318
[2576]1319
1320void QtGlRendererWidget::RenderViewCells()
1321{
1322        mUseFalseColors = true;
1323
1324        //glPushAttrib(GL_CURRENT_BIT | GL_ENABLE_BIT | GL_POLYGON_BIT);
1325
1326        glEnable(GL_CULL_FACE);
1327        glCullFace(GL_FRONT);
1328        //glDisable(GL_CULL_FACE);
1329
1330        double eq[4];
1331        eq[0] = mSceneCutPlane.mNormal.x;
1332        eq[1] = mSceneCutPlane.mNormal.y;
1333        eq[2] = mSceneCutPlane.mNormal.z;
1334        eq[3] = mSceneCutPlane.mD;
1335
1336        if (mCutViewCells)
1337        {
1338                glClipPlane(GL_CLIP_PLANE0, eq);
1339                glEnable(GL_CLIP_PLANE0);
1340        }
1341
1342        ViewCellContainer &viewcells = mViewCellsManager->GetViewCells();
[2584]1343       
[2576]1344        int maxPvs, maxPiercingRays;
1345        float maxRelativeRays, maxRcCost;
1346
1347        ComputeMaxValues(viewcells, maxPvs, maxPiercingRays, maxRelativeRays, maxRcCost);
1348
[2581]1349        int i;
[2576]1350
[2569]1351        if (!mShowPvsSizes && !mShowPiercingRays && !mShowWeightedRays && !mShowWeightedCost)
[2560]1352        {
[2562]1353                for (i = 0; i < viewcells.size(); ++ i)
1354                {
1355                        ViewCell *vc = viewcells[i];
1356                        RgbColor c;
[2543]1357
[2562]1358                        if (!mShowPvsSizes && !mShowPiercingRays)
1359                        {
1360                                mWireFrame = true;
1361                                c = vc->GetColor();
1362                        }
[2543]1363
[2562]1364                        glColor3f(c.r, c.g, c.b);
1365
[2569]1366                        if (!mHideByCost || (mHidingCost < (vc->GetNumPiercingRays() / (float)maxPiercingRays)))
[2562]1367                        {
1368                RenderViewCell(vc);
1369                        }
1370                }
1371        }
1372        else
1373        {
[2576]1374                // transparency
[2564]1375                if (!mUseTransparency)
1376                {
1377                        glEnable(GL_DEPTH_TEST);
1378                        glDisable(GL_BLEND);
1379                }
1380                else
1381                {
1382                        glDisable(GL_DEPTH_TEST);
1383                        glEnable(GL_BLEND);
[2562]1384
[2564]1385                        glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1386                        //glBlendFunc(GL_SRC_ALPHA, GL_ONE);
[2562]1387
[2564]1388                        for (i = 0; i < viewcells.size(); ++ i)
1389                        {
1390                                ViewCell *vc = viewcells[i];
[2562]1391
[2566]1392                                const float dist = SqrDistance(mDummyViewPoint, vc->GetBox().Center());
[2564]1393                                vc->SetDistance(dist);
1394                        }
1395
1396                        sort(viewcells.begin(), viewcells.end(), nearerThan);
[2560]1397                }
[2562]1398
[2584]1399                if (!mShowComparison)
[2576]1400                        AssignImportanceByRelativeValue(viewcells, maxPvs, maxPiercingRays, maxRelativeRays, maxRcCost);
1401                else
[2577]1402                {
[2584]1403                        if (mCompareInfo.empty())
1404                        {
1405                                LogReader reader;
1406                                reader.SetFilename("compare.log");
1407                                int samples;
1408                                reader.Read(samples, mCompareInfo);
1409                        }
[2580]1410
[2584]1411                        AssignColorByComparison(viewcells, mCompareInfo);
[2577]1412                }
[2566]1413
[2562]1414                glEnable(GL_DEPTH_TEST);       
[1252]1415        }
[2571]1416       
1417        glEnable(GL_CULL_FACE);
1418        glDisable(GL_CULL_FACE);
[1252]1419
[2571]1420        glDisable(GL_CLIP_PLANE0);
1421
[2543]1422        mUseFalseColors = false;
1423        mWireFrame = false;
[1942]1424
[2543]1425        glPopAttrib();
[1252]1426}
1427
1428
[2576]1429void QtGlRendererWidget::AssignImportanceByRelativeValue(const ViewCellContainer &viewCells,
1430                                                                                                                 int &maxPvs,
1431                                                                                                                 int &maxPiercingRays,
1432                                                                                                                 float &maxRelativeRays,
1433                                                                                                                 float &maxRcCost)
1434{
1435        for (size_t i = 0; i < viewCells.size(); ++ i)
1436        {
1437                RgbColor c;
1438                ViewCell *vc = viewCells[i];
[1252]1439
[2576]1440                float importance;
[1252]1441
[2576]1442                if (mShowPiercingRays)
1443                {
1444                        importance = mTransferFunction *
1445                                ((float)vc->GetNumPiercingRays() / (float)maxPiercingRays);
1446                }
1447                else if (mShowWeightedRays) // relative number of rays
1448                {
1449                        float relativeArea = vc->GetBox().SurfaceArea() / mViewCellsManager->GetViewSpaceBox().SurfaceArea();
[1252]1450
[2576]1451                        if (relativeArea < Limits::Small)
1452                                relativeArea = Limits::Small;
1453
1454                        importance = mTransferFunction * ((float)vc->GetNumPiercingRays() / relativeArea) / maxRelativeRays;
1455                }
1456                else if (mShowPvsSizes) // pvs size
1457                {
1458                        importance = mTransferFunction *
1459                                ((float)vc->GetPvs().GetSize() / (float)maxPvs);
1460                } // weighted render cost
1461                else if (mShowWeightedCost)
1462                {
1463                        const float rcCost = mTransferFunction * ComputeRenderCost(vc);
1464                        importance = rcCost / maxRcCost;
1465                }
1466
1467                // c = RgbColor(importance, 1.0f - importance, 0.0f);
1468                c = RainbowColorMapping(importance);
1469
1470                glColor4f(c.r, c.g, c.b, 1.0f - mTransparency);
1471
1472                if (!mHideByCost || (mHidingCost < importance))
1473                {
1474                        RenderViewCell(vc);
1475                }
1476        }
[1252]1477}
1478
1479
[2576]1480void QtGlRendererWidget::AssignColorByComparison(const ViewCellContainer &viewcells,
[2580]1481                                                                                                 //const ViewCellInfoContainer &infos1,
1482                                                                                                 const ViewCellInfoContainer &compareInfo)
[1252]1483{
[2584]1484        //cout << "comparing " << viewcells.size() << " view cells to " << compareInfo.size() << " values" << endl;
[2543]1485
[2584]1486        if (viewcells.size() > compareInfo.size())
1487        {
1488                cerr << "loaded size (" << compareInfo.size() << ") does not fit to view cells size (" << viewcells.size() << ")" << endl;
1489                return;
1490        }
[2576]1491
[2584]1492        //const float maxRatio = 1.0f;
1493        const float maxRatio = 2.0f;
1494        const float minRatio = 0.0f;
1495
[2576]1496        const float scale = 1.0f / (maxRatio - minRatio);
1497
1498        for (size_t i = 0; i < viewcells.size(); ++ i)
1499        {
1500                RgbColor c;
1501                ViewCell *vc = viewcells[i];
1502
[2580]1503                //ViewCellInfo vc1Info = infos1[i];
1504                ViewCellInfo vc2Info = compareInfo[i];
[2576]1505
[2584]1506                //const float vcRatio = vc->GetNumPiercingRays() / vc2Info.mPiercingRays + Limits::Small;
1507                float vcRatio = 1.0f;//maxRatio;
1508               
1509                if (vc2Info.mPvsSize > Limits::Small)
1510                        vcRatio = (float)vc->GetPvs().GetSize() / vc2Info.mPvsSize;
[2576]1511
[2584]1512                // truncate here
1513                if (vcRatio > maxRatio) vcRatio = 1.0f;
1514
[2576]1515                const float importance = (vcRatio - minRatio) * scale;
[2584]1516       
1517                if (i < 20)
1518                {
1519                        cout << "pvs1: " << vc->GetPvs().GetSize() << " pvs2: " << compareInfo[i].mPvsSize << " importance: " << importance << endl;
1520                }
[2576]1521
1522                // c = RgbColor(importance, 1.0f - importance, 0.0f);
1523                c = RainbowColorMapping(importance);
1524
1525                glColor4f(c.r, c.g, c.b, 1.0f);
1526
1527                if (1)//!mHideByCost || (mHidingCost < importance))
1528                {
1529                        RenderViewCell(vc);
1530                }
1531        }
[1252]1532}
1533
1534
[2576]1535
1536/**********************************************************************/
1537/*              QtRendererControlWidget implementation                */
1538/**********************************************************************/
1539
[2580]1540
[2569]1541QGroupBox *QtRendererControlWidget::CreateVisualizationPanel(QWidget *parent)
1542{
1543        QRadioButton *rb1, *rb2, *rb3, *rb4, *rb5;
1544       
1545        // Create a check box to be in the group box
1546        rb1 = new QRadioButton("piercing rays", parent);
1547        rb1->setText("piercing rays");
1548        //vl->addWidget(rb1);
1549        connect(rb1, SIGNAL(toggled(bool)), SIGNAL(SetShowPiercingRays(bool)));
1550
1551        rb2 = new QRadioButton("pvs size", parent);
1552        rb2->setText("pvs size");
1553        connect(rb2, SIGNAL(toggled(bool)), SIGNAL(SetShowPvsSizes(bool)));
1554
1555        rb3 = new QRadioButton("wireframe", parent);
1556        rb3->setText("wireframe");
1557       
1558        rb4 = new QRadioButton("weighted rays", parent);
1559        rb4->setText("weighted rays");
1560        connect(rb4, SIGNAL(toggled(bool)), SIGNAL(SetShowWeightedRays(bool)));
1561       
1562        rb5 = new QRadioButton("pvs cost", parent);
1563        rb5->setText("pvs cost");
1564        connect(rb5, SIGNAL(toggled(bool)), SIGNAL(SetShowWeightedCost(bool)));
1565
1566        QGroupBox *groupBox = new QGroupBox("Visualization options");
1567
1568        QVBoxLayout *vbox2 = new QVBoxLayout;
1569   
1570        vbox2->addWidget(rb3);
1571        vbox2->addWidget(rb1);
1572        vbox2->addWidget(rb2);
1573        vbox2->addWidget(rb4);
1574        vbox2->addWidget(rb5);
1575       
1576        rb3->setChecked(true);
1577
1578        vbox2->addStretch(1);
1579        groupBox->setLayout(vbox2);
1580
1581        return groupBox;
1582}
1583
1584
1585
1586QGroupBox *QtRendererControlWidget::CreateRenderCostPanel(QWidget *parent)
1587{
1588        QRadioButton *rb1, *rb2, *rb3;
1589       
1590        // Create a check box to be in the group box
1591        rb1 = new QRadioButton("triangles", parent);
1592        rb1->setText("triangles");
1593        //vl->addWidget(rb1);
1594        connect(rb1, SIGNAL(toggled(bool)), SIGNAL(SetShowWeightedTriangles(bool)));
1595
1596        rb2 = new QRadioButton("distance weighted pvs", parent);
1597        rb2->setText("distance weighted");
1598        connect(rb2, SIGNAL(toggled(bool)), SIGNAL(SetShowDistanceWeightedPvs(bool)));
1599
1600        rb3 = new QRadioButton("distance weighted triangles", parent);
1601        rb3->setText("distance weighted triangles");
1602        connect(rb3, SIGNAL(toggled(bool)), SIGNAL(SetShowDistanceWeightedTriangles(bool)));
1603
1604        QGroupBox *groupBox = new QGroupBox("Render cost options");
1605
1606        QVBoxLayout *vbox2 = new QVBoxLayout;
1607   
1608        vbox2->addWidget(rb1);
1609        vbox2->addWidget(rb2);
1610        vbox2->addWidget(rb3);
1611       
1612        rb2->setChecked(true);
1613
1614        vbox2->addStretch(1);
1615        groupBox->setLayout(vbox2);
1616
1617        return groupBox;
1618}
1619
[2576]1620
[1252]1621QtRendererControlWidget::QtRendererControlWidget(QWidget * parent, Qt::WFlags f):
[2543]1622QWidget(parent, f)
[1252]1623{
1624
[2543]1625        QVBoxLayout *vl = new QVBoxLayout;
1626        setLayout(vl);
[1252]1627
[2543]1628        QWidget *vbox = new QGroupBox("ViewCells", this);
1629        layout()->addWidget(vbox);
[1252]1630
[2543]1631        vl = new QVBoxLayout;
1632        vbox->setLayout(vl);
[1252]1633
[2543]1634        QLabel *label = new QLabel("Granularity");
1635        vbox->layout()->addWidget(label);
[1942]1636
[2543]1637        QSlider *slider = new QSlider(Qt::Horizontal, vbox);
1638        vl->addWidget(slider);
1639        slider->show();
1640        slider->setRange(1, 10000);
1641        slider->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
1642        slider->setValue(200);
[1942]1643
[2543]1644        connect(slider, SIGNAL(valueChanged(int)), SIGNAL(SetViewCellGranularity(int)));
[1252]1645
[2563]1646        ///////////////////////////
1647
[2543]1648        label = new QLabel("Transfer function");
1649        vbox->layout()->addWidget(label);
[1252]1650
[2543]1651        slider = new QSlider(Qt::Horizontal, vbox);
1652        vl->addWidget(slider);
1653        slider->show();
1654        slider->setRange(1, 10000);
1655        slider->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
1656        slider->setValue(100);
[1252]1657
1658
[2543]1659        connect(slider, SIGNAL(valueChanged(int)), SIGNAL(SetTransferFunction(int)));
[1252]1660
[2563]1661        ////////////////////////////////////////7
[1252]1662
1663
[2563]1664        QPushButton *button = new QPushButton("Update all PVSs", vbox);
1665        vbox->layout()->addWidget(button);
1666        connect(button, SIGNAL(clicked()), SLOT(UpdateAllPvs()));
1667
1668
1669        ////////////////////////////////////////77777
1670
[2543]1671        label = new QLabel("Filter size");
1672        vbox->layout()->addWidget(label);
[1942]1673
[2543]1674        slider = new QSlider(Qt::Horizontal, vbox);
1675        vbox->layout()->addWidget(slider);
1676        slider->show();
1677        slider->setRange(1, 100);
1678        slider->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
1679        slider->setValue(3);
[1252]1680
[2543]1681        connect(slider, SIGNAL(valueChanged(int)), SIGNAL(SetVisibilityFilterSize(int)));
[1252]1682
1683
[2563]1684        //////////////////////////////////////////
1685
[2543]1686        label = new QLabel("Spatial Filter size");
1687        vbox->layout()->addWidget(label);
[1252]1688
[2543]1689        slider = new QSlider(Qt::Horizontal, vbox);
1690        vbox->layout()->addWidget(slider);
1691        slider->show();
1692        slider->setRange(1, 100);
1693        slider->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
1694        slider->setValue(10);
[1942]1695
[2543]1696        connect(slider, SIGNAL(valueChanged(int)), SIGNAL(SetSpatialFilterSize(int)));
[1942]1697
[1252]1698
[2563]1699        ///////////////////////////////////
[1252]1700
[2563]1701
[2543]1702        QWidget *hbox = new QWidget(vbox);
1703        vl->addWidget(hbox);
1704        QHBoxLayout *hlayout = new QHBoxLayout;
1705        hbox->setLayout(hlayout);
[1942]1706
[2543]1707        QCheckBox *cb = new QCheckBox("Show viewcells", hbox);
1708        hlayout->addWidget(cb);
1709        cb->setChecked(false);
1710        connect(cb, SIGNAL(toggled(bool)), SIGNAL(SetShowViewCells(bool)));
[1942]1711
[2543]1712        cb = new QCheckBox("Render cost curve", hbox);
1713        hlayout->addWidget(cb);
1714        cb->setChecked(false);
1715        connect(cb, SIGNAL(toggled(bool)), SIGNAL(SetShowRenderCost(bool)));
[1942]1716
[2543]1717        cb = new QCheckBox("Show rays", hbox);
1718        hlayout->addWidget(cb);
1719        cb->setChecked(false);
1720        connect(cb, SIGNAL(toggled(bool)), SIGNAL(SetShowRays(bool)));
[2580]1721       
1722        cb = new QCheckBox("Show Comparison", hbox);
1723        hlayout->addWidget(cb);
1724        cb->setChecked(false);
1725        connect(cb, SIGNAL(toggled(bool)), SIGNAL(SetShowComparison(bool)));
[1252]1726
[1942]1727
[2569]1728        //////////////////
[2563]1729
[2569]1730        QHBoxLayout *vh = new QHBoxLayout;
[2564]1731
[2569]1732        QGroupBox *myBox = new QGroupBox("Visualization");
[2563]1733
[2569]1734        myBox->setLayout(vh);
1735        vl->addWidget(myBox, 0, 0);
[2566]1736
[2569]1737        QGroupBox *groupBox = CreateVisualizationPanel(hbox);
1738        vh->addWidget(groupBox, 0, 0);
[2565]1739
[2569]1740        QGroupBox *groupBox2 = CreateRenderCostPanel(hbox);
1741        vh->addWidget(groupBox2, 0, 0);
[2565]1742
[2563]1743        //////////////////////////////////
1744
1745
1746        /*cb = new QRadiobox("Top View", vbox);
1747        vbox->layout()->addWidget(cb);
1748        cb->setChecked(false);
1749        connect(cb, SIGNAL(toggled(bool)), SIGNAL(SetTopView(bool)));
1750*/
1751
[2560]1752        vbox->resize(800,150);
[1252]1753
[2560]1754
[2543]1755        vbox = new QGroupBox("Rendering", this);
1756        layout()->addWidget(vbox);
[1252]1757
[2543]1758        vl = new QVBoxLayout;
1759        vbox->setLayout(vl);
[1252]1760
1761
1762
[2543]1763        cb = new QCheckBox("Cut view cells", vbox);
1764        vbox->layout()->addWidget(cb);
1765        cb->setChecked(false);
1766        connect(cb, SIGNAL(toggled(bool)), SIGNAL(SetCutViewCells(bool)));
[1252]1767
[1942]1768
[2543]1769        slider = new QSlider(Qt::Horizontal, vbox);
1770        vbox->layout()->addWidget(slider);
1771        slider->show();
1772        slider->setRange(0, 1000);
1773        slider->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
1774        slider->setValue(1000);
[1942]1775
[2543]1776        connect(slider, SIGNAL(valueChanged(int)), SIGNAL(SetSceneCut(int)));
[1252]1777
[2563]1778       
1779        cb = new QCheckBox("Hide view cells by render cost ", vbox);
1780        vbox->layout()->addWidget(cb);
1781        cb->setChecked(false);
1782        connect(cb, SIGNAL(toggled(bool)), SIGNAL(SetHideByCost(bool)));
[1252]1783
[2563]1784        ////////////////////////////
1785
1786        const int range = 1000;
1787
1788        label = new QLabel("Hide by cost");
1789        vbox->layout()->addWidget(label);
1790
1791        // the render cost visualization
1792        slider = new QSlider(Qt::Horizontal, vbox);
1793        vbox->layout()->addWidget(slider);
1794        slider->show();
1795        slider->setRange(0, range);
1796        slider->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
1797        slider->setValue(0);
1798        connect(slider, SIGNAL(valueChanged(int)), SIGNAL(SetHidingCost(int)));
1799
1800        ///////////////////////////////////////////
1801
1802        cb = new QCheckBox("Top View", vbox);
1803        vbox->layout()->addWidget(cb);
1804        cb->setChecked(false);
1805        connect(cb, SIGNAL(toggled(bool)), SIGNAL(SetTopView(bool)));
1806
1807
1808        label = new QLabel("Top distance");
1809        vbox->layout()->addWidget(label);
1810       
1811        slider = new QSlider(Qt::Horizontal, vbox);
1812        vbox->layout()->addWidget(slider);
1813        slider->show();
1814        slider->setRange(1, 1000);
1815        slider->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
1816        slider->setValue(500);
1817
1818        connect(slider, SIGNAL(valueChanged(int)), SIGNAL(SetTopDistance(int)));
1819       
1820
1821        ///////////////////////////////////////////
1822
1823        cb = new QCheckBox("Transparency", vbox);
1824        vbox->layout()->addWidget(cb);
1825        cb->setChecked(false);
[2564]1826        connect(cb, SIGNAL(toggled(bool)), SIGNAL(SetUseTransparency(bool)));
[2563]1827
1828        label = new QLabel("Use transparency");
1829        vbox->layout()->addWidget(label);
1830
1831        // the render cost visualization
1832        slider = new QSlider(Qt::Horizontal, vbox);
1833        vbox->layout()->addWidget(slider);
1834        slider->show();
1835        slider->setRange(0, range);
1836        slider->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
1837        slider->setValue(0);
1838
1839        connect(slider, SIGNAL(valueChanged(int)), SIGNAL(SetTransparency(int)));
1840
1841        //////////////////////////////
1842
1843
[2543]1844        cb = new QCheckBox("Cut scene", vbox);
1845        vbox->layout()->addWidget(cb);
1846        cb->setChecked(false);
1847        connect(cb, SIGNAL(toggled(bool)), SIGNAL(SetCutScene(bool)));
[1942]1848
[2543]1849        cb = new QCheckBox("Render boxes", vbox);
1850        vbox->layout()->addWidget(cb);
1851        cb->setChecked(false);
1852        connect(cb, SIGNAL(toggled(bool)), SIGNAL(SetRenderBoxes(bool)));
[1942]1853
[2543]1854        cb = new QCheckBox("Render visibility estimates", vbox);
1855        vbox->layout()->addWidget(cb);
1856        cb->setChecked(false);
1857        connect(cb, SIGNAL(toggled(bool)), SIGNAL(SetRenderVisibilityEstimates(bool)));
[1942]1858
[2543]1859
1860        cb = new QCheckBox("Render errors", vbox);
1861        vbox->layout()->addWidget(cb);
1862        cb->setChecked(false);
1863        connect(cb, SIGNAL(toggled(bool)), SIGNAL(SetRenderErrors(bool)));
1864
1865
1866        bool tmp;
1867
1868        cb = new QCheckBox("Use filter", vbox);
1869        vbox->layout()->addWidget(cb);
1870        Environment::GetSingleton()->GetBoolValue("Preprocessor.applyVisibilityFilter", tmp );
1871        cb->setChecked(tmp);
1872        connect(cb, SIGNAL(toggled(bool)), SIGNAL(SetUseFilter(bool)));
1873
1874
1875        cb = new QCheckBox("Use spatial filter", vbox);
1876        vbox->layout()->addWidget(cb);
1877        Environment::GetSingleton()->GetBoolValue("Preprocessor.applyVisibilitySpatialFilter",
1878                tmp );
1879        cb->setChecked(tmp);
1880        connect(cb, SIGNAL(toggled(bool)), SIGNAL(SetUseSpatialFilter(bool)));
1881
1882        cb = new QCheckBox("Render filter", vbox);
1883        vbox->layout()->addWidget(cb);
1884        cb->setChecked(true);
1885        connect(cb, SIGNAL(toggled(bool)), SIGNAL(SetRenderFilter(bool)));
1886
1887
[2584]1888        /*vbox = new QGroupBox("PVS Errors", this);
[1942]1889        layout()->addWidget(vbox);
[2543]1890
[1942]1891        vl = new QVBoxLayout;
1892        vbox->setLayout(vl);
[2543]1893
[2563]1894        button = new QPushButton("Compute Visibility", vbox);
[2543]1895        vbox->layout()->addWidget(button);
1896        connect(button, SIGNAL(clicked()), SLOT(ComputeVisibility()));
1897
1898        button = new QPushButton("Stop Computation", vbox);
1899        vbox->layout()->addWidget(button);
1900        connect(button, SIGNAL(clicked()), SLOT(StopComputation()));
1901
1902        button = new QPushButton("Set Random View Point", vbox);
1903        vbox->layout()->addWidget(button);
[2584]1904        connect(button, SIGNAL(clicked()), SLOT(SetRandomViewPoint()));*/
[2543]1905
[2584]1906        button = new QPushButton("Store statistics", vbox);
1907        vbox->layout()->addWidget(button);
1908        connect(button, SIGNAL(clicked()), SIGNAL(StoreStatistics()));
[2543]1909
[2584]1910        /*cb = new QCheckBox("Stats", vbox);
1911        vbox->layout()->addWidget(cb);
1912        cb->setChecked(false);
1913        connect(cb, SIGNAL(toggled(bool)), SIGNAL(StoreStatistics()));
1914*/
[2569]1915        if (0)
1916        {
[2543]1917                vbox = new QGroupBox("PVS Errors", this);
1918                layout()->addWidget(vbox);
1919
1920                vl = new QVBoxLayout;
1921                vbox->setLayout(vl);
1922
1923                mPvsErrorWidget = new QListWidget(vbox);
1924                vbox->layout()->addWidget(mPvsErrorWidget);
1925
1926                connect(mPvsErrorWidget,
[1942]1927                        SIGNAL(doubleClicked(const QModelIndex &)),
1928                        this,
1929                        SLOT(PvsErrorClicked(const QModelIndex &)));
[2543]1930
[2563]1931                button = new QPushButton("Next Error Frame", vbox);
[2543]1932                vbox->layout()->addWidget(button);
1933                connect(button, SIGNAL(clicked(void)), SLOT(FocusNextPvsErrorFrame(void)));
1934        }
[2562]1935       
[2584]1936        //connect(button, SIGNAL(clicked(void)), SLOT(StoreStatistics(void)));
[2563]1937        //mHidingCost = 0.1f;
[2562]1938
[2543]1939        setWindowTitle("Preprocessor Control Widget");
1940        adjustSize();
[1252]1941}
1942
1943
[1942]1944
1945
[1252]1946void
1947QtRendererControlWidget::FocusNextPvsErrorFrame(void)
[2576]1948{}
[2543]1949
[1252]1950void
1951QtRendererControlWidget::UpdatePvsErrorItem(int row,
[2543]1952                                                                                        GlRendererBuffer::PvsErrorEntry &pvsErrorEntry)
[1252]1953{
1954
[2543]1955        QListWidgetItem *i = mPvsErrorWidget->item(row);
1956        QString s;
1957        s.sprintf("%5.5f", pvsErrorEntry.mError);
1958        if (i) {
1959                i->setText(s);
1960        } else {
1961                new QListWidgetItem(s, mPvsErrorWidget);
1962        }
1963        mPvsErrorWidget->update();
[1252]1964}
1965
1966
[2576]1967
1968
1969/*********************************************************************/
1970/*                   QtGlDebuggerWidget implementation               */
1971/*********************************************************************/
1972
1973
1974QtGlDebuggerWidget::QtGlDebuggerWidget(QtGlRendererBuffer *buf, QWidget *parent)
1975: QGLWidget(QGLFormat(QGL::SampleBuffers), parent), mRenderBuffer(buf)
1976{
1977        // create the pbuffer
1978        //pbuffer = new QGLPixelBuffer(QSize(512, 512), format(), this);
1979        timerId = startTimer(20);
1980        setWindowTitle(("OpenGL pbuffers"));
1981}
1982
1983
1984QtGlDebuggerWidget::~QtGlDebuggerWidget()
1985{
1986        mRenderBuffer->releaseFromDynamicTexture();
1987        glDeleteTextures(1, &dynamicTexture);
1988
1989        DEL_PTR(mRenderBuffer);
1990}
1991
1992
[1252]1993void QtGlDebuggerWidget::initializeGL()
1994{
1995        glMatrixMode(GL_PROJECTION);
1996        glLoadIdentity();
1997
1998        glFrustum(-1, 1, -1, 1, 10, 100);
1999        glTranslatef(-0.5f, -0.5f, -0.5f);
2000        glTranslatef(0.0f, 0.0f, -15.0f);
2001        glMatrixMode(GL_MODELVIEW);
2002
2003        glEnable(GL_CULL_FACE);
2004        initCommon();
2005        initPbuffer();
2006
2007}
2008
2009
2010void QtGlDebuggerWidget::resizeGL(int w, int h)
2011{
2012        glViewport(0, 0, w, h);
2013}
2014
2015
2016void QtGlDebuggerWidget::paintGL()
2017{
2018        // draw a spinning cube into the pbuffer..
2019        mRenderBuffer->makeCurrent();
[2543]2020
[1252]2021        BeamSampleStatistics stats;
2022        mRenderBuffer->SampleBeamContributions(mSourceObject, mBeam, mSamples, stats);
2023
2024        glFlush();
2025
2026        // rendering directly to a texture is not supported on X11, unfortunately
[2543]2027        mRenderBuffer->updateDynamicTexture(dynamicTexture);
[1252]2028
[2543]2029        // and use the pbuffer contents as a texture when rendering the
2030        // background and the bouncing cubes
2031        makeCurrent();
2032        glBindTexture(GL_TEXTURE_2D, dynamicTexture);
2033        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
[1252]2034
[2543]2035        // draw the background
[1252]2036        glMatrixMode(GL_MODELVIEW);
[2543]2037        glPushMatrix();
2038        glLoadIdentity();
2039        glMatrixMode(GL_PROJECTION);
2040        glPushMatrix();
2041        glLoadIdentity();
2042
[1252]2043        glPopMatrix();
[2543]2044        glMatrixMode(GL_MODELVIEW);
2045        glPopMatrix();
[1252]2046}
2047
2048
2049void QtGlDebuggerWidget::initPbuffer()
2050{
2051        // set up the pbuffer context
[2543]2052        mRenderBuffer->makeCurrent();
[1252]2053        /*mRenderBuffer->InitGL();
2054
2055        glViewport(0, 0, mRenderBuffer->size().width(), mRenderBuffer->size().height());
2056        glMatrixMode(GL_PROJECTION);
2057        glLoadIdentity();
2058        glOrtho(-1, 1, -1, 1, -99, 99);
2059        glTranslatef(-0.5f, -0.5f, 0.0f);
2060        glMatrixMode(GL_MODELVIEW);
2061        glLoadIdentity();
2062
2063        glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);*/
[2543]2064
[1252]2065        // generate a texture that has the same size/format as the pbuffer
[2543]2066        dynamicTexture = mRenderBuffer->generateDynamicTexture();
[1252]2067
2068        // bind the dynamic texture to the pbuffer - this is a no-op under X11
2069        mRenderBuffer->bindToDynamicTexture(dynamicTexture);
2070        makeCurrent();
2071}
2072
[2576]2073
[1252]2074void QtGlDebuggerWidget::initCommon()
2075{
2076        glEnable(GL_TEXTURE_2D);
2077        glEnable(GL_DEPTH_TEST);
2078
2079        glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
2080}
2081
2082
[2576]2083}
Note: See TracBrowser for help on using the repository browser.