From af9286fbc9277b4d69c7f034a3945efbbf479a14 Mon Sep 17 00:00:00 2001 From: David Vereb Date: Tue, 15 Jun 2021 21:13:59 -0400 Subject: [PATCH] Added missing camera directory. --- camera/Camera.cpp | 117 ++++++++++++++++++++++++++++++++++++++++++++++ camera/Camera.h | 36 ++++++++++++++ 2 files changed, 153 insertions(+) create mode 100644 camera/Camera.cpp create mode 100644 camera/Camera.h diff --git a/camera/Camera.cpp b/camera/Camera.cpp new file mode 100644 index 0000000..ed10cca --- /dev/null +++ b/camera/Camera.cpp @@ -0,0 +1,117 @@ +#include +#include +#include +#include "Camera.h" + +void Camera::Init() +{ + m_yaw = 0.0; + m_pitch = 0.0; + + SetPos(0, 0, 0); +} + +void Camera::Refresh() +{ + // Camera parameter according to Riegl's co-ordinate system + // x/y for flat, z for height + m_lx = cos(m_yaw) * cos(m_pitch); + m_ly = sin(m_pitch); + m_lz = sin(m_yaw) * cos(m_pitch); + + m_strafe_lx = cos(m_yaw - M_PI_2); + m_strafe_lz = sin(m_yaw - M_PI_2); + + glMatrixMode(GL_MODELVIEW); + glLoadIdentity(); + gluLookAt(m_x, m_y, m_z, m_x + m_lx, m_y + m_ly, m_z + m_lz, 0.0,1.0,0.0); + + printf("Camera: %f %f %f Direction vector: %f %f %f\n", m_x, m_y, m_z, m_lx, m_ly, m_lz); +} + +void Camera::SetPos(float x, float y, float z) +{ + m_x = x; + m_y = y; + m_z =z; + + Refresh(); +} + +void Camera::GetPos(float &x, float &y, float &z) +{ + x = m_x; + y = m_y; + z = m_z; +} + +void Camera::GetDirectionVector(float &x, float &y, float &z) +{ + x = m_lx; + y = m_ly; + z = m_lz; +} + +void Camera::Move(float incr) +{ + float lx = cos(m_yaw)*cos(m_pitch); + float ly = sin(m_pitch); + float lz = sin(m_yaw)*cos(m_pitch); + + m_x = m_x + incr*lx; + m_y = m_y + incr*ly; + m_z = m_z + incr*lz; + + Refresh(); +} + +void Camera::Strafe(float incr) +{ + m_x = m_x + incr*m_strafe_lx; + m_z = m_z + incr*m_strafe_lz; + + Refresh(); +} + +void Camera::Fly(float incr) +{ + m_y = m_y + incr; + + Refresh(); +} + +void Camera::RotateYaw(float angle) +{ + m_yaw += angle; + + Refresh(); +} + +void Camera::RotatePitch(float angle) +{ + const float limit = 89.0 * M_PI / 180.0; + + m_pitch += angle; + + if(m_pitch < -limit) + m_pitch = -limit; + + if(m_pitch > limit) + m_pitch = limit; + + Refresh(); +} + +void Camera::SetYaw(float angle) +{ + m_yaw = angle; + + Refresh(); +} + +void Camera::SetPitch(float angle) +{ + m_pitch = angle; + + Refresh(); +} diff --git a/camera/Camera.h b/camera/Camera.h new file mode 100644 index 0000000..8dece1f --- /dev/null +++ b/camera/Camera.h @@ -0,0 +1,36 @@ +#ifndef __CAMERA_H__ +#define __CAMERA_H__ + +/* +Generic camera class by Nghia Ho +*/ + +class Camera +{ +public: + Camera() { Init(); } + ~Camera(){} + + void Init(); + void Refresh(); + void SetPos(float x, float y, float z); + void GetPos(float &x, float &y, float &z); + void GetDirectionVector(float &x, float &y, float &z); + void SetYaw(float angle); + void SetPitch(float angle); + + // Navigation + void Move(float incr); + void Strafe(float incr); + void Fly(float incr); + void RotateYaw(float angle); + void RotatePitch(float angle); + +private: + float m_x, m_y, m_z; // Position + float m_lx, m_ly, m_lz; // Direction vector of where we are looking at + float m_yaw, m_pitch; // Various rotation angles + float m_strafe_lx, m_strafe_lz; // Always 90 degree to direction vector +}; + +#endif