2021-06-15 21:13:59 -04:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <math.h>
|
|
|
|
#include <GL/glut.h>
|
|
|
|
#include "Camera.h"
|
|
|
|
|
|
|
|
void Camera::Init()
|
|
|
|
{
|
|
|
|
m_yaw = 0.0;
|
|
|
|
m_pitch = 0.0;
|
|
|
|
|
2021-06-21 23:11:21 -04:00
|
|
|
target = Target::FORWARD;
|
|
|
|
t_x = t_y = t_z = 0.0f;
|
|
|
|
|
|
|
|
SetPos(0, 20, 0);
|
2021-06-15 21:13:59 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void Camera::Refresh()
|
|
|
|
{
|
|
|
|
// Camera parameter according to Riegl's co-ordinate system
|
2021-06-21 23:11:21 -04:00
|
|
|
// x/z for flat, y for height
|
2021-06-15 21:13:59 -04:00
|
|
|
m_lx = cos(m_yaw) * cos(m_pitch);
|
2021-06-21 23:11:21 -04:00
|
|
|
m_ly = sin(m_yaw) * cos(m_pitch);
|
|
|
|
m_lz = sin(m_pitch);
|
2021-06-15 21:13:59 -04:00
|
|
|
|
|
|
|
m_strafe_lx = cos(m_yaw - M_PI_2);
|
2021-06-21 23:11:21 -04:00
|
|
|
m_strafe_ly = sin(m_yaw - M_PI_2);
|
2021-06-15 21:13:59 -04:00
|
|
|
|
|
|
|
glMatrixMode(GL_MODELVIEW);
|
|
|
|
glLoadIdentity();
|
2021-06-21 23:11:21 -04:00
|
|
|
if(target == Target::FORWARD)
|
|
|
|
gluLookAt(m_x, m_y, m_z, // eye at
|
|
|
|
m_x + m_lx, m_y + m_ly, m_z + m_lz, // look at
|
|
|
|
0.0,1.0,0.0); // up
|
|
|
|
else
|
|
|
|
gluLookAt(m_x, m_y, m_z, // eye at
|
2022-06-16 15:11:58 -04:00
|
|
|
2.0f, 0.0f, -4.0f, // look at
|
2021-06-21 23:11:21 -04:00
|
|
|
0.0f, 1.0f, 0.0f); // up
|
|
|
|
|
|
|
|
// printf("Camera: %f %f %f Direction vector: %f %f %f\n", m_x, m_y, m_z, m_lx, m_ly, m_lz);
|
2021-06-15 21:13:59 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void Camera::SetPos(float x, float y, float z)
|
|
|
|
{
|
|
|
|
m_x = x;
|
|
|
|
m_y = y;
|
2021-06-21 23:11:21 -04:00
|
|
|
m_z = z;
|
2021-06-15 21:13:59 -04:00
|
|
|
|
2022-06-16 15:11:58 -04:00
|
|
|
// Refresh();
|
2021-06-15 21:13:59 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void Camera::GetPos(float &x, float &y, float &z)
|
|
|
|
{
|
2021-06-21 23:11:21 -04:00
|
|
|
x = m_x;
|
|
|
|
y = m_y;
|
|
|
|
z = m_z;
|
2021-06-15 21:13:59 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void Camera::GetDirectionVector(float &x, float &y, float &z)
|
|
|
|
{
|
2021-06-21 23:11:21 -04:00
|
|
|
x = m_lx;
|
|
|
|
y = m_ly;
|
|
|
|
z = m_lz;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Camera::SetTarget(Target t, float x, float y, float z)
|
|
|
|
{
|
|
|
|
target = t;
|
|
|
|
|
|
|
|
if(target == Target::POINT)
|
|
|
|
{
|
|
|
|
t_x = x;
|
|
|
|
t_y = y;
|
|
|
|
t_z = z;
|
|
|
|
}
|
|
|
|
|
2022-06-16 15:11:58 -04:00
|
|
|
// Refresh();
|
2021-06-15 21:13:59 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void Camera::Move(float incr)
|
|
|
|
{
|
2021-06-21 23:11:21 -04:00
|
|
|
float lx = cos(m_yaw)*cos(m_pitch);
|
|
|
|
float ly = sin(m_yaw)*cos(m_pitch);
|
|
|
|
float lz = sin(m_pitch);
|
2021-06-15 21:13:59 -04:00
|
|
|
|
|
|
|
m_x = m_x + incr*lx;
|
|
|
|
m_y = m_y + incr*ly;
|
|
|
|
m_z = m_z + incr*lz;
|
|
|
|
|
2022-06-16 15:11:58 -04:00
|
|
|
// Refresh();
|
2021-06-15 21:13:59 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void Camera::Strafe(float incr)
|
|
|
|
{
|
|
|
|
m_x = m_x + incr*m_strafe_lx;
|
2021-06-21 23:11:21 -04:00
|
|
|
m_y = m_y + incr*m_strafe_ly;
|
2021-06-15 21:13:59 -04:00
|
|
|
|
2022-06-16 15:11:58 -04:00
|
|
|
// Refresh();
|
2021-06-15 21:13:59 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void Camera::Fly(float incr)
|
|
|
|
{
|
|
|
|
m_y = m_y + incr;
|
|
|
|
|
2022-06-16 15:11:58 -04:00
|
|
|
// Refresh();
|
2021-06-15 21:13:59 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void Camera::RotateYaw(float angle)
|
|
|
|
{
|
|
|
|
m_yaw += angle;
|
|
|
|
|
2022-06-16 15:11:58 -04:00
|
|
|
// Refresh();
|
2021-06-15 21:13:59 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void Camera::RotatePitch(float angle)
|
|
|
|
{
|
2021-06-21 23:11:21 -04:00
|
|
|
const float limit = 89.0 * M_PI / 180.0;
|
2021-06-15 21:13:59 -04:00
|
|
|
|
|
|
|
m_pitch += angle;
|
|
|
|
|
2021-06-21 23:11:21 -04:00
|
|
|
if(m_pitch < -limit)
|
|
|
|
m_pitch = -limit;
|
2021-06-15 21:13:59 -04:00
|
|
|
|
2021-06-21 23:11:21 -04:00
|
|
|
if(m_pitch > limit)
|
|
|
|
m_pitch = limit;
|
2021-06-15 21:13:59 -04:00
|
|
|
|
2022-06-16 15:11:58 -04:00
|
|
|
// Refresh();
|
2021-06-15 21:13:59 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void Camera::SetYaw(float angle)
|
|
|
|
{
|
|
|
|
m_yaw = angle;
|
|
|
|
|
2022-06-16 15:11:58 -04:00
|
|
|
// Refresh();
|
2021-06-15 21:13:59 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void Camera::SetPitch(float angle)
|
|
|
|
{
|
2021-06-21 23:11:21 -04:00
|
|
|
m_pitch = angle;
|
2021-06-15 21:13:59 -04:00
|
|
|
|
2022-06-16 15:11:58 -04:00
|
|
|
// Refresh();
|
2021-06-15 21:13:59 -04:00
|
|
|
}
|