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