00001 /* 00002 Copyright (c) 2010, The Barbarian Group 00003 All rights reserved. 00004 00005 Redistribution and use in source and binary forms, with or without modification, are permitted provided that 00006 the following conditions are met: 00007 00008 * Redistributions of source code must retain the above copyright notice, this list of conditions and 00009 the following disclaimer. 00010 * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and 00011 the following disclaimer in the documentation and/or other materials provided with the distribution. 00012 00013 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED 00014 WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 00015 PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR 00016 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 00017 TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 00018 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 00019 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 00020 POSSIBILITY OF SUCH DAMAGE. 00021 */ 00022 00023 #pragma once 00024 00025 #include "cinder/Vector.h" 00026 #include "cinder/Matrix.h" 00027 #include "cinder/Quaternion.h" 00028 #include "cinder/Ray.h" 00029 00030 namespace cinder { 00031 00032 // By default the camera is looking down -Z 00033 class Camera 00034 { 00035 public: 00036 Camera() : mInverseModelViewCached( false ), mWorldUp( Vec3f::yAxis() ) {} 00037 virtual ~Camera() {} 00038 00039 Vec3f getEyePoint() const { return mEyePoint; } 00040 void setEyePoint( const Vec3f &aEyePoint ); 00041 00042 float getCenterOfInterest() const { return mCenterOfInterest; } 00043 void setCenterOfInterest( float aCenterOfInterest ) { mCenterOfInterest = aCenterOfInterest; } 00044 00045 Vec3f getCenterOfInterestPoint() const { return mEyePoint + mViewDirection * mCenterOfInterest; } 00046 void setCenterOfInterestPoint( const Vec3f ¢erOfInterestPoint ); 00047 00048 Vec3f getWorldUp() const { return mWorldUp; } 00049 void setWorldUp( const Vec3f &aWorldUp ); 00050 00051 void lookAt( const Vec3f &target ); 00052 void lookAt( const Vec3f &aEyePoint, const Vec3f &target ); 00053 void lookAt( const Vec3f &aEyePoint, const Vec3f &target, const Vec3f &aUp ); 00054 Vec3f getViewDirection() const { return mViewDirection; } 00055 void setViewDirection( const Vec3f &aViewDirection ); 00056 00057 Quatf getOrientation() const { return mOrientation; } 00058 void setOrientation( const Quatf &aOrientation ); 00059 00060 float getFov() const { return mFov; } 00061 void setFov( float aFov ) { mFov = aFov; calcProjection(); } 00062 float getAspectRatio() const { return mAspectRatio; } 00063 void setAspectRatio( float aAspectRatio ) { mAspectRatio = aAspectRatio; calcProjection(); } 00064 float getNearClip() const { return mNearClip; } 00065 void setNearClip( float aNearClip ) { mNearClip = aNearClip; calcProjection(); } 00066 float getFarClip() const { return mFarClip; } 00067 void setFarClip( float aFarClip ) { mFarClip = aFarClip; calcProjection(); } 00068 void getNearClipCoordinates( Vec3f *topLeft, Vec3f *topRight, Vec3f *bottomLeft, Vec3f *bottomRight ) const; 00069 void getFarClipCoordinates( Vec3f *topLeft, Vec3f *topRight, Vec3f *bottomLeft, Vec3f *bottomRight ) const; 00070 00072 void getFrustum( float *left, float *top, float *right, float *bottom, float *near, float *far ) const; 00074 virtual bool isPersp() const = 0; 00075 00076 const Matrix44f& getProjectionMatrix() const { return mProjectionMatrix; } 00077 const Matrix44f& getModelViewMatrix() const { return mModelViewMatrix; } 00078 const Matrix44f& getInverseModelViewMatrix() const { if( ! mInverseModelViewCached ) calcInverseModelView(); return mInverseModelViewMatrix; } 00079 00080 Ray generateRay( float u, float v, float imagePlaneAspectRatio ) const; 00081 void getBillboardVectors( Vec3f *right, Vec3f *up ) const; 00082 Vec2f worldToScreen( const Vec3f &worldCoord, float screenWidth, float screenHeight ) const; 00083 00084 float getScreenRadius( const class Sphere &sphere, float screenWidth, float screenHeight ) const; 00085 00086 protected: 00087 Vec3f mEyePoint; 00088 Vec3f mViewDirection; 00089 Quatf mOrientation; 00090 float mCenterOfInterest; 00091 Vec3f mWorldUp; 00092 Vec3f mU; // Right vector 00093 Vec3f mV; // Readjust up-vector 00094 Vec3f mW; // Negative view direction 00095 00096 float mFov; 00097 float mAspectRatio; 00098 float mNearClip; 00099 float mFarClip; 00100 00101 Matrix44f mProjectionMatrix, mInverseProjectionMatrix; 00102 Matrix44f mModelViewMatrix; 00103 mutable Matrix44f mInverseModelViewMatrix; 00104 mutable bool mInverseModelViewCached; 00105 00106 float mFrustumLeft, mFrustumRight, mFrustumTop, mFrustumBottom; 00107 00108 void calcModelView(); 00109 void calcInverseModelView() const; 00110 virtual void calcProjection() = 0; 00111 }; 00112 00113 class CameraPersp : public Camera { 00114 public: 00115 CameraPersp(); 00116 CameraPersp( int pixelWidth, int pixelHeight, float fov ); // constructs screen-aligned camera 00117 CameraPersp( int pixelWidth, int pixelHeight, float fov, float nearPlane, float parPlane ); // constructs screen-aligned camera 00118 00119 void setPerspective( float horizFovDegrees, float aspectRatio, float nearPlane, float farPlane ); 00120 00121 virtual bool isPersp() const { return true; } 00122 00123 CameraPersp getFrameSphere( const class Sphere &worldSpaceSphere, int maxIterations = 20 ) const; 00124 00125 protected: 00126 virtual void calcProjection(); 00127 }; 00128 00129 class CameraOrtho : public Camera { 00130 public: 00131 CameraOrtho(); 00132 CameraOrtho( float left, float right, float bottom, float top, float nearPlane, float farPlane ); 00133 00134 void setOrtho( float left, float right, float bottom, float top, float nearPlane, float farPlane ); 00135 00136 virtual bool isPersp() const { return false; } 00137 00138 protected: 00139 virtual void calcProjection(); 00140 }; 00141 00142 } // namespace cinder