Visualization Library

A lightweight C++ OpenGL middleware for 2D/3D graphics
[Home] [Tutorials] [All Classes] [Grouped Classes]

X:/dropbox/visualizationlibrary/src/vlGraphics/Camera.hpp

Go to the documentation of this file.
00001 /**************************************************************************************/
00002 /*                                                                                    */
00003 /*  Visualization Library                                                             */
00004 /*  http://www.visualizationlibrary.org                                               */
00005 /*                                                                                    */
00006 /*  Copyright (c) 2005-2010, Michele Bosi                                             */
00007 /*  All rights reserved.                                                              */
00008 /*                                                                                    */
00009 /*  Redistribution and use in source and binary forms, with or without modification,  */
00010 /*  are permitted provided that the following conditions are met:                     */
00011 /*                                                                                    */
00012 /*  - Redistributions of source code must retain the above copyright notice, this     */
00013 /*  list of conditions and the following disclaimer.                                  */
00014 /*                                                                                    */
00015 /*  - Redistributions in binary form must reproduce the above copyright notice, this  */
00016 /*  list of conditions and the following disclaimer in the documentation and/or       */
00017 /*  other materials provided with the distribution.                                   */
00018 /*                                                                                    */
00019 /*  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND   */
00020 /*  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED     */
00021 /*  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE            */
00022 /*  DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR  */
00023 /*  ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES    */
00024 /*  (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;      */
00025 /*  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON    */
00026 /*  ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT           */
00027 /*  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS     */
00028 /*  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.                      */
00029 /*                                                                                    */
00030 /**************************************************************************************/
00031 
00032 #ifndef Camera_INCLUDE_ONCE
00033 #define Camera_INCLUDE_ONCE
00034 
00035 #include <vlCore/checks.hpp>
00036 #include <vlCore/math_utils.hpp>
00037 #include <vlCore/Vector4.hpp>
00038 #include <vlGraphics/Framebuffer.hpp>
00039 #include <vlGraphics/Viewport.hpp>
00040 #include <vlGraphics/Frustum.hpp>
00041 #include <vlCore/Ray.hpp>
00042 
00043 namespace vl
00044 {
00045   class AABB;
00046   //-----------------------------------------------------------------------------
00047   // Camera
00048   //-----------------------------------------------------------------------------
00050   class VLGRAPHICS_EXPORT Camera: public Object
00051   {
00052     VL_INSTRUMENT_CLASS(vl::Camera, Object)
00053 
00054   public:
00056     Camera();
00057 
00065     void computeNearFarOptimizedProjMatrix(const Sphere& scene_bounding_sphere);
00066 
00070     void computeFrustumPlanes();
00071 
00073     void applyModelViewMatrix(const mat4& model_matrix) const;
00074 
00076     void applyViewMatrix() const;
00077 
00079     void applyProjMatrix() const;
00080 
00083     real aspectRatio() const 
00084     {
00085       if (viewport())
00086         return (real)viewport()->width()/viewport()->height();
00087       else
00088         return 0;
00089     }
00090 
00093     void setFOV(real fov) { mFOV = fov; }
00094     
00096     real fov() const { return mFOV; }
00097 
00100     void setNearPlane(real nearplane) { mNearPlane = nearplane; }
00101 
00103     real nearPlane() const { return mNearPlane; }
00104 
00107     void setFarPlane(real farplane) { mFarPlane = farplane; }
00108 
00110     real farPlane() const { return mFarPlane; }
00111 
00113     real left() const { return mLeft; }
00114     void setLeft(real v) { mLeft = v; }
00115 
00117     real right() const { return mRight; }
00118     void setRight(real v) { mRight = v; }
00119 
00121     real bottom() const { return mBottom; }
00122     void setBottom(real v) { mBottom = v; }
00123 
00125     real top() const { return mTop; }
00126     void setTop(real v) { mTop = v; }
00127 
00129     void setFrustum(const Frustum& frustum) { mFrustum = frustum; }
00130 
00132     const Frustum& frustum() const { return mFrustum; }
00133 
00135     Frustum& frustum() { return mFrustum; }
00136 
00138     void setViewport(Viewport* viewport) { mViewport = viewport; }
00139 
00141     Viewport* viewport() { return mViewport.get(); }
00142 
00144     const Viewport* viewport() const { return mViewport.get(); }
00145 
00147     void bindTransform(Transform* transform) { mBoundTransform = transform; }
00148 
00150     const Transform* boundTransform() const { return mBoundTransform.get(); }
00151 
00153     Transform* boundTransform() { return mBoundTransform.get(); }
00154 
00157     void setViewMatrix(const mat4& mat) { mViewMatrix = mat; mViewMatrix.getInverse(mModelingMatrix); }
00158 
00161     const mat4& viewMatrix() const { return mViewMatrix; }
00162 
00165     void setModelingMatrix(const mat4& mat) { mModelingMatrix = mat; mModelingMatrix.getInverse(mViewMatrix); }
00166 
00169     const mat4& modelingMatrix() const { return mModelingMatrix; }
00170 
00172     void setProjectionMatrix(const mat4& mat, EProjectionMatrixType proj_type) { mProjectionMatrix = mat; mProjectionType = proj_type; }
00173 
00175     const mat4& projectionMatrix() const { return mProjectionMatrix; }
00176 
00178     EProjectionMatrixType projectionMatrixType() const { return mProjectionType; }
00179 
00182     void setProjectionPerspective();
00183 
00186     void setProjectionPerspective(real fov, real near, real far);
00187 
00198     void setProjectionFrustum(real left, real right, real bottom, real top, real znear, real zfar);
00199 
00206     void setProjectionOrtho(real left, real right, real bottom, real top, real znear, real zfar);
00207 
00214     void setProjectionOrtho();
00215 
00224     void setProjectionOrtho(real offset);
00225 
00230     void setViewMatrixLookAt(const vec3& eye, const vec3& at, const vec3& up);
00231 
00237     void getViewMatrixAsLookAt(vec3& eye, vec3& at, vec3& up, vec3& right) const;
00238 
00240     bool project(const vec4& in_world, vec4& out_viewp) const;
00241 
00248     bool unproject(const vec3& in_viewp, vec4& out_world) const;
00249 
00251     bool unproject(std::vector<vec3>& points) const;
00252 
00260     Ray computeRay(int viewp_x, int viewp_y);
00261 
00263     Frustum computeRayFrustum(int viewp_x, int viewp_y);
00264 
00270     void adjustView(const AABB& aabb, const vec3& dir, const vec3& up, real bias=1.0f);
00271 
00272   protected:
00273     mat4 mViewMatrix;
00274     mat4 mModelingMatrix;
00275     mat4 mProjectionMatrix;
00276     ref<Viewport> mViewport;
00277     Frustum mFrustum;
00278     ref<Transform> mBoundTransform;
00279     real mFOV;
00280     real mLeft, mRight, mBottom, mTop;
00281     real mNearPlane;
00282     real mFarPlane;
00283     EProjectionMatrixType mProjectionType;
00284   };
00285 }
00286 
00287 #endif

Visualization Library 2011.09.1160 Reference Documentation
Copyright 2005-2011 Michele Bosi. All rights reserved.
Updated on Thu May 2 2013 13:40:35.
Permission is granted to use this page to write and publish articles regarding Visualization Library.