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

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