Browse Source

Use Vector2<unsigned int> for viewport size in Camera instead of ints.

pull/279/head
Vladimír Vondruš 15 years ago
parent
commit
4a23efa307
  1. 23
      src/Camera.cpp
  2. 13
      src/Camera.h

23
src/Camera.cpp

@ -18,7 +18,7 @@
namespace Magnum { namespace Magnum {
Camera::Camera(Object* parent): Object(parent), _active(0), viewportWidth(0), viewportHeight(0), _aspectRatioPolicy(Extend) { Camera::Camera(Object* parent): Object(parent), _active(0), _aspectRatioPolicy(Extend) {
setOrthographic(2, 1, 1000); setOrthographic(2, 1, 1000);
} }
@ -79,11 +79,10 @@ void Camera::setPerspective(GLfloat fov, GLfloat near, GLfloat far) {
fixAspectRatio(); fixAspectRatio();
} }
void Camera::setViewport(int width, int height) { void Camera::setViewport(const Math::Vector2<unsigned int>& size) {
glViewport(0, 0, width, height); glViewport(0, 0, size.x(), size.y());
viewportWidth = width; _viewport = size;
viewportHeight = height;
fixAspectRatio(); fixAspectRatio();
} }
@ -113,23 +112,23 @@ void Camera::setDirty() {
void Camera::fixAspectRatio() { void Camera::fixAspectRatio() {
/* Don't divide by zero */ /* Don't divide by zero */
if(viewportWidth == 0 || viewportHeight == 0) { if(_viewport.x() == 0 || _viewport.y() == 0) {
_projectionMatrix = rawProjectionMatrix; _projectionMatrix = rawProjectionMatrix;
return; return;
} }
/* Extend on larger side = scale larger side down */ /* Extend on larger side = scale larger side down */
if(_aspectRatioPolicy == Extend) { if(_aspectRatioPolicy == Extend) {
_projectionMatrix = ((viewportWidth > viewportHeight) ? _projectionMatrix = ((_viewport.x() > _viewport.y()) ?
Matrix4::scaling(static_cast<GLfloat>(viewportHeight)/viewportWidth, 1, 1) : Matrix4::scaling(static_cast<GLfloat>(_viewport.y())/_viewport.x(), 1, 1) :
Matrix4::scaling(1, static_cast<GLfloat>(viewportWidth)/viewportHeight, 1) Matrix4::scaling(1, static_cast<GLfloat>(_viewport.x())/_viewport.y(), 1)
)*rawProjectionMatrix; )*rawProjectionMatrix;
/* Clip on smaller side = scale smaller side up */ /* Clip on smaller side = scale smaller side up */
} else if(_aspectRatioPolicy == Clip) { } else if(_aspectRatioPolicy == Clip) {
_projectionMatrix = ((viewportWidth > viewportHeight) ? _projectionMatrix = ((_viewport.x() > _viewport.y()) ?
Matrix4::scaling(1, static_cast<GLfloat>(viewportWidth)/viewportHeight, 1) : Matrix4::scaling(1, static_cast<GLfloat>(_viewport.x())/_viewport.y(), 1) :
Matrix4::scaling(static_cast<GLfloat>(viewportHeight)/viewportWidth, 1, 1) Matrix4::scaling(static_cast<GLfloat>(_viewport.y())/_viewport.x(), 1, 1)
)*rawProjectionMatrix; )*rawProjectionMatrix;
/* Don't preserve anything */ /* Don't preserve anything */

13
src/Camera.h

@ -110,14 +110,17 @@ class Camera: public Object {
inline Matrix4 projectionMatrix() const { return _projectionMatrix; } inline Matrix4 projectionMatrix() const { return _projectionMatrix; }
/** /**
* @brief Set viewport * @brief Set viewport size
* @param width Window / buffer width
* @param height Window / buffer height
* *
* Called when assigning the camera to the scene or when window * Called when assigning the camera to the scene or when window
* size changes. * size changes.
*/ */
void setViewport(int width, int height); void setViewport(const Math::Vector2<unsigned int>& size);
/** @copydoc setViewport(const Math::Vector2<size_t>& size); */
inline void setViewport(unsigned int width, unsigned int height) {
setViewport({width, height});
}
/** /**
* Recalculates camera matrix. * Recalculates camera matrix.
@ -138,7 +141,7 @@ class Camera: public Object {
GLfloat _near, _far; GLfloat _near, _far;
Scene* _active; Scene* _active;
int viewportWidth, viewportHeight; Math::Vector2<unsigned int> _viewport;
AspectRatioPolicy _aspectRatioPolicy; AspectRatioPolicy _aspectRatioPolicy;
void fixAspectRatio(); void fixAspectRatio();

Loading…
Cancel
Save