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

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,
[2664]26                                           QtGlRendererWidget *renderer)
27: QGLWidget(parent), mRenderer(renderer)
[1931]28{
29        scale = 1.0f;
30        mWireframe = false;
[2567]31        mModelMatrix = IdentityMatrix();
32
[2615]33        //setWindowTitle("PVS visualization");
34        setWindowTitle("Global View Cell Visualization");
35
[1931]36        trackball(manipulatorLastQuat, 0.0f, 0.0f, 0.0f, 0.0f);
[2562]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);
[1931]42}
43 
[2664]44
[2573]45QSize QtGlViewer::minimumSizeHint() const
[1931]46{
47  return QSize(100, 100);
48}
49
[2573]50QSize QtGlViewer::sizeHint() const
51{
[2615]52        // set viewer size here
53        return QSize(512, 320);
54        //return QSize(512, 384);
55        //return QSize(640, 480);
[1931]56}
57 
58// This method has to be rendefined by all renderer implementations
[2571]59void QtGlViewer::RenderScene()
[1931]60{
[2705]61        GLfloat mat_ambient[]   = {0.5f, 0.5f, 0.5f, 1.0f};
[2604]62       
[2571]63        // mat_specular and mat_shininess are NOT default values
[2705]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  };
[2571]67
[2705]68        GLfloat light_ambient[]  = {0.2f, 0.2f, 0.2f, 1.0f};
[2709]69        GLfloat light_diffuse[]  = {0.6f, 0.6f, 0.6f, 1.0f};
[2705]70        GLfloat light_specular[] = {0.3f, 0.3f, 0.3f, 1.0f};
[2571]71
[2686]72        GLfloat lmodel_ambient[] = {0.3f, 0.3f, 0.3f, 1.0f};
[2571]73
74
[2604]75        // set position of the light
[2705]76        GLfloat infinite_light[] = {1.0f, 0.8f, 1.0f, 0.0f};
[2604]77        glLightfv (GL_LIGHT0, GL_POSITION, infinite_light);
78
79        // set position of the light2
[2705]80        GLfloat infinite_light2[] = {-0.3f, 1.5f, 1.0f, 0.0f};
[2604]81        glLightfv (GL_LIGHT1, GL_POSITION, infinite_light2);
82
[2571]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);
[2604]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       
[2571]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);
[2604]96        //glColor3f(1.0f, 1.0f, 1.0f);
[2571]97        glColor3f(0.8f, 0.8f, 0.8f);
98       
99        glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE);
[2604]100        glEnable(GL_COLOR_MATERIAL);
[2571]101
[2562]102        ++ mRenderer->mFrame;
[1931]103       
[2571]104        glEnable(GL_DEPTH_TEST);
105        glDepthMask(GL_TRUE);
[2562]106
[2571]107        glCullFace(GL_BACK);
108        glShadeModel(GL_FLAT);
109
110        glDisable(GL_BLEND);
111
[2566]112        mRenderer->mDummyViewPoint = mDummyViewPoint;
[2571]113       
[2604]114        glEnable(GL_NORMALIZE);
115        glEnable(GL_LIGHTING);
116        glEnable(GL_LIGHT0);
117        glEnable(GL_LIGHT1);
[2571]118
[2670]119        mRenderer->VisualizePvs();
[1931]120       
[2571]121        glDisable(GL_LIGHTING);
[2562]122
[2571]123        if (0 && mRenderer->mShowRays)
124        {
125                mRenderer->RenderRays(mRenderer->mViewCellsManager->mVizBuffer.GetRays());
126        }
127
[1931]128        if (mRenderer->mShowRenderCost)
129          mRenderer->RenderRenderCost();
130
[2562]131        // render into the right side of the window buffer
132        if (mRenderer->mRenderViewCells)
133                mRenderer->RenderViewCells();
134
[2564]135        mRenderer->RenderViewPoint();
136
[1931]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
[2571]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
[1931]167  return;
168}
169
170void
171QtGlViewer::paintGL()
172{
[2538]173        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
174        glLoadIdentity();
[1931]175
[2566]176        //float m[4][4];
177        Matrix4x4 m;
[1931]178
[2538]179        glLoadIdentity();
[2609]180        gluLookAt(0.0f, 0.0f, 15.0f,  // eye is at (0,0,30)
[2604]181                      0.0f, 0.0f, 0.0f,  // center is at (0,0,0)
[2605]182                      0.0f, 1.0f, 0.0f); // up is in positive Y direction
[1931]183
[2566]184        build_rotmatrix(m.x, manipulatorLastQuat);
185        glMultMatrixf((float *)m.x);
[1931]186
[2566]187        float s = scale * 20.0f / Magnitude(mRenderer->mSceneGraph->GetBox().Diagonal());
[2538]188        glScalef(s, s, s);
189
[2569]190        // TODO: determine model matrix for establishing right rendering order for transparency
[2538]191        Vector3 t = -mRenderer->mSceneGraph->GetBox().Center();
192        glTranslatef(t.x, t.y, t.z);
193
[2609]194        //mDummyViewPoint = -t;
195        //mDummyViewPoint /= s;
[2566]196        //glGetFloatv(GL_MODELVIEW_MATRIX, m1);
[2609]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()) {
[2615]231  case Qt::Key_V:
232                mRenderer->mRenderViewCells = !mRenderer->mRenderViewCells;
233                updateGL();
234                break;
235
[1931]236  case Qt::Key_W:
[2604]237                mWireframe = !mWireframe;
[1931]238                updateGL();
[2615]239                break;
240
[1931]241  default:
242                e->ignore();
[2604]243                cerr << "unknown key" << endl;
[1931]244                break;
245  }
[2604]246  //updateGL();
[1931]247}
248
249void
250QtGlViewer::mouseMoveEvent(QMouseEvent *event)
251{
[2614]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        {
[1931]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();
[2614]270
[1931]271                float quat[4];
272                trackball(quat,
[2614]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                        );
[2605]278
[1931]279                add_quats(quat, manipulatorLastQuat, manipulatorLastQuat);
[2614]280        }
[1969]281
[2614]282        lastPos = event->pos();
283        updateGL();
[1931]284}
[1936]285
[2614]286        void QtGlViewer::timerEvent(QTimerEvent *event)
287        {
288                update();
289        }
[2562]290
[2614]291
[2562]292}
293
Note: See TracBrowser for help on using the repository browser.