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

Revision 2709, 6.8 KB checked in by mattausch, 16 years ago (diff)

sheduling dynamic object only when necessary

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        //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.5f, 0.5f, 0.5f, 1.0f};
62       
63        // mat_specular and mat_shininess are NOT default values
64        GLfloat mat_diffuse[]   = {1.0f, 1.0f, 1.0f, 1.0f};
65        GLfloat mat_specular[]  = {0.3f, 0.3f, 0.3f, 1.0f};
66        GLfloat mat_shininess[] = {1.0f  };
67
68        GLfloat light_ambient[]  = {0.2f, 0.2f, 0.2f, 1.0f};
69        GLfloat light_diffuse[]  = {0.6f, 0.6f, 0.6f, 1.0f};
70        GLfloat light_specular[] = {0.3f, 0.3f, 0.3f, 1.0f};
71
72        GLfloat lmodel_ambient[] = {0.3f, 0.3f, 0.3f, 1.0f};
73
74
75        // set position of the light
76        GLfloat infinite_light[] = {1.0f, 0.8f, 1.0f, 0.0f};
77        glLightfv (GL_LIGHT0, GL_POSITION, infinite_light);
78
79        // set position of the light2
80        GLfloat infinite_light2[] = {-0.3f, 1.5f, 1.0f, 0.0f};
81        glLightfv (GL_LIGHT1, GL_POSITION, infinite_light2);
82
83        // a light
84        glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
85        glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
86        glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
87       
88        glLightfv(GL_LIGHT1, GL_AMBIENT, light_ambient);
89        glLightfv(GL_LIGHT1, GL_DIFFUSE, light_diffuse);
90        glLightfv(GL_LIGHT1, GL_SPECULAR, light_specular);
91       
92        glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lmodel_ambient);
93
94        //GLfloat infinite_light2[] = {-0.3, 1.5f, 1.0f, 0.0f};
95        //glLightfv (GL_LIGHT1, GL_POSITION, infinite_light2);
96        //glColor3f(1.0f, 1.0f, 1.0f);
97        glColor3f(0.8f, 0.8f, 0.8f);
98       
99        glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE);
100        glEnable(GL_COLOR_MATERIAL);
101
102        ++ mRenderer->mFrame;
103       
104        glEnable(GL_DEPTH_TEST);
105        glDepthMask(GL_TRUE);
106
107        glCullFace(GL_BACK);
108        glShadeModel(GL_FLAT);
109
110        glDisable(GL_BLEND);
111
112        mRenderer->mDummyViewPoint = mDummyViewPoint;
113       
114        glEnable(GL_NORMALIZE);
115        glEnable(GL_LIGHTING);
116        glEnable(GL_LIGHT0);
117        glEnable(GL_LIGHT1);
118
119        mRenderer->VisualizePvs();
120       
121        glDisable(GL_LIGHTING);
122
123        if (0 && mRenderer->mShowRays)
124        {
125                mRenderer->RenderRays(mRenderer->mViewCellsManager->mVizBuffer.GetRays());
126        }
127
128        if (mRenderer->mShowRenderCost)
129          mRenderer->RenderRenderCost();
130
131        // render into the right side of the window buffer
132        if (mRenderer->mRenderViewCells)
133                mRenderer->RenderViewCells();
134
135        mRenderer->RenderViewPoint();
136
137        glColor3f(1.0f, 1.0f, 1.0f);
138        mRenderer->RenderInfo();
139}
140
141
142void
143QtGlViewer::initializeGL()
144
145  const char *vendorString, *rendererString, *versionString, *extString;
146
147  // get GL info
148  vendorString = (const char *) glGetString(GL_VENDOR);
149  rendererString = (const char *) glGetString(GL_RENDERER);
150  versionString = (const char *) glGetString(GL_VERSION);
151  extString = (const char *) glGetString(GL_EXTENSIONS);
152 
153  Debug << "Info: GL Vendor = "<<vendorString<<endl
154        << "Info: GL Renderer = "<<rendererString<<endl
155        << "Info: GL Version = "<<versionString<<endl
156        << "Info: GL Extensions = "<<extString<<endl<<flush;
157
158
159        glEnable(GL_LIGHTING);
160        glEnable(GL_LIGHT0);
161        //glEnable(GL_LIGHT1);
162
163        // set position of the light
164        GLfloat infinite_light[] = {0, -1.0, 0, 1.0f};
165        glLightfv(GL_LIGHT0, GL_POSITION, infinite_light);
166
167  return;
168}
169
170void
171QtGlViewer::paintGL()
172{
173        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
174        glLoadIdentity();
175
176        //float m[4][4];
177        Matrix4x4 m;
178
179        glLoadIdentity();
180        gluLookAt(0.0f, 0.0f, 15.0f,  // eye is at (0,0,30)
181                      0.0f, 0.0f, 0.0f,  // center is at (0,0,0)
182                      0.0f, 1.0f, 0.0f); // up is in positive Y direction
183
184        build_rotmatrix(m.x, manipulatorLastQuat);
185        glMultMatrixf((float *)m.x);
186
187        float s = scale * 20.0f / Magnitude(mRenderer->mSceneGraph->GetBox().Diagonal());
188        glScalef(s, s, s);
189
190        // TODO: determine model matrix for establishing right rendering order for transparency
191        Vector3 t = -mRenderer->mSceneGraph->GetBox().Center();
192        glTranslatef(t.x, t.y, t.z);
193
194        //mDummyViewPoint = -t;
195        //mDummyViewPoint /= s;
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_V:
232                mRenderer->mRenderViewCells = !mRenderer->mRenderViewCells;
233                updateGL();
234                break;
235
236  case Qt::Key_W:
237                mWireframe = !mWireframe;
238                updateGL();
239                break;
240
241  default:
242                e->ignore();
243                cerr << "unknown key" << endl;
244                break;
245  }
246  //updateGL();
247}
248
249void
250QtGlViewer::mouseMoveEvent(QMouseEvent *event)
251{
252        int dx = event->x() - lastPos.x();
253        int dy = event->y() - lastPos.y(); 
254
255        if (event->modifiers() & Qt::CTRL)
256        {
257                scale = scale*(1.0f - dy/(float)height());
258                if (scale < 0.01f)
259                        scale = 0.01f;
260                updateGL();
261        }
262        else
263        {
264                float W = width();
265                float H = height();
266                int x = event->x();
267                int lastX = lastPos.x();
268                int y = event->y();
269                int lastY = lastPos.y();
270
271                float quat[4];
272                trackball(quat,
273                        (2.0 * lastX - W) / W,
274                        (H - 2.0 * lastY) / H,
275                        (2.0 * x - W) / W,
276                        (H - 2.0 * y) / H
277                        );
278
279                add_quats(quat, manipulatorLastQuat, manipulatorLastQuat);
280        }
281
282        lastPos = event->pos();
283        updateGL();
284}
285
286        void QtGlViewer::timerEvent(QTimerEvent *event)
287        {
288                update();
289        }
290
291
292}
293
Note: See TracBrowser for help on using the repository browser.