202 lines
4.8 KiB
C++
202 lines
4.8 KiB
C++
#include "Defines.h"
|
|
#include "Camera.h"
|
|
#include <stdio.h>
|
|
|
|
Camera* Camera::mInstance = NULL;
|
|
|
|
void Camera::Create() {
|
|
if (mInstance == NULL)
|
|
mInstance = new Camera();
|
|
}
|
|
|
|
void Camera::Destroy() {
|
|
if (mInstance != NULL)
|
|
delete mInstance;
|
|
mInstance = NULL;
|
|
}
|
|
|
|
Camera* Camera::Instance() {
|
|
return mInstance;
|
|
}
|
|
|
|
//************************************
|
|
// Update camera position and orientation for both camera modes based on change in mouse position
|
|
//************************************
|
|
void Camera::UpdateMouse(glm::vec2 mouseChange, float dTime) {
|
|
if (mFreeCam) {
|
|
mHorizontalRotation += mRotationSpeed * mouseChange.x;
|
|
mVerticalRotation += mRotationSpeed * mouseChange.y;
|
|
mVerticalRotation = glm::min(mVerticalRotation, mPi2 - .1f);
|
|
mVerticalRotation = glm::max(mVerticalRotation, -mPi2 + .1f);
|
|
}
|
|
else {
|
|
mAzimuth += mSurfingSpeed * mouseChange.x;
|
|
mAltitude -= mSurfingSpeed * mouseChange.y;
|
|
mAltitude = glm::min(mAltitude, mPi2 - .1f);
|
|
mAltitude = glm::max(mAltitude, -mPi2 + .1f);
|
|
}
|
|
Compute();
|
|
}
|
|
|
|
//************************************
|
|
// Update camera position for free camera mode based on key presses
|
|
//************************************
|
|
void Camera::UpdateKeys(glm::vec3 movement, float dTime) {
|
|
if (mFreeCam)
|
|
mPosition += mMovingSpeed * dTime * (movement.x * mDirection + movement.y * mRight + movement.z * mUp);
|
|
else
|
|
mPivot += mMovingSpeed * dTime * glm::vec3(movement.x, movement.z, movement.y);
|
|
}
|
|
|
|
//************************************
|
|
// Update camera zoom or field of view for both camera modes based on change in scroll
|
|
//************************************
|
|
void Camera::UpdateScroll(glm::vec2 offset, float dTime) {
|
|
|
|
// Change FOV in free camera mode
|
|
if (mFreeCam) {
|
|
mFOV -= mZoomSpeed * offset.y;
|
|
mFOV = glm::clamp(mFOV, .1f, mPi2);
|
|
}
|
|
else {
|
|
mPosition -= mPosition * mZoomSpeed * offset.y;
|
|
}
|
|
}
|
|
|
|
void Camera::ToggleMode() {
|
|
mFreeCam = !mFreeCam;
|
|
|
|
float r = glm::length(mPosition);
|
|
if (mFreeCam) {
|
|
mVerticalRotation = glm::asin(-mPosition.y / r);
|
|
mHorizontalRotation = glm::atan(-mPosition.x, -mPosition.z);
|
|
}
|
|
//else {
|
|
// mAltitude = glm::asin(mPosition.y / r);
|
|
// mAzimuth = glm::atan(mPosition.x, mPosition.z);
|
|
//}
|
|
|
|
Compute();
|
|
|
|
//////Reset();
|
|
}
|
|
|
|
bool Camera::IsFree()
|
|
{
|
|
return mFreeCam;
|
|
}
|
|
|
|
//************************************
|
|
// Compute camera orientation from rotations for free camera mode or camera position from trackball camera mode
|
|
//************************************
|
|
void Camera::Compute() {
|
|
if (mFreeCam) {
|
|
|
|
mDirection = glm::vec3(
|
|
cos(mVerticalRotation) * sin(mHorizontalRotation),
|
|
sin(mVerticalRotation),
|
|
cos(mVerticalRotation) * cos(mHorizontalRotation)
|
|
);
|
|
mRight = glm::vec3(
|
|
sin(mHorizontalRotation - 1.57f),
|
|
0,
|
|
cos(mHorizontalRotation - 1.57f)
|
|
);
|
|
mUp = glm::cross(mRight, mDirection);
|
|
|
|
}
|
|
else {
|
|
double r = glm::length(mPosition - mPivot);
|
|
double x = r * cos(mAltitude) * sin(mAzimuth);
|
|
double y = r * sin(mAltitude);
|
|
double z = r * cos(mAltitude) * cos(mAzimuth);
|
|
mPosition = mPivot + glm::vec3(x, y, z);
|
|
mDirection = glm::normalize(mPivot - mPosition);
|
|
}
|
|
}
|
|
|
|
void Camera::Reset() {
|
|
mFOV = 1.f;//QT
|
|
|
|
mPosition = glm::vec3(785.f, 544.f, -847.f);//QT
|
|
mHorizontalRotation = 0.f;//QT
|
|
mVerticalRotation = 0.f;//QT
|
|
mAltitude = .44f;//QT
|
|
mAzimuth = 2.39f;//QT
|
|
mPivot = glm::vec3(0);//QT
|
|
|
|
Compute();
|
|
}
|
|
|
|
glm::vec3 Camera::GetPosition() const {
|
|
return mPosition;
|
|
}
|
|
|
|
void Camera::SetPosition(glm::vec3 position)
|
|
{
|
|
mPosition = position;
|
|
}
|
|
|
|
glm::vec3 Camera::GetDirection() const {
|
|
return mDirection;
|
|
}
|
|
|
|
void Camera::SetDirection(glm::vec3 direction)
|
|
{
|
|
mDirection = glm::normalize(direction);
|
|
}
|
|
|
|
glm::vec3 Camera::GetTarget() const {
|
|
if (mFreeCam)
|
|
return mPosition + mDirection;
|
|
else
|
|
return mPivot;
|
|
}
|
|
|
|
void Camera::SetTarget(glm::vec3 target)
|
|
{
|
|
if (mFreeCam)
|
|
{
|
|
mDirection = glm::normalize(target - mPosition);
|
|
} else
|
|
mPivot = mPosition;
|
|
}
|
|
|
|
glm::vec3 Camera::GetPivot() const { return mPivot; }
|
|
void Camera::SetPivot(glm::vec3 pivot) { mPivot = pivot; }
|
|
|
|
double Camera::GetAltitude() const { return mAltitude; }
|
|
void Camera::SetAltitude(double altitude) { mAltitude = altitude; }
|
|
|
|
float Camera::GetFOV() const { return mFOV; }
|
|
void Camera::SetFOV(float fov) { mFOV = fov; }
|
|
|
|
float Camera::GetNear() const { return mNear; }
|
|
void Camera::SetNear(float near) { mNear = near; }
|
|
|
|
float Camera::GetFar() const { return mFar; }
|
|
void Camera::SetFar(float far) { mFar = far; }
|
|
|
|
void Camera::SetMovingSpeed(float speed) { mMovingSpeed = speed; }
|
|
float Camera::GetMovingSpeed() { return mMovingSpeed; }
|
|
void Camera::SetRotationSpeed(float speed) { mRotationSpeed = speed; }
|
|
float Camera::GetRotationSpeed() { return mRotationSpeed; }
|
|
|
|
Camera::Camera() {
|
|
mNear = 1.f;//QT
|
|
mFar = 500000.f;//QT
|
|
|
|
mMovingSpeed = 200.f;//QT
|
|
mRotationSpeed = 0.005f;//QT
|
|
mSurfingSpeed = 0.01f;//QT
|
|
mZoomSpeed = 0.1f;//QT
|
|
|
|
mFreeCam = false;//QT
|
|
|
|
Reset();
|
|
}
|
|
|
|
Camera::~Camera() {
|
|
|
|
}
|