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