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

Revision 2567, 44.1 KB checked in by mattausch, 17 years ago (diff)

worked on visualization

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 if (mShowPvsSizes) // 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                        } // weighted render cost
1307                /*      else
1308                        {
1309                                ObjectPvs basePvs = viewCell->GetPvs();
1310                                ObjectPvsIterator pit = basePvs.GetIterator();
1311
1312                                // first mark all objects from this pvs
1313                                while (pit.HasMoreEntries())   
1314                                {
1315                                        if (pit.Next()->Mail();
1316                                }
1317                        }*/
1318
1319                        // c = RgbColor(importance, 1.0f - importance, 0.0f);
1320                        c = RainbowColorMapping(5.0f * importance);
1321
1322                        glColor4f(c.r, c.g, c.b, 1.0f - mTransparency);
1323
1324                        if (!mHideByCost || (mHidingCost < importance))
1325                        {
1326                                RenderViewCell(vc);
1327                        }
1328                }
1329
1330                glEnable(GL_DEPTH_TEST);       
1331        }
1332
1333        mUseFalseColors = false;
1334        mWireFrame = false;
1335
1336        glPopAttrib();
1337}
1338
1339
1340
1341/*********************************************************************/
1342/*                   QtGlDebuggerWidget implementation               */
1343/*********************************************************************/
1344
1345
1346QtGlDebuggerWidget::QtGlDebuggerWidget(QtGlRendererBuffer *buf, QWidget *parent)
1347: QGLWidget(QGLFormat(QGL::SampleBuffers), parent), mRenderBuffer(buf)
1348{
1349        // create the pbuffer
1350        //pbuffer = new QGLPixelBuffer(QSize(512, 512), format(), this);
1351        timerId = startTimer(20);
1352        setWindowTitle(("OpenGL pbuffers"));
1353}
1354
1355
1356QtGlDebuggerWidget::~QtGlDebuggerWidget()
1357{
1358        mRenderBuffer->releaseFromDynamicTexture();
1359        glDeleteTextures(1, &dynamicTexture);
1360
1361        DEL_PTR(mRenderBuffer);
1362}
1363
1364
1365QtRendererControlWidget::QtRendererControlWidget(QWidget * parent, Qt::WFlags f):
1366QWidget(parent, f)
1367{
1368
1369        QVBoxLayout *vl = new QVBoxLayout;
1370        setLayout(vl);
1371
1372        QWidget *vbox = new QGroupBox("ViewCells", this);
1373        layout()->addWidget(vbox);
1374
1375        vl = new QVBoxLayout;
1376        vbox->setLayout(vl);
1377
1378        QLabel *label = new QLabel("Granularity");
1379        vbox->layout()->addWidget(label);
1380
1381        QSlider *slider = new QSlider(Qt::Horizontal, vbox);
1382        vl->addWidget(slider);
1383        slider->show();
1384        slider->setRange(1, 10000);
1385        slider->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
1386        slider->setValue(200);
1387
1388        connect(slider, SIGNAL(valueChanged(int)), SIGNAL(SetViewCellGranularity(int)));
1389
1390        ///////////////////////////
1391
1392        label = new QLabel("Transfer function");
1393        vbox->layout()->addWidget(label);
1394
1395        slider = new QSlider(Qt::Horizontal, vbox);
1396        vl->addWidget(slider);
1397        slider->show();
1398        slider->setRange(1, 10000);
1399        slider->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
1400        slider->setValue(100);
1401
1402
1403        connect(slider, SIGNAL(valueChanged(int)), SIGNAL(SetTransferFunction(int)));
1404
1405        ////////////////////////////////////////7
1406
1407
1408        QPushButton *button = new QPushButton("Update all PVSs", vbox);
1409        vbox->layout()->addWidget(button);
1410        connect(button, SIGNAL(clicked()), SLOT(UpdateAllPvs()));
1411
1412
1413        ////////////////////////////////////////77777
1414
1415        label = new QLabel("Filter size");
1416        vbox->layout()->addWidget(label);
1417
1418        slider = new QSlider(Qt::Horizontal, vbox);
1419        vbox->layout()->addWidget(slider);
1420        slider->show();
1421        slider->setRange(1, 100);
1422        slider->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
1423        slider->setValue(3);
1424
1425        connect(slider, SIGNAL(valueChanged(int)), SIGNAL(SetVisibilityFilterSize(int)));
1426
1427
1428        //////////////////////////////////////////
1429
1430        label = new QLabel("Spatial Filter size");
1431        vbox->layout()->addWidget(label);
1432
1433        slider = new QSlider(Qt::Horizontal, vbox);
1434        vbox->layout()->addWidget(slider);
1435        slider->show();
1436        slider->setRange(1, 100);
1437        slider->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
1438        slider->setValue(10);
1439
1440        connect(slider, SIGNAL(valueChanged(int)), SIGNAL(SetSpatialFilterSize(int)));
1441
1442
1443        ///////////////////////////////////
1444
1445
1446        QWidget *hbox = new QWidget(vbox);
1447        vl->addWidget(hbox);
1448        QHBoxLayout *hlayout = new QHBoxLayout;
1449        hbox->setLayout(hlayout);
1450
1451        QCheckBox *cb = new QCheckBox("Show viewcells", hbox);
1452        hlayout->addWidget(cb);
1453        cb->setChecked(false);
1454        connect(cb, SIGNAL(toggled(bool)), SIGNAL(SetShowViewCells(bool)));
1455
1456        cb = new QCheckBox("Render cost curve", hbox);
1457        hlayout->addWidget(cb);
1458        cb->setChecked(false);
1459        connect(cb, SIGNAL(toggled(bool)), SIGNAL(SetShowRenderCost(bool)));
1460
1461        /*cb = new QCheckBox("Show render cost", hbox);
1462        hlayout->addWidget(cb);
1463        cb->setChecked(false);
1464        connect(cb, SIGNAL(toggled(bool)), SIGNAL(SetShowPvsSizes(bool)));
1465        */
1466
1467        cb = new QCheckBox("Show rays", hbox);
1468        hlayout->addWidget(cb);
1469        cb->setChecked(false);
1470        connect(cb, SIGNAL(toggled(bool)), SIGNAL(SetShowRays(bool)));
1471
1472        /*cb = new QCheckBox("Show piercing rays", hbox);
1473        hlayout->addWidget(cb);
1474        cb->setChecked(false);
1475        connect(cb, SIGNAL(toggled(bool)), SIGNAL(SetShowPiercingRays(bool)));
1476        */
1477
1478
1479        QRadioButton *rb1, *rb2, *rb3, *rb4;
1480
1481        // Create a check box to be in the group box
1482       
1483        rb1 = new QRadioButton("piercing rays", hbox);
1484        rb1->setText("piercing rays");
1485        //vl->addWidget(rb1);
1486        connect(rb1, SIGNAL(toggled(bool)), SIGNAL(SetShowPiercingRays(bool)));
1487
1488        rb2 = new QRadioButton("render cost", hbox);
1489        rb2->setText("render cost");
1490        //vl->addWidget(rb2);
1491        connect(rb2, SIGNAL(toggled(bool)), SIGNAL(SetShowPvsSizes(bool)));
1492
1493        rb3 = new QRadioButton("wireframe", hbox);
1494        rb3->setText("wireframe");
1495       
1496        rb4 = new QRadioButton("weighted rays", hbox);
1497        rb4->setText("weighted rays");
1498        connect(rb4, SIGNAL(toggled(bool)), SIGNAL(SetShowWeightedRays(bool)));
1499
1500        //vl->addWidget(rb2);
1501        //connect(rb2, SIGNAL(toggled(bool)), SIGNAL(SetShowPvsSizes(bool)));
1502
1503        QGroupBox *groupBox = new QGroupBox("Visualization options");
1504
1505        QVBoxLayout *vbox2 = new QVBoxLayout;
1506   
1507        vbox2->addWidget(rb3);
1508        vbox2->addWidget(rb1);
1509        vbox2->addWidget(rb2);
1510        vbox2->addWidget(rb4);
1511        rb3->setChecked(true);
1512
1513        vbox2->addStretch(1);
1514        groupBox->setLayout(vbox2);
1515
1516        vl->addWidget(groupBox, 0, 0);
1517
1518        //////////////////////////////////
1519
1520
1521        /*cb = new QRadiobox("Top View", vbox);
1522        vbox->layout()->addWidget(cb);
1523        cb->setChecked(false);
1524        connect(cb, SIGNAL(toggled(bool)), SIGNAL(SetTopView(bool)));
1525*/
1526
1527        vbox->resize(800,150);
1528
1529
1530        vbox = new QGroupBox("Rendering", this);
1531        layout()->addWidget(vbox);
1532
1533        vl = new QVBoxLayout;
1534        vbox->setLayout(vl);
1535
1536
1537
1538        cb = new QCheckBox("Cut view cells", vbox);
1539        vbox->layout()->addWidget(cb);
1540        cb->setChecked(false);
1541        connect(cb, SIGNAL(toggled(bool)), SIGNAL(SetCutViewCells(bool)));
1542
1543
1544        slider = new QSlider(Qt::Horizontal, vbox);
1545        vbox->layout()->addWidget(slider);
1546        slider->show();
1547        slider->setRange(0, 1000);
1548        slider->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
1549        slider->setValue(1000);
1550
1551        connect(slider, SIGNAL(valueChanged(int)), SIGNAL(SetSceneCut(int)));
1552
1553       
1554        cb = new QCheckBox("Hide view cells by render cost ", vbox);
1555        vbox->layout()->addWidget(cb);
1556        cb->setChecked(false);
1557        connect(cb, SIGNAL(toggled(bool)), SIGNAL(SetHideByCost(bool)));
1558
1559        ////////////////////////////
1560
1561        const int range = 1000;
1562
1563        label = new QLabel("Hide by cost");
1564        vbox->layout()->addWidget(label);
1565
1566        // the render cost visualization
1567        slider = new QSlider(Qt::Horizontal, vbox);
1568        vbox->layout()->addWidget(slider);
1569        slider->show();
1570        slider->setRange(0, range);
1571        slider->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
1572        slider->setValue(0);
1573        connect(slider, SIGNAL(valueChanged(int)), SIGNAL(SetHidingCost(int)));
1574
1575        ///////////////////////////////////////////
1576
1577        cb = new QCheckBox("Top View", vbox);
1578        vbox->layout()->addWidget(cb);
1579        cb->setChecked(false);
1580        connect(cb, SIGNAL(toggled(bool)), SIGNAL(SetTopView(bool)));
1581
1582
1583        label = new QLabel("Top distance");
1584        vbox->layout()->addWidget(label);
1585       
1586        slider = new QSlider(Qt::Horizontal, vbox);
1587        vbox->layout()->addWidget(slider);
1588        slider->show();
1589        slider->setRange(1, 1000);
1590        slider->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
1591        slider->setValue(500);
1592
1593        connect(slider, SIGNAL(valueChanged(int)), SIGNAL(SetTopDistance(int)));
1594       
1595
1596        ///////////////////////////////////////////
1597
1598        cb = new QCheckBox("Transparency", vbox);
1599        vbox->layout()->addWidget(cb);
1600        cb->setChecked(false);
1601        connect(cb, SIGNAL(toggled(bool)), SIGNAL(SetUseTransparency(bool)));
1602
1603        label = new QLabel("Use transparency");
1604        vbox->layout()->addWidget(label);
1605
1606        // the render cost visualization
1607        slider = new QSlider(Qt::Horizontal, vbox);
1608        vbox->layout()->addWidget(slider);
1609        slider->show();
1610        slider->setRange(0, range);
1611        slider->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
1612        slider->setValue(0);
1613
1614        connect(slider, SIGNAL(valueChanged(int)), SIGNAL(SetTransparency(int)));
1615
1616        //////////////////////////////
1617
1618
1619        cb = new QCheckBox("Cut scene", vbox);
1620        vbox->layout()->addWidget(cb);
1621        cb->setChecked(false);
1622        connect(cb, SIGNAL(toggled(bool)), SIGNAL(SetCutScene(bool)));
1623
1624        cb = new QCheckBox("Render boxes", vbox);
1625        vbox->layout()->addWidget(cb);
1626        cb->setChecked(false);
1627        connect(cb, SIGNAL(toggled(bool)), SIGNAL(SetRenderBoxes(bool)));
1628
1629        cb = new QCheckBox("Render visibility estimates", vbox);
1630        vbox->layout()->addWidget(cb);
1631        cb->setChecked(false);
1632        connect(cb, SIGNAL(toggled(bool)), SIGNAL(SetRenderVisibilityEstimates(bool)));
1633
1634
1635        cb = new QCheckBox("Render errors", vbox);
1636        vbox->layout()->addWidget(cb);
1637        cb->setChecked(false);
1638        connect(cb, SIGNAL(toggled(bool)), SIGNAL(SetRenderErrors(bool)));
1639
1640
1641        bool tmp;
1642
1643        cb = new QCheckBox("Use filter", vbox);
1644        vbox->layout()->addWidget(cb);
1645        Environment::GetSingleton()->GetBoolValue("Preprocessor.applyVisibilityFilter", tmp );
1646        cb->setChecked(tmp);
1647        connect(cb, SIGNAL(toggled(bool)), SIGNAL(SetUseFilter(bool)));
1648
1649
1650        cb = new QCheckBox("Use spatial filter", vbox);
1651        vbox->layout()->addWidget(cb);
1652        Environment::GetSingleton()->GetBoolValue("Preprocessor.applyVisibilitySpatialFilter",
1653                tmp );
1654        cb->setChecked(tmp);
1655        connect(cb, SIGNAL(toggled(bool)), SIGNAL(SetUseSpatialFilter(bool)));
1656
1657        cb = new QCheckBox("Render filter", vbox);
1658        vbox->layout()->addWidget(cb);
1659        cb->setChecked(true);
1660        connect(cb, SIGNAL(toggled(bool)), SIGNAL(SetRenderFilter(bool)));
1661
1662
1663        vbox = new QGroupBox("PVS Errors", this);
1664        layout()->addWidget(vbox);
1665
1666        vl = new QVBoxLayout;
1667        vbox->setLayout(vl);
1668
1669        button = new QPushButton("Compute Visibility", vbox);
1670        vbox->layout()->addWidget(button);
1671        connect(button, SIGNAL(clicked()), SLOT(ComputeVisibility()));
1672
1673        button = new QPushButton("Stop Computation", vbox);
1674        vbox->layout()->addWidget(button);
1675        connect(button, SIGNAL(clicked()), SLOT(StopComputation()));
1676
1677        button = new QPushButton("Set Random View Point", vbox);
1678        vbox->layout()->addWidget(button);
1679        connect(button, SIGNAL(clicked()), SLOT(SetRandomViewPoint()));
1680
1681
1682        if (0) {
1683                vbox = new QGroupBox("PVS Errors", this);
1684                layout()->addWidget(vbox);
1685
1686                vl = new QVBoxLayout;
1687                vbox->setLayout(vl);
1688
1689                mPvsErrorWidget = new QListWidget(vbox);
1690                vbox->layout()->addWidget(mPvsErrorWidget);
1691
1692                connect(mPvsErrorWidget,
1693                        SIGNAL(doubleClicked(const QModelIndex &)),
1694                        this,
1695                        SLOT(PvsErrorClicked(const QModelIndex &)));
1696
1697                button = new QPushButton("Next Error Frame", vbox);
1698                vbox->layout()->addWidget(button);
1699                connect(button, SIGNAL(clicked(void)), SLOT(FocusNextPvsErrorFrame(void)));
1700        }
1701
1702       
1703        //mHidingCost = 0.1f;
1704
1705        setWindowTitle("Preprocessor Control Widget");
1706        adjustSize();
1707}
1708
1709
1710
1711
1712void
1713QtRendererControlWidget::FocusNextPvsErrorFrame(void)
1714{
1715
1716
1717}
1718
1719void
1720QtRendererControlWidget::UpdatePvsErrorItem(int row,
1721                                                                                        GlRendererBuffer::PvsErrorEntry &pvsErrorEntry)
1722{
1723
1724        QListWidgetItem *i = mPvsErrorWidget->item(row);
1725        QString s;
1726        s.sprintf("%5.5f", pvsErrorEntry.mError);
1727        if (i) {
1728                i->setText(s);
1729        } else {
1730                new QListWidgetItem(s, mPvsErrorWidget);
1731        }
1732        mPvsErrorWidget->update();
1733}
1734
1735
1736void QtGlDebuggerWidget::initializeGL()
1737{
1738        glMatrixMode(GL_PROJECTION);
1739        glLoadIdentity();
1740
1741        glFrustum(-1, 1, -1, 1, 10, 100);
1742        glTranslatef(-0.5f, -0.5f, -0.5f);
1743        glTranslatef(0.0f, 0.0f, -15.0f);
1744        glMatrixMode(GL_MODELVIEW);
1745
1746        glEnable(GL_CULL_FACE);
1747        initCommon();
1748        initPbuffer();
1749
1750}
1751
1752
1753void QtGlDebuggerWidget::resizeGL(int w, int h)
1754{
1755        glViewport(0, 0, w, h);
1756}
1757
1758
1759void QtGlDebuggerWidget::paintGL()
1760{
1761        // draw a spinning cube into the pbuffer..
1762        mRenderBuffer->makeCurrent();
1763
1764        BeamSampleStatistics stats;
1765        mRenderBuffer->SampleBeamContributions(mSourceObject, mBeam, mSamples, stats);
1766
1767        glFlush();
1768
1769        // rendering directly to a texture is not supported on X11, unfortunately
1770        mRenderBuffer->updateDynamicTexture(dynamicTexture);
1771
1772        // and use the pbuffer contents as a texture when rendering the
1773        // background and the bouncing cubes
1774        makeCurrent();
1775        glBindTexture(GL_TEXTURE_2D, dynamicTexture);
1776        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
1777
1778        // draw the background
1779        glMatrixMode(GL_MODELVIEW);
1780        glPushMatrix();
1781        glLoadIdentity();
1782        glMatrixMode(GL_PROJECTION);
1783        glPushMatrix();
1784        glLoadIdentity();
1785
1786        glPopMatrix();
1787        glMatrixMode(GL_MODELVIEW);
1788        glPopMatrix();
1789}
1790
1791
1792void QtGlDebuggerWidget::initPbuffer()
1793{
1794        // set up the pbuffer context
1795        mRenderBuffer->makeCurrent();
1796        /*mRenderBuffer->InitGL();
1797
1798        glViewport(0, 0, mRenderBuffer->size().width(), mRenderBuffer->size().height());
1799        glMatrixMode(GL_PROJECTION);
1800        glLoadIdentity();
1801        glOrtho(-1, 1, -1, 1, -99, 99);
1802        glTranslatef(-0.5f, -0.5f, 0.0f);
1803        glMatrixMode(GL_MODELVIEW);
1804        glLoadIdentity();
1805
1806        glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);*/
1807
1808        // generate a texture that has the same size/format as the pbuffer
1809        dynamicTexture = mRenderBuffer->generateDynamicTexture();
1810
1811        // bind the dynamic texture to the pbuffer - this is a no-op under X11
1812        mRenderBuffer->bindToDynamicTexture(dynamicTexture);
1813        makeCurrent();
1814}
1815
1816void QtGlDebuggerWidget::initCommon()
1817{
1818        glEnable(GL_TEXTURE_2D);
1819        glEnable(GL_DEPTH_TEST);
1820
1821        glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
1822}
1823
1824
1825
1826}
Note: See TracBrowser for help on using the repository browser.