Commit 5f4bbdbc authored by Robert Menzel's avatar Robert Menzel

a camera is now a movable object. glm::perspective assumes input in radians

parent 09621224
......@@ -10,6 +10,7 @@
#include <glm/gtx/quaternion.hpp>
#include "CameraBase.hh"
#include "MoveableObject.hh"
/*
* What you definitly want to set:
......@@ -48,7 +49,7 @@
namespace ACGL{
namespace Scene{
class GenericCamera : public CameraBase
class GenericCamera : public CameraBase, public MoveableObject
{
public:
///////////////////////////////////////////////////////////////////////////////////////////////////////////////
......@@ -220,22 +221,8 @@ class GenericCamera : public CameraBase
// actually used.
//
/// Gets the orthonormal rotation matrix (mat3)
const glm::mat3 &getRotationMatrix3() const { return mRotationMatrix; }
/// Gets the inverse orthonormal rotation matrix (mat3)
glm::mat3 getInverseRotationMatrix3() const { return glm::transpose(mRotationMatrix); }
/// Gets the orthonormal rotation matrix as a mat4
glm::mat4 getRotationMatrix4() const { return glm::mat4(mRotationMatrix); }
/// Sets the rotation matrix (mat3)
void setRotationMatrix( glm::mat3 _matrix );
/// Sets the rotation matrix (mat3-part of a mat4)
void setRotationMatrix( glm::mat4 _matrix );
/// Sets the complete camera lookat (position and rotation)
void setLookAtMatrix( const glm::vec3 &_position, const glm::vec3 &_target, const glm::vec3 &_up );
/// Gets the translation matrix of this camera
glm::mat4 getTranslationMatrix4() const;
/// forward to MovableObject to implement the full CameraBase interface:
glm::vec3 getPosition() const { return MoveableObject::getPosition(); }
///////////////////////////////////////////////////////////////////////////////////////////
//
......@@ -302,51 +289,6 @@ class GenericCamera : public CameraBase
//
///////////////////////////////////////////////////////////////////////////////////////////////////////////////
/**
* Set the position of the camera.
* @param _position New position of the camera.
*/
void setPosition( const glm::vec3 &_position ) { mPosition = _position; }
virtual glm::vec3 getPosition() const { return mPosition; }
/// Moves the camera by a given vector (in eyespace)
void move( const glm::vec3 &_vector );
void moveRight( float _distance ) { move( glm::vec3(_distance,0,0) ); }
void moveBack( float _distance ) { move( glm::vec3(0,0,_distance) ); }
void moveUp( float _distance ) { move( glm::vec3(0,_distance,0) ); }
void moveLeft( float _distance ) { move( glm::vec3(-_distance,0,0) ); }
void moveForward( float _distance ) { move( glm::vec3(0,0,-_distance) ); }
void moveDown( float _distance ) { move( glm::vec3(0,-_distance,0) ); }
/**
* Set the distance of the camera to the object we're looking at.
* Will change the target!
* @param _distance New focal distance of the camera.
*/
void setLookAtDistance( float _distance );
/// Gets the look-at distance
float getLookAtDistance() const { return mLookAtDistance; }
/// Will change the look at distance!
/// Will change the rotation!
/// Uses stored up-vector as reference
void setTarget( const glm::vec3 &_target ) { setTarget(_target, getUpDirection()); }
/// Will change the look at distance!
/// Will change the rotation!
/// Uses given up-vector as reference
void setTarget( const glm::vec3 &_target, const glm::vec3 &_up );
/// Gets the reconstructed target
glm::vec3 getTarget() const { return mPosition + getForwardDirection() * getLookAtDistance(); }
/// Get the unit up direction
glm::vec3 getUpDirection () const;
/// Get the unit right direction
glm::vec3 getRightDirection () const;
/// Get the unit forward direction
glm::vec3 getForwardDirection() const;
/**
* Look around with a mouse, works like a FPS camera:
* No roll possible.
......@@ -364,13 +306,6 @@ class GenericCamera : public CameraBase
/// States: update the storeStateToString() & setStateFromString() functions whenever adding a new state!
///
/// Current camera position
glm::vec3 mPosition;
/// Current camera rotation
glm::mat3 mRotationMatrix;
/// Current camera projection mode
ProjectionMode mProjectionMode;
/// stereo view mode
......@@ -391,9 +326,6 @@ class GenericCamera : public CameraBase
float mNearClippingPlane;
/// Current camera far clipping plane
float mFarClippingPlane;
/// Current camera focal distance
float mLookAtDistance;
/// viewport in pixel:
glm::uvec2 mViewportSize;
......
......@@ -40,7 +40,7 @@ public:
/**
* Destructor.
*/
~MoveableObject() {}
virtual ~MoveableObject() {}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
......@@ -138,7 +138,7 @@ public:
/// Get the unit forward direction
glm::vec3 getForwardDirection() const;
private:
protected:
/// Current position
glm::vec3 mPosition;
......
......@@ -21,7 +21,6 @@ using namespace ACGL::Utils::StringHelpers;
using namespace std;
GenericCamera::GenericCamera() :
mPosition(),
mProjectionMode(PERSPECTIVE_PROJECTION),
mStereoMode(MONO),
mCurrentEye(EYE_LEFT),
......@@ -29,8 +28,7 @@ GenericCamera::GenericCamera() :
mAspectRatio( 4.0/3.0 ),
mInterpupillaryDistance( 0.064 ), // 0.064 m = 6.4 cm - mean human eye distance: 6.47cm (male), 6.23cm (female)
mNearClippingPlane(0.1), // 10 cm
mFarClippingPlane(5000.0), // 5000 meter
mLookAtDistance(500.0) // half a kilometer away of the screen
mFarClippingPlane(5000.0) // 5000 meter
{
setRotationMatrix( glm::mat3(1.0f) );
}
......@@ -79,75 +77,6 @@ void GenericCamera::FPSstyleLookAround( float _deltaX, float _deltaY )
setRotationMatrix( newRotX * newRotY );
}
void GenericCamera::setRotationMatrix(glm::mat3 _matrix)
{
mRotationMatrix = _matrix;
assert(isOrthonormalMatrix(mRotationMatrix));
}
void GenericCamera::setRotationMatrix(glm::mat4 _matrix)
{
mRotationMatrix = glm::mat3(_matrix);
assert(isOrthonormalMatrix(mRotationMatrix));
}
void GenericCamera::setLookAtMatrix(const glm::vec3 &_position, const glm::vec3 &_target, const glm::vec3 &_up)
{
setPosition(_position);
setTarget(_target, _up);
}
glm::mat4 GenericCamera::getTranslationMatrix4() const
{
glm::mat4 trans;
trans[3][0] = -mPosition.x;
trans[3][1] = -mPosition.y;
trans[3][2] = -mPosition.z;
return trans;
}
glm::vec3 GenericCamera::getUpDirection () const
{
glm::vec3 up(mRotationMatrix[0][1], mRotationMatrix[1][1], mRotationMatrix[2][1]);
assert(glm::distance(getInverseRotationMatrix3() * glm::vec3(0.0f, 1.0f, 0.0f), up) < .01);
return up;
}
glm::vec3 GenericCamera::getRightDirection () const
{
glm::vec3 right(mRotationMatrix[0][0], mRotationMatrix[1][0], mRotationMatrix[2][0]);
assert(glm::distance(getInverseRotationMatrix3() * glm::vec3(1.0f, 0.0f, 0.0f), right) < .01);
return right;
}
glm::vec3 GenericCamera::getForwardDirection() const
{
glm::vec3 forward(-mRotationMatrix[0][2], -mRotationMatrix[1][2], -mRotationMatrix[2][2]);
assert(glm::distance(getInverseRotationMatrix3() * glm::vec3(0.0f, 0.0f, -1.0f), forward) < .01);
return forward;
}
void GenericCamera::setTarget(const glm::vec3 &_target, const glm::vec3 &_up)
{
glm::vec3 forwardVector = _target - mPosition;
mLookAtDistance = glm::length( forwardVector );
forwardVector = forwardVector / (float)mLookAtDistance; // normalize
glm::vec3 rightVector = glm::normalize(glm::cross( forwardVector, _up ));
glm::vec3 upVector = glm::cross( rightVector, forwardVector );
glm::mat3 rotMatrix;
rotMatrix[0][0] = rightVector.x;
rotMatrix[0][1] = upVector.x;
rotMatrix[0][2] = -forwardVector.x;
rotMatrix[1][0] = rightVector.y;
rotMatrix[1][1] = upVector.y;
rotMatrix[1][2] = -forwardVector.y;
rotMatrix[2][0] = rightVector.z;
rotMatrix[2][1] = upVector.z;
rotMatrix[2][2] = -forwardVector.z;
setRotationMatrix( rotMatrix );
}
void GenericCamera::setHorizontalFieldOfView(float _fovh)
{
assert( _fovh < 180.0f );
......@@ -186,12 +115,6 @@ void GenericCamera::setFarClippingPlane(float _plane)
mFarClippingPlane = _plane;
}
void GenericCamera::setLookAtDistance(float _distance)
{
assert (_distance > 0.0f);
mLookAtDistance = _distance;
}
glm::mat4 GenericCamera::getViewMatrix() const
{
if (mStereoMode == MONO) {
......@@ -286,19 +209,13 @@ glm::mat4 GenericCamera::getMonoProjectionMatrix() const
}
else if ( mProjectionMode == PERSPECTIVE_PROJECTION )
{
projectionMatrix = glm::perspective( (float)getHorizontalFieldOfView(), (float)getAspectRatio(), (float)mNearClippingPlane, (float)mFarClippingPlane );
projectionMatrix = glm::perspective( glm::radians( (float)getHorizontalFieldOfView()), (float)getAspectRatio(), (float)mNearClippingPlane, (float)mFarClippingPlane );
}
else assert(0 && "unsupported projection mode");
return projectionMatrix;
}
void GenericCamera::move( const glm::vec3 &_vector )
{
glm::mat3 inverseRotation = getInverseRotationMatrix3();
mPosition += (inverseRotation * _vector);
}
glm::mat4 GenericCamera::getMonoViewMatrix() const
{
glm::mat4 m(mRotationMatrix);
......
......@@ -58,9 +58,9 @@ glm::mat4 HMDCamera::getProjectionMatrix() const
{
glm::mat4 projectionOffset;
if (getEye() == GenericCamera::EYE_RIGHT) {
projectionOffset = glm::translate( -mProjectionCenterOffset.x, mProjectionCenterOffset.y, 0.0f );
projectionOffset = glm::translate( glm::vec3(-mProjectionCenterOffset.x, mProjectionCenterOffset.y, 0.0f) );
} else {
projectionOffset = glm::translate( mProjectionCenterOffset.x, mProjectionCenterOffset.y, 0.0f );
projectionOffset = glm::translate( glm::vec3( mProjectionCenterOffset.x, mProjectionCenterOffset.y, 0.0f) );
}
return projectionOffset * GenericCamera::getProjectionMatrix(); // yes, from the left side!
}
......
......@@ -9,15 +9,19 @@
#include <cassert>
#include <ACGL/Math/Math.hh>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/quaternion.hpp>
#include <glm/gtx/euler_angles.hpp>
//#include <glm/gtc/matrix_transform.hpp>
//#include <glm/gtc/quaternion.hpp>
//#include <glm/gtx/euler_angles.hpp>
using namespace ACGL;
using namespace ACGL::Math::Functions;
using namespace ACGL::Scene;
using namespace std;
MoveableObject::MoveableObject() : mLookAtDistance(500.0) // half a kilometer away of the screen
{
setRotationMatrix( glm::mat3(1.0f) );
}
void MoveableObject::setRotationMatrix(glm::mat3 _matrix)
{
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment