source: GTP/trunk/Lib/Vis/Preprocessing/src/QtInterface/QtGlViewer.cpp @ 2670

Revision 2670, 7.0 KB checked in by mattausch, 16 years ago (diff)
RevLine 
[1931]1// ===================================================================
2// $Id$
3
4// Qt headers
5#include <QtGui>
6#include <QtOpenGL>
7
8// C++ standard headers
9#include <cmath>
10
11// local headers
12#include "common.h"
13#include "QtGlViewer.h"
14#include "QtGlRenderer.h"
15#include "Trackball.h"
16#include "RssPreprocessor.h"
17#include "SceneGraph.h"
[2571]18#include "ViewCellsManager.h"
[1931]19
20
21namespace GtpVisibilityPreprocessor {
22
23QtGlRendererWidget *renderer;
24
25QtGlViewer::QtGlViewer(QWidget *parent,
[2664]26                                           QtGlRendererWidget *renderer)
27: QGLWidget(parent), mRenderer(renderer)
[1931]28{
29        scale = 1.0f;
30        mWireframe = false;
[2567]31        mModelMatrix = IdentityMatrix();
32
[2615]33        //setWindowTitle("PVS visualization");
34        setWindowTitle("Global View Cell Visualization");
35
[1931]36        trackball(manipulatorLastQuat, 0.0f, 0.0f, 0.0f, 0.0f);
[2562]37        //connect(renderer, SIGNAL(PvsUpdated()), this, SLOT(updateGL()));
38        //connect(renderer, SIGNAL(cutupdated), this, SLOT(updateGL()));
39
40        const int delay = 250; // in milliseconds
41        timerId = startTimer(delay);
[1931]42}
43 
[2664]44
[2573]45QSize QtGlViewer::minimumSizeHint() const
[1931]46{
47  return QSize(100, 100);
48}
49
[2573]50QSize QtGlViewer::sizeHint() const
51{
[2615]52        // set viewer size here
53        return QSize(512, 320);
54
55        //return QSize(512, 384);
56        //return QSize(640, 480);
[1931]57}
58 
59// This method has to be rendefined by all renderer implementations
[2571]60void QtGlViewer::RenderScene()
[1931]61{
[2604]62        GLfloat mat_ambient[]   = {  0.5, 0.5, 0.5, 1.0  };
63       
[2571]64        // mat_specular and mat_shininess are NOT default values
[2604]65        GLfloat mat_diffuse[]   = {  1.0, 1.0, 1.0, 1.0  };
66        GLfloat mat_specular[]  = {  0.3, 0.3, 0.3, 1.0  };
67        GLfloat mat_shininess[] = {  1.0  };
[2571]68
[2604]69        GLfloat light_ambient[]  = {  0.2, 0.2, 0.2, 1.0  };
70        GLfloat light_diffuse[]  = {  0.4, 0.4, 0.4, 1.0  };
71        GLfloat light_specular[] = {  0.3, 0.3, 0.3, 1.0  };
[2571]72
[2604]73        GLfloat lmodel_ambient[] = {0.3, 0.3, 0.3, 1.0};
[2571]74
75
[2604]76        // set position of the light
77        GLfloat infinite_light[] = {1.0, 0.8, 1.0, 0.0};
78        glLightfv (GL_LIGHT0, GL_POSITION, infinite_light);
79
80        // set position of the light2
81        GLfloat infinite_light2[] = {-0.3, 1.5, 1.0, 0.0};
82        glLightfv (GL_LIGHT1, GL_POSITION, infinite_light2);
83
[2571]84        // default material
85        /*glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, mat_ambient);
86        glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, mat_diffuse);
87        glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, mat_specular);
88        glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, mat_shininess);
89*/
90        // a light
91        glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
92        glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
93        glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
[2604]94       
95        glLightfv(GL_LIGHT1, GL_AMBIENT, light_ambient);
96        glLightfv(GL_LIGHT1, GL_DIFFUSE, light_diffuse);
97        glLightfv(GL_LIGHT1, GL_SPECULAR, light_specular);
98       
[2571]99        glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lmodel_ambient);
100
101        //GLfloat infinite_light2[] = {-0.3, 1.5f, 1.0f, 0.0f};
102        //glLightfv (GL_LIGHT1, GL_POSITION, infinite_light2);
[2604]103        //glColor3f(1.0f, 1.0f, 1.0f);
[2571]104        glColor3f(0.8f, 0.8f, 0.8f);
105       
106        glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE);
[2604]107        glEnable(GL_COLOR_MATERIAL);
[2571]108
[2562]109        ++ mRenderer->mFrame;
[1931]110       
[2571]111        glEnable(GL_DEPTH_TEST);
112        glDepthMask(GL_TRUE);
[2562]113
[2571]114        glCullFace(GL_BACK);
115        glShadeModel(GL_FLAT);
116
117        glDisable(GL_BLEND);
118
[2566]119        mRenderer->mDummyViewPoint = mDummyViewPoint;
[2571]120       
[2604]121        glEnable(GL_NORMALIZE);
122        glEnable(GL_LIGHTING);
123        glEnable(GL_LIGHT0);
124        glEnable(GL_LIGHT1);
[2571]125
[2670]126        mRenderer->VisualizePvs();
[1931]127       
[2571]128        glDisable(GL_LIGHTING);
[2562]129
[2571]130        if (0 && mRenderer->mShowRays)
131        {
132                mRenderer->RenderRays(mRenderer->mViewCellsManager->mVizBuffer.GetRays());
133        }
134
[1931]135        if (mRenderer->mShowRenderCost)
136          mRenderer->RenderRenderCost();
137
[2562]138        // render into the right side of the window buffer
139        if (mRenderer->mRenderViewCells)
140                mRenderer->RenderViewCells();
141
[2564]142        mRenderer->RenderViewPoint();
143
[1931]144        glColor3f(1.0f, 1.0f, 1.0f);
145        mRenderer->RenderInfo();
146}
147
148
149void
150QtGlViewer::initializeGL()
151
152  const char *vendorString, *rendererString, *versionString, *extString;
153
154  // get GL info
155  vendorString = (const char *) glGetString(GL_VENDOR);
156  rendererString = (const char *) glGetString(GL_RENDERER);
157  versionString = (const char *) glGetString(GL_VERSION);
158  extString = (const char *) glGetString(GL_EXTENSIONS);
159 
160  Debug << "Info: GL Vendor = "<<vendorString<<endl
161        << "Info: GL Renderer = "<<rendererString<<endl
162        << "Info: GL Version = "<<versionString<<endl
163        << "Info: GL Extensions = "<<extString<<endl<<flush;
164
165
[2571]166        glEnable(GL_LIGHTING);
167        glEnable(GL_LIGHT0);
168        //glEnable(GL_LIGHT1);
169
170        // set position of the light
171        GLfloat infinite_light[] = {0, -1.0, 0, 1.0f};
172        glLightfv(GL_LIGHT0, GL_POSITION, infinite_light);
173
[1931]174  return;
175}
176
177void
178QtGlViewer::paintGL()
179{
[2538]180        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
181        glLoadIdentity();
[1931]182
[2566]183        //float m[4][4];
184        Matrix4x4 m;
[1931]185
[2538]186        glLoadIdentity();
[2609]187        gluLookAt(0.0f, 0.0f, 15.0f,  // eye is at (0,0,30)
[2604]188                      0.0f, 0.0f, 0.0f,  // center is at (0,0,0)
[2605]189                      0.0f, 1.0f, 0.0f); // up is in positive Y direction
[1931]190
[2566]191        build_rotmatrix(m.x, manipulatorLastQuat);
192        glMultMatrixf((float *)m.x);
[1931]193
[2566]194        float s = scale * 20.0f / Magnitude(mRenderer->mSceneGraph->GetBox().Diagonal());
[2538]195        glScalef(s, s, s);
196
[2569]197        // TODO: determine model matrix for establishing right rendering order for transparency
[2538]198        Vector3 t = -mRenderer->mSceneGraph->GetBox().Center();
199        glTranslatef(t.x, t.y, t.z);
200
[2609]201        //mDummyViewPoint = -t;
202        //mDummyViewPoint /= s;
[2566]203        //glGetFloatv(GL_MODELVIEW_MATRIX, m1);
[2609]204
[2566]205        m.Invert();
206
[2567]207        mModelMatrix *= m;
208        mDummyViewPoint = mModelMatrix * Vector3(0, 0, 0);
209
210        //mDummyViewPoint
211        //cout << "view point: " << mDummyViewPoint << endl;
[1931]212        RenderScene();
213}
214
215void
216QtGlViewer::resizeGL(int width, int height)
217{
218  glViewport(0, 0, width, height);
219 
220  glMatrixMode(GL_PROJECTION);
221  glLoadIdentity();
222  gluPerspective(70.0, 1.0, 0.1, 100.0);
223  //  glOrtho(-0.5*width/height, +0.5*width/height, 0.5, -0.5, 4.0, 15.0);
224  glMatrixMode(GL_MODELVIEW);
225}
226
227
228void
229QtGlViewer::mousePressEvent(QMouseEvent *event)
230{
231  lastPos = event->pos();
232}
233
234void
[2604]235QtGlViewer::keyPressEvent(QKeyEvent * e)
[1931]236{
237  switch (e->key()) {
[2615]238  case Qt::Key_V:
239                mRenderer->mRenderViewCells = !mRenderer->mRenderViewCells;
240                updateGL();
241                break;
242
[1931]243  case Qt::Key_W:
[2604]244                mWireframe = !mWireframe;
[1931]245                updateGL();
[2615]246                break;
247
[1931]248  default:
249                e->ignore();
[2604]250                cerr << "unknown key" << endl;
[1931]251                break;
252  }
[2604]253  //updateGL();
[1931]254}
255
256void
257QtGlViewer::mouseMoveEvent(QMouseEvent *event)
258{
[2614]259        int dx = event->x() - lastPos.x();
260        int dy = event->y() - lastPos.y(); 
261
262        if (event->modifiers() & Qt::CTRL)
263        {
264                scale = scale*(1.0f - dy/(float)height());
265                if (scale < 0.01f)
266                        scale = 0.01f;
267                updateGL();
268        }
269        else
270        {
[1931]271                float W = width();
272                float H = height();
273                int x = event->x();
274                int lastX = lastPos.x();
275                int y = event->y();
276                int lastY = lastPos.y();
[2614]277
[1931]278                float quat[4];
279                trackball(quat,
[2614]280                        (2.0 * lastX - W) / W,
281                        (H - 2.0 * lastY) / H,
282                        (2.0 * x - W) / W,
283                        (H - 2.0 * y) / H
284                        );
[2605]285
[1931]286                add_quats(quat, manipulatorLastQuat, manipulatorLastQuat);
[2614]287        }
[1969]288
[2614]289        lastPos = event->pos();
290        updateGL();
[1931]291}
[1936]292
[2614]293        void QtGlViewer::timerEvent(QTimerEvent *event)
294        {
295                update();
296        }
[2562]297
[2614]298
[2562]299}
300
Note: See TracBrowser for help on using the repository browser.