#ifndef Magnum_SceneGraph_AbstractCamera_hpp #define Magnum_SceneGraph_AbstractCamera_hpp /* Copyright © 2010, 2011, 2012 Vladimír Vondruš 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. */ /** @file * @brief @ref compilation-speedup-hpp "Template implementation" for AbstractCamera.h */ #include "AbstractCamera.h" #include "Drawable.h" using namespace std; namespace Magnum { namespace SceneGraph { #ifndef DOXYGEN_GENERATING_OUTPUT namespace Implementation { template class Camera {}; template class Camera<2, T> { public: inline constexpr static Math::Matrix3 aspectRatioScale(const Math::Vector2& scale) { return Math::Matrix3::scaling({scale.x(), scale.y()}); } }; template class Camera<3, T> { public: inline constexpr static Math::Matrix4 aspectRatioScale(const Math::Vector2& scale) { return Math::Matrix4::scaling({scale.x(), scale.y(), 1.0f}); } }; template typename DimensionTraits::MatrixType aspectRatioFix(AspectRatioPolicy aspectRatioPolicy, const Math::Vector2& projectionScale, const Math::Vector2& viewport) { /* Don't divide by zero / don't preserve anything */ if(projectionScale.x() == 0 || projectionScale.y() == 0 || viewport.x() == 0 || viewport.y() == 0 || aspectRatioPolicy == AspectRatioPolicy::NotPreserved) return {}; Math::Vector2 relativeAspectRatio = Math::Vector2::from(viewport)*projectionScale; /* Extend on larger side = scale larger side down Clip on smaller side = scale smaller side up */ return Camera::aspectRatioScale( (relativeAspectRatio.x() > relativeAspectRatio.y()) == (aspectRatioPolicy == AspectRatioPolicy::Extend) ? Vector2(relativeAspectRatio.y()/relativeAspectRatio.x(), T(1.0)) : Vector2(T(1.0), relativeAspectRatio.x()/relativeAspectRatio.y())); } } #endif template AbstractCamera* AbstractCamera::setAspectRatioPolicy(AspectRatioPolicy policy) { _aspectRatioPolicy = policy; fixAspectRatio(); return this; } template void AbstractCamera::setViewport(const Math::Vector2& size) { _viewport = size; fixAspectRatio(); } template void AbstractCamera::draw(DrawableGroup& group) { AbstractObject* scene = AbstractFeature::object()->sceneObject(); CORRADE_ASSERT(scene, "Camera::draw(): cannot draw when camera is not part of any scene", ); /* Compute camera matrix */ AbstractFeature::object()->setClean(); /* Compute transformations of all objects in the group relative to the camera */ std::vector*> objects(group.size()); for(std::size_t i = 0; i != group.size(); ++i) objects[i] = group[i]->object(); std::vector::MatrixType> transformations = scene->transformationMatrices(objects, _cameraMatrix); /* Perform the drawing */ for(std::size_t i = 0; i != transformations.size(); ++i) group[i]->draw(transformations[i], this); } }} #endif