Clean up camera class.

This commit is contained in:
Kalle Bladin
2016-05-19 23:21:48 -04:00
parent 9dda48d44b
commit 1db9f2f60c
9 changed files with 356 additions and 393 deletions

View File

@@ -30,235 +30,230 @@
#include <glm/gtx/vector_angle.hpp>
namespace openspace {
Camera::Camera()
: _maxFov(0.f)
, _sinMaxFov(0.f)
, _viewDirection(0,0,-1)
, _viewDirectionInCameraSpace(0.f, 0.f, -1.f)
, _focusPosition()
{
_scaling.local = glm::vec2(1.f, 0.f);
_viewRotationMatrix.local = glm::mat4(1.0f);
_position.local = psc();
}
Camera::~Camera() { }
//////////////////////////////////////////////////////////////////////////////////////////
// CAMERA MUTATORS (SETTERS) //
//////////////////////////////////////////////////////////////////////////////////////////
void Camera::setPosition(psc pos){
std::lock_guard<std::mutex> _lock(_mutex);
_position.local = std::move(pos);
}
void Camera::setFocusPosition(psc pos) {
std::lock_guard<std::mutex> _lock(_mutex);
_focusPosition = pos;
}
void Camera::setRotation(glm::quat rotation) {
std::lock_guard<std::mutex> _lock(_mutex);
_viewRotationMatrix.local = glm::mat4_cast(glm::normalize(rotation));
}
void Camera::setLookUpVector(glm::vec3 lookUp) {
std::lock_guard<std::mutex> _lock(_mutex);
_lookUp = std::move(lookUp);
}
void Camera::setScaling(glm::vec2 scaling) {
std::lock_guard<std::mutex> _lock(_mutex);
_scaling.local = std::move(scaling);
}
void Camera::setMaxFov(float fov) {
std::lock_guard<std::mutex> _lock(_mutex);
_maxFov = fov;
_sinMaxFov = sin(_maxFov);
}
//////////////////////////////////////////////////////////////////////////////////////////
// CAMERA ACCESSORS (GETTERS) //
//////////////////////////////////////////////////////////////////////////////////////////
const psc& Camera::position() const {
return _position.synced;
}
const psc& Camera::unsynchedPosition() const {
return _position.local;
}
const psc& Camera::focusPosition() const {
return _focusPosition;
}
const glm::vec3& Camera::viewDirection() const {
return _viewDirection;
}
const glm::vec3& Camera::lookUpVector() const {
return _lookUp;
}
const glm::vec2& Camera::scaling() const {
return _scaling.synced;
}
float Camera::maxFov() const {
return _maxFov;
}
float Camera::sinMaxFov() const {
return _sinMaxFov;
}
const glm::mat4& Camera::viewRotationMatrix() const {
return _viewRotationMatrix.synced;
}
const glm::mat4& Camera::combinedViewMatrix() const {
glm::vec3 cameraPosition = position().vec3();
glm::mat4 viewTransform = glm::inverse(glm::translate(glm::mat4(1.0), cameraPosition));
viewTransform = glm::mat4(viewRotationMatrix()) * viewTransform;
return viewTransform;
}
//////////////////////////////////////////////////////////////////////////////////////////
// DEPRECATED CAMERA ACCESSORS (GETTERS) //
//////////////////////////////////////////////////////////////////////////////////////////
const glm::mat4& Camera::viewMatrix() const {
return sgctInternal.viewMatrix();
}
const glm::mat4& Camera::projectionMatrix() const {
return sgctInternal.projectionMatrix();
}
const glm::mat4& Camera::viewProjectionMatrix() const {
return sgctInternal.viewProjectionMatrix();
}
//////////////////////////////////////////////////////////////////////////////////////////
// CAMERA RELATICVE MUTATORS //
//////////////////////////////////////////////////////////////////////////////////////////
void Camera::rotate(const glm::quat& rotation) {
std::lock_guard<std::mutex> _lock(_mutex);
glm::mat4 tmp = glm::mat4_cast(rotation);
_viewRotationMatrix.local = _viewRotationMatrix.local * tmp;
}
//////////////////////////////////////////////////////////////////////////////////////////
// CAMERA SYNCHRONIZATION //
//////////////////////////////////////////////////////////////////////////////////////////
void Camera::serialize(SyncBuffer* syncBuffer){
_mutex.lock();
_viewRotationMatrix.serialize(syncBuffer);
_position.serialize(syncBuffer);
_scaling.serialize(syncBuffer);
_mutex.unlock();
}
void Camera::deserialize(SyncBuffer* syncBuffer){
_mutex.lock();
_viewRotationMatrix.deserialize(syncBuffer);
_position.deserialize(syncBuffer);
_scaling.deserialize(syncBuffer);
_mutex.unlock();
}
void Camera::postSynchronizationPreDraw(){
_mutex.lock();
_viewRotationMatrix.postSynchronizationPreDraw();
_position.postSynchronizationPreDraw();
_scaling.postSynchronizationPreDraw();
glm::vec4 localViewDir = glm::vec4(_viewDirectionInCameraSpace, 0.f);
_viewDirection = (glm::inverse(_viewRotationMatrix.local) * localViewDir).xyz();
_viewDirection = glm::normalize(_viewDirection);
_mutex.unlock();
}
void Camera::preSynchronization(){
_mutex.lock();
_viewRotationMatrix.preSynchronization();
_position.preSynchronization();
_scaling.preSynchronization();
_mutex.unlock();
}
//////////////////////////////////////////////////////////////////////////////////////////
// SGCT NODE DEPENTENT //
//////////////////////////////////////////////////////////////////////////////////////////
Camera::SgctInternal::SgctInternal()
: _viewMatrix()
, _projectionMatrix()
, _dirtyViewProjectionMatrix(true)
{
}
void Camera::SgctInternal::setViewMatrix(glm::mat4 viewMatrix) {
std::lock_guard<std::mutex> _lock(_mutex);
_viewMatrix = std::move(viewMatrix);
_dirtyViewProjectionMatrix = true;
}
void Camera::SgctInternal::setProjectionMatrix(glm::mat4 projectionMatrix) {
std::lock_guard<std::mutex> _lock(_mutex);
_projectionMatrix = std::move(projectionMatrix);
_dirtyViewProjectionMatrix = true;
}
const glm::mat4& Camera::SgctInternal::viewMatrix() const {
return _viewMatrix;
}
const glm::mat4& Camera::SgctInternal::projectionMatrix() const {
return _projectionMatrix;
}
const glm::mat4& Camera::SgctInternal::viewProjectionMatrix() const {
if (_dirtyViewProjectionMatrix) {
std::lock_guard<std::mutex> _lock(_mutex);
_viewProjectionMatrix = _projectionMatrix * _viewMatrix;
_dirtyViewProjectionMatrix = false;
}
return _viewProjectionMatrix;
}
} // namespace openspace
const Camera::Vec3 Camera::_VIEW_DIRECTION_CAMERA_SPACE = Camera::Vec3(0, 0, -1);
const Camera::Vec3 Camera::_LOOKUP_VECTOR_CAMERA_SPACE = Camera::Vec3(0, 1, 0);
Camera::Camera()
: _maxFov(0.f)
, _focusPosition()
{
_scaling.local = glm::vec2(1.f, 0.f);
_position.local = psc();
Vec3 eulerAngles(0.0f, 0.0f, 0.0f);
_rotation.local = Quat(eulerAngles);
}
Camera::Camera(const Camera& o)
: sgctInternal(o.sgctInternal)
, _focusPosition(o._focusPosition)
, _cachedViewDirection(o._cachedViewDirection)
, _rotation(o._rotation)
, _scaling(o._scaling)
, _position(o._position)
, _maxFov(o._maxFov)
{ }
Camera::~Camera() { }
//////////////////////////////////////////////////////////////////////////////////////
// CAMERA MUTATORS (SETTERS) //
//////////////////////////////////////////////////////////////////////////////////////
void Camera::setPosition(psc pos) {
std::lock_guard<std::mutex> _lock(_mutex);
_position.local = std::move(pos);
}
void Camera::setFocusPosition(psc pos) {
std::lock_guard<std::mutex> _lock(_mutex);
_focusPosition = pos;
}
void Camera::setRotation(Quat rotation) {
std::lock_guard<std::mutex> _lock(_mutex);
_rotation.local = rotation;
_cachedViewRotationMatrix.isDirty = true;
_cachedCombinedViewMatrix.isDirty = true;
}
void Camera::setScaling(glm::vec2 scaling) {
std::lock_guard<std::mutex> _lock(_mutex);
_scaling.local = std::move(scaling);
}
void Camera::setMaxFov(float fov) {
std::lock_guard<std::mutex> _lock(_mutex);
_maxFov = fov;
_cachedSinMaxFov.isDirty = true;
}
//////////////////////////////////////////////////////////////////////////////////////
// CAMERA RELATICVE MUTATORS //
//////////////////////////////////////////////////////////////////////////////////////
void Camera::rotate(Quat rotation) {
std::lock_guard<std::mutex> _lock(_mutex);
_rotation.local = _rotation.local * rotation;
}
//////////////////////////////////////////////////////////////////////////////////////
// CAMERA ACCESSORS (GETTERS) //
//////////////////////////////////////////////////////////////////////////////////////
const psc& Camera::position() const {
return _position.synced;
}
const psc& Camera::unsynchedPosition() const {
return _position.local;
}
const psc& Camera::focusPosition() const {
return _focusPosition;
}
const glm::vec3 Camera::viewDirectionWorldSpace() const {
if (_cachedViewDirection.isDirty) {
_cachedViewDirection.datum =
glm::inverse(_rotation.local) * Vec3(_VIEW_DIRECTION_CAMERA_SPACE);
_cachedViewDirection.datum = glm::normalize(_cachedViewDirection.datum);
}
return _cachedViewDirection.datum;
}
const glm::vec3 Camera::lookUpVectorCameraSpace() const {
return _LOOKUP_VECTOR_CAMERA_SPACE;
}
const glm::vec2& Camera::scaling() const {
return _scaling.synced;
}
float Camera::maxFov() const {
return _maxFov;
}
float Camera::sinMaxFov() const {
if (_cachedSinMaxFov.isDirty) {
_cachedSinMaxFov.datum = sin(_maxFov);
}
return _cachedSinMaxFov.datum;
}
const glm::mat4 Camera::viewRotationMatrix() const {
if (_cachedViewRotationMatrix.isDirty) {
_cachedViewRotationMatrix.datum = glm::mat4_cast(_rotation.local);
}
return _cachedViewRotationMatrix.datum;
}
const glm::quat Camera::rotationQuaternion() const {
return _rotation.synced;
}
glm::mat4 Camera::combinedViewMatrix() const {
if (_cachedCombinedViewMatrix.isDirty) {
glm::vec3 cameraPosition = position().vec3();
glm::mat4 cameraTranslation =
glm::inverse(glm::translate(glm::mat4(1.0), cameraPosition));
_cachedCombinedViewMatrix.datum =
glm::mat4(viewRotationMatrix()) * cameraTranslation;
}
return _cachedCombinedViewMatrix.datum;
}
//////////////////////////////////////////////////////////////////////////////////////
// DEPRECATED CAMERA ACCESSORS (GETTERS) //
//////////////////////////////////////////////////////////////////////////////////////
const glm::mat4& Camera::viewMatrix() const {
return sgctInternal.viewMatrix();
}
const glm::mat4& Camera::projectionMatrix() const {
return sgctInternal.projectionMatrix();
}
const glm::mat4& Camera::viewProjectionMatrix() const {
return sgctInternal.viewProjectionMatrix();
}
//////////////////////////////////////////////////////////////////////////////////////
// CAMERA SYNCHRONIZATION //
//////////////////////////////////////////////////////////////////////////////////////
void Camera::serialize(SyncBuffer* syncBuffer) {
std::lock_guard<std::mutex> _lock(_mutex);
_rotation.serialize(syncBuffer);
_position.serialize(syncBuffer);
_scaling.serialize(syncBuffer);
}
void Camera::deserialize(SyncBuffer* syncBuffer) {
std::lock_guard<std::mutex> _lock(_mutex);
_rotation.deserialize(syncBuffer);
_position.deserialize(syncBuffer);
_scaling.deserialize(syncBuffer);
}
void Camera::postSynchronizationPreDraw() {
std::lock_guard<std::mutex> _lock(_mutex);
_rotation.postSynchronizationPreDraw();
_position.postSynchronizationPreDraw();
_scaling.postSynchronizationPreDraw();
_cachedViewDirection.isDirty = true;
}
void Camera::preSynchronization() {
std::lock_guard<std::mutex> _lock(_mutex);
_rotation.preSynchronization();
_position.preSynchronization();
_scaling.preSynchronization();
}
//////////////////////////////////////////////////////////////////////////////////////
// SGCT NODE DEPENTENT //
//////////////////////////////////////////////////////////////////////////////////////
Camera::SgctInternal::SgctInternal()
: _viewMatrix()
, _projectionMatrix()
{ }
void Camera::SgctInternal::setViewMatrix(glm::mat4 viewMatrix) {
std::lock_guard<std::mutex> _lock(_mutex);
_viewMatrix = std::move(viewMatrix);
_cachedViewProjectionMatrix.isDirty = true;
}
void Camera::SgctInternal::setProjectionMatrix(glm::mat4 projectionMatrix) {
std::lock_guard<std::mutex> _lock(_mutex);
_projectionMatrix = std::move(projectionMatrix);
_cachedViewProjectionMatrix.isDirty = true;
}
const glm::mat4& Camera::SgctInternal::viewMatrix() const {
return _viewMatrix;
}
const glm::mat4& Camera::SgctInternal::projectionMatrix() const {
return _projectionMatrix;
}
const glm::mat4& Camera::SgctInternal::viewProjectionMatrix() const {
if (_cachedViewProjectionMatrix.isDirty) {
std::lock_guard<std::mutex> _lock(_mutex);
_cachedViewProjectionMatrix.datum = _projectionMatrix * _viewMatrix;
_cachedViewProjectionMatrix.isDirty = false;
}
return _cachedViewProjectionMatrix.datum;
}
} // namespace openspace