Fixed issues with render.
This commit is contained in:
@@ -8,55 +8,79 @@ void Camera::Init()
|
||||
m_yaw = 0.0;
|
||||
m_pitch = 0.0;
|
||||
|
||||
SetPos(0, 0, 0);
|
||||
target = Target::FORWARD;
|
||||
t_x = t_y = t_z = 0.0f;
|
||||
|
||||
SetPos(0, 20, 0);
|
||||
}
|
||||
|
||||
void Camera::Refresh()
|
||||
{
|
||||
// Camera parameter according to Riegl's co-ordinate system
|
||||
// x/y for flat, z for height
|
||||
// x/z for flat, y for height
|
||||
m_lx = cos(m_yaw) * cos(m_pitch);
|
||||
m_ly = sin(m_pitch);
|
||||
m_lz = sin(m_yaw) * cos(m_pitch);
|
||||
m_ly = sin(m_yaw) * cos(m_pitch);
|
||||
m_lz = sin(m_pitch);
|
||||
|
||||
m_strafe_lx = cos(m_yaw - M_PI_2);
|
||||
m_strafe_lz = sin(m_yaw - M_PI_2);
|
||||
m_strafe_ly = 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);
|
||||
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
|
||||
0.0f, 0.0f, 0.0f, // look at
|
||||
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);
|
||||
// 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;
|
||||
m_z = z;
|
||||
|
||||
Refresh();
|
||||
}
|
||||
|
||||
void Camera::GetPos(float &x, float &y, float &z)
|
||||
{
|
||||
x = m_x;
|
||||
y = m_y;
|
||||
z = m_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;
|
||||
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;
|
||||
}
|
||||
|
||||
Refresh();
|
||||
}
|
||||
|
||||
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);
|
||||
float lx = cos(m_yaw)*cos(m_pitch);
|
||||
float ly = sin(m_yaw)*cos(m_pitch);
|
||||
float lz = sin(m_pitch);
|
||||
|
||||
m_x = m_x + incr*lx;
|
||||
m_y = m_y + incr*ly;
|
||||
@@ -68,7 +92,7 @@ void Camera::Move(float incr)
|
||||
void Camera::Strafe(float incr)
|
||||
{
|
||||
m_x = m_x + incr*m_strafe_lx;
|
||||
m_z = m_z + incr*m_strafe_lz;
|
||||
m_y = m_y + incr*m_strafe_ly;
|
||||
|
||||
Refresh();
|
||||
}
|
||||
@@ -89,15 +113,15 @@ void Camera::RotateYaw(float angle)
|
||||
|
||||
void Camera::RotatePitch(float angle)
|
||||
{
|
||||
const float limit = 89.0 * M_PI / 180.0;
|
||||
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;
|
||||
|
||||
if(m_pitch > limit)
|
||||
m_pitch = limit;
|
||||
if(m_pitch > limit)
|
||||
m_pitch = limit;
|
||||
|
||||
Refresh();
|
||||
}
|
||||
@@ -111,7 +135,7 @@ void Camera::SetYaw(float angle)
|
||||
|
||||
void Camera::SetPitch(float angle)
|
||||
{
|
||||
m_pitch = angle;
|
||||
m_pitch = angle;
|
||||
|
||||
Refresh();
|
||||
Refresh();
|
||||
}
|
||||
|
@@ -11,6 +11,11 @@ public:
|
||||
Camera() { Init(); }
|
||||
~Camera(){}
|
||||
|
||||
enum class Target {
|
||||
FORWARD,
|
||||
POINT,
|
||||
};
|
||||
|
||||
void Init();
|
||||
void Refresh();
|
||||
void SetPos(float x, float y, float z);
|
||||
@@ -19,6 +24,9 @@ public:
|
||||
void SetYaw(float angle);
|
||||
void SetPitch(float angle);
|
||||
|
||||
// NOTE(dev): x,y,z is only set if target is POINT
|
||||
void SetTarget(Target target, float x = 0.0f, float y = 0.0f, float z = 0.0f);
|
||||
|
||||
// Navigation
|
||||
void Move(float incr);
|
||||
void Strafe(float incr);
|
||||
@@ -27,10 +35,13 @@ public:
|
||||
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_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
|
||||
float m_strafe_lx, m_strafe_ly; // Always 90 degree to direction vector
|
||||
float t_x, t_y, t_z; // when in TARGET mode, look at this x,y,z coordinate
|
||||
|
||||
Target target;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
Reference in New Issue
Block a user