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