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

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