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

Revision 2645, 61.2 KB checked in by mattausch, 16 years ago (diff)
Line 
1#include "glInterface.h"
2#include "QtGlRenderer.h"
3#include "Mesh.h"
4#include "OcclusionQuery.h"
5#include "ViewCellsManager.h"
6#include "SceneGraph.h"
7#include "Pvs.h"
8#include "Viewcell.h"
9#include "Beam.h"
10#include "KdTree.h"
11#include "Environment.h"
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#include "LogWriter.h"
19#include "RayCaster.h"
20
21
22#define USE_CG 1
23
24#define TEST_PVS_RENDERING 0
25
26#if USE_CG
27#include <Cg/cg.h>
28#include <Cg/cgGL.h>
29#endif
30
31#include <QVBoxLayout>
32
33namespace GtpVisibilityPreprocessor {
34
35
36 
37class ViewCellsManager;
38
39static CGcontext sCgContext = NULL;
40static CGprogram sCgFragmentProgram = NULL;
41static CGprofile sCgFragmentProfile;
42
43
44//static vector<OcclusionQuery *> sQueries;
45
46QtGlRendererWidget *rendererWidget = NULL;
47QtGlDebuggerWidget *debuggerWidget = NULL;
48
49const static int SAMPLES_INCR = 2000000;
50
51
52
53static inline bool ilt(Intersectable *obj1, Intersectable *obj2)
54{
55        return obj1->mId < obj2->mId;
56}
57
58
59inline static bool nearerThan(ViewCell *vc1, ViewCell *vc2)
60{
61        return vc1->GetDistance() > vc2->GetDistance();
62}
63
64
65#if USE_CG
66static void handleCgError()
67{
68    Debug << "Cg error: " << cgGetErrorString(cgGetError()) << endl;
69    exit(1);
70}
71#endif
72
73void
74QtGlRendererBuffer::MakeCurrent()
75{
76  makeCurrent();
77}
78
79void
80QtGlRendererBuffer::DoneCurrent()
81{
82  doneCurrent();
83}
84 
85
86QtGlRendererBuffer::QtGlRendererBuffer(const int w,
87                                                                           const int h,
88                                                                           SceneGraph *sceneGraph,
89                                                                           ViewCellsManager *viewcells,
90                                                                           KdTree *tree):
91  QGLPixelBuffer(QSize(w, h)),
92  GlRendererBuffer(sceneGraph, viewcells, tree)
93{
94  mUseVbos = true;
95  MakeCurrent();
96  InitGL();
97  DoneCurrent();
98}
99
100void
101QtGlRendererBuffer::RenderPvs(const ObjectPvs &pvs)
102{
103#if TEST_PVS_RENDERING
104
105  // Render PVS
106  ObjectPvsIterator it = pvs.GetIterator();
107 
108  Intersectable::NewMail();
109  mCurrentFrame++;
110  while (it.HasMoreEntries())
111        {
112          RenderIntersectable(it.Next());
113        }
114
115#else
116 
117  PreparePvs(pvs);
118
119  int offset = (int)mObjects.size() * 3;
120  char *arrayPtr = mUseVbos ? NULL : (char *)mData;
121
122  glVertexPointer(3, GL_FLOAT, 0, (char *)arrayPtr);
123  glNormalPointer(GL_FLOAT, 0, (char *)arrayPtr + offset * sizeof(Vector3));
124  glDrawElements(GL_TRIANGLES, mIndexBufferSize, GL_UNSIGNED_INT, mIndices);
125 
126#endif
127}
128
129// reimplemented here so that we can snap the error windows
130float
131QtGlRendererBuffer::GetPixelError(int &pvsSize)
132{
133
134  MakeCurrent();
135
136  float pErrorPixels = -1.0f;
137
138
139        mUseFalseColors = false;
140        unsigned int pixelCount;
141
142
143        ViewCell *viewcell = mViewCellsManager->GetViewCell(mViewPoint);
144        if (viewcell == NULL)
145                return 0.0f;
146
147        ObjectPvs pvs;
148
149        bool evaluateFilter;
150        Environment::GetSingleton()->GetBoolValue("Preprocessor.evaluateFilter", evaluateFilter);
151
152        if (!evaluateFilter)
153          pvs = viewcell->GetPvs();
154        else {
155
156          mViewCellsManager->ApplyFilter2(viewcell,
157                                                                          false,
158                                                                          mViewCellsManager->GetFilterWidth(),
159                                                                          pvs);
160        }
161
162        mUseForcedColors = true;
163
164        SetupCamera();
165        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
166        glColorMask(GL_FALSE, GL_TRUE, GL_FALSE, GL_FALSE);
167        glColor3f(0,1,0);
168
169
170        glStencilFunc(GL_EQUAL, 0x0, 0x1);
171        glStencilOp(GL_KEEP, GL_KEEP, GL_INCR);
172
173
174        pvsSize = pvs.GetSize();
175        cout<<pvsSize<<endl;
176       
177        RenderPvs(pvs);
178       
179        //glColorMask(GL_TRUE, GL_FALSE, GL_FALSE, GL_FALSE);
180        glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
181        glEnable(GL_STENCIL_TEST);
182        glColor3f(1,0,0);
183
184       
185        OcclusionQuery *query = mOcclusionQueries[0];
186
187        //      SetupCamera();
188
189        Intersectable::NewMail();
190
191        query->BeginQuery();
192
193        RenderScene();
194       
195        query->EndQuery();
196        glDisable(GL_STENCIL_TEST);
197
198        // reenable other state
199        //  int wait=0;
200        //  while (!query.ResultAvailable()) {
201        //      wait++;
202        //  }
203       
204
205        pixelCount = query->GetQueryResult();
206        //      cout<<"pc="<<pixelCount<<endl;
207       
208        pErrorPixels = ((float)pixelCount)/(GetWidth()*GetHeight());
209
210
211        if (mSnapErrorFrames && (pErrorPixels >= 0.01f)) {
212          glReadBuffer(GL_BACK);
213          //glReadBuffer(GL_FRONT);
214         
215          char filename[256];
216          sprintf(filename, "error-frame-%04d-%0.5f.png", mFrame, pErrorPixels);
217          QImage im = toImage();
218          string str = mSnapPrefix + filename;
219          QString qstr(str.c_str());
220         
221          im.save(qstr, "PNG");
222          if (0) { //0 && mFrame == 1543) {
223                int x,y;
224                int lastIndex = -1;
225                for (y=0; y < im.height(); y++)
226                  for (x=0; x < im.width(); x++) {
227                        QRgb p = im.pixel(x,y);
228                        int index = qRed(p) + (qGreen(p)<<8) + (qBlue(p)<<16);
229                        if (qGreen(p) != 255 && index!=0) {
230                          if (index != lastIndex) {
231                                //                              Debug<<"ei="<<index<<" ";
232                                lastIndex = index;
233                          }
234                        }
235                  }
236          }
237         
238          mUseFalseColors = false;
239          glPushAttrib(GL_CURRENT_BIT);
240          glColor3f(0,1,0);
241          glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
242          glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
243         
244          RenderPvs(pvs);
245
246          mUseForcedColors = false;
247          im = toImage();
248          sprintf(filename, "error-frame-%04d-%0.5f-pvs.png", mFrame, pErrorPixels);
249          str = mSnapPrefix + filename;
250          qstr = str.c_str();
251          im.save(qstr, "PNG");
252          glPopAttrib();
253        }
254       
255        glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
256
257        DoneCurrent();
258
259        return pErrorPixels;
260}
261
262int
263QtGlRendererBuffer::ComputePvs(ObjectContainer &objects,
264                                                           ObjectContainer &pvs) const
265{
266        int pvsSize = 0;
267        QImage image = toImage();
268        Intersectable::NewMail();
269
270        std::stable_sort(objects.begin(), objects.end(), ilt);
271
272        MeshInstance dummy(NULL);
273
274        Intersectable *obj = NULL;
275
276        for (int x = 0; x < image.width(); ++ x)
277        {
278                for (int y = 0; y < image.height(); ++ y)
279                {
280                        QRgb pix = image.pixel(x, y);
281                        const int id = GetId(qRed(pix), qGreen(pix), qBlue(pix));
282
283                        dummy.SetId(id);
284
285                        ObjectContainer::iterator oit =
286                                lower_bound(objects.begin(), objects.end(), &dummy, ilt);
287
288
289                        if (//(oit != oit.end()) &&
290                                ((*oit)->GetId() == id) &&
291                                !obj->Mailed())
292                        {
293                                obj = *oit;
294                                obj->Mail();
295                                ++ pvsSize;
296                                pvs.push_back(obj);
297                        }
298                }
299        }
300
301        return pvsSize;
302}
303
304
305void QtGlRendererWidget::InitGL()
306{
307        GlRenderer::InitGL();
308
309        //glEnable(GL_FOG);
310        //glFogi(GL_FOG_MODE, GL_EXP);
311        glFogi(GL_FOG_MODE, GL_LINEAR);
312
313//      glFogf(GL_FOG_DENSITY, .2f);
314        glFogf(GL_FOG_START, 50.f);
315        glFogf(GL_FOG_END, 500.f);
316
317        GLfloat light_ambient[] = {0.3, 0.3, 0.3, 1.0};
318    GLfloat light_diffuse[] = {0.6, 0.6, 0.6, 1.0};
319    GLfloat light_specular[] = {1.0, 1.0, 1.0, 1.0};
320    GLfloat light_position[] =  //{278.0f, 548.8f,279.0f, 1.0f };
321    { 0.f,0.f,0.f,1.0f };
322
323    /*glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
324    glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
325    glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
326    glLightfv(GL_LIGHT0, GL_POSITION, light_position);
327*/
328    glEnable(GL_LIGHT0);
329
330        GLfloat mat_ambient[] = {0.5f, 0.5f, 0.5f, 1.0f};
331       
332        // mat_specular and mat_shininess are NOT default values
333        GLfloat mat_diffuse[] = {1.0f, 1.0f, 1.0f, 1.0f};
334        GLfloat mat_specular[] = {0.3f, 0.3f, 0.3f, 1.0f};
335        GLfloat mat_shininess[] = {1.0f};
336
337/*      GLfloat light_ambient[] = {0.2f, 0.2f, 0.2f, 1.0f};
338        GLfloat light_diffuse[] = {0.4f, 0.4f, 0.4f, 1.0f};
339        GLfloat light_specular[] = {0.3f, 0.3f, 0.3f, 1.0f};
340
341        GLfloat lmodel_ambient[] = {0.3f, 0.3f, 0.3f, 1.0f};
342*/
343
344        // default Material
345        glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient);
346        glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
347        glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
348        glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
349
350        // a light
351        glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
352        glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
353        glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
354
355        /*glLightfv(GL_LIGHT1, GL_AMBIENT, light_ambient);
356        glLightfv(GL_LIGHT1, GL_DIFFUSE, light_diffuse);
357        glLightfv(GL_LIGHT1, GL_SPECULAR, light_specular);
358*/
359        //glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lmodel_ambient);
360
361        glEnable(GL_LIGHTING);
362        glEnable(GL_LIGHT0);
363//      glEnable(GL_LIGHT1);
364
365        // set position of the light
366        /*GLfloat infinite_light[] = {  1.0, 0.8, 1.0, 0.0  };
367        glLightfv (GL_LIGHT0, GL_POSITION, infinite_light);
368
369        // set position of the light2
370        GLfloat infinite_light2[] = {  -0.3, 1.5, 1.0, 0.0  };
371        glLightfv (GL_LIGHT1, GL_POSITION, infinite_light2);
372*/
373        glLightfv (GL_LIGHT0, GL_POSITION, light_position);
374
375        glColorMaterial( GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE);
376        // glColorMaterial( GL_FRONT_AND_BACK, GL_SPECULAR);
377        glEnable(GL_COLOR_MATERIAL);
378
379        glShadeModel(GL_SMOOTH);
380}
381
382
383void
384QtGlRendererWidget::SetupCameraProjection(const int w, const int h, const float angle)
385{
386        if (!mTopView) {
387                int ww = w;
388                int hh = h;
389                glViewport(0, 0, ww, hh);
390                glMatrixMode(GL_PROJECTION);
391                glLoadIdentity();
392                gluPerspective(angle, ww/(float)hh, 0.1, 2.0 * Magnitude(mSceneGraph->GetBox().Diagonal()));
393                glMatrixMode(GL_MODELVIEW);
394        } else {
395                int ww = w;
396                int hh = h;
397                glViewport(0, 0, ww, hh);
398                glMatrixMode(GL_PROJECTION);
399                glLoadIdentity();
400                gluPerspective(50.0, ww / (float)hh, 0.1, 20.0 * Magnitude(mSceneGraph->GetBox().Diagonal()));
401                glMatrixMode(GL_MODELVIEW);
402        }
403}
404
405
406bool QtGlRendererWidget::PvsChanged(ViewCell *viewCell) const
407{
408        if (viewCell != mPvsCache.mViewCell)
409                return true;
410
411        if (viewCell->GetPvs().GetSize() != mPvsCache.mUnfilteredPvsSize)
412                return true;
413
414        return false;
415}
416
417
418void QtGlRendererWidget::_RenderPvs()
419{
420        mUseFalseColors = false;
421
422        int offset = (int)mObjects.size() * 3;
423        char *arrayPtr = mUseVbos ? NULL : (char *)mData;
424
425        glVertexPointer(3, GL_FLOAT, 0, (char *)arrayPtr);
426        glNormalPointer(GL_FLOAT, 0, (char *)arrayPtr + offset * sizeof(Vector3));
427        glDrawElements(GL_TRIANGLES, mIndexBufferSize, GL_UNSIGNED_INT, mIndices);
428#if DYNAMIC_OBJECTS_HACK
429        // handle dynamic objects
430        DynamicObjectsContainer::const_iterator dit, dit_end = mDynamicObjects.end();
431
432
433        for (dit = mDynamicObjects.begin(); dit != dit_end; ++ dit)
434        {
435#if USE_TRANSFORMED_MESH_INSTANCE_HACK
436                //cerr << "here45\n****"<<endl;
437                RenderIntersectable(*dit);
438#else
439                _RenderDynamicObject(*dit);
440#endif
441        }
442#endif
443}
444
445
446void QtGlRendererWidget::PreparePvs2(const ObjectPvs &pvs)
447{
448        int indexBufferSize = 0;
449       
450        KdNode::NewMail2();
451        //Intersectable::NewMail();
452
453        mPvsSize = pvs.GetSize();
454
455#if DYNAMIC_OBJECTS_HACK
456        mDynamicObjects.clear();
457#endif
458
459        ObjectPvsIterator it = pvs.GetIterator();
460
461        while (it.HasMoreEntries())
462        {
463                Intersectable *obj = it.Next();
464                switch (obj->Type())
465                {
466                case Intersectable::KD_INTERSECTABLE:
467                        {
468                                KdNode *node = static_cast<KdIntersectable *>(obj)->GetItem();
469                                _UpdatePvsIndices(node, indexBufferSize);
470                        }
471                        break;
472#if DYNAMIC_OBJECTS_HACK
473#if USE_TRANSFORMED_MESH_INSTANCE_HACK
474
475                case Intersectable::TRANSFORMED_MESH_INSTANCE:
476                        mDynamicObjects.push_back(static_cast<TransformedMeshInstance *>(obj));
477                        break;
478
479#else
480                case Intersectable::SCENEGRAPHLEAF_INTERSECTABLE:
481                        mDynamicObjects.push_back(static_cast<SceneGraphLeafIntersectable *>(obj)->GetItem());
482                        break;
483#endif
484#endif
485                default:
486                        cerr << "PreparePvs: type " << Intersectable::GetTypeName(obj) << " not handled yet" << endl;
487                }
488        }
489
490        mIndexBufferSize = indexBufferSize;
491}
492
493
494
495
496void QtGlRendererWidget::RenderPvs()
497{
498        if (mUseVbos)
499                glBindBufferARB(GL_ARRAY_BUFFER_ARB, mVboId);
500
501        ++ mCurrentFrame;
502
503        EnableDrawArrays();
504        //Intersectable::NewMail();
505       
506        if (mDetectEmptyViewSpace)
507                glEnable(GL_CULL_FACE);
508        else
509                glDisable(GL_CULL_FACE);
510
511        ViewCell *viewcell = NULL;
512        viewcell = mViewCellsManager->GetViewCell(mViewPoint, true);
513
514        if (viewcell)
515        {
516#if 0
517                // copy the pvs so that it can be filtered ...
518                if (PvsChanged(viewcell))
519                {
520                        mPvsCache.Reset();
521                        mPvsCache.mViewCell = viewcell;
522                        mPvsCache.mUnfilteredPvsSize = viewcell->GetPvs().GetSize();
523
524                        if (mUseSpatialFilter)
525                        {
526                                //mMutex.lock();
527                                // mSpatialFilter size is in range 0.001 - 0.1
528                                mViewCellsManager->ApplyFilter2(viewcell,
529                                        mUseFilter,
530                                        100.0f * mSpatialFilterSize,
531                                        mPvsCache.mPvs,         
532                                        &mPvsCache.filteredBoxes);
533                                //mPvsCache.mPvs = pvs;
534                                //mMutex.unlock();
535                                //cout << "pvs size: " << mPvsCache.mPvs.GetSize() << endl;
536                        }
537                        else
538                        {
539                                mPvsCache.mPvs = viewcell->GetPvs();
540                        }
541                       
542                        // update the indices for rendering
543                        PreparePvs2(mPvsCache.mPvs);
544                        emit PvsUpdated();
545                        mCurrentPvsCost = mPvsCache.mPvs.EvalPvsCost();
546                }
547#else
548       
549                PreparePvs2(viewcell->GetPvs());
550                emit PvsUpdated();
551
552#endif
553
554
555                // Render PVS
556                if (mUseSpatialFilter && mRenderBoxes)
557                {
558                        for (int i=0; i < mPvsCache.filteredBoxes.size(); ++ i)
559                        {
560                                RenderBox(mPvsCache.filteredBoxes[i]);
561                        }
562                }
563                else
564                {
565                        if (!mRenderVisibilityEstimates && !mUseRandomColorPerPvsObject)
566                                _RenderPvs();
567                        else
568                                _RenderColoredPvs();
569                }
570
571                if (mRenderFilter)
572                {
573                        mWireFrame = true;
574                        RenderIntersectable(viewcell);
575                       
576                        /*glPushMatrix();
577                        glTranslatef(mViewPoint.x, mViewPoint.y, mViewPoint.z);
578                        glScalef(5.0f,5.0f,5.0f);
579                        glPushAttrib(GL_CURRENT_BIT);
580                        glColor3f(1.0f, 0.0f, 0.0f);
581                        //        gluSphere((::GLUquadric *)mSphere,
582                        //                              1e-3*Magnitude(mViewCellsManager->GetViewSpaceBox().Size()), 6, 6);
583                        glPopAttrib();
584                        glPopMatrix();
585                        */
586                        mWireFrame = false;
587                }
588                mCurrentPvsCost = viewcell->GetPvs().EvalPvsCost();
589        }
590        else
591        {
592                //ObjectContainer::const_iterator oi = mObjects.begin();
593                //for (; oi != mObjects.end(); oi++)
594                //      RenderIntersectable(*oi);
595                RenderScene();
596        }
597
598        //cout << "vp: " << mViewPoint << " vd: " << mViewDirection << endl;
599}
600
601float
602QtGlRendererWidget::RenderErrors()
603{
604        float pErrorPixels = -1.0f;
605
606        SetupCamera();
607        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
608
609        glPushAttrib(GL_ENABLE_BIT);
610
611        glStencilFunc(GL_EQUAL, 0x0, 0x1);
612        glStencilOp(GL_KEEP, GL_KEEP, GL_INCR);
613
614        //mUseForcedColors = true;
615
616        //      glColor3f(0.0f, 0.8f, 0.2f);
617        //      glColor3f(0.5f, 0.5f, 0.5f);
618
619        glColor3f(0.6f, 0.6f, 0.6f);
620
621        // Render PVS
622        RenderPvs();
623
624        glEnable(GL_STENCIL_TEST);
625
626        //mUseFalseColors = true;
627
628        glDisable(GL_LIGHTING);
629
630        OcclusionQuery *query = mOcclusionQueries[0];
631        query->BeginQuery();
632
633        SetupCamera();
634
635        mUseForcedColors = true;
636
637        glColor3f(1.0f, 0.0f, 0.0f);
638
639        RenderScene();
640
641        mUseForcedColors = false;
642
643        query->EndQuery();
644
645        glDisable(GL_STENCIL_TEST);
646        glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
647
648        glPopAttrib();
649
650        // reenable other state
651        //  int wait=0;
652        //  while (!query.ResultAvailable()) {
653        //      wait++;
654        //  }
655
656        int pixelCount = query->GetQueryResult();
657        pErrorPixels = ((float)pixelCount)/(GetWidth()*GetHeight());
658        if (0) cout<<"error pixels="<<pixelCount<<endl;
659
660        mRenderError = pErrorPixels;
661
662        return pErrorPixels;
663}
664
665
666void QtGlRendererWidget::timerEvent(QTimerEvent *event)
667{
668        //std::cout << "Timer ID:" << event->timerId();
669        update();
670}
671
672
673void QtGlRendererWidget::mousePressEvent(QMouseEvent *e)
674{
675        int x = e->pos().x();
676        int y = e->pos().y();
677
678        mousePoint.x = x;
679        mousePoint.y = y;
680
681}
682
683void QtGlRendererWidget::mouseReleaseEvent(QMouseEvent *e)
684{
685
686  if (e->modifiers() & Qt::AltModifier)
687        if (mCurrentDynamicObjectIdx >= 0) {
688          //      preprocessor->ScheduleUpdateDynamicObjects();
689        }
690}
691
692void
693QtGlRendererWidget::mouseMoveEvent(QMouseEvent *e)
694{
695        float MOVE_SENSITIVITY = Magnitude(mSceneGraph->GetBox().Diagonal())*1e-3;
696        float TURN_SENSITIVITY = 0.1f;
697        float TILT_SENSITIVITY = 32.0 ;
698        float TURN_ANGLE= M_PI  /36.0 ;
699
700        int x = e->pos().x();
701        int y = e->pos().y();
702
703        int diffx = -(mousePoint.x - x);
704        int diffy = -(mousePoint.y - y);
705
706        const float t = 1.0f;
707
708        if (e->modifiers() & Qt::ControlModifier)
709        {
710                mViewPoint.y += (y-mousePoint.y)*MOVE_SENSITIVITY / 2.0;
711                mViewPoint.x += (x-mousePoint.x)*MOVE_SENSITIVITY / 2.0;
712        }
713#if DYNAMIC_OBJECTS_HACK
714        else if (e->modifiers() & Qt::AltModifier)
715        {
716                if (mCurrentDynamicObjectIdx >= 0)
717                {
718                        Matrix4x4 tm;
719
720                        switch (mTrafoType)
721                        {
722                        case 0:
723                                {
724                                  if (e->modifiers() & Qt::ShiftModifier) {
725                                        const Vector3 transl(0, diffy, 0);
726                                        tm = TranslationMatrix(transl);
727                                 
728                                  } else {
729                                        const Vector3 transl(diffx, 0, diffy);
730                                        tm = TranslationMatrix(transl);
731                                  }
732                                }
733                                break;
734                        case 1:
735                                {
736                                        float scalef = 1.0f + 0.01f * (diffx + diffy);
737                                        if (scalef < 0.9) scalef = 0.9f;
738                                        else if (scalef > 1.1f) scalef = 1.1f;
739                                        tm = ScaleMatrix(scalef, scalef, scalef);
740                                }
741                                break;
742                        case 2:
743                                {
744                                  //                                    tm = RotationXMatrix(diffx) * RotationYMatrix(diffy);
745                                  tm = RotationYMatrix(diffx);
746                                }
747                                break;
748                        default:
749                                cerr << "not implemented" << endl;
750                        }
751
752#if USE_TRANSFORMED_MESH_INSTANCE_HACK
753                        TransformedMeshInstance *tmi = mViewCellsManager->GetPreprocessor()->mDynamicObjects[mCurrentDynamicObjectIdx];
754                        tmi->ApplyWorldTransform(tm);
755#else
756                        SceneGraphLeaf *l =
757                          mViewCellsManager->GetPreprocessor()->mDynamicObjects[mCurrentDynamicObjectIdx];
758                        l->ApplyTransform(tm);
759                       
760#endif
761                        updateGL();
762                }
763        }
764#endif
765        else
766        {
767                mViewPoint += mViewDirection*((mousePoint.y - y)*MOVE_SENSITIVITY);
768                float adiff = TURN_ANGLE*(x - mousePoint.x)*-TURN_SENSITIVITY;
769                float angle = atan2(mViewDirection.x, mViewDirection.z);
770                mViewDirection.x = sin(angle + adiff);
771                mViewDirection.z = cos(angle + adiff);
772        }
773
774        mousePoint.x = x;
775        mousePoint.y = y;
776
777        updateGL();
778}
779
780
781void
782QtGlRendererWidget::resizeGL(int w, int h)
783{
784        SetupCameraProjection(w, h);
785        updateGL();
786}
787
788void
789QtGlRendererWidget::paintGL()
790{
791        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
792
793        if (1)
794        {
795                SetupCameraProjection(width(), height());
796                SetupCamera();
797
798                if (mRenderErrors)
799                {
800                        RenderErrors();
801                }
802                else
803                {
804                        glColor3f(0.6f, 0.6f, 0.6f);
805                        RenderPvs();
806                }
807
808                if (mShowRays)
809                {
810                        RenderRays(mViewCellsManager->mVizBuffer.GetRays(), mRayVisualizationMethod, mShowDistribution, 1);
811                }
812        }
813
814        RenderInfo();
815        mFrame ++;
816        //      cout<<"vp="<<mViewPoint<<" vd="<<mViewDirection<<endl;
817}
818
819
820void
821QtGlRendererWidget::SetupCamera()
822{
823        if (!mTopView)
824                GlRenderer::SetupCamera();
825        else
826        {
827                if (0)
828                {
829                        float dist = Magnitude(mSceneGraph->GetBox().Diagonal())*0.05;
830                        Vector3 pos = mViewPoint - dist*Vector3(mViewDirection.x,
831                                -1,
832                                mViewDirection.y);
833
834                        Vector3 target = mViewPoint + dist*mViewDirection;
835                        Vector3 up(0,1,0);
836
837                        glLoadIdentity();
838                        gluLookAt(pos.x, pos.y, pos.z,
839                                target.x, target.y, target.z,
840                                up.x, up.y, up.z);
841                }
842                else
843                {
844                        float dist = Magnitude(mSceneGraph->GetBox().Diagonal()) * mTopDistance;
845                        Vector3 pos = mViewPoint  + dist * Vector3(0,   1, 0);
846
847                        Vector3 target = mViewPoint;
848                        Vector3 up(mViewDirection.x, 0, mViewDirection.z);
849
850                        glLoadIdentity();
851                        gluLookAt(pos.x, pos.y, pos.z,
852                                target.x, target.y, target.z,
853                                up.x, up.y, up.z);
854
855                }
856        }
857
858}
859
860
861void
862QtGlRendererWidget::keyPressEvent ( QKeyEvent * e )
863{
864        switch (e->key())
865        {
866case Qt::Key_E:
867        mRenderErrors = !mRenderErrors;
868        updateGL();
869        break;
870case Qt::Key_R:
871        mUseRandomColorPerPvsObject = !mUseRandomColorPerPvsObject;;
872        updateGL();
873        break;
874case Qt::Key_T:
875        mTopView = !mTopView;
876        SetupCameraProjection(width(), height());
877        updateGL();
878        break;
879case Qt::Key_V:
880        mRenderViewCells = !mRenderViewCells;
881        updateGL();
882        break;
883case Qt::Key_P:
884        // set random viewpoint
885        mViewCellsManager->GetViewPoint(mViewPoint);
886        updateGL();
887        break;
888case Qt::Key_S: {
889        // set view poitn and direction
890        QString text;
891        bool ok;
892        text.sprintf("%f %f %f", mViewPoint.x, mViewPoint.y, mViewPoint.z);
893        text = QInputDialog::getText(this,
894                "Enter a view point",
895                "",
896                QLineEdit::Normal,
897                text,
898                &ok);
899        if (!ok)
900                break;
901
902        if (sscanf(text.toAscii(), "%f %f %f", &mViewPoint.x, &mViewPoint.y, &mViewPoint.z) == 3) {
903                text.sprintf("%f %f %f", mViewDirection.x, mViewDirection.y, mViewDirection.z);
904                text = QInputDialog::getText(this,
905                        "Enter a direction",
906                        "",
907                        QLineEdit::Normal,
908                        text,
909                        &ok);
910                if (!ok)
911                        break;
912                if (sscanf(text.toAscii(), "%f %f %f", &mViewDirection.x,
913                        &mViewDirection.y, &mViewDirection.z) == 3) {
914                                updateGL();
915                        }
916                        break;
917        }
918                                }
919default:
920        cerr << "unknown key" << endl;
921        e->ignore();
922        break;
923        }
924}
925
926
927
928QtGlRendererWidget::QtGlRendererWidget(
929                                                                           SceneGraph *sceneGraph,
930                                                                           ViewCellsManager *viewcells,
931                                                                           KdTree *tree,
932                                                                           QWidget * parent,
933                                                                           const QGLWidget * shareWidget,
934                                                                           Qt::WFlags f
935                                                                           )
936                                                                           :
937GlRendererWidget(sceneGraph, viewcells, tree), QGLWidget(QGLFormat(QGL::SampleBuffers), parent, shareWidget, f)
938{
939        mPreprocessorThread = NULL;
940        mTopView = false;
941        mRenderViewCells = false;
942        mTopDistance = 1.0f;
943        mCutViewCells = false;
944        mCutScene = false;
945        mRenderErrors = false;
946        mRenderBoxes = false;
947        mRenderFilter = true;
948        mRenderVisibilityEstimates = false;
949        //mRenderVisibilityEstimates = true;
950
951        mComputeGVS = false;
952        mUseRandomColorPerPvsObject = false;
953
954        mHideByCost = false;
955        mUseTransparency = false;
956
957        mTransferFunction = 1.0f;
958        mIndexBufferSize = 0;
959
960        const int delay = 250; // in milliseconds
961        timerId = startTimer(delay);
962
963        bool tmp;
964
965        Environment::GetSingleton()->GetBoolValue("Preprocessor.applyVisibilityFilter", tmp );
966        mUseFilter = tmp;
967
968        Environment::GetSingleton()->GetBoolValue("Preprocessor.applyVisibilitySpatialFilter", tmp );
969        mUseSpatialFilter = tmp;
970
971        //mLogWriter = new LogWriter("myfile.out");
972
973        mShowRenderCost = false;
974        mShowPvsSizes = false;
975        mShowComparison = false;
976        mShowPiercingRays = false;
977        mShowWeightedRays = false;
978        mUseStandardColors = false;
979        mShowWeightedCost = true;
980
981        mShowDistanceWeightedPvs = true;
982        mShowDistanceWeightedTriangles = false;
983        mShowWeightedTriangles = false;
984        mShowDistribution = 15;
985        mCurrentDynamicObjectIdx = -1;
986
987        mSpatialFilterSize = 0.01;
988        mPvsSize = 0;
989        mRayVisualizationMethod = 0;
990        mTrafoType = 0;
991
992        mRenderError = 0.0f;
993        mShowRays = false;
994
995        SetSceneCut(1000);
996        mControlWidget = new QtRendererControlWidget(NULL);
997
998        connect(mControlWidget, SIGNAL(SetViewCellGranularity(int)), this, SLOT(SetViewCellGranularity(int)));
999        connect(mControlWidget, SIGNAL(SetTransferFunction(int)), this, SLOT(SetTransferFunction(int)));
1000
1001        connect(mControlWidget, SIGNAL(UpdateAllPvs()), this, SLOT(UpdateAllPvs()));
1002        connect(mControlWidget, SIGNAL(ComputeVisibility()), this, SLOT(ComputeVisibility()));
1003        connect(mControlWidget, SIGNAL(StopComputation()), this, SLOT(StopComputation()));
1004        connect(mControlWidget, SIGNAL(SetRandomViewPoint()), this,     SLOT(SetRandomViewPoint()));
1005        connect(mControlWidget, SIGNAL(StoreStatistics(void)), this, SLOT(StoreStatistics(void)));
1006        connect(mControlWidget, SIGNAL(ComputeGVS(void)), this, SLOT(ComputeGVS(void)));
1007        connect(mControlWidget, SIGNAL(LoadObject(void)), this, SLOT(LoadObject(void)));
1008
1009        connect(mControlWidget, SIGNAL(SetSceneCut(int)), this, SLOT(SetSceneCut(int)));
1010        connect(mControlWidget, SIGNAL(SetTopDistance(int)), this, SLOT(SetTopDistance(int)));
1011        connect(mControlWidget, SIGNAL(SetTransparency(int)), this, SLOT(SetTransparency(int)));
1012
1013        connect(mControlWidget, SIGNAL(SetVisibilityFilterSize(int)), this, SLOT(SetVisibilityFilterSize(int)));
1014        connect(mControlWidget, SIGNAL(SetSpatialFilterSize(int)), this, SLOT(SetSpatialFilterSize(int)));
1015        connect(mControlWidget, SIGNAL(SetHidingCost(int)), this, SLOT(SetHidingCost(int)));
1016
1017        connect(mControlWidget, SIGNAL(SetShowViewCells(bool)), this, SLOT(SetShowViewCells(bool)));
1018        connect(mControlWidget, SIGNAL(SetShowRenderCost(bool)), this, SLOT(SetShowRenderCost(bool)));
1019        connect(mControlWidget, SIGNAL(SetUseTransparency(bool)), this, SLOT(SetUseTransparency(bool)));
1020        connect(mControlWidget, SIGNAL(SetShowPvsSizes(bool)), this, SLOT(SetShowPvsSizes(bool)));
1021        connect(mControlWidget, SIGNAL(SetShowComparison(bool)), this, SLOT(SetShowComparison(bool)));
1022        connect(mControlWidget, SIGNAL(SetTopView(bool)), this, SLOT(SetTopView(bool)));
1023        connect(mControlWidget, SIGNAL(SetCutViewCells(bool)), this, SLOT(SetCutViewCells(bool)));
1024        connect(mControlWidget, SIGNAL(SetHideByCost(bool)), this, SLOT(SetHideByCost(bool)));
1025        connect(mControlWidget, SIGNAL(SetCutScene(bool)), this, SLOT(SetCutScene(bool)));
1026        connect(mControlWidget, SIGNAL(SetRenderErrors(bool)), this, SLOT(SetRenderErrors(bool)));
1027        connect(mControlWidget, SIGNAL(SetRenderBoxes(bool)), this, SLOT(SetRenderBoxes(bool)));
1028        connect(mControlWidget, SIGNAL(SetRenderFilter(bool)), this, SLOT(SetRenderFilter(bool)));
1029        connect(mControlWidget, SIGNAL(SetRenderVisibilityEstimates(bool)), this, SLOT(SetRenderVisibilityEstimates(bool)));
1030        connect(mControlWidget, SIGNAL(SetUseFilter(bool)), this, SLOT(SetUseFilter(bool)));
1031        connect(mControlWidget, SIGNAL(SetUseSpatialFilter(bool)), this, SLOT(SetUseSpatialFilter(bool)));
1032        connect(mControlWidget, SIGNAL(SetShowPiercingRays(bool)), this, SLOT(SetShowPiercingRays(bool)));
1033        connect(mControlWidget, SIGNAL(SetShowWireFrame(bool)), this, SLOT(SetShowWireFrame(bool)));
1034        connect(mControlWidget, SIGNAL(SetShowWeightedRays(bool)), this, SLOT(SetShowWeightedRays(bool)));
1035        connect(mControlWidget, SIGNAL(SetShowWeightedCost(bool)), this, SLOT(SetShowWeightedCost(bool)));
1036
1037        connect(mControlWidget, SIGNAL(SetShowDistanceWeightedTriangles(bool)), this, SLOT(SetShowDistanceWeightedTriangles(bool)));
1038        connect(mControlWidget, SIGNAL(SetShowDistanceWeightedPvs(bool)), this, SLOT(SetShowDistanceWeightedPvs(bool)));
1039        connect(mControlWidget, SIGNAL(SetShowWeightedTriangles(bool)), this, SLOT(SetShowWeightedTriangles(bool)));
1040
1041        connect(mControlWidget, SIGNAL(UseConstColorForRayViz(bool)), this, SLOT(UseConstColorForRayViz(bool)));
1042        connect(mControlWidget, SIGNAL(UseRayLengthForRayViz(bool)), this, SLOT(UseRayLengthForRayViz(bool)));
1043        connect(mControlWidget, SIGNAL(SetShowContribution(bool)), this, SLOT(SetShowContribution(bool)));
1044        connect(mControlWidget, SIGNAL(SetShowDistribution(bool)), this, SLOT(SetShowDistribution(bool)));
1045
1046        connect(mControlWidget, SIGNAL(SetShowDistribution1(bool)), this, SLOT(SetShowDistribution1(bool)));
1047        connect(mControlWidget, SIGNAL(SetShowDistribution2(bool)), this, SLOT(SetShowDistribution2(bool)));
1048        connect(mControlWidget, SIGNAL(SetShowDistribution3(bool)), this, SLOT(SetShowDistribution3(bool)));
1049        connect(mControlWidget, SIGNAL(SetShowDistribution4(bool)), this, SLOT(SetShowDistribution4(bool)));
1050
1051        connect(mControlWidget, SIGNAL(SetShowRays(bool)), this, SLOT(SetShowRays(bool)));
1052
1053        connect(mControlWidget, SIGNAL(SetTranslation(bool)), this, SLOT(SetTranslation(bool)));
1054        connect(mControlWidget, SIGNAL(SetRotation(bool)), this, SLOT(SetRotation(bool)));
1055        connect(mControlWidget, SIGNAL(SetScale(bool)), this, SLOT(SetScale(bool)));
1056
1057        connect(mControlWidget, SIGNAL(UpdateDynamicObjects()), this, SLOT(UpdateDynamicObjects()));
1058
1059        setWindowTitle("PVS Visualization");
1060
1061        // setting the main window size here
1062        resize(800, 600);
1063        //resize(640, 400);
1064       
1065        mControlWidget->show();
1066}
1067
1068void
1069QtGlRendererWidget::UpdateAllPvs()
1070{
1071        // $$ does not work so far:(
1072        mViewCellsManager->UpdatePvsForEvaluation();
1073        //      mViewCellsManager->FinalizeViewCells(false);
1074}
1075
1076void
1077QtGlRendererWidget::ComputeVisibility()
1078{
1079        cerr<<"Compute Visibility called!\n"<<endl;
1080        if (!mPreprocessorThread->isRunning())
1081                mPreprocessorThread->RunThread();
1082}
1083
1084void
1085QtGlRendererWidget::StopComputation()
1086{
1087        cerr<<"stop computation called!\n"<<endl;
1088        mViewCellsManager->GetPreprocessor()->mStopComputation = true;
1089}
1090
1091void
1092QtGlRendererWidget::SetRandomViewPoint()
1093{
1094        cerr<<"setting random view point!\n"<<endl;
1095        mViewCellsManager->GetViewPoint(mViewPoint);
1096        updateGL();
1097}
1098
1099
1100void QtGlRendererWidget::StoreStatistics()
1101{
1102        cerr<<"storing statistics!\n"<<endl;
1103        const int currentSamples = mViewCellsManager->GetPreprocessor()->mCurrentSamples;
1104
1105        cout<<"**********************************************" << endl;
1106        cout << "reached " << currentSamples << " samples " << " => writing file" << endl;
1107               
1108        LogWriter writer;
1109        writer.SetFilename("compare.log");
1110        writer.Write(currentSamples, mViewCellsManager->GetViewCells());
1111        cout << "finished writing file" << endl;
1112        mCompareInfo.clear();
1113        updateGL();
1114}
1115
1116
1117void QtGlRendererWidget::LoadObject()
1118{
1119        string filename("../data/teapot.bn");
1120        //string filename("../data/cube.obj");
1121       
1122        cout << "Loading model << " << filename << endl;
1123
1124        ++ mCurrentDynamicObjectIdx;
1125
1126        if (mViewCellsManager->GetPreprocessor()->LoadDynamicGeometry(filename)) {
1127         
1128          cout << "Loading finished" << endl;
1129        } else
1130          cerr << "Loading failed" << endl;
1131       
1132    updateGL();
1133}
1134
1135void
1136QtGlRendererWidget::RenderRenderCost()
1137{
1138        static vector<float> costFunction;
1139        static float maxCost = -1;
1140        if (costFunction.size()==0) {
1141                ViewCellsTree *tree = mViewCellsManager->GetViewCellsTree();
1142                if (tree) {
1143                        tree->GetCostFunction(costFunction);
1144                        maxCost = -1;
1145                        for (int i=0;  i < costFunction.size(); i++) {
1146                                //                cout<<i<<":"<<costFunction[i]<<" ";
1147                                // update cost function to an absolute value based on the total geometry count
1148                                costFunction[i] *= mSceneGraph->GetSize();
1149                                if (costFunction[i] > maxCost)
1150                                        maxCost = costFunction[i];
1151                        }
1152                }
1153        }
1154
1155
1156        int currentPos = (int)mViewCellsManager->GetViewCells().size();
1157        float currentCost= -1;
1158
1159        if (currentPos < costFunction.size())
1160                currentCost = costFunction[currentPos];
1161#if 1   
1162        cout<<"costFunction.size()="<<(int)costFunction.size()<<endl;
1163        cout<<"CP="<<currentPos<<endl;
1164        cout<<"MC="<<maxCost<<endl;
1165        cout<<"CC="<<currentCost<<endl;
1166#endif
1167        if (costFunction.size()) {
1168                float scaley = 1.0f/log10(maxCost);
1169                float scalex = 1.0f/(float)costFunction.size();
1170
1171                glDisable(GL_DEPTH_TEST);
1172                // init ortographic projection
1173                glMatrixMode(GL_PROJECTION);
1174
1175                glPushMatrix();
1176
1177                glLoadIdentity();
1178                gluOrtho2D(0, 1.0f, 0, 1.0f);
1179
1180                glTranslatef(0.1f, 0.1f, 0.0f);
1181                glScalef(0.8f, 0.8f, 1.0f);
1182                glMatrixMode(GL_MODELVIEW);
1183                glLoadIdentity();
1184
1185                glColor3f(1.0f,0,0);
1186                glBegin(GL_LINE_STRIP);
1187                //        glVertex3f(0,0,0);
1188
1189                for (int i=0;  i < costFunction.size(); i++) {
1190                        float x =  i*scalex;
1191                        float y = log10(costFunction[i])*scaley;
1192                        glVertex3f(x,y,0.0f);
1193                }
1194                glEnd();
1195
1196                glColor3f(1.0f,0,0);
1197                glBegin(GL_LINES);
1198                float x =  currentPos*scalex;
1199                glVertex3f(x,0.0,0.0f);
1200                glVertex3f(x,1.0f,0.0f);
1201                glEnd();
1202
1203                glColor3f(0.0f,0,0);
1204                // show a grid
1205                glBegin(GL_LINE_LOOP);
1206                glVertex3f(0,0,0.0f);
1207                glVertex3f(1,0,0.0f);
1208                glVertex3f(1,1,0.0f);
1209                glVertex3f(0,1,0.0f);
1210                glEnd();
1211
1212                glBegin(GL_LINES);
1213                for (int i=0;  i < costFunction.size(); i += 1000) {
1214                        float x =  i*scalex;
1215                        glVertex3f(x,0.0,0.0f);
1216                        glVertex3f(x,1.0f,0.0f);
1217                }
1218
1219                for (int i=0;  pow(10.0f, i) < maxCost; i+=1) {
1220                        float y = i*scaley;
1221                        //              QString s;
1222                        //              s.sprintf("%d", (int)pow(10,i));
1223                        //              renderText(width()/2+5, y*height(), s);
1224                        glVertex3f(0.0f, y, 0.0f);
1225                        glVertex3f(1.0f, y, 0.0f);
1226                }
1227
1228                glEnd();
1229
1230
1231                // restore the projection matrix
1232                glMatrixMode(GL_PROJECTION);
1233                glPopMatrix();
1234                glMatrixMode(GL_MODELVIEW);
1235                glEnable(GL_DEPTH_TEST);
1236
1237        }
1238
1239
1240
1241}
1242
1243void
1244QtGlRendererWidget::RenderInfo()
1245{
1246
1247        QString s;
1248
1249        int vc = 0;
1250        if (mViewCellsManager)
1251                vc = (int)mViewCellsManager->GetViewCells().size();
1252
1253        int filter = 0;
1254        if (mViewCellsManager)
1255                filter = mViewCellsManager->GetMaxFilterSize();
1256
1257        glColor3f(0.0f, 0.0f, 1.0f);
1258
1259#if REMOVE_TEMPORARY
1260        s.sprintf("frame:%04d viewpoint:(%4.1f,%4.1f,%4.1f) dir:(%4.1f,%4.1f,%4.1f)",
1261                mFrame,
1262                mViewPoint.x,
1263                mViewPoint.y,
1264                mViewPoint.z,
1265                mViewDirection.x,
1266                mViewDirection.y,
1267                mViewDirection.z
1268                );
1269
1270        renderText(20, 20, s);
1271
1272        s.sprintf("viewcells:%04d filter:%04d pvs:%04d error:%5.5f %",
1273                vc,
1274                filter,
1275                mPvsSize,
1276                mRenderError*100.0f);
1277
1278        renderText(20, 40, s);
1279#endif
1280
1281        QFont font40; font40.setPointSize(30);
1282        //s.sprintf("PVS: %04d", mPvsSize);
1283        //renderText(20, 40, s, font40);
1284       
1285        //s.sprintf("RAW TRI: %07d", mViewCellsManager->GetPreprocessor()->mGenericStats);
1286        //renderText(290, 40, s, font40);
1287        //s.sprintf("PVS TRI: %07d", mViewCellsManager->GetPreprocessor()->mGenericStats2);
1288        //renderText(290, 70, s, font40);
1289
1290        s.sprintf("PVS TRI: %07d", (int)mCurrentPvsCost);
1291        renderText(290, 70, s, font40);
1292        //renderText(290, 70, s, font40);
1293
1294}
1295
1296
1297void
1298QtGlRendererWidget::SetViewCellGranularity(int number)
1299{
1300        if (mViewCellsManager)
1301        {
1302                //      mViewCellsManager->SetMaxFilterSize(number);
1303
1304                // $$ tmp off
1305                mViewCellsManager->CollectViewCells(number);
1306
1307                // $$ does not work so far:(
1308                //      mViewCellsManager->UpdatePvsForEvaluation();
1309                //      mViewCellsManager->FinalizeViewCells(false);
1310        }
1311        updateGL();
1312}
1313
1314void
1315QtGlRendererWidget::SetVisibilityFilterSize(int number)
1316{
1317        if (mViewCellsManager)
1318                mViewCellsManager->SetMaxFilterSize(number);
1319
1320        mPvsCache.Reset();
1321        updateGL();
1322}
1323
1324void
1325QtGlRendererWidget::SetSpatialFilterSize(int number)
1326{
1327        mSpatialFilterSize = 1e-3*number;
1328        mPvsCache.Reset();
1329        updateGL();
1330}
1331
1332void
1333QtGlRendererWidget::SetSceneCut(int number)
1334{
1335        // assume the cut plane can only be aligned with xz plane
1336        // shift it along y according to number, which is percentage of the bounding
1337        // box position
1338        if (mViewCellsManager)
1339        {
1340                const float f = number / 1000.0f;
1341                AxisAlignedBox3 box = mViewCellsManager->GetViewSpaceBox();
1342                Vector3 p = (1.0f - f) * box.Min() + f * box.Max();
1343                mSceneCutPlane.mNormal = Vector3(0, -1, 0);
1344                mSceneCutPlane.mD = -DotProd(mSceneCutPlane.mNormal, p);
1345
1346                updateGL();
1347        }
1348}
1349
1350void
1351QtGlRendererWidget::SetHidingCost(int number)
1352{
1353        mHidingCost = (float)number / 1000.0f;
1354}
1355
1356
1357void
1358QtGlRendererWidget::SetTopDistance(int number)
1359{
1360        mTopDistance = number / 1000.0f;
1361        updateGL();
1362}
1363
1364void QtGlRendererWidget::SetTransparency(int number)
1365{
1366        mTransparency = number / 1000.0f;
1367        updateGL();
1368}
1369
1370#if 0
1371float QtGlRendererWidget::ComputeRenderCost(ViewCell *vc)
1372{
1373        ObjectPvs basePvs;
1374       
1375        basePvs = vc->CopyPvs();
1376        ObjectPvsIterator pit = basePvs.GetIterator();
1377
1378        float renderCost = 0;
1379
1380        //cout << "cost vis: " << mShowDistanceWeightedPvs << " " << " " << mShowDistanceWeightedTriangles << " " << mShowWeightedTriangles << endl;
1381       
1382        // first mark all objects from this pvs
1383        while (pit.HasMoreEntries())   
1384        {
1385                KdIntersectable *kdObj = static_cast<KdIntersectable *>(pit.Next());
1386
1387                if (mShowDistanceWeightedPvs)
1388                {
1389                        const AxisAlignedBox3 box = kdObj->GetBox();
1390
1391                        const float dist = SqrDistance(vc->GetBox().Center(), box.Center());
1392                        renderCost += 1.0f / dist;
1393                }
1394                else if (mShowDistanceWeightedTriangles)
1395                {
1396                        const AxisAlignedBox3 box = kdObj->GetBox();
1397
1398                        const float dist = SqrDistance(vc->GetBox().Center(), box.Center());
1399                        renderCost += kdObj->ComputeNumTriangles() / dist;
1400                }
1401                else //if (mShowWeightedTriangles)
1402                {
1403                        renderCost += kdObj->ComputeNumTriangles();
1404                }
1405                //if (pit.Next()->Mail();
1406        }
1407
1408        return renderCost;
1409}
1410#else
1411
1412float QtGlRendererWidget::ComputeRenderCost(ViewCell *vc)
1413{
1414        float renderCost = 0;
1415
1416#ifdef USE_VERBOSE_PVS
1417        if (mShowDistanceWeightedPvs)
1418        {
1419                return vc->GetPvs().mStats.mDistanceWeightedPvs;
1420        }
1421        else if (mShowDistanceWeightedTriangles)
1422        {
1423                return vc->GetPvs().mStats.mDistanceWeightedTriangles;
1424        }
1425        else //if (mShowWeightedTriangles)
1426        {
1427                return vc->GetPvs().mStats.mWeightedTriangles;
1428        }
1429#else
1430        return 0.0f;
1431#endif
1432}
1433#endif
1434
1435
1436
1437void QtGlRendererWidget::ComputeMaxValues(const ViewCellContainer &viewCells,
1438                                                                                  int &maxPvs,
1439                                                                                  int &maxPiercingRays,
1440                                                                                  float &maxRelativeRays,
1441                                                                                  float &maxRcCost)
1442{
1443        maxPvs = -1;
1444        maxPiercingRays = 1; // not zero for savety
1445        maxRelativeRays = Limits::Small; // not zero for savety
1446        maxRcCost = Limits::Small;
1447
1448        for (size_t i = 0; i < viewCells.size(); ++ i)
1449        {
1450                ViewCell *vc = viewCells[i];
1451
1452                if (mShowPvsSizes) // pvs size
1453                {
1454                        //const int p = vc->GetPvs().CountObjectsInPvs();
1455                        const int p = vc->GetPvs().GetSize();
1456                        if (p > maxPvs)
1457                                maxPvs = p;
1458                }
1459                else if (mShowPiercingRays) // relative number of rays
1460                {
1461                        const int piercingRays = vc->GetNumPiercingRays();
1462
1463                        if (piercingRays > maxPiercingRays)
1464                                maxPiercingRays = piercingRays;
1465                }
1466                else if (mShowWeightedRays)
1467                {
1468                        const int piercingRays = vc->GetNumPiercingRays();
1469
1470                        const float relativeArea =
1471                                vc->GetBox().SurfaceArea() / mViewCellsManager->GetViewSpaceBox().SurfaceArea();
1472
1473                        if ((float)piercingRays / relativeArea > maxRelativeRays)
1474                                maxRelativeRays = (float)piercingRays / relativeArea;
1475                }
1476                else if (mShowWeightedCost)
1477                {
1478                        const float rcCost = ComputeRenderCost(vc);
1479                        mViewCellsManager->UpdateScalarPvsCost(vc, rcCost);
1480
1481                        if (rcCost > maxRcCost)
1482                                maxRcCost = rcCost;
1483                }
1484        }
1485}
1486
1487
1488void QtGlRendererWidget::RenderViewCells()
1489{
1490        mUseFalseColors = true;
1491        //glPushAttrib(GL_CURRENT_BIT | GL_ENABLE_BIT | GL_POLYGON_BIT);
1492
1493        glEnable(GL_CULL_FACE);
1494        glCullFace(GL_FRONT);
1495        //glDisable(GL_CULL_FACE);
1496
1497        if (mCutViewCells)
1498        {
1499                double eq[4];
1500                eq[0] = mSceneCutPlane.mNormal.x;
1501                eq[1] = mSceneCutPlane.mNormal.y;
1502                eq[2] = mSceneCutPlane.mNormal.z;
1503                eq[3] = mSceneCutPlane.mD;
1504
1505                glClipPlane(GL_CLIP_PLANE0, eq);
1506                glEnable(GL_CLIP_PLANE0);
1507        }
1508
1509        ViewCellContainer &viewcells = mViewCellsManager->GetViewCells();
1510       
1511        int maxPvs, maxPiercingRays;
1512        float maxRelativeRays, maxRcCost;
1513
1514        ComputeMaxValues(viewcells, maxPvs, maxPiercingRays, maxRelativeRays, maxRcCost);
1515       
1516        // matt: temp hack
1517        //maxRcCost = 5000.0f;
1518        //cout << "maxRcCost: " << maxRcCost << endl;
1519
1520        int i;
1521
1522        // transparency
1523        if (!mUseTransparency)
1524        {
1525                glEnable(GL_DEPTH_TEST);
1526                glDisable(GL_BLEND);
1527        }
1528        else
1529        {
1530                glDisable(GL_DEPTH_TEST);
1531                glEnable(GL_BLEND);
1532
1533                glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1534                //glBlendFunc(GL_SRC_ALPHA, GL_ONE);
1535
1536                for (i = 0; i < viewcells.size(); ++ i)
1537                {
1538                        ViewCell *vc = viewcells[i];
1539
1540                        const float dist = SqrDistance(mDummyViewPoint, vc->GetBox().Center());
1541                        vc->SetDistance(dist);
1542                }
1543
1544                sort(viewcells.begin(), viewcells.end(), nearerThan);
1545        }
1546
1547        //mWireFrame = true;
1548               
1549        // normal rendering
1550        //if (!mShowPvsSizes && !mShowPiercingRays && !mShowWeightedRays && !mShowWeightedCost && !mShowComparison)
1551        if (mUseStandardColors)
1552        {
1553                for (i = 0; i < viewcells.size(); ++ i)
1554                {
1555                        ViewCell *vc = viewcells[i];
1556                        RgbColor c;
1557
1558                        //if (!mShowPvsSizes && !mShowPiercingRays)
1559                        c = vc->GetColor();
1560                       
1561                        glColor3f(c.r, c.g, c.b);
1562
1563                        if (!mHideByCost || (mHidingCost < (vc->GetNumPiercingRays() / (float)maxPiercingRays)))
1564                        {
1565                RenderViewCell(vc);
1566                        }
1567                }
1568        }
1569        else // using specialised colors
1570        {
1571                if (!mShowComparison)
1572                        AssignImportanceByRelativeValue(viewcells, maxPvs, maxPiercingRays, maxRelativeRays, maxRcCost);
1573                else
1574                {
1575                        if (mCompareInfo.empty())
1576                        {
1577                                LogReader reader;
1578                                reader.SetFilename("compare.log");
1579                                int samples;
1580                                reader.Read(samples, mCompareInfo);
1581                        }
1582
1583                        AssignColorByComparison(viewcells, mCompareInfo);
1584                }
1585
1586                glEnable(GL_DEPTH_TEST);       
1587        }
1588       
1589        glEnable(GL_CULL_FACE);
1590        glDisable(GL_CULL_FACE);
1591
1592        glDisable(GL_CLIP_PLANE0);
1593
1594        mUseFalseColors = false;
1595        mWireFrame = false;
1596
1597        glPopAttrib();
1598}
1599
1600
1601void QtGlRendererWidget::AssignImportanceByRelativeValue(const ViewCellContainer &viewCells,
1602                                                                                                                 int &maxPvs,
1603                                                                                                                 int &maxPiercingRays,
1604                                                                                                                 float &maxRelativeRays,
1605                                                                                                                 float &maxRcCost)
1606{
1607        for (size_t i = 0; i < viewCells.size(); ++ i)
1608        {
1609                RgbColor c;
1610                ViewCell *vc = viewCells[i];
1611
1612                float importance;
1613
1614                if (mShowPiercingRays)
1615                {
1616                        importance = mTransferFunction *
1617                                ((float)vc->GetNumPiercingRays() / (float)maxPiercingRays);
1618                }
1619                else if (mShowWeightedRays) // relative number of rays
1620                {
1621                        float relativeArea = vc->GetBox().SurfaceArea() / mViewCellsManager->GetViewSpaceBox().SurfaceArea();
1622
1623                        if (relativeArea < Limits::Small)
1624                                relativeArea = Limits::Small;
1625
1626                        importance = mTransferFunction * ((float)vc->GetNumPiercingRays() / relativeArea) / maxRelativeRays;
1627                }
1628                else if (mShowPvsSizes) // pvs size
1629                {
1630                        importance = mTransferFunction *
1631                                ((float)vc->GetPvs().GetSize() / (float)maxPvs);
1632                } // weighted render cost
1633                else if (mShowWeightedCost)
1634                {
1635                        const float rcCost = mTransferFunction * ComputeRenderCost(vc);
1636                        importance = rcCost / maxRcCost;
1637                }
1638if (importance > 1.0f) importance = 1.0f;
1639                // c = RgbColor(importance, 1.0f - importance, 0.0f);
1640                c = RainbowColorMapping(importance);
1641
1642                glColor4f(c.r, c.g, c.b, 1.0f - mTransparency);
1643
1644                if (!mHideByCost || (mHidingCost < importance))
1645                {
1646                        RenderViewCell(vc);
1647                }
1648        }
1649}
1650
1651
1652void QtGlRendererWidget::AssignColorByComparison(const ViewCellContainer &viewcells,
1653                                                                                                 //const ViewCellInfoContainer &infos1,
1654                                                                                                 const ViewCellInfoContainer &compareInfo)
1655{
1656        //cout << "comparing " << viewcells.size() << " view cells to " << compareInfo.size() << " values" << endl;
1657
1658        if (viewcells.size() > compareInfo.size())
1659        {
1660                cerr << "loaded size (" << (int)compareInfo.size() << ") does not fit to view cells size (" << (int)viewcells.size() << ")" << endl;
1661                return;
1662        }
1663
1664        //const float maxRatio = 1.0f;
1665        const float maxRatio = 2.0f;
1666        const float minRatio = 0.0f;
1667
1668        const float scale = 1.0f / (maxRatio - minRatio);
1669
1670        for (size_t i = 0; i < viewcells.size(); ++ i)
1671        {
1672                RgbColor c;
1673                ViewCell *vc = viewcells[i];
1674
1675                //ViewCellInfo vc1Info = infos1[i];
1676                ViewCellInfo vc2Info = compareInfo[i];
1677
1678                //const float vcRatio = vc->GetNumPiercingRays() / vc2Info.mPiercingRays + Limits::Small;
1679                float vcRatio = 1.0f;//maxRatio;
1680               
1681                if (vc2Info.mPvsSize > Limits::Small)
1682                        vcRatio = (float)vc->GetPvs().GetSize() / vc2Info.mPvsSize;
1683
1684                // truncate here
1685                if (vcRatio > maxRatio) vcRatio = 1.0f;
1686
1687                const float importance = (vcRatio - minRatio) * scale;
1688       
1689                if (0 && (i < 20))
1690                {
1691                        cout << "pvs1: " << vc->GetPvs().GetSize() << " pvs2: " << compareInfo[i].mPvsSize << " importance: " << importance << endl;
1692                }
1693
1694                // c = RgbColor(importance, 1.0f - importance, 0.0f);
1695                c = RainbowColorMapping(importance);
1696
1697                glColor4f(c.r, c.g, c.b, 1.0f);
1698
1699                if (1)//!mHideByCost || (mHidingCost < importance))
1700                {
1701                        RenderViewCell(vc);
1702                }
1703        }
1704}
1705
1706
1707
1708/**********************************************************************/
1709/*              QtRendererControlWidget implementation                */
1710/**********************************************************************/
1711
1712
1713QGroupBox *QtRendererControlWidget::CreateVisualizationPanel(QWidget *parent)
1714{
1715        QRadioButton *rb1, *rb2, *rb3, *rb4, *rb5;
1716       
1717        rb1 = new QRadioButton("random colors", parent);
1718        rb1->setText("random");
1719        connect(rb1, SIGNAL(toggled(bool)), SIGNAL(SetShowWireFrame(bool)));
1720
1721        // Create a check box to be in the group box
1722        rb2 = new QRadioButton("piercing rays", parent);
1723        rb2->setText("piercing rays");
1724        //vl->addWidget(rb1);
1725        connect(rb2, SIGNAL(toggled(bool)), SIGNAL(SetShowPiercingRays(bool)));
1726
1727        rb3 = new QRadioButton("pvs size", parent);
1728        rb3->setText("pvs size");
1729        connect(rb3, SIGNAL(toggled(bool)), SIGNAL(SetShowPvsSizes(bool)));
1730       
1731        rb4 = new QRadioButton("weighted rays", parent);
1732        rb4->setText("weighted rays");
1733        connect(rb4, SIGNAL(toggled(bool)), SIGNAL(SetShowWeightedRays(bool)));
1734       
1735        rb5 = new QRadioButton("pvs cost", parent);
1736        rb5->setText("pvs cost");
1737        connect(rb5, SIGNAL(toggled(bool)), SIGNAL(SetShowWeightedCost(bool)));
1738
1739        QGroupBox *groupBox = new QGroupBox("PVS Visualization");
1740
1741        QVBoxLayout *vbox2 = new QVBoxLayout;
1742   
1743        vbox2->addWidget(rb1);
1744        vbox2->addWidget(rb2);
1745        vbox2->addWidget(rb3);
1746        vbox2->addWidget(rb4);
1747        vbox2->addWidget(rb5);
1748       
1749        rb5->setChecked(true);
1750
1751        vbox2->addStretch(1);
1752        groupBox->setLayout(vbox2);
1753
1754        return groupBox;
1755}
1756
1757
1758QGroupBox *QtRendererControlWidget::CreateTrafoPanel(QWidget *parent)
1759{
1760        QRadioButton *rb1, *rb2, *rb3;
1761       
1762        rb1 = new QRadioButton("translation", parent);
1763        rb1->setText("translation");
1764        connect(rb1, SIGNAL(toggled(bool)), SIGNAL(SetTranslation(bool)));
1765
1766        // Create a check box to be in the group box
1767        rb2 = new QRadioButton("scale", parent);
1768        rb2->setText("scale");
1769        //vl->addWidget(rb1);
1770        connect(rb2, SIGNAL(toggled(bool)), SIGNAL(SetScale(bool)));
1771
1772        rb3 = new QRadioButton("rotation", parent);
1773        rb3->setText("rotation");
1774        connect(rb3, SIGNAL(toggled(bool)), SIGNAL(SetRotation(bool)));
1775   
1776        QVBoxLayout *vbox2 = new QVBoxLayout;
1777        QGroupBox *groupBox = new QGroupBox("Trafo types");
1778
1779        vbox2->addWidget(rb1);
1780        vbox2->addWidget(rb2);
1781        vbox2->addWidget(rb3);
1782       
1783        rb1->setChecked(true);
1784
1785        vbox2->addStretch(1);
1786
1787
1788        QPushButton *button = new QPushButton("Update", groupBox);
1789        vbox2->addWidget(button);
1790        connect(button, SIGNAL(clicked()), SIGNAL(UpdateDynamicObjects()));
1791       
1792       
1793        groupBox->setLayout(vbox2);
1794       
1795        return groupBox;
1796}
1797
1798
1799QGroupBox *QtRendererControlWidget::CreateRenderCostPanel(QWidget *parent)
1800{
1801        QRadioButton *rb1, *rb2, *rb3;
1802       
1803        // Create a check box to be in the group box
1804        rb1 = new QRadioButton("triangles", parent);
1805        rb1->setText("triangles");
1806        connect(rb1, SIGNAL(toggled(bool)), SIGNAL(SetShowWeightedTriangles(bool)));
1807
1808        rb2 = new QRadioButton("distance weighted pvs", parent);
1809        rb2->setText("distance weighted");
1810        connect(rb2, SIGNAL(toggled(bool)), SIGNAL(SetShowDistanceWeightedPvs(bool)));
1811
1812        rb3 = new QRadioButton("distance weighted triangles", parent);
1813        rb3->setText("distance weighted triangles");
1814        connect(rb3, SIGNAL(toggled(bool)), SIGNAL(SetShowDistanceWeightedTriangles(bool)));
1815
1816        QGroupBox *groupBox = new QGroupBox("Render cost options");
1817        QVBoxLayout *vbox2 = new QVBoxLayout;
1818   
1819        vbox2->addWidget(rb1);
1820        vbox2->addWidget(rb2);
1821        vbox2->addWidget(rb3);
1822       
1823        rb1->setChecked(true);
1824
1825        vbox2->addStretch(1);
1826        groupBox->setLayout(vbox2);
1827
1828        return groupBox;
1829}
1830
1831
1832QGroupBox *QtRendererControlWidget::CreateRayVisualizationPanel(QWidget *parent)
1833{
1834        QRadioButton *rb1, *rb2, *rb3, *rb4;
1835       
1836        // Create a check box to be in the group box
1837        rb1 = new QRadioButton("const color", parent);
1838        rb1->setText("const color");
1839        //vl->addWidget(rb1);
1840        connect(rb1, SIGNAL(toggled(bool)), SIGNAL(UseConstColorForRayViz(bool)));
1841
1842        rb2 = new QRadioButton("ray length", parent);
1843        rb2->setText("ray length");
1844        connect(rb2, SIGNAL(toggled(bool)), SIGNAL(UseRayLengthForRayViz(bool)));
1845       
1846        rb3 = new QRadioButton("contribution", parent);
1847        rb3->setText("contribution");
1848        connect(rb3, SIGNAL(toggled(bool)), SIGNAL(SetShowContribution(bool)));
1849       
1850        rb4 = new QRadioButton("distribution", parent);
1851        rb4->setText("distribution");
1852        connect(rb4, SIGNAL(toggled(bool)), SIGNAL(SetShowDistribution(bool)));
1853
1854
1855        ///////////////////////////
1856
1857       
1858        QGroupBox *groupBox = new QGroupBox("Ray visualization");
1859        QVBoxLayout *vbox2 = new QVBoxLayout;
1860   
1861        vbox2->addWidget(rb1);
1862        vbox2->addWidget(rb2);
1863        vbox2->addWidget(rb3);
1864        vbox2->addWidget(rb4);
1865       
1866        rb1->setChecked(true);
1867
1868
1869        QCheckBox *cb = new QCheckBox("Distribution 1", parent);
1870        vbox2->addWidget(cb);
1871        cb->setChecked(true);
1872        connect(cb, SIGNAL(toggled(bool)), SIGNAL(SetShowDistribution1(bool)));
1873
1874        cb = new QCheckBox("Distribution 2", parent);
1875        vbox2->addWidget(cb);
1876        cb->setChecked(true);
1877        connect(cb, SIGNAL(toggled(bool)), SIGNAL(SetShowDistribution2(bool)));
1878
1879        cb = new QCheckBox("Distribution 3", parent);
1880        vbox2->addWidget(cb);
1881        cb->setChecked(true);
1882        connect(cb, SIGNAL(toggled(bool)), SIGNAL(SetShowDistribution3(bool)));
1883       
1884        cb = new QCheckBox("Distribution 4", parent);
1885        vbox2->addWidget(cb);
1886        cb->setChecked(true);
1887        connect(cb, SIGNAL(toggled(bool)), SIGNAL(SetShowDistribution4(bool)));
1888
1889
1890        vbox2->addStretch(1);
1891        groupBox->setLayout(vbox2);
1892
1893        return groupBox;
1894}
1895
1896
1897QtRendererControlWidget::QtRendererControlWidget(QWidget * parent, Qt::WFlags f):
1898QWidget(parent, f)
1899{
1900
1901        QVBoxLayout *vl = new QVBoxLayout;
1902        setLayout(vl);
1903       
1904        //QWidget *vbox;
1905
1906        //vbox = new QGroupBox("Render Controls", this);
1907        //layout()->addWidget(vbox);
1908        //vl = new QVBoxLayout;
1909        //vbox->setLayout(vl);
1910
1911        QLabel *label;
1912        QSlider *slider;
1913        QPushButton *button;
1914
1915#if 0//REMOVE_TEMPORARY
1916
1917        label = new QLabel("Granularity");
1918        //vbox->layout()->addWidget(label);
1919        vl->addWidget(label);
1920
1921        slider = new QSlider(Qt::Horizontal);
1922        vl->addWidget(slider);
1923        slider->show();
1924        slider->setRange(1, 10000);
1925        slider->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
1926        slider->setValue(200);
1927
1928        connect(slider, SIGNAL(valueChanged(int)), SIGNAL(SetViewCellGranularity(int)));
1929
1930        ///////////////////////////
1931
1932        label = new QLabel("Transfer function");
1933        vl->addWidget(label);
1934
1935        slider = new QSlider(Qt::Horizontal);
1936        vl->addWidget(slider);
1937        slider->show();
1938        slider->setRange(1, 10000);
1939        slider->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
1940        slider->setValue(100);
1941
1942
1943        connect(slider, SIGNAL(valueChanged(int)), SIGNAL(SetTransferFunction(int)));
1944
1945        ////////////////////////////////////////7
1946
1947        button = new QPushButton("Update all PVSs");
1948        vl->addWidget(button);
1949        connect(button, SIGNAL(clicked()), SLOT(UpdateAllPvs()));
1950
1951        ////////////////////////////////////////77777
1952
1953        label = new QLabel("Filter size");
1954        vl->addWidget(label);
1955
1956        slider = new QSlider(Qt::Horizontal);
1957        vl->addWidget(slider);
1958        slider->show();
1959        slider->setRange(1, 100);
1960        slider->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
1961        slider->setValue(3);
1962
1963        connect(slider, SIGNAL(valueChanged(int)), SIGNAL(SetVisibilityFilterSize(int)));
1964
1965#endif
1966
1967
1968       
1969        ///////////////////////////////////
1970
1971
1972        QWidget *hbox = new QWidget();
1973        vl->addWidget(hbox);
1974        QHBoxLayout *hlayout = new QHBoxLayout;
1975        hbox->setLayout(hlayout);
1976
1977        QCheckBox *cb = new QCheckBox("Show viewcells", hbox);
1978        hlayout->addWidget(cb);
1979        cb->setChecked(false);
1980        connect(cb, SIGNAL(toggled(bool)), SIGNAL(SetShowViewCells(bool)));
1981       
1982        cb = new QCheckBox("Render errors", hbox);
1983        hlayout->layout()->addWidget(cb);
1984        cb->setChecked(false);
1985        connect(cb, SIGNAL(toggled(bool)), SIGNAL(SetRenderErrors(bool)));
1986
1987#if REMOVE_TEMPORARY
1988        cb = new QCheckBox("Render cost curve", hbox);
1989        hlayout->addWidget(cb);
1990        cb->setChecked(false);
1991        connect(cb, SIGNAL(toggled(bool)), SIGNAL(SetShowRenderCost(bool)));
1992#endif
1993        cb = new QCheckBox("Show rays", hbox);
1994        hlayout->addWidget(cb);
1995        cb->setChecked(false);
1996        connect(cb, SIGNAL(toggled(bool)), SIGNAL(SetShowRays(bool)));
1997#if REMOVE_TEMPORARY   
1998        cb = new QCheckBox("Show Comparison", hbox);
1999        hlayout->addWidget(cb);
2000        cb->setChecked(false);
2001        connect(cb, SIGNAL(toggled(bool)), SIGNAL(SetShowComparison(bool)));
2002#endif
2003
2004        //////////////////
2005
2006        QHBoxLayout *vh = new QHBoxLayout;
2007
2008        QGroupBox *myBox = new QGroupBox("Visualization");
2009
2010        myBox->setLayout(vh);
2011        vl->addWidget(myBox, 0, 0);
2012
2013        QGroupBox *groupBox = CreateVisualizationPanel(hbox);
2014        vh->addWidget(groupBox, 0, 0);
2015
2016#if REMOVE_TEMPORARY
2017        QGroupBox *groupBox2 = CreateRenderCostPanel(hbox);
2018        vh->addWidget(groupBox2, 0, 0);
2019       
2020        QGroupBox *groupBox3 = CreateRayVisualizationPanel(hbox);
2021        vh->addWidget(groupBox3, 0, 0);
2022
2023        QGroupBox *groupBox4 = CreateTrafoPanel(hbox);
2024        vh->addWidget(groupBox4, 0, 0);
2025#endif
2026
2027        //////////////////////////////////
2028
2029        bool tmp = false;
2030        const int range = 1000;
2031
2032        //vbox->resize(800,150);
2033        QWidget *vbox;
2034
2035        vbox = new QGroupBox("Rendering", this);
2036        layout()->addWidget(vbox);
2037
2038        vl = new QVBoxLayout;
2039        vbox->setLayout(vl);
2040
2041
2042        cb = new QCheckBox("Cut view cells", vbox);
2043        vbox->layout()->addWidget(cb);
2044        cb->setChecked(false);
2045        connect(cb, SIGNAL(toggled(bool)), SIGNAL(SetCutViewCells(bool)));
2046
2047
2048        slider = new QSlider(Qt::Horizontal, vbox);
2049        vbox->layout()->addWidget(slider);
2050        slider->show();
2051        slider->setRange(0, 1000);
2052        slider->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
2053        slider->setValue(1000);
2054
2055        connect(slider, SIGNAL(valueChanged(int)), SIGNAL(SetSceneCut(int)));
2056
2057
2058        cb = new QCheckBox("Use spatial filter", vbox);
2059        vbox->layout()->addWidget(cb);
2060        Environment::GetSingleton()->GetBoolValue("Preprocessor.applyVisibilitySpatialFilter", tmp);
2061        cb->setChecked(tmp);
2062        connect(cb, SIGNAL(toggled(bool)), SIGNAL(SetUseSpatialFilter(bool)));
2063
2064
2065        label = new QLabel("Spatial Filter size");
2066        vbox->layout()->addWidget(label);
2067       
2068        slider = new QSlider(Qt::Horizontal, vbox);
2069        vbox->layout()->addWidget(slider);
2070        slider->show();
2071        slider->setRange(1, 100);
2072        slider->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
2073        slider->setValue(10);
2074
2075        connect(slider, SIGNAL(valueChanged(int)), SIGNAL(SetSpatialFilterSize(int)));
2076
2077        ////////////////////////////
2078
2079       
2080        cb = new QCheckBox("Hide view cells by render cost ", vbox);
2081        vbox->layout()->addWidget(cb);
2082        cb->setChecked(false);
2083        connect(cb, SIGNAL(toggled(bool)), SIGNAL(SetHideByCost(bool)));
2084
2085        label = new QLabel("Hide by cost");
2086        vbox->layout()->addWidget(label);
2087
2088        // the render cost visualization
2089        slider = new QSlider(Qt::Horizontal, vbox);
2090        vbox->layout()->addWidget(slider);
2091        slider->show();
2092        slider->setRange(0, range);
2093        slider->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
2094        slider->setValue(0);
2095        connect(slider, SIGNAL(valueChanged(int)), SIGNAL(SetHidingCost(int)));
2096
2097        ///////////////////////////////////////////
2098
2099        cb = new QCheckBox("Top View", vbox);
2100        vbox->layout()->addWidget(cb);
2101        cb->setChecked(false);
2102        connect(cb, SIGNAL(toggled(bool)), SIGNAL(SetTopView(bool)));
2103
2104
2105        label = new QLabel("Top distance");
2106        vbox->layout()->addWidget(label);
2107       
2108        slider = new QSlider(Qt::Horizontal, vbox);
2109        vbox->layout()->addWidget(slider);
2110        slider->show();
2111        slider->setRange(1, 1000);
2112        slider->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
2113        slider->setValue(500);
2114
2115        connect(slider, SIGNAL(valueChanged(int)), SIGNAL(SetTopDistance(int)));
2116       
2117
2118        ///////////////////////////////////////////
2119#if REMOVE_TEMPORARY
2120        cb = new QCheckBox("Transparency", vbox);
2121        vbox->layout()->addWidget(cb);
2122        cb->setChecked(false);
2123        connect(cb, SIGNAL(toggled(bool)), SIGNAL(SetUseTransparency(bool)));
2124
2125        label = new QLabel("Use transparency");
2126        vbox->layout()->addWidget(label);
2127
2128        // the render cost visualization
2129        slider = new QSlider(Qt::Horizontal, vbox);
2130        vbox->layout()->addWidget(slider);
2131        slider->show();
2132        slider->setRange(0, range);
2133        slider->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
2134        slider->setValue(0);
2135        connect(slider, SIGNAL(valueChanged(int)), SIGNAL(SetTransparency(int)));
2136#endif
2137       
2138
2139        //////////////////////////////
2140
2141#if REMOVE_TEMPORARY
2142
2143        cb = new QCheckBox("Cut scene", vbox);
2144        vbox->layout()->addWidget(cb);
2145        cb->setChecked(false);
2146        connect(cb, SIGNAL(toggled(bool)), SIGNAL(SetCutScene(bool)));
2147
2148        cb = new QCheckBox("Render boxes", vbox);
2149        vbox->layout()->addWidget(cb);
2150        cb->setChecked(false);
2151        connect(cb, SIGNAL(toggled(bool)), SIGNAL(SetRenderBoxes(bool)));
2152
2153        cb = new QCheckBox("Render visibility estimates", vbox);
2154        vbox->layout()->addWidget(cb);
2155        cb->setChecked(false);
2156        connect(cb, SIGNAL(toggled(bool)), SIGNAL(SetRenderVisibilityEstimates(bool)));
2157#endif
2158
2159#if REMOVE_TEMPORARY
2160
2161        cb = new QCheckBox("Use filter", vbox);
2162        vbox->layout()->addWidget(cb);
2163        Environment::GetSingleton()->GetBoolValue("Preprocessor.applyVisibilityFilter", tmp);
2164        cb->setChecked(tmp);
2165        connect(cb, SIGNAL(toggled(bool)), SIGNAL(SetUseFilter(bool)));
2166#endif
2167
2168
2169       
2170
2171#if REMOVE_TEMPORARY
2172       
2173        cb = new QCheckBox("Render filter", vbox);
2174        vbox->layout()->addWidget(cb);
2175        cb->setChecked(true);
2176        connect(cb, SIGNAL(toggled(bool)), SIGNAL(SetRenderFilter(bool)));
2177
2178
2179        /*vbox = new QGroupBox("PVS Errors", this);
2180        layout()->addWidget(vbox);
2181
2182        vl = new QVBoxLayout;
2183        vbox->setLayout(vl);
2184
2185        button = new QPushButton("Compute Visibility", vbox);
2186        vbox->layout()->addWidget(button);
2187        connect(button, SIGNAL(clicked()), SLOT(ComputeVisibility()));
2188
2189        button = new QPushButton("Stop Computation", vbox);
2190        vbox->layout()->addWidget(button);
2191        connect(button, SIGNAL(clicked()), SLOT(StopComputation()));
2192
2193        button = new QPushButton("Set Random View Point", vbox);
2194        vbox->layout()->addWidget(button);
2195        connect(button, SIGNAL(clicked()), SLOT(SetRandomViewPoint()));*/
2196
2197        button = new QPushButton("Store statistics", vbox);
2198        vbox->layout()->addWidget(button);
2199        connect(button, SIGNAL(clicked()), SIGNAL(StoreStatistics()));
2200
2201#endif
2202
2203        button = new QPushButton("Compute GVS", vbox);
2204        vbox->layout()->addWidget(button);
2205        connect(button, SIGNAL(clicked()), SIGNAL(ComputeGVS()));
2206
2207#if DYNAMIC_OBJECTS_HACK
2208
2209        button = new QPushButton("Load object", vbox);
2210        vbox->layout()->addWidget(button);
2211        connect(button, SIGNAL(clicked()), SIGNAL(LoadObject()));
2212#endif
2213
2214        /*cb = new QCheckBox("Stats", vbox);
2215        vbox->layout()->addWidget(cb);
2216        cb->setChecked(false);
2217        connect(cb, SIGNAL(toggled(bool)), SIGNAL(StoreStatistics()));*/
2218
2219        if (0)
2220        {
2221                vbox = new QGroupBox("PVS Errors", this);
2222                layout()->addWidget(vbox);
2223
2224                vl = new QVBoxLayout;
2225                vbox->setLayout(vl);
2226
2227                mPvsErrorWidget = new QListWidget(vbox);
2228                vbox->layout()->addWidget(mPvsErrorWidget);
2229
2230                connect(mPvsErrorWidget,
2231                        SIGNAL(doubleClicked(const QModelIndex &)),
2232                        this,
2233                        SLOT(PvsErrorClicked(const QModelIndex &)));
2234
2235                button = new QPushButton("Next Error Frame", vbox);
2236                vbox->layout()->addWidget(button);
2237                connect(button, SIGNAL(clicked(void)), SLOT(FocusNextPvsErrorFrame(void)));
2238        }
2239       
2240        //connect(button, SIGNAL(clicked(void)), SLOT(StoreStatistics(void)));
2241       
2242        //////////////////////////////////////////
2243
2244
2245        setWindowTitle("Preprocessor Control Widget");
2246        adjustSize();
2247}
2248
2249
2250
2251
2252void
2253QtRendererControlWidget::FocusNextPvsErrorFrame(void)
2254{}
2255
2256void
2257QtRendererControlWidget::UpdatePvsErrorItem(int row,
2258                                                                                        GlRendererBuffer::PvsErrorEntry &pvsErrorEntry)
2259{
2260
2261        QListWidgetItem *i = mPvsErrorWidget->item(row);
2262        QString s;
2263        s.sprintf("%5.5f", pvsErrorEntry.mError);
2264        if (i) {
2265                i->setText(s);
2266        } else {
2267                new QListWidgetItem(s, mPvsErrorWidget);
2268        }
2269        mPvsErrorWidget->update();
2270}
2271
2272
2273
2274
2275/*********************************************************************/
2276/*                   QtGlDebuggerWidget implementation               */
2277/*********************************************************************/
2278
2279
2280QtGlDebuggerWidget::QtGlDebuggerWidget(QtGlRendererBuffer *buf, QWidget *parent)
2281: QGLWidget(QGLFormat(QGL::SampleBuffers), parent), mRenderBuffer(buf)
2282{
2283        // create the pbuffer
2284        //pbuffer = new QGLPixelBuffer(QSize(512, 512), format(), this);
2285        timerId = startTimer(20);
2286        setWindowTitle(("OpenGL pbuffers"));
2287}
2288
2289
2290QtGlDebuggerWidget::~QtGlDebuggerWidget()
2291{
2292        mRenderBuffer->releaseFromDynamicTexture();
2293        glDeleteTextures(1, &dynamicTexture);
2294
2295        DEL_PTR(mRenderBuffer);
2296}
2297
2298
2299void QtGlDebuggerWidget::initializeGL()
2300{
2301        glMatrixMode(GL_PROJECTION);
2302        glLoadIdentity();
2303
2304        glFrustum(-1, 1, -1, 1, 10, 100);
2305        glTranslatef(-0.5f, -0.5f, -0.5f);
2306        glTranslatef(0.0f, 0.0f, -15.0f);
2307        glMatrixMode(GL_MODELVIEW);
2308
2309        glEnable(GL_CULL_FACE);
2310        initCommon();
2311        initPbuffer();
2312
2313}
2314
2315
2316void QtGlDebuggerWidget::resizeGL(int w, int h)
2317{
2318        glViewport(0, 0, w, h);
2319}
2320
2321
2322void QtGlDebuggerWidget::paintGL()
2323{
2324        // draw a spinning cube into the pbuffer..
2325        mRenderBuffer->makeCurrent();
2326
2327        BeamSampleStatistics stats;
2328        mRenderBuffer->SampleBeamContributions(mSourceObject, mBeam, mSamples, stats);
2329
2330        glFlush();
2331
2332        // rendering directly to a texture is not supported on X11, unfortunately
2333        mRenderBuffer->updateDynamicTexture(dynamicTexture);
2334
2335        // and use the pbuffer contents as a texture when rendering the
2336        // background and the bouncing cubes
2337        makeCurrent();
2338        glBindTexture(GL_TEXTURE_2D, dynamicTexture);
2339        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
2340
2341        // draw the background
2342        glMatrixMode(GL_MODELVIEW);
2343        glPushMatrix();
2344        glLoadIdentity();
2345        glMatrixMode(GL_PROJECTION);
2346        glPushMatrix();
2347        glLoadIdentity();
2348
2349        glPopMatrix();
2350        glMatrixMode(GL_MODELVIEW);
2351        glPopMatrix();
2352}
2353
2354
2355void QtGlDebuggerWidget::initPbuffer()
2356{
2357        // set up the pbuffer context
2358        mRenderBuffer->makeCurrent();
2359        /*mRenderBuffer->InitGL();
2360
2361        glViewport(0, 0, mRenderBuffer->size().width(), mRenderBuffer->size().height());
2362        glMatrixMode(GL_PROJECTION);
2363        glLoadIdentity();
2364        glOrtho(-1, 1, -1, 1, -99, 99);
2365        glTranslatef(-0.5f, -0.5f, 0.0f);
2366        glMatrixMode(GL_MODELVIEW);
2367        glLoadIdentity();
2368
2369        glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);*/
2370
2371        // generate a texture that has the same size/format as the pbuffer
2372        dynamicTexture = mRenderBuffer->generateDynamicTexture();
2373
2374        // bind the dynamic texture to the pbuffer - this is a no-op under X11
2375        mRenderBuffer->bindToDynamicTexture(dynamicTexture);
2376        makeCurrent();
2377}
2378
2379
2380void QtGlDebuggerWidget::initCommon()
2381{
2382        glEnable(GL_TEXTURE_2D);
2383        glEnable(GL_DEPTH_TEST);
2384
2385        glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
2386}
2387
2388
2389void QtGlRendererWidget::_RenderColoredPvs()
2390{
2391        // note: could be done more efficiently using color buffers
2392        mPvsSize = mPvsCache.mPvs.GetSize();
2393
2394        ObjectPvsIterator it = mPvsCache.mPvs.GetIterator();
2395
2396        PvsData pvsData;
2397
2398        while (it.HasMoreEntries())
2399        {
2400                Intersectable *object = it.Next(pvsData);
2401
2402                RgbColor color;
2403
2404                //float visibility = mTransferFunction*log10(entry.mData.mSumPdf + 1); // /5.0f
2405                // glColor3f(visibility, 0.0f, 0.0f);
2406                //cerr << "sumpdf: " << pvsData.mSumPdf << endl;
2407                if (mUseRandomColorPerPvsObject)
2408                {
2409                        KdIntersectable *kdint = static_cast<KdIntersectable *>(object);
2410
2411                        if (kdint->mGenericIdx == -1)
2412                        {
2413                                kdint->mGenericIdx = (int)mColors.size();
2414                                mColors.push_back(RandomColor());
2415                        }
2416                        color = mColors[kdint->mGenericIdx];
2417                }
2418                else
2419                {
2420                        color = RainbowColorMapping(mTransferFunction * log10(pvsData.mSumPdf + 1));
2421                }
2422
2423                glColor3f(color.r, color.g, color.b);
2424
2425                mUseForcedColors = true;
2426                RenderIntersectable(object);
2427                mUseForcedColors = false;
2428        }
2429}
2430
2431void
2432QtGlRendererWidget::UpdateDynamicObjects()
2433{
2434  preprocessor->ScheduleUpdateDynamicObjects();
2435 
2436}
2437
2438}
Note: See TracBrowser for help on using the repository browser.