1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408
|
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>libQGLViewer standardCamera example</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link href="../qglviewer.css" rel="stylesheet" type="text/css" />
<link rel="shortcut icon" href="../images/qglviewer.ico" type="image/x-icon" />
<link rel="icon" href="../images/qglviewer.icon.png" type="image/png" />
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-23223012-2']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
</script>
</head>
<body>
<div class="banner">
<a class="qindex" href="../index.html">Home</a>
<a class="qindex" href="../download.html">Download</a>
<a class="qindex highlight" href="index.html">Gallery</a>
<a class="qindex" href="../refManual/hierarchy.html">Documentation</a>
<a class="qindex" href="../developer.html">Developer</a>
</div>
<h1>The standardCamera example</h1>
<center>
<img src="../images/standardCamera.jpg" width="330" height="228" alt="standardCamera"/>
</center>
<p>
A 'standard' Camera with fixed near and far planes.
</p>
<p>
A new <code>StandardCamera</code> class is derived from <code>Camera</code>.
Its near and far planes distances are set to fixed values (instead of being fit to
scene dimensions as is done in the <code>QGLViewer::Camera</code>).
</p>
<p>
The orthographic frustum dimensions are fixed instead of depending on the distance
to the <code>pivotPoint()</code>. Since this may be needed for some applications,
you may want to use this <code>standardCamera</code> class in your code.
</p>
<h2>standardCamera.h</h2>
<pre>
#include <QGLViewer/camera.h>
class StandardCamera : public qglviewer::Camera {
public:
StandardCamera();
virtual qreal zNear() const;
virtual qreal zFar() const;
void toggleMode() { standard = !standard; }
bool isStandard() { return standard; }
void changeOrthoFrustumSize(int delta);
virtual void getOrthoWidthHeight(GLdouble &halfWidth,
GLdouble &halfHeight) const;
private:
bool standard;
float orthoSize;
};
</pre>
<h2>standardCamera.cpp</h2>
<pre>
#include "standardCamera.h"
#include <QWheelEvent>
using namespace qglviewer;
StandardCamera::StandardCamera() {
standard = true;
orthoSize = 1.0;
}
qreal StandardCamera::zNear() const {
if (standard)
return 0.001;
else
return Camera::zNear();
}
qreal StandardCamera::zFar() const {
if (standard)
return 1000.0;
else
return Camera::zFar();
}
void StandardCamera::changeOrthoFrustumSize(int delta) {
if (delta > 0)
orthoSize *= 1.1;
else
orthoSize /= 1.1;
}
void StandardCamera::getOrthoWidthHeight(GLdouble &halfWidth,
GLdouble &halfHeight) const {
if (standard) {
halfHeight = orthoSize;
halfWidth = aspectRatio() * orthoSize;
} else
Camera::getOrthoWidthHeight(halfWidth, halfHeight);
}
</pre>
<h2>cameraViewer.h</h2>
<pre>
#include <QGLViewer/qglviewer.h>
class CameraViewer : public QGLViewer {
public:
CameraViewer(qglviewer::Camera *camera);
protected:
virtual void draw();
virtual void init();
private:
qglviewer::Camera *c;
};
</pre>
<h2>viewer.h</h2>
<pre>
#include <QGLViewer/qglviewer.h>
class StandardCamera;
class Viewer : public QGLViewer {
Q_OBJECT
public:
Viewer(StandardCamera *camera);
public:
Q_SIGNALS:
void cameraChanged();
protected:
virtual void draw();
virtual void init();
virtual QString helpString() const;
virtual void keyPressEvent(QKeyEvent *e);
virtual void wheelEvent(QWheelEvent *e);
private:
void showMessage();
};
</pre>
<h2>cameraViewer.cpp</h2>
<pre>
#include "cameraViewer.h"
using namespace qglviewer;
CameraViewer::CameraViewer(Camera *camera) : c(camera){};
void CameraViewer::draw() {
// Exactly the same draw than for 'Viewer'. The two viewers could also share a
// 'Scene' instance.
const float nbSteps = 200.0;
glBegin(GL_QUAD_STRIP);
for (int i = 0; i < nbSteps; ++i) {
const float ratio = i / nbSteps;
const float angle = 21.0 * ratio;
const float c = cos(angle);
const float s = sin(angle);
const float r1 = 1.0 - 0.8f * ratio;
const float r2 = 0.8f - 0.8f * ratio;
const float alt = ratio - 0.5f;
const float nor = 0.5f;
const float up = sqrt(1.0 - nor * nor);
glColor3f(1.0 - ratio, 0.2f, ratio);
glNormal3f(nor * c, up, nor * s);
glVertex3f(r1 * c, alt, r1 * s);
glVertex3f(r2 * c, alt + 0.05f, r2 * s);
}
glEnd();
// Draws the other viewer's camera
glDisable(GL_LIGHTING);
glLineWidth(4.0);
glColor4f(1.0, 1.0, 1.0, 0.5);
c->draw();
glEnable(GL_LIGHTING);
}
void CameraViewer::init() {
// Places the cameraViewer's camera far away in order to see the (other
// viewer) camera.
if (!restoreStateFromFile()) {
// Make near and far planes much further from scene in order not to clip c's
// display.
camera()->setZClippingCoefficient(50.0);
camera()->setViewDirection(qglviewer::Vec(0.0, -1.0, 0.0));
showEntireScene();
}
// Enable semi-transparent culling planes
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
}
</pre>
<h2>main.cpp</h2>
<pre>
#include "cameraViewer.h"
#include "standardCamera.h"
#include "viewer.h"
#include <QApplication>
#include <QGLViewer/manipulatedCameraFrame.h>
int main(int argc, char **argv) {
QApplication application(argc, argv);
// Instantiate the two viewers.
StandardCamera *sc = new StandardCamera();
Viewer viewer(sc);
CameraViewer cviewer(sc);
// Make sure every v camera movement updates the camera viewer
QObject::connect(viewer.camera()->frame(), SIGNAL(manipulated()), &cviewer,
SLOT(updateGL()));
QObject::connect(viewer.camera()->frame(), SIGNAL(spun()), &cviewer,
SLOT(updateGL()));
// Also update on camera change (type or mode)
QObject::connect(&viewer, SIGNAL(cameraChanged()), &cviewer,
SLOT(updateGL()));
viewer.setWindowTitle("standardCamera");
cviewer.setWindowTitle("Camera viewer");
cviewer.show();
viewer.show();
return application.exec();
}
</pre>
<h2>viewer.cpp</h2>
<pre>
#include "viewer.h"
#include "standardCamera.h"
#include <QKeyEvent>
#if QT_VERSION > QT_VERSION_CHECK(6, 0, 0)
#define MidButton MiddleButton
#endif
using namespace std;
using namespace qglviewer;
Viewer::Viewer(StandardCamera *nfc) {
// Change the camera.
Camera *c = camera();
setCamera(nfc);
delete c;
}
// Draws a spiral
void Viewer::draw() {
const float nbSteps = 200.0;
glBegin(GL_QUAD_STRIP);
for (int i = 0; i < nbSteps; ++i) {
const float ratio = i / nbSteps;
const float angle = 21.0 * ratio;
const float c = cos(angle);
const float s = sin(angle);
const float r1 = 1.0 - 0.8f * ratio;
const float r2 = 0.8f - 0.8f * ratio;
const float alt = ratio - 0.5f;
const float nor = 0.5f;
const float up = sqrt(1.0 - nor * nor);
glColor3f(1.0 - ratio, 0.2f, ratio);
glNormal3f(nor * c, up, nor * s);
glVertex3f(r1 * c, alt, r1 * s);
glVertex3f(r2 * c, alt + 0.05f, r2 * s);
}
glEnd();
}
void Viewer::init() {
// Restore previous viewer state.
if (!restoreStateFromFile())
showEntireScene();
setKeyDescription(Qt::Key_T,
"Toggles camera type (perspective or orthographic)");
setKeyDescription(Qt::Key_M, "Toggles camera mode (standard or QGLViewer)");
setMouseBindingDescription(
Qt::ShiftModifier, Qt::MidButton,
"Change frustum size (for standard camera in orthographic mode)");
// Display help window.
help();
}
void Viewer::showMessage() {
QString std = ((StandardCamera *)camera())->isStandard() ? "Standard camera"
: "QGLViewer camera";
QString type =
camera()->type() == Camera::PERSPECTIVE ? "Perspective" : "Orthographic";
displayMessage(std + " - " + type);
Q_EMIT cameraChanged();
}
void Viewer::keyPressEvent(QKeyEvent *e) {
if (e->key() == Qt::Key_M) {
// 'M' changes mode : standard or QGLViewer camera
((StandardCamera *)camera())->toggleMode();
showMessage();
} else if (e->key() == Qt::Key_T) {
// 'T' changes the projection type : perspective or orthogonal
if (camera()->type() == Camera::ORTHOGRAPHIC)
camera()->setType(Camera::PERSPECTIVE);
else
camera()->setType(Camera::ORTHOGRAPHIC);
showMessage();
} else
QGLViewer::keyPressEvent(e);
}
void Viewer::wheelEvent(QWheelEvent *e) {
if ((camera()->type() == Camera::ORTHOGRAPHIC) &&
(((StandardCamera *)camera())->isStandard()) &&
(e->modifiers() & Qt::ShiftModifier)) {
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
((StandardCamera *)camera())->changeOrthoFrustumSize(e->delta());
#else
((StandardCamera *)camera())->changeOrthoFrustumSize(e->angleDelta().y());
#endif
Q_EMIT cameraChanged();
update();
} else
QGLViewer::wheelEvent(e);
}
QString Viewer::helpString() const {
QString text("<h2>S t a n d a r d C a m e r a</h2>");
text += "An overloaded <code>Camera</code> class is used, that reproduces "
"the 'standard' OpenGL settings.<br><br>";
text += "With this camera, the near and (resp. far) plane distance is set to "
"a very small (resp. very large) value. ";
text += "With the orthographic camera type, the frustum dimensions are "
"fixed. Use <code>Shift</code> and the mouse wheel to change "
"them.<br><br>";
text += "On the other hand, the QGLViewer camera fits the near and far "
"distances to the scene radius. ";
text += "Fine tuning is available using <code>zClippingCoefficient()</code> "
"and <code>zNearCoefficient()</code>. ";
text += "However, visual results do not seem to be impacted by this zBuffer "
"fitted range.<br><br>";
text += "The QGLViewer camera also adapts the orthographic frustum "
"dimensions to the distance to the <code>pivotPoint()</code> to "
"mimic a perspective camera. ";
text += "Since this behavior may not be needed, this example shows how to "
"override it.<br><br>";
text += "The second viewer displays the first one's camera to show its "
"configuration.<br><br>";
text += "Use <b>M</b> to switch between 'standard' and QGLViewer camera "
"behavior.<br>";
text += "Use <b>T</b> to switch between perspective and orthographic camera "
"type.<br><br>";
text += "Use <b>Shift+wheel</b> to change standard camera orthographic size.";
return text;
}
</pre>
<p>
Back to the <a href="index.html">examples main page</a>.
</p>
</body>
</html>
|