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

Revision 2564, 42.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        mSpatialFilterSize = 0.01;
868        mPvsSize = 0;
869        mRenderError = 0.0f;
870        mShowRays = false;
871
872        mControlWidget = new QtRendererControlWidget(NULL);
873
874        connect(mControlWidget, SIGNAL(SetViewCellGranularity(int)), this, SLOT(SetViewCellGranularity(int)));
875        connect(mControlWidget,
876                SIGNAL(SetTransferFunction(int)),
877                this,
878                SLOT(SetTransferFunction(int)));
879        connect(mControlWidget, SIGNAL(UpdateAllPvs()), this, SLOT(UpdateAllPvs()));
880        connect(mControlWidget, SIGNAL(ComputeVisibility()), this, SLOT(ComputeVisibility()));
881        connect(mControlWidget, SIGNAL(StopComputation()), this, SLOT(StopComputation()));
882        connect(mControlWidget, SIGNAL(SetRandomViewPoint()), this,
883                SLOT(SetRandomViewPoint()));
884
885        connect(mControlWidget, SIGNAL(SetSceneCut(int)), this, SLOT(SetSceneCut(int)));
886        connect(mControlWidget, SIGNAL(SetTopDistance(int)), this, SLOT(SetTopDistance(int)));
887        connect(mControlWidget, SIGNAL(SetTransparency(int)), this, SLOT(SetTransparency(int)));
888
889        connect(mControlWidget, SIGNAL(SetVisibilityFilterSize(int)), this, SLOT(SetVisibilityFilterSize(int)));
890        connect(mControlWidget, SIGNAL(SetSpatialFilterSize(int)), this, SLOT(SetSpatialFilterSize(int)));
891        connect(mControlWidget, SIGNAL(SetHidingCost(int)), this, SLOT(SetHidingCost(int)));
892
893        connect(mControlWidget, SIGNAL(SetShowViewCells(bool)), this, SLOT(SetShowViewCells(bool)));
894        connect(mControlWidget, SIGNAL(SetShowRenderCost(bool)), this, SLOT(SetShowRenderCost(bool)));
895        connect(mControlWidget, SIGNAL(SetUseTransparency(bool)), this, SLOT(SetUseTransparency(bool)));
896        connect(mControlWidget, SIGNAL(SetShowPvsSizes(bool)), this, SLOT(SetShowPvsSizes(bool)));
897        connect(mControlWidget, SIGNAL(SetTopView(bool)), this, SLOT(SetTopView(bool)));
898        connect(mControlWidget, SIGNAL(SetCutViewCells(bool)), this, SLOT(SetCutViewCells(bool)));
899        connect(mControlWidget, SIGNAL(SetHideByCost(bool)), this, SLOT(SetHideByCost(bool)));
900        connect(mControlWidget, SIGNAL(SetCutScene(bool)), this, SLOT(SetCutScene(bool)));
901        connect(mControlWidget, SIGNAL(SetRenderErrors(bool)), this, SLOT(SetRenderErrors(bool)));
902        connect(mControlWidget, SIGNAL(SetRenderBoxes(bool)), this, SLOT(SetRenderBoxes(bool)));
903        connect(mControlWidget, SIGNAL(SetRenderFilter(bool)), this, SLOT(SetRenderFilter(bool)));
904        connect(mControlWidget, SIGNAL(SetRenderVisibilityEstimates(bool)),
905                this, SLOT(SetRenderVisibilityEstimates(bool)));
906        connect(mControlWidget, SIGNAL(SetUseFilter(bool)), this, SLOT(SetUseFilter(bool)));
907        connect(mControlWidget, SIGNAL(SetUseSpatialFilter(bool)),
908                this, SLOT(SetUseSpatialFilter(bool)));
909        connect(mControlWidget, SIGNAL(SetShowPiercingRays(bool)), this, SLOT(SetShowPiercingRays(bool)));
910
911        connect(mControlWidget,
912                SIGNAL(SetShowRays(bool)),
913                this,
914                SLOT(SetShowRays(bool)));
915
916        resize(1000, 500);
917        mControlWidget->show();
918}
919
920void
921QtGlRendererWidget::UpdateAllPvs()
922{
923        // $$ does not work so far:(
924        mViewCellsManager->UpdatePvsForEvaluation();
925        //      mViewCellsManager->FinalizeViewCells(false);
926}
927
928void
929QtGlRendererWidget::ComputeVisibility()
930{
931        cerr<<"Compute Visibility called!\n"<<endl;
932        if (!mPreprocessorThread->isRunning())
933                mPreprocessorThread->RunThread();
934}
935
936void
937QtGlRendererWidget::StopComputation()
938{
939        cerr<<"stop computation called!\n"<<endl;
940        mViewCellsManager->GetPreprocessor()->mStopComputation = true;
941}
942
943void
944QtGlRendererWidget::SetRandomViewPoint()
945{
946        cerr<<"stop computation called!\n"<<endl;
947        mViewCellsManager->GetViewPoint(mViewPoint);
948        updateGL();
949}
950
951void
952QtGlRendererWidget::RenderRenderCost()
953{
954        static vector<float> costFunction;
955        static float maxCost = -1;
956        if (costFunction.size()==0) {
957                ViewCellsTree *tree = mViewCellsManager->GetViewCellsTree();
958                if (tree) {
959                        tree->GetCostFunction(costFunction);
960                        maxCost = -1;
961                        for (int i=0;  i < costFunction.size(); i++) {
962                                //                cout<<i<<":"<<costFunction[i]<<" ";
963                                // update cost function to an absolute value based on the total geometry count
964                                costFunction[i]*=mSceneGraph->GetRoot()->mGeometry.size();
965                                if (costFunction[i] > maxCost)
966                                        maxCost = costFunction[i];
967                        }
968                }
969        }
970
971
972        int currentPos = (int)mViewCellsManager->GetViewCells().size();
973        float currentCost= -1;
974
975        if (currentPos < costFunction.size())
976                currentCost = costFunction[currentPos];
977#if 1   
978        cout<<"costFunction.size()="<<(int)costFunction.size()<<endl;
979        cout<<"CP="<<currentPos<<endl;
980        cout<<"MC="<<maxCost<<endl;
981        cout<<"CC="<<currentCost<<endl;
982#endif
983        if (costFunction.size()) {
984                float scaley = 1.0f/log10(maxCost);
985                float scalex = 1.0f/(float)costFunction.size();
986
987                glDisable(GL_DEPTH_TEST);
988                // init ortographic projection
989                glMatrixMode(GL_PROJECTION);
990
991                glPushMatrix();
992
993                glLoadIdentity();
994                gluOrtho2D(0, 1.0f, 0, 1.0f);
995
996                glTranslatef(0.1f, 0.1f, 0.0f);
997                glScalef(0.8f, 0.8f, 1.0f);
998                glMatrixMode(GL_MODELVIEW);
999                glLoadIdentity();
1000
1001                glColor3f(1.0f,0,0);
1002                glBegin(GL_LINE_STRIP);
1003                //        glVertex3f(0,0,0);
1004
1005                for (int i=0;  i < costFunction.size(); i++) {
1006                        float x =  i*scalex;
1007                        float y = log10(costFunction[i])*scaley;
1008                        glVertex3f(x,y,0.0f);
1009                }
1010                glEnd();
1011
1012                glColor3f(1.0f,0,0);
1013                glBegin(GL_LINES);
1014                float x =  currentPos*scalex;
1015                glVertex3f(x,0.0,0.0f);
1016                glVertex3f(x,1.0f,0.0f);
1017                glEnd();
1018
1019                glColor3f(0.0f,0,0);
1020                // show a grid
1021                glBegin(GL_LINE_LOOP);
1022                glVertex3f(0,0,0.0f);
1023                glVertex3f(1,0,0.0f);
1024                glVertex3f(1,1,0.0f);
1025                glVertex3f(0,1,0.0f);
1026                glEnd();
1027
1028                glBegin(GL_LINES);
1029                for (int i=0;  i < costFunction.size(); i += 1000) {
1030                        float x =  i*scalex;
1031                        glVertex3f(x,0.0,0.0f);
1032                        glVertex3f(x,1.0f,0.0f);
1033                }
1034
1035                for (int i=0;  pow(10.0f, i) < maxCost; i+=1) {
1036                        float y = i*scaley;
1037                        //              QString s;
1038                        //              s.sprintf("%d", (int)pow(10,i));
1039                        //              renderText(width()/2+5, y*height(), s);
1040                        glVertex3f(0.0f, y, 0.0f);
1041                        glVertex3f(1.0f, y, 0.0f);
1042                }
1043
1044                glEnd();
1045
1046
1047                // restore the projection matrix
1048                glMatrixMode(GL_PROJECTION);
1049                glPopMatrix();
1050                glMatrixMode(GL_MODELVIEW);
1051                glEnable(GL_DEPTH_TEST);
1052
1053        }
1054
1055
1056
1057}
1058
1059void
1060QtGlRendererWidget::RenderInfo()
1061{
1062
1063        QString s;
1064        int vc = 0;
1065        if (mViewCellsManager)
1066                vc = (int)mViewCellsManager->GetViewCells().size();
1067
1068        int filter = 0;
1069        if (mViewCellsManager)
1070                filter = mViewCellsManager->GetMaxFilterSize();
1071
1072        glColor3f(1.0f,1.0f,1.0f);
1073
1074        s.sprintf("frame:%04d viewpoint:(%4.1f,%4.1f,%4.1f) dir:(%4.1f,%4.1f,%4.1f)",
1075                mFrame,
1076                mViewPoint.x,
1077                mViewPoint.y,
1078                mViewPoint.z,
1079                mViewDirection.x,
1080                mViewDirection.y,
1081                mViewDirection.z
1082                );
1083
1084        renderText(20,20,s);
1085
1086        s.sprintf("viewcells:%04d filter:%04d pvs:%04d error:%5.5f %",
1087                vc,
1088                filter,
1089                mPvsSize,
1090                mRenderError*100.0f);
1091
1092
1093        renderText(20,40,s);
1094
1095
1096
1097
1098}
1099
1100
1101void
1102QtGlRendererWidget::SetViewCellGranularity(int number)
1103{
1104        if (mViewCellsManager) {
1105                //      mViewCellsManager->SetMaxFilterSize(number);
1106
1107                // $$ tmp off
1108                mViewCellsManager->CollectViewCells(number);
1109
1110                // $$ does not work so far:(
1111                //      mViewCellsManager->UpdatePvsForEvaluation();
1112                //      mViewCellsManager->FinalizeViewCells(false);
1113        }
1114        updateGL();
1115}
1116
1117void
1118QtGlRendererWidget::SetVisibilityFilterSize(int number)
1119{
1120        if (mViewCellsManager)
1121                mViewCellsManager->SetMaxFilterSize(number);
1122        mPvsCache.Reset();
1123        updateGL();
1124}
1125
1126void
1127QtGlRendererWidget::SetSpatialFilterSize(int number)
1128{
1129        mSpatialFilterSize = 1e-3*number;
1130        mPvsCache.Reset();
1131        updateGL();
1132}
1133
1134void
1135QtGlRendererWidget::SetSceneCut(int number)
1136{
1137        // assume the cut plane can only be aligned with xz plane
1138        // shift it along y according to number, which is percentage of the bounding
1139        // box position
1140        if (mViewCellsManager)
1141        {
1142                AxisAlignedBox3 box = mViewCellsManager->GetViewSpaceBox();
1143                Vector3 p = box.Min() + (number/1000.0f)*box.Max();
1144                mSceneCutPlane.mNormal = Vector3(0,-1,0);
1145                mSceneCutPlane.mD = -DotProd(mSceneCutPlane.mNormal, p);
1146
1147                updateGL();
1148        }
1149}
1150
1151void
1152QtGlRendererWidget::SetHidingCost(int number)
1153{
1154        mHidingCost = (float)number / 1000.0f;
1155        //cout << "hiding cost " << mHidingCost << endl;
1156}
1157
1158
1159void
1160QtGlRendererWidget::SetTopDistance(int number)
1161{
1162        mTopDistance = number/1000.0f;
1163        updateGL();
1164}
1165
1166void
1167QtGlRendererWidget::SetTransparency(int number)
1168{
1169        mTransparency = number/1000.0f;
1170        updateGL();
1171}
1172
1173void
1174QtGlRendererWidget::RenderViewCells()
1175{
1176        mUseFalseColors = true;
1177
1178        //glPushAttrib(GL_CURRENT_BIT | GL_ENABLE_BIT | GL_POLYGON_BIT);
1179
1180        glEnable(GL_CULL_FACE);
1181        glCullFace(GL_FRONT);
1182        //glDisable(GL_CULL_FACE);
1183
1184        double eq[4];
1185        eq[0] = mSceneCutPlane.mNormal.x;
1186        eq[1] = mSceneCutPlane.mNormal.y;
1187        eq[2] = mSceneCutPlane.mNormal.z;
1188        eq[3] = mSceneCutPlane.mD;
1189
1190        if (mCutViewCells)
1191        {
1192                glClipPlane(GL_CLIP_PLANE0, eq);
1193                glEnable(GL_CLIP_PLANE0);
1194        }
1195
1196        int i;
1197
1198        ViewCellContainer &viewcells = mViewCellsManager->GetViewCells();
1199        int maxPvs = -1;
1200        int maxPiercingRays = 1; // for savety
1201
1202        for (i = 0; i < viewcells.size(); ++ i)
1203        {
1204                ViewCell *vc = viewcells[i];
1205
1206                //const int p = vc->GetPvs().CountObjectsInPvs();
1207                const int p = vc->GetPvs().GetSize();
1208                if (p > maxPvs)
1209                        maxPvs = p;
1210
1211                const int piercingRays = vc->GetNumPiercingRays();
1212               
1213                if (piercingRays > maxPiercingRays)
1214                        maxPiercingRays = piercingRays;
1215        }
1216
1217        if (!mShowPvsSizes && !mShowPiercingRays)
1218        {
1219                for (i = 0; i < viewcells.size(); ++ i)
1220                {
1221                        ViewCell *vc = viewcells[i];
1222                        RgbColor c;
1223
1224                        if (!mShowPvsSizes && !mShowPiercingRays)
1225                        {
1226                                mWireFrame = true;
1227                                c = vc->GetColor();
1228                        }
1229
1230                        glColor3f(c.r, c.g, c.b);
1231
1232                        if (!mHideByCost || (mHidingCost < vc->GetNumPiercingRays() / (float)maxPiercingRays))
1233                        {
1234                RenderViewCell(vc);
1235                        }
1236                }
1237        }
1238        else
1239        {
1240                //glDisable(GL_CULL_FACE);
1241                //glEnable(GL_CULL_FACE);
1242                //glFrontFace(GL_CCW); 
1243                //glCullFace(GL_BACK);
1244                if (!mUseTransparency)
1245                {
1246                        glEnable(GL_DEPTH_TEST);
1247                        glDisable(GL_BLEND);
1248                }
1249                else
1250                {
1251                        //glCullFace(GL_BACK);
1252                        //cout << "here29 " << mTransparency << endl;
1253                        glDisable(GL_DEPTH_TEST);
1254                        glEnable(GL_BLEND);
1255
1256                        glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1257                        //glBlendFunc(GL_SRC_ALPHA, GL_ONE);
1258
1259                        for (i = 0; i < viewcells.size(); ++ i)
1260                        {
1261                                ViewCell *vc = viewcells[i];
1262                                const Vector3 dummyViewPoint(0.0, 0.0, 1.0);
1263                                const float dist = SqrDistance(dummyViewPoint, vc->GetBox().Center());
1264
1265                                vc->SetDistance(dist);
1266                        }
1267
1268                        sort(viewcells.begin(), viewcells.end(), nearerThan);
1269                }
1270
1271                RgbColor c;
1272
1273        for (i = 0; i < viewcells.size(); ++ i)
1274                {
1275            ViewCell *vc = viewcells[i];
1276                       
1277                        //cout << "dist: " << vc->GetDistance() << " ";
1278
1279                        if (mShowPiercingRays)
1280                        {
1281                                // const float importance = 5.0f*mTransferFunction * ((float)vc->GetPvs().CountObjectsInPvs() / (float)maxPvs);
1282                                const float importance = 5.0f * mTransferFunction *
1283                                        ((float)vc->GetNumPiercingRays() / (float)maxPiercingRays);
1284                                // c = RgbColor(importance, 1.0f - importance, 0.0f);
1285                                c = RainbowColorMapping(importance);
1286                        }
1287                        else
1288                        {
1289                                // const float importance = 5.0f*mTransferFunction * ((float)vc->GetPvs().CountObjectsInPvs() / (float)maxPvs);
1290                                const float importance = 5.0f * mTransferFunction *
1291                                        ((float)vc->GetPvs().GetSize() / (float)maxPvs);
1292                                // c = RgbColor(importance, 1.0f - importance, 0.0f);
1293                                c = RainbowColorMapping(importance);
1294                        }
1295                       
1296                        glColor4f(c.r, c.g, c.b, 1.0f - mTransparency);
1297                       
1298                        if (!mHideByCost || (mHidingCost < vc->GetNumPiercingRays() / (float)maxPiercingRays))
1299                        {
1300                RenderViewCell(vc);
1301                        }
1302                }
1303
1304                glEnable(GL_DEPTH_TEST);       
1305        }
1306
1307        mUseFalseColors = false;
1308        mWireFrame = false;
1309
1310        glPopAttrib();
1311}
1312
1313
1314
1315/*********************************************************************/
1316/*                   QtGlDebuggerWidget implementation               */
1317/*********************************************************************/
1318
1319
1320QtGlDebuggerWidget::QtGlDebuggerWidget(QtGlRendererBuffer *buf, QWidget *parent)
1321: QGLWidget(QGLFormat(QGL::SampleBuffers), parent), mRenderBuffer(buf)
1322{
1323        // create the pbuffer
1324        //pbuffer = new QGLPixelBuffer(QSize(512, 512), format(), this);
1325        timerId = startTimer(20);
1326        setWindowTitle(("OpenGL pbuffers"));
1327}
1328
1329
1330QtGlDebuggerWidget::~QtGlDebuggerWidget()
1331{
1332        mRenderBuffer->releaseFromDynamicTexture();
1333        glDeleteTextures(1, &dynamicTexture);
1334
1335        DEL_PTR(mRenderBuffer);
1336}
1337
1338
1339QtRendererControlWidget::QtRendererControlWidget(QWidget * parent, Qt::WFlags f):
1340QWidget(parent, f)
1341{
1342
1343        QVBoxLayout *vl = new QVBoxLayout;
1344        setLayout(vl);
1345
1346        QWidget *vbox = new QGroupBox("ViewCells", this);
1347        layout()->addWidget(vbox);
1348
1349        vl = new QVBoxLayout;
1350        vbox->setLayout(vl);
1351
1352        QLabel *label = new QLabel("Granularity");
1353        vbox->layout()->addWidget(label);
1354
1355        QSlider *slider = new QSlider(Qt::Horizontal, vbox);
1356        vl->addWidget(slider);
1357        slider->show();
1358        slider->setRange(1, 10000);
1359        slider->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
1360        slider->setValue(200);
1361
1362        connect(slider, SIGNAL(valueChanged(int)), SIGNAL(SetViewCellGranularity(int)));
1363
1364        ///////////////////////////
1365
1366        label = new QLabel("Transfer function");
1367        vbox->layout()->addWidget(label);
1368
1369        slider = new QSlider(Qt::Horizontal, vbox);
1370        vl->addWidget(slider);
1371        slider->show();
1372        slider->setRange(1, 10000);
1373        slider->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
1374        slider->setValue(100);
1375
1376
1377        connect(slider, SIGNAL(valueChanged(int)), SIGNAL(SetTransferFunction(int)));
1378
1379        ////////////////////////////////////////7
1380
1381
1382        QPushButton *button = new QPushButton("Update all PVSs", vbox);
1383        vbox->layout()->addWidget(button);
1384        connect(button, SIGNAL(clicked()), SLOT(UpdateAllPvs()));
1385
1386
1387        ////////////////////////////////////////77777
1388
1389        label = new QLabel("Filter size");
1390        vbox->layout()->addWidget(label);
1391
1392        slider = new QSlider(Qt::Horizontal, vbox);
1393        vbox->layout()->addWidget(slider);
1394        slider->show();
1395        slider->setRange(1, 100);
1396        slider->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
1397        slider->setValue(3);
1398
1399        connect(slider, SIGNAL(valueChanged(int)), SIGNAL(SetVisibilityFilterSize(int)));
1400
1401
1402        //////////////////////////////////////////
1403
1404        label = new QLabel("Spatial 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(10);
1413
1414        connect(slider, SIGNAL(valueChanged(int)), SIGNAL(SetSpatialFilterSize(int)));
1415
1416
1417        ///////////////////////////////////
1418
1419
1420        QWidget *hbox = new QWidget(vbox);
1421        vl->addWidget(hbox);
1422        QHBoxLayout *hlayout = new QHBoxLayout;
1423        hbox->setLayout(hlayout);
1424
1425        QCheckBox *cb = new QCheckBox("Show viewcells", hbox);
1426        hlayout->addWidget(cb);
1427        cb->setChecked(false);
1428        connect(cb, SIGNAL(toggled(bool)), SIGNAL(SetShowViewCells(bool)));
1429
1430        cb = new QCheckBox("Render cost curve", hbox);
1431        hlayout->addWidget(cb);
1432        cb->setChecked(false);
1433        connect(cb, SIGNAL(toggled(bool)), SIGNAL(SetShowRenderCost(bool)));
1434
1435        cb = new QCheckBox("Show render cost", hbox);
1436        hlayout->addWidget(cb);
1437        cb->setChecked(false);
1438        connect(cb, SIGNAL(toggled(bool)), SIGNAL(SetShowPvsSizes(bool)));
1439
1440        cb = new QCheckBox("Show rays", hbox);
1441        hlayout->addWidget(cb);
1442        cb->setChecked(false);
1443        connect(cb, SIGNAL(toggled(bool)), SIGNAL(SetShowRays(bool)));
1444
1445        cb = new QCheckBox("Show piercing rays", hbox);
1446        hlayout->addWidget(cb);
1447        cb->setChecked(false);
1448        connect(cb, SIGNAL(toggled(bool)), SIGNAL(SetShowPiercingRays(bool)));
1449
1450
1451        QButtonGroup *qbuttongroup = new QButtonGroup(hbox);//, "RenderingGroupBox");
1452        //qbuttongroup->setTitle(i18n("Rendering options2"));
1453        //qbuttongroup->setColumnLayout(0, Qt::Horizontal);
1454        //qbuttongroup->layout()->setSpacing(0);
1455        //qbuttongroup->layout()->setMargin(0);
1456
1457        // Create a layout to organize the widgets.
1458        /*QVBoxLayout *qvboxlayout = new QVBoxLayout;//->layout());
1459        qvboxlayout->setAlignment(Qt::AlignTop);
1460        qvboxlayout->setSpacing(6);
1461        qvboxlayout->setMargin(11);
1462*/
1463        QRadioButton *rb;
1464
1465        // Create a check box to be in the group box
1466       
1467        rb = new QRadioButton("piercing rays", hbox);
1468        rb->setText("piercing rays");
1469        vl->addWidget(rb);
1470       
1471        rb = new QRadioButton("render cost", hbox);
1472        rb->setText("render cost");
1473        vl->addWidget(rb);
1474
1475        //////////////////////////////////
1476
1477
1478        /*cb = new QRadiobox("Top View", vbox);
1479        vbox->layout()->addWidget(cb);
1480        cb->setChecked(false);
1481        connect(cb, SIGNAL(toggled(bool)), SIGNAL(SetTopView(bool)));
1482*/
1483
1484        vbox->resize(800,150);
1485
1486
1487        vbox = new QGroupBox("Rendering", this);
1488        layout()->addWidget(vbox);
1489
1490        vl = new QVBoxLayout;
1491        vbox->setLayout(vl);
1492
1493
1494
1495        cb = new QCheckBox("Cut view cells", vbox);
1496        vbox->layout()->addWidget(cb);
1497        cb->setChecked(false);
1498        connect(cb, SIGNAL(toggled(bool)), SIGNAL(SetCutViewCells(bool)));
1499
1500
1501        slider = new QSlider(Qt::Horizontal, vbox);
1502        vbox->layout()->addWidget(slider);
1503        slider->show();
1504        slider->setRange(0, 1000);
1505        slider->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
1506        slider->setValue(1000);
1507
1508        connect(slider, SIGNAL(valueChanged(int)), SIGNAL(SetSceneCut(int)));
1509
1510       
1511        cb = new QCheckBox("Hide view cells by render cost ", vbox);
1512        vbox->layout()->addWidget(cb);
1513        cb->setChecked(false);
1514        connect(cb, SIGNAL(toggled(bool)), SIGNAL(SetHideByCost(bool)));
1515
1516        ////////////////////////////
1517
1518        const int range = 1000;
1519
1520        label = new QLabel("Hide by cost");
1521        vbox->layout()->addWidget(label);
1522
1523        // the render cost visualization
1524        slider = new QSlider(Qt::Horizontal, vbox);
1525        vbox->layout()->addWidget(slider);
1526        slider->show();
1527        slider->setRange(0, range);
1528        slider->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
1529        slider->setValue(0);
1530        connect(slider, SIGNAL(valueChanged(int)), SIGNAL(SetHidingCost(int)));
1531
1532        ///////////////////////////////////////////
1533
1534        cb = new QCheckBox("Top View", vbox);
1535        vbox->layout()->addWidget(cb);
1536        cb->setChecked(false);
1537        connect(cb, SIGNAL(toggled(bool)), SIGNAL(SetTopView(bool)));
1538
1539
1540        label = new QLabel("Top distance");
1541        vbox->layout()->addWidget(label);
1542       
1543        slider = new QSlider(Qt::Horizontal, vbox);
1544        vbox->layout()->addWidget(slider);
1545        slider->show();
1546        slider->setRange(1, 1000);
1547        slider->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
1548        slider->setValue(500);
1549
1550        connect(slider, SIGNAL(valueChanged(int)), SIGNAL(SetTopDistance(int)));
1551       
1552
1553        ///////////////////////////////////////////
1554
1555        cb = new QCheckBox("Transparency", vbox);
1556        vbox->layout()->addWidget(cb);
1557        cb->setChecked(false);
1558        connect(cb, SIGNAL(toggled(bool)), SIGNAL(SetUseTransparency(bool)));
1559
1560        label = new QLabel("Use transparency");
1561        vbox->layout()->addWidget(label);
1562
1563        // the render cost visualization
1564        slider = new QSlider(Qt::Horizontal, vbox);
1565        vbox->layout()->addWidget(slider);
1566        slider->show();
1567        slider->setRange(0, range);
1568        slider->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
1569        slider->setValue(0);
1570
1571        connect(slider, SIGNAL(valueChanged(int)), SIGNAL(SetTransparency(int)));
1572
1573        //////////////////////////////
1574
1575
1576        cb = new QCheckBox("Cut scene", vbox);
1577        vbox->layout()->addWidget(cb);
1578        cb->setChecked(false);
1579        connect(cb, SIGNAL(toggled(bool)), SIGNAL(SetCutScene(bool)));
1580
1581        cb = new QCheckBox("Render boxes", vbox);
1582        vbox->layout()->addWidget(cb);
1583        cb->setChecked(false);
1584        connect(cb, SIGNAL(toggled(bool)), SIGNAL(SetRenderBoxes(bool)));
1585
1586        cb = new QCheckBox("Render visibility estimates", vbox);
1587        vbox->layout()->addWidget(cb);
1588        cb->setChecked(false);
1589        connect(cb, SIGNAL(toggled(bool)), SIGNAL(SetRenderVisibilityEstimates(bool)));
1590
1591
1592        cb = new QCheckBox("Render errors", vbox);
1593        vbox->layout()->addWidget(cb);
1594        cb->setChecked(false);
1595        connect(cb, SIGNAL(toggled(bool)), SIGNAL(SetRenderErrors(bool)));
1596
1597
1598        bool tmp;
1599
1600        cb = new QCheckBox("Use filter", vbox);
1601        vbox->layout()->addWidget(cb);
1602        Environment::GetSingleton()->GetBoolValue("Preprocessor.applyVisibilityFilter", tmp );
1603        cb->setChecked(tmp);
1604        connect(cb, SIGNAL(toggled(bool)), SIGNAL(SetUseFilter(bool)));
1605
1606
1607        cb = new QCheckBox("Use spatial filter", vbox);
1608        vbox->layout()->addWidget(cb);
1609        Environment::GetSingleton()->GetBoolValue("Preprocessor.applyVisibilitySpatialFilter",
1610                tmp );
1611        cb->setChecked(tmp);
1612        connect(cb, SIGNAL(toggled(bool)), SIGNAL(SetUseSpatialFilter(bool)));
1613
1614        cb = new QCheckBox("Render filter", vbox);
1615        vbox->layout()->addWidget(cb);
1616        cb->setChecked(true);
1617        connect(cb, SIGNAL(toggled(bool)), SIGNAL(SetRenderFilter(bool)));
1618
1619
1620        vbox = new QGroupBox("PVS Errors", this);
1621        layout()->addWidget(vbox);
1622
1623        vl = new QVBoxLayout;
1624        vbox->setLayout(vl);
1625
1626        button = new QPushButton("Compute Visibility", vbox);
1627        vbox->layout()->addWidget(button);
1628        connect(button, SIGNAL(clicked()), SLOT(ComputeVisibility()));
1629
1630        button = new QPushButton("Stop Computation", vbox);
1631        vbox->layout()->addWidget(button);
1632        connect(button, SIGNAL(clicked()), SLOT(StopComputation()));
1633
1634        button = new QPushButton("Set Random View Point", vbox);
1635        vbox->layout()->addWidget(button);
1636        connect(button, SIGNAL(clicked()), SLOT(SetRandomViewPoint()));
1637
1638
1639        if (0) {
1640                vbox = new QGroupBox("PVS Errors", this);
1641                layout()->addWidget(vbox);
1642
1643                vl = new QVBoxLayout;
1644                vbox->setLayout(vl);
1645
1646                mPvsErrorWidget = new QListWidget(vbox);
1647                vbox->layout()->addWidget(mPvsErrorWidget);
1648
1649                connect(mPvsErrorWidget,
1650                        SIGNAL(doubleClicked(const QModelIndex &)),
1651                        this,
1652                        SLOT(PvsErrorClicked(const QModelIndex &)));
1653
1654                button = new QPushButton("Next Error Frame", vbox);
1655                vbox->layout()->addWidget(button);
1656                connect(button, SIGNAL(clicked(void)), SLOT(FocusNextPvsErrorFrame(void)));
1657        }
1658
1659       
1660        //mHidingCost = 0.1f;
1661
1662        setWindowTitle("Preprocessor Control Widget");
1663        adjustSize();
1664}
1665
1666
1667
1668
1669void
1670QtRendererControlWidget::FocusNextPvsErrorFrame(void)
1671{
1672
1673
1674}
1675
1676void
1677QtRendererControlWidget::UpdatePvsErrorItem(int row,
1678                                                                                        GlRendererBuffer::PvsErrorEntry &pvsErrorEntry)
1679{
1680
1681        QListWidgetItem *i = mPvsErrorWidget->item(row);
1682        QString s;
1683        s.sprintf("%5.5f", pvsErrorEntry.mError);
1684        if (i) {
1685                i->setText(s);
1686        } else {
1687                new QListWidgetItem(s, mPvsErrorWidget);
1688        }
1689        mPvsErrorWidget->update();
1690}
1691
1692
1693void QtGlDebuggerWidget::initializeGL()
1694{
1695        glMatrixMode(GL_PROJECTION);
1696        glLoadIdentity();
1697
1698        glFrustum(-1, 1, -1, 1, 10, 100);
1699        glTranslatef(-0.5f, -0.5f, -0.5f);
1700        glTranslatef(0.0f, 0.0f, -15.0f);
1701        glMatrixMode(GL_MODELVIEW);
1702
1703        glEnable(GL_CULL_FACE);
1704        initCommon();
1705        initPbuffer();
1706
1707}
1708
1709
1710void QtGlDebuggerWidget::resizeGL(int w, int h)
1711{
1712        glViewport(0, 0, w, h);
1713}
1714
1715
1716void QtGlDebuggerWidget::paintGL()
1717{
1718        // draw a spinning cube into the pbuffer..
1719        mRenderBuffer->makeCurrent();
1720
1721        BeamSampleStatistics stats;
1722        mRenderBuffer->SampleBeamContributions(mSourceObject, mBeam, mSamples, stats);
1723
1724        glFlush();
1725
1726        // rendering directly to a texture is not supported on X11, unfortunately
1727        mRenderBuffer->updateDynamicTexture(dynamicTexture);
1728
1729        // and use the pbuffer contents as a texture when rendering the
1730        // background and the bouncing cubes
1731        makeCurrent();
1732        glBindTexture(GL_TEXTURE_2D, dynamicTexture);
1733        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
1734
1735        // draw the background
1736        glMatrixMode(GL_MODELVIEW);
1737        glPushMatrix();
1738        glLoadIdentity();
1739        glMatrixMode(GL_PROJECTION);
1740        glPushMatrix();
1741        glLoadIdentity();
1742
1743        glPopMatrix();
1744        glMatrixMode(GL_MODELVIEW);
1745        glPopMatrix();
1746}
1747
1748
1749void QtGlDebuggerWidget::initPbuffer()
1750{
1751        // set up the pbuffer context
1752        mRenderBuffer->makeCurrent();
1753        /*mRenderBuffer->InitGL();
1754
1755        glViewport(0, 0, mRenderBuffer->size().width(), mRenderBuffer->size().height());
1756        glMatrixMode(GL_PROJECTION);
1757        glLoadIdentity();
1758        glOrtho(-1, 1, -1, 1, -99, 99);
1759        glTranslatef(-0.5f, -0.5f, 0.0f);
1760        glMatrixMode(GL_MODELVIEW);
1761        glLoadIdentity();
1762
1763        glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);*/
1764
1765        // generate a texture that has the same size/format as the pbuffer
1766        dynamicTexture = mRenderBuffer->generateDynamicTexture();
1767
1768        // bind the dynamic texture to the pbuffer - this is a no-op under X11
1769        mRenderBuffer->bindToDynamicTexture(dynamicTexture);
1770        makeCurrent();
1771}
1772
1773void QtGlDebuggerWidget::initCommon()
1774{
1775        glEnable(GL_TEXTURE_2D);
1776        glEnable(GL_DEPTH_TEST);
1777
1778        glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
1779}
1780
1781
1782
1783}
Note: See TracBrowser for help on using the repository browser.