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

Revision 2615, 7.0 KB checked in by mattausch, 17 years ago (diff)

did some stuff for the visualization

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