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

Revision 2670, 7.0 KB checked in by mattausch, 17 years ago (diff)
Line 
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"
18#include "ViewCellsManager.h"
19
20
21namespace GtpVisibilityPreprocessor {
22
23QtGlRendererWidget *renderer;
24
25QtGlViewer::QtGlViewer(QWidget *parent,
26                                           QtGlRendererWidget *renderer)
27: QGLWidget(parent), mRenderer(renderer)
28{
29        scale = 1.0f;
30        mWireframe = false;
31        mModelMatrix = IdentityMatrix();
32
33        //setWindowTitle("PVS visualization");
34        setWindowTitle("Global View Cell Visualization");
35
36        trackball(manipulatorLastQuat, 0.0f, 0.0f, 0.0f, 0.0f);
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);
42}
43 
44
45QSize QtGlViewer::minimumSizeHint() const
46{
47  return QSize(100, 100);
48}
49
50QSize QtGlViewer::sizeHint() const
51{
52        // set viewer size here
53        return QSize(512, 320);
54
55        //return QSize(512, 384);
56        //return QSize(640, 480);
57}
58 
59// This method has to be rendefined by all renderer implementations
60void QtGlViewer::RenderScene()
61{
62        GLfloat mat_ambient[]   = {  0.5, 0.5, 0.5, 1.0  };
63       
64        // mat_specular and mat_shininess are NOT default values
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  };
68
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  };
72
73        GLfloat lmodel_ambient[] = {0.3, 0.3, 0.3, 1.0};
74
75
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
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);
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       
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);
103        //glColor3f(1.0f, 1.0f, 1.0f);
104        glColor3f(0.8f, 0.8f, 0.8f);
105       
106        glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE);
107        glEnable(GL_COLOR_MATERIAL);
108
109        ++ mRenderer->mFrame;
110       
111        glEnable(GL_DEPTH_TEST);
112        glDepthMask(GL_TRUE);
113
114        glCullFace(GL_BACK);
115        glShadeModel(GL_FLAT);
116
117        glDisable(GL_BLEND);
118
119        mRenderer->mDummyViewPoint = mDummyViewPoint;
120       
121        glEnable(GL_NORMALIZE);
122        glEnable(GL_LIGHTING);
123        glEnable(GL_LIGHT0);
124        glEnable(GL_LIGHT1);
125
126        mRenderer->VisualizePvs();
127       
128        glDisable(GL_LIGHTING);
129
130        if (0 && mRenderer->mShowRays)
131        {
132                mRenderer->RenderRays(mRenderer->mViewCellsManager->mVizBuffer.GetRays());
133        }
134
135        if (mRenderer->mShowRenderCost)
136          mRenderer->RenderRenderCost();
137
138        // render into the right side of the window buffer
139        if (mRenderer->mRenderViewCells)
140                mRenderer->RenderViewCells();
141
142        mRenderer->RenderViewPoint();
143
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
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
174  return;
175}
176
177void
178QtGlViewer::paintGL()
179{
180        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
181        glLoadIdentity();
182
183        //float m[4][4];
184        Matrix4x4 m;
185
186        glLoadIdentity();
187        gluLookAt(0.0f, 0.0f, 15.0f,  // eye is at (0,0,30)
188                      0.0f, 0.0f, 0.0f,  // center is at (0,0,0)
189                      0.0f, 1.0f, 0.0f); // up is in positive Y direction
190
191        build_rotmatrix(m.x, manipulatorLastQuat);
192        glMultMatrixf((float *)m.x);
193
194        float s = scale * 20.0f / Magnitude(mRenderer->mSceneGraph->GetBox().Diagonal());
195        glScalef(s, s, s);
196
197        // TODO: determine model matrix for establishing right rendering order for transparency
198        Vector3 t = -mRenderer->mSceneGraph->GetBox().Center();
199        glTranslatef(t.x, t.y, t.z);
200
201        //mDummyViewPoint = -t;
202        //mDummyViewPoint /= s;
203        //glGetFloatv(GL_MODELVIEW_MATRIX, m1);
204
205        m.Invert();
206
207        mModelMatrix *= m;
208        mDummyViewPoint = mModelMatrix * Vector3(0, 0, 0);
209
210        //mDummyViewPoint
211        //cout << "view point: " << mDummyViewPoint << endl;
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
235QtGlViewer::keyPressEvent(QKeyEvent * e)
236{
237  switch (e->key()) {
238  case Qt::Key_V:
239                mRenderer->mRenderViewCells = !mRenderer->mRenderViewCells;
240                updateGL();
241                break;
242
243  case Qt::Key_W:
244                mWireframe = !mWireframe;
245                updateGL();
246                break;
247
248  default:
249                e->ignore();
250                cerr << "unknown key" << endl;
251                break;
252  }
253  //updateGL();
254}
255
256void
257QtGlViewer::mouseMoveEvent(QMouseEvent *event)
258{
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        {
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();
277
278                float quat[4];
279                trackball(quat,
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                        );
285
286                add_quats(quat, manipulatorLastQuat, manipulatorLastQuat);
287        }
288
289        lastPos = event->pos();
290        updateGL();
291}
292
293        void QtGlViewer::timerEvent(QTimerEvent *event)
294        {
295                update();
296        }
297
298
299}
300
Note: See TracBrowser for help on using the repository browser.