mirror of https://github.com/mosra/magnum.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
126 lines
4.1 KiB
126 lines
4.1 KiB
|
16 years ago
|
/*
|
||
|
15 years ago
|
Copyright © 2010, 2011, 2012 Vladimír Vondruš <mosra@centrum.cz>
|
||
|
16 years ago
|
|
||
|
|
This file is part of Magnum.
|
||
|
|
|
||
|
|
Magnum is free software: you can redistribute it and/or modify
|
||
|
|
it under the terms of the GNU Lesser General Public License version 3
|
||
|
|
only, as published by the Free Software Foundation.
|
||
|
|
|
||
|
|
Magnum is distributed in the hope that it will be useful,
|
||
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
|
|
GNU Lesser General Public License version 3 for more details.
|
||
|
|
*/
|
||
|
|
|
||
|
|
#include "Camera.h"
|
||
|
14 years ago
|
#include "Framebuffer.h"
|
||
|
16 years ago
|
#include "Scene.h"
|
||
|
16 years ago
|
|
||
|
14 years ago
|
using namespace std;
|
||
|
|
|
||
|
14 years ago
|
namespace Magnum { namespace SceneGraph {
|
||
|
16 years ago
|
|
||
|
14 years ago
|
Camera::Camera(Object* parent): Object(parent), _aspectRatioPolicy(AspectRatioPolicy::Extend) {}
|
||
|
16 years ago
|
|
||
|
|
void Camera::setOrthographic(GLfloat size, GLfloat near, GLfloat far) {
|
||
|
15 years ago
|
_near = near;
|
||
|
|
_far = far;
|
||
|
|
|
||
|
16 years ago
|
/* Scale the volume down so it fits in (-1, 1) in all directions */
|
||
|
|
GLfloat xyScale = 2/size;
|
||
|
|
GLfloat zScale = 2/(far-near);
|
||
|
14 years ago
|
rawProjectionMatrix = Matrix4::scaling({xyScale, xyScale, -zScale});
|
||
|
16 years ago
|
|
||
|
|
/* Move the volume on z into (-1, 1) range */
|
||
|
14 years ago
|
rawProjectionMatrix = Matrix4::translation(Vector3::zAxis(-1-near*zScale))*rawProjectionMatrix;
|
||
|
16 years ago
|
|
||
|
|
fixAspectRatio();
|
||
|
|
}
|
||
|
|
|
||
|
|
void Camera::setPerspective(GLfloat fov, GLfloat near, GLfloat far) {
|
||
|
15 years ago
|
_near = near;
|
||
|
|
_far = far;
|
||
|
|
|
||
|
16 years ago
|
/* First move the volume on z in (-1, 1) range */
|
||
|
14 years ago
|
rawProjectionMatrix = Matrix4::translation(Vector3::zAxis(2*far*near/(far+near)));
|
||
|
16 years ago
|
|
||
|
|
/* Then apply magic perspective matrix (with reversed Z) */
|
||
|
14 years ago
|
static const Matrix4 a(1.0f, 0.0f, 0.0f, 0.0f,
|
||
|
|
0.0f, 1.0f, 0.0f, 0.0f,
|
||
|
|
0.0f, 0.0f, -1.0f, -1.0f,
|
||
|
|
0.0f, 0.0f, 0.0f, 0.0f);
|
||
|
14 years ago
|
rawProjectionMatrix = a*rawProjectionMatrix;
|
||
|
16 years ago
|
|
||
|
|
/* Then scale the volume down so it fits in (-1, 1) in all directions */
|
||
|
|
GLfloat xyScale = 1/tan(fov/2);
|
||
|
|
GLfloat zScale = 1+2*near/(far-near);
|
||
|
14 years ago
|
rawProjectionMatrix = Matrix4::scaling({xyScale, xyScale, zScale})*rawProjectionMatrix;
|
||
|
16 years ago
|
|
||
|
|
/* And... another magic */
|
||
|
14 years ago
|
rawProjectionMatrix[3][3] = 0;
|
||
|
16 years ago
|
|
||
|
|
fixAspectRatio();
|
||
|
16 years ago
|
}
|
||
|
|
|
||
|
14 years ago
|
void Camera::setViewport(const Math::Vector2<GLsizei>& size) {
|
||
|
14 years ago
|
Framebuffer::setViewport({0, 0}, size);
|
||
|
16 years ago
|
|
||
|
15 years ago
|
_viewport = size;
|
||
|
16 years ago
|
fixAspectRatio();
|
||
|
|
}
|
||
|
|
|
||
|
14 years ago
|
void Camera::clean(const Matrix4& absoluteTransformation) {
|
||
|
|
Object::clean(absoluteTransformation);
|
||
|
|
|
||
|
|
_cameraMatrix = absoluteTransformation.inverted();
|
||
|
16 years ago
|
}
|
||
|
|
|
||
|
16 years ago
|
void Camera::fixAspectRatio() {
|
||
|
|
/* Don't divide by zero */
|
||
|
15 years ago
|
if(_viewport.x() == 0 || _viewport.y() == 0) {
|
||
|
16 years ago
|
_projectionMatrix = rawProjectionMatrix;
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
/* Extend on larger side = scale larger side down */
|
||
|
14 years ago
|
if(_aspectRatioPolicy == AspectRatioPolicy::Extend) {
|
||
|
15 years ago
|
_projectionMatrix = ((_viewport.x() > _viewport.y()) ?
|
||
|
14 years ago
|
Matrix4::scaling({GLfloat(_viewport.y())/_viewport.x(), 1, 1}) :
|
||
|
|
Matrix4::scaling({1, GLfloat(_viewport.x())/_viewport.y(), 1})
|
||
|
16 years ago
|
)*rawProjectionMatrix;
|
||
|
|
|
||
|
|
/* Clip on smaller side = scale smaller side up */
|
||
|
14 years ago
|
} else if(_aspectRatioPolicy == AspectRatioPolicy::Clip) {
|
||
|
15 years ago
|
_projectionMatrix = ((_viewport.x() > _viewport.y()) ?
|
||
|
14 years ago
|
Matrix4::scaling({1, GLfloat(_viewport.x())/_viewport.y(), 1}) :
|
||
|
|
Matrix4::scaling({GLfloat(_viewport.y())/_viewport.x(), 1, 1})
|
||
|
16 years ago
|
)*rawProjectionMatrix;
|
||
|
|
|
||
|
|
/* Don't preserve anything */
|
||
|
|
} else _projectionMatrix = rawProjectionMatrix;
|
||
|
16 years ago
|
}
|
||
|
|
|
||
|
14 years ago
|
void Camera::draw() {
|
||
|
14 years ago
|
Scene* s = scene();
|
||
|
14 years ago
|
CORRADE_ASSERT(s, "Camera: cannot draw without camera attached to scene", );
|
||
|
14 years ago
|
|
||
|
14 years ago
|
Framebuffer::clear();
|
||
|
14 years ago
|
|
||
|
|
/* Recursively draw child objects */
|
||
|
14 years ago
|
drawChildren(s, cameraMatrix());
|
||
|
14 years ago
|
}
|
||
|
|
|
||
|
|
void Camera::drawChildren(Object* object, const Matrix4& transformationMatrix) {
|
||
|
|
for(set<Object*>::const_iterator it = object->children().begin(); it != object->children().end(); ++it) {
|
||
|
|
/* Transformation matrix for the object */
|
||
|
|
Matrix4 matrix = transformationMatrix*(*it)->transformation();
|
||
|
|
|
||
|
|
/* Draw the object and its children */
|
||
|
|
(*it)->draw(matrix, this);
|
||
|
|
drawChildren(*it, matrix);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
14 years ago
|
}}
|