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 |
|
---|
19 |
|
---|
20 | namespace GtpVisibilityPreprocessor {
|
---|
21 |
|
---|
22 | QtGlRendererWidget *renderer;
|
---|
23 |
|
---|
24 | QtGlViewer::QtGlViewer(QWidget *parent,
|
---|
25 | QtGlRendererWidget *renderer):
|
---|
26 | QGLWidget(parent), mRenderer(renderer)
|
---|
27 | {
|
---|
28 | scale = 1.0f;
|
---|
29 | mWireframe = false;
|
---|
30 | mModelMatrix = IdentityMatrix();
|
---|
31 |
|
---|
32 | trackball(manipulatorLastQuat, 0.0f, 0.0f, 0.0f, 0.0f);
|
---|
33 | //connect(renderer, SIGNAL(PvsUpdated()), this, SLOT(updateGL()));
|
---|
34 | //connect(renderer, SIGNAL(cutupdated), this, SLOT(updateGL()));
|
---|
35 |
|
---|
36 | const int delay = 250; // in milliseconds
|
---|
37 | timerId = startTimer(delay);
|
---|
38 | }
|
---|
39 |
|
---|
40 | QSize
|
---|
41 | QtGlViewer::minimumSizeHint() const
|
---|
42 | {
|
---|
43 | return QSize(100, 100);
|
---|
44 | }
|
---|
45 |
|
---|
46 | QSize
|
---|
47 | QtGlViewer::sizeHint() const {
|
---|
48 | return QSize(500, 500);
|
---|
49 | }
|
---|
50 |
|
---|
51 | // This method has to be rendefined by all renderer implementations
|
---|
52 | void
|
---|
53 | QtGlViewer::RenderScene()
|
---|
54 | {
|
---|
55 | ++ mRenderer->mFrame;
|
---|
56 |
|
---|
57 | //glDisable(GL_DEPTH_TEST);
|
---|
58 |
|
---|
59 | mRenderer->mDummyViewPoint = mDummyViewPoint;
|
---|
60 | glColor3f(0.8f, 0.8f, 0.8f);
|
---|
61 | mRenderer->RenderPvs();
|
---|
62 |
|
---|
63 | // if (mRenderer->mShowRays) {
|
---|
64 | // RssPreprocessor *p = (RssPreprocessor *)
|
---|
65 | // mRenderer->mViewCellsManager->GetPreprocessor();
|
---|
66 | // if (p->mRssTree) {
|
---|
67 | // VssRayContainer rays;
|
---|
68 | // p->mRssTree->CollectRays(rays, 10000);
|
---|
69 | // mRenderer->RenderRays(rays);
|
---|
70 | // }
|
---|
71 | // }
|
---|
72 |
|
---|
73 | if (mRenderer->mShowRenderCost)
|
---|
74 | mRenderer->RenderRenderCost();
|
---|
75 |
|
---|
76 | // render into the right side of the window buffer
|
---|
77 | if (mRenderer->mRenderViewCells)
|
---|
78 | mRenderer->RenderViewCells();
|
---|
79 |
|
---|
80 | mRenderer->RenderViewPoint();
|
---|
81 |
|
---|
82 | glColor3f(1.0f, 1.0f, 1.0f);
|
---|
83 | mRenderer->RenderInfo();
|
---|
84 | }
|
---|
85 |
|
---|
86 |
|
---|
87 | void
|
---|
88 | QtGlViewer::initializeGL()
|
---|
89 | {
|
---|
90 | const char *vendorString, *rendererString, *versionString, *extString;
|
---|
91 |
|
---|
92 | // get GL info
|
---|
93 | vendorString = (const char *) glGetString(GL_VENDOR);
|
---|
94 | rendererString = (const char *) glGetString(GL_RENDERER);
|
---|
95 | versionString = (const char *) glGetString(GL_VERSION);
|
---|
96 | extString = (const char *) glGetString(GL_EXTENSIONS);
|
---|
97 |
|
---|
98 | Debug << "Info: GL Vendor = "<<vendorString<<endl
|
---|
99 | << "Info: GL Renderer = "<<rendererString<<endl
|
---|
100 | << "Info: GL Version = "<<versionString<<endl
|
---|
101 | << "Info: GL Extensions = "<<extString<<endl<<flush;
|
---|
102 |
|
---|
103 |
|
---|
104 | return;
|
---|
105 | }
|
---|
106 |
|
---|
107 | void
|
---|
108 | QtGlViewer::paintGL()
|
---|
109 | {
|
---|
110 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
---|
111 | glLoadIdentity();
|
---|
112 |
|
---|
113 | //float m[4][4];
|
---|
114 | Matrix4x4 m;
|
---|
115 |
|
---|
116 | glLoadIdentity();
|
---|
117 | gluLookAt(0.0, 0.0, 1.0, /* eye is at (0,0,30) */
|
---|
118 | 0.0, 0.0, 0.0, /* center is at (0,0,0) */
|
---|
119 | 0.0, 1.0, 0.); /* up is in positive Y direction */
|
---|
120 |
|
---|
121 | build_rotmatrix(m.x, manipulatorLastQuat);
|
---|
122 | glMultMatrixf((float *)m.x);
|
---|
123 |
|
---|
124 | float s = scale * 20.0f / Magnitude(mRenderer->mSceneGraph->GetBox().Diagonal());
|
---|
125 | glScalef(s, s, s);
|
---|
126 |
|
---|
127 | Vector3 t = -mRenderer->mSceneGraph->GetBox().Center();
|
---|
128 | glTranslatef(t.x, t.y, t.z);
|
---|
129 |
|
---|
130 | /*mDummyViewPoint = -t;
|
---|
131 | mDummyViewPoint /= s;
|
---|
132 |
|
---|
133 | //glGetFloatv(GL_MODELVIEW_MATRIX, m1);
|
---|
134 | */
|
---|
135 | m.Invert();
|
---|
136 |
|
---|
137 | mModelMatrix *= m;
|
---|
138 | mDummyViewPoint = mModelMatrix * Vector3(0, 0, 0);
|
---|
139 |
|
---|
140 | //mDummyViewPoint
|
---|
141 | //cout << "view point: " << mDummyViewPoint << endl;
|
---|
142 | RenderScene();
|
---|
143 | }
|
---|
144 |
|
---|
145 | void
|
---|
146 | QtGlViewer::resizeGL(int width, int height)
|
---|
147 | {
|
---|
148 | glViewport(0, 0, width, height);
|
---|
149 |
|
---|
150 | glMatrixMode(GL_PROJECTION);
|
---|
151 | glLoadIdentity();
|
---|
152 | gluPerspective(70.0, 1.0, 0.1, 100.0);
|
---|
153 | // glOrtho(-0.5*width/height, +0.5*width/height, 0.5, -0.5, 4.0, 15.0);
|
---|
154 | glMatrixMode(GL_MODELVIEW);
|
---|
155 | }
|
---|
156 |
|
---|
157 |
|
---|
158 | void
|
---|
159 | QtGlViewer::mousePressEvent(QMouseEvent *event)
|
---|
160 | {
|
---|
161 | lastPos = event->pos();
|
---|
162 | }
|
---|
163 |
|
---|
164 | void
|
---|
165 | QtGlViewer::keyPressEvent ( QKeyEvent * e )
|
---|
166 | {
|
---|
167 | switch (e->key()) {
|
---|
168 | case Qt::Key_W:
|
---|
169 | mWireframe = !mWireframe;
|
---|
170 | updateGL();
|
---|
171 | default:
|
---|
172 | e->ignore();
|
---|
173 | break;
|
---|
174 | }
|
---|
175 | updateGL();
|
---|
176 | }
|
---|
177 |
|
---|
178 | void
|
---|
179 | QtGlViewer::mouseMoveEvent(QMouseEvent *event)
|
---|
180 | {
|
---|
181 | int dx = event->x() - lastPos.x();
|
---|
182 | int dy = event->y() - lastPos.y();
|
---|
183 |
|
---|
184 | if (event->modifiers() & Qt::CTRL)
|
---|
185 | {
|
---|
186 | scale = scale*(1.0f - dy/(float)height());
|
---|
187 | if (scale < 0.01f)
|
---|
188 | scale = 0.01f;
|
---|
189 | updateGL();
|
---|
190 | }
|
---|
191 | else {
|
---|
192 | float W = width();
|
---|
193 | float H = height();
|
---|
194 | int x = event->x();
|
---|
195 | int lastX = lastPos.x();
|
---|
196 | int y = event->y();
|
---|
197 | int lastY = lastPos.y();
|
---|
198 |
|
---|
199 | float quat[4];
|
---|
200 | trackball(quat,
|
---|
201 | (2.0 * lastX - W) / W,
|
---|
202 | (H - 2.0 * lastY) / H,
|
---|
203 | (2.0 * x - W) / W,
|
---|
204 | (H - 2.0 * y) / H
|
---|
205 | );
|
---|
206 | add_quats(quat, manipulatorLastQuat, manipulatorLastQuat);
|
---|
207 | }
|
---|
208 |
|
---|
209 | lastPos = event->pos();
|
---|
210 | updateGL();
|
---|
211 | }
|
---|
212 |
|
---|
213 | void QtGlViewer::timerEvent(QTimerEvent *event)
|
---|
214 | {
|
---|
215 | update();
|
---|
216 | }
|
---|
217 |
|
---|
218 |
|
---|
219 | }
|
---|
220 |
|
---|