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

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