[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 |
|
---|
| 21 | namespace GtpVisibilityPreprocessor {
|
---|
| 22 |
|
---|
| 23 | QtGlRendererWidget *renderer;
|
---|
| 24 |
|
---|
| 25 | QtGlViewer::QtGlViewer(QWidget *parent,
|
---|
| 26 | QtGlRendererWidget *renderer):
|
---|
| 27 | QGLWidget(parent), mRenderer(renderer)
|
---|
| 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 |
|
---|
[2573] | 44 | QSize QtGlViewer::minimumSizeHint() const
|
---|
[1931] | 45 | {
|
---|
| 46 | return QSize(100, 100);
|
---|
| 47 | }
|
---|
| 48 |
|
---|
[2573] | 49 | QSize QtGlViewer::sizeHint() const
|
---|
| 50 | {
|
---|
[2615] | 51 | // set viewer size here
|
---|
| 52 | return QSize(512, 320);
|
---|
| 53 |
|
---|
| 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] | 59 | void QtGlViewer::RenderScene()
|
---|
[1931] | 60 | {
|
---|
[2604] | 61 | GLfloat mat_ambient[] = { 0.5, 0.5, 0.5, 1.0 };
|
---|
| 62 |
|
---|
[2571] | 63 | // mat_specular and mat_shininess are NOT default values
|
---|
[2604] | 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 };
|
---|
[2571] | 67 |
|
---|
[2604] | 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 };
|
---|
[2571] | 71 |
|
---|
[2604] | 72 | GLfloat lmodel_ambient[] = {0.3, 0.3, 0.3, 1.0};
|
---|
[2571] | 73 |
|
---|
| 74 |
|
---|
[2604] | 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 |
|
---|
[2571] | 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);
|
---|
[2604] | 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 |
|
---|
[2571] | 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);
|
---|
[2604] | 102 | //glColor3f(1.0f, 1.0f, 1.0f);
|
---|
[2571] | 103 | glColor3f(0.8f, 0.8f, 0.8f);
|
---|
| 104 |
|
---|
| 105 | glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE);
|
---|
[2604] | 106 | glEnable(GL_COLOR_MATERIAL);
|
---|
[2571] | 107 |
|
---|
[2562] | 108 | ++ mRenderer->mFrame;
|
---|
[1931] | 109 |
|
---|
[2571] | 110 | glEnable(GL_DEPTH_TEST);
|
---|
| 111 | glDepthMask(GL_TRUE);
|
---|
[2562] | 112 |
|
---|
[2571] | 113 | glCullFace(GL_BACK);
|
---|
| 114 | glShadeModel(GL_FLAT);
|
---|
| 115 |
|
---|
| 116 | glDisable(GL_BLEND);
|
---|
| 117 |
|
---|
[2566] | 118 | mRenderer->mDummyViewPoint = mDummyViewPoint;
|
---|
[2571] | 119 |
|
---|
[2604] | 120 | glEnable(GL_NORMALIZE);
|
---|
| 121 | glEnable(GL_LIGHTING);
|
---|
| 122 | glEnable(GL_LIGHT0);
|
---|
| 123 | glEnable(GL_LIGHT1);
|
---|
[2571] | 124 |
|
---|
[1931] | 125 | mRenderer->RenderPvs();
|
---|
| 126 |
|
---|
[2571] | 127 | glDisable(GL_LIGHTING);
|
---|
[2562] | 128 |
|
---|
[2571] | 129 | if (0 && mRenderer->mShowRays)
|
---|
| 130 | {
|
---|
| 131 | mRenderer->RenderRays(mRenderer->mViewCellsManager->mVizBuffer.GetRays());
|
---|
| 132 | }
|
---|
| 133 |
|
---|
[1931] | 134 | if (mRenderer->mShowRenderCost)
|
---|
| 135 | mRenderer->RenderRenderCost();
|
---|
| 136 |
|
---|
[2562] | 137 | // render into the right side of the window buffer
|
---|
| 138 | if (mRenderer->mRenderViewCells)
|
---|
| 139 | mRenderer->RenderViewCells();
|
---|
| 140 |
|
---|
[2564] | 141 | mRenderer->RenderViewPoint();
|
---|
| 142 |
|
---|
[1931] | 143 | glColor3f(1.0f, 1.0f, 1.0f);
|
---|
| 144 | mRenderer->RenderInfo();
|
---|
| 145 | }
|
---|
| 146 |
|
---|
| 147 |
|
---|
| 148 | void
|
---|
| 149 | QtGlViewer::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 |
|
---|
[2571] | 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 |
|
---|
[1931] | 173 | return;
|
---|
| 174 | }
|
---|
| 175 |
|
---|
| 176 | void
|
---|
| 177 | QtGlViewer::paintGL()
|
---|
| 178 | {
|
---|
[2538] | 179 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
---|
| 180 | glLoadIdentity();
|
---|
[1931] | 181 |
|
---|
[2566] | 182 | //float m[4][4];
|
---|
| 183 | Matrix4x4 m;
|
---|
[1931] | 184 |
|
---|
[2538] | 185 | glLoadIdentity();
|
---|
[2609] | 186 | gluLookAt(0.0f, 0.0f, 15.0f, // eye is at (0,0,30)
|
---|
[2604] | 187 | 0.0f, 0.0f, 0.0f, // center is at (0,0,0)
|
---|
[2605] | 188 | 0.0f, 1.0f, 0.0f); // up is in positive Y direction
|
---|
[1931] | 189 |
|
---|
[2566] | 190 | build_rotmatrix(m.x, manipulatorLastQuat);
|
---|
| 191 | glMultMatrixf((float *)m.x);
|
---|
[1931] | 192 |
|
---|
[2566] | 193 | float s = scale * 20.0f / Magnitude(mRenderer->mSceneGraph->GetBox().Diagonal());
|
---|
[2538] | 194 | glScalef(s, s, s);
|
---|
| 195 |
|
---|
[2569] | 196 | // TODO: determine model matrix for establishing right rendering order for transparency
|
---|
[2538] | 197 | Vector3 t = -mRenderer->mSceneGraph->GetBox().Center();
|
---|
| 198 | glTranslatef(t.x, t.y, t.z);
|
---|
| 199 |
|
---|
[2609] | 200 | //mDummyViewPoint = -t;
|
---|
| 201 | //mDummyViewPoint /= s;
|
---|
[2566] | 202 | //glGetFloatv(GL_MODELVIEW_MATRIX, m1);
|
---|
[2609] | 203 |
|
---|
[2566] | 204 | m.Invert();
|
---|
| 205 |
|
---|
[2567] | 206 | mModelMatrix *= m;
|
---|
| 207 | mDummyViewPoint = mModelMatrix * Vector3(0, 0, 0);
|
---|
| 208 |
|
---|
| 209 | //mDummyViewPoint
|
---|
| 210 | //cout << "view point: " << mDummyViewPoint << endl;
|
---|
[1931] | 211 | RenderScene();
|
---|
| 212 | }
|
---|
| 213 |
|
---|
| 214 | void
|
---|
| 215 | QtGlViewer::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 |
|
---|
| 227 | void
|
---|
| 228 | QtGlViewer::mousePressEvent(QMouseEvent *event)
|
---|
| 229 | {
|
---|
| 230 | lastPos = event->pos();
|
---|
| 231 | }
|
---|
| 232 |
|
---|
| 233 | void
|
---|
[2604] | 234 | QtGlViewer::keyPressEvent(QKeyEvent * e)
|
---|
[1931] | 235 | {
|
---|
| 236 | switch (e->key()) {
|
---|
[2615] | 237 | case Qt::Key_V:
|
---|
| 238 | mRenderer->mRenderViewCells = !mRenderer->mRenderViewCells;
|
---|
| 239 | updateGL();
|
---|
| 240 | break;
|
---|
| 241 |
|
---|
[1931] | 242 | case Qt::Key_W:
|
---|
[2604] | 243 | mWireframe = !mWireframe;
|
---|
[1931] | 244 | updateGL();
|
---|
[2615] | 245 | break;
|
---|
| 246 |
|
---|
[1931] | 247 | default:
|
---|
| 248 | e->ignore();
|
---|
[2604] | 249 | cerr << "unknown key" << endl;
|
---|
[1931] | 250 | break;
|
---|
| 251 | }
|
---|
[2604] | 252 | //updateGL();
|
---|
[1931] | 253 | }
|
---|
| 254 |
|
---|
| 255 | void
|
---|
| 256 | QtGlViewer::mouseMoveEvent(QMouseEvent *event)
|
---|
| 257 | {
|
---|
[2614] | 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 | {
|
---|
[1931] | 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();
|
---|
[2614] | 276 |
|
---|
[1931] | 277 | float quat[4];
|
---|
| 278 | trackball(quat,
|
---|
[2614] | 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 | );
|
---|
[2605] | 284 |
|
---|
[1931] | 285 | add_quats(quat, manipulatorLastQuat, manipulatorLastQuat);
|
---|
[2614] | 286 | }
|
---|
[1969] | 287 |
|
---|
[2614] | 288 | lastPos = event->pos();
|
---|
| 289 | updateGL();
|
---|
[1931] | 290 | }
|
---|
[1936] | 291 |
|
---|
[2614] | 292 | void QtGlViewer::timerEvent(QTimerEvent *event)
|
---|
| 293 | {
|
---|
| 294 | update();
|
---|
| 295 | }
|
---|
[2562] | 296 |
|
---|
[2614] | 297 |
|
---|
[2562] | 298 | }
|
---|
| 299 |
|
---|