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

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