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

Revision 2605, 6.8 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,
26                                           QtGlRendererWidget *renderer):
27  QGLWidget(parent), mRenderer(renderer)
28{
29        scale = 1.0f;
30        mWireframe = false;
[2567]31        mModelMatrix = IdentityMatrix();
32
[1931]33        trackball(manipulatorLastQuat, 0.0f, 0.0f, 0.0f, 0.0f);
[2562]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);
[1931]39}
40 
[2573]41QSize QtGlViewer::minimumSizeHint() const
[1931]42{
43  return QSize(100, 100);
44}
45
[2573]46QSize QtGlViewer::sizeHint() const
47{
[2593]48        return QSize(640, 480);
[1931]49}
50 
51// This method has to be rendefined by all renderer implementations
[2571]52void QtGlViewer::RenderScene()
[1931]53{
[2604]54        GLfloat mat_ambient[]   = {  0.5, 0.5, 0.5, 1.0  };
55       
[2571]56        // mat_specular and mat_shininess are NOT default values
[2604]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  };
[2571]60
[2604]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  };
[2571]64
[2604]65        GLfloat lmodel_ambient[] = {0.3, 0.3, 0.3, 1.0};
[2571]66
67
[2604]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
[2571]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);
[2604]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       
[2571]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);
[2604]95        //glColor3f(1.0f, 1.0f, 1.0f);
[2571]96        glColor3f(0.8f, 0.8f, 0.8f);
97       
98        glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE);
[2604]99        glEnable(GL_COLOR_MATERIAL);
[2571]100
[2562]101        ++ mRenderer->mFrame;
[1931]102       
[2571]103        glEnable(GL_DEPTH_TEST);
104        glDepthMask(GL_TRUE);
[2562]105
[2571]106        glCullFace(GL_BACK);
107        glShadeModel(GL_FLAT);
108
109        glDisable(GL_BLEND);
110
[2566]111        mRenderer->mDummyViewPoint = mDummyViewPoint;
[2571]112       
[2604]113        glEnable(GL_NORMALIZE);
114        glEnable(GL_LIGHTING);
115        glEnable(GL_LIGHT0);
116        glEnable(GL_LIGHT1);
[2571]117
[1931]118        mRenderer->RenderPvs();
119       
[2571]120        glDisable(GL_LIGHTING);
[2562]121
[2571]122        if (0 && mRenderer->mShowRays)
123        {
124                mRenderer->RenderRays(mRenderer->mViewCellsManager->mVizBuffer.GetRays());
125        }
126
[1931]127        if (mRenderer->mShowRenderCost)
128          mRenderer->RenderRenderCost();
129
[2562]130        // render into the right side of the window buffer
131        if (mRenderer->mRenderViewCells)
132                mRenderer->RenderViewCells();
133
[2564]134        mRenderer->RenderViewPoint();
135
[1931]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
[2571]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
[1931]166  return;
167}
168
169void
170QtGlViewer::paintGL()
171{
[2538]172        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
173        glLoadIdentity();
[1931]174
[2566]175        //float m[4][4];
176        Matrix4x4 m;
[1931]177
[2538]178        glLoadIdentity();
[2605]179        gluLookAt(0.0f, 0.0f, 10.0f,  // eye is at (0,0,30)
[2604]180                      0.0f, 0.0f, 0.0f,  // center is at (0,0,0)
[2605]181                      0.0f, 1.0f, 0.0f); // up is in positive Y direction
[1931]182
[2566]183        build_rotmatrix(m.x, manipulatorLastQuat);
184        glMultMatrixf((float *)m.x);
[1931]185
[2566]186        float s = scale * 20.0f / Magnitude(mRenderer->mSceneGraph->GetBox().Diagonal());
[2538]187        glScalef(s, s, s);
188
[2569]189        // TODO: determine model matrix for establishing right rendering order for transparency
[2538]190        Vector3 t = -mRenderer->mSceneGraph->GetBox().Center();
191        glTranslatef(t.x, t.y, t.z);
192
[2567]193        /*mDummyViewPoint = -t;
[2566]194        mDummyViewPoint /= s;
195
196        //glGetFloatv(GL_MODELVIEW_MATRIX, m1);
[2567]197*/
[2566]198        m.Invert();
199
[2567]200        mModelMatrix *= m;
201        mDummyViewPoint = mModelMatrix * Vector3(0, 0, 0);
202
203        //mDummyViewPoint
204        //cout << "view point: " << mDummyViewPoint << endl;
[1931]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
[2604]228QtGlViewer::keyPressEvent(QKeyEvent * e)
[1931]229{
230  switch (e->key()) {
231  case Qt::Key_W:
[2604]232                mWireframe = !mWireframe;
[1931]233                updateGL();
234  default:
235                e->ignore();
[2604]236                cerr << "unknown key" << endl;
[1931]237                break;
238  }
[2604]239  //updateGL();
[1931]240}
241
242void
243QtGlViewer::mouseMoveEvent(QMouseEvent *event)
244{
[2604]245  int dx = event->x() + lastPos.x();
246  int dy = event->y() + lastPos.y(); 
[1931]247 
248  if (event->modifiers() & Qt::CTRL)
249                {
250                        scale = scale*(1.0f - dy/(float)height());
[1936]251                        if (scale < 0.01f)
252                                scale = 0.01f;
[1931]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,
[2605]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
[1931]271                add_quats(quat, manipulatorLastQuat, manipulatorLastQuat);
272  }
273 
274  lastPos = event->pos();
[1972]275  updateGL();
[1931]276}
[1969]277
[2562]278void QtGlViewer::timerEvent(QTimerEvent *event)
279{
280        update();
[1931]281}
[1936]282
[2562]283
284}
285
Note: See TracBrowser for help on using the repository browser.