windmill-gint/src/camera.cpp

226 lines
5.8 KiB
C++

#include "camera.hpp"
//-----------------------------------------------------------------------------
// CAMERA
//-----------------------------------------------------------------------------
Camera::Camera()
{
x = 0;
y = 0;
z = 0;
yaw = 0;
pitch = 0;
fov = 50; // degrés
near = 5;
far = 1000;
scale_coef = 128 / 2 * tanf(to_rad(fov)); // = 64 * 1 * 1,19 = 76,27
//scale_coef2 = tanf(to_rad(fov)); // = 1 * 1,19 = 1.19
//cos_fov = SCALE_AI * cosf(fov_rad);
//sin_fov = SCALE_AI * sinf(fov_rad);
//sin_fov = sinf(fov_rad);
//cos_fov_square = cosf(fov_rad) * cosf(fov_rad);
//near_coef = - far / float(far - near);
//far_coef = - far * near * 128 / (far - near);
setup_frustrum();
}
//-----------------------------------------------------------------------------
// SET
//-----------------------------------------------------------------------------
void Camera::set(float _x, float _y, float _z, float _yaw, float _pitch)
{
x = _x;
y = _y;
z = _z;
yaw = to_rad(_yaw);
pitch = to_rad(_pitch);
}
void Camera::set(Camera* _camera)
{
x = _camera->x;
y = _camera->y;
z = _camera->z;
yaw = _camera->yaw;
pitch = _camera->pitch;
}
//-----------------------------------------------------------------------------
// UPDATE_CAMERA
//-----------------------------------------------------------------------------
void Camera::update()
{
/*if (look_at_duration)
{
yaw = (yaw * (look_at_duration - 1) + look_at_yaw) / look_at_duration;
look_at_duration -= 1;
}*/
cos_yaw = cosf(yaw);
sin_yaw = sinf(yaw);
cos_pitch = cosf(pitch);
sin_pitch = sinf(pitch);
//nx = SCALE_AI * cos_pitch * cos_yaw;
//ny = SCALE_AI * cos_pitch * sin_yaw;
//nz = SCALE_AI * sin_pitch;
nx = cos_pitch * cos_yaw;
ny = cos_pitch * sin_yaw;
nz = sin_pitch;
// a1 a2 a3
// a4 a5 a6
// a7 a8 a9
a1 = SCALE_AI * cos_pitch * cos_yaw;
a2 = SCALE_AI * cos_pitch * sin_yaw;
a3 = SCALE_AI * sin_pitch;
a4 = SCALE_AI * -sin_yaw;
a5 = SCALE_AI * cos_yaw;
a6 = SCALE_AI * 0;
a7 = SCALE_AI * -sin_pitch * cos_yaw;
a8 = SCALE_AI * sin_pitch * -sin_yaw;
a9 = SCALE_AI * cos_pitch;
}
/*
//-----------------------------------------------------------------------------
// NORMAL_VECTOR
//-----------------------------------------------------------------------------
void Camera::direction_vector(float* _x, float* _y)
{
*_x = cos_yaw;
*_y = sin_yaw;
}
//-----------------------------------------------------------------------------
// MOVE_FRONT (valeur positive signifie devant)
//-----------------------------------------------------------------------------
void Camera::move_front(float value)
{
x += value * cosf(yaw);
y -= value * sinf(yaw);
}
//-----------------------------------------------------------------------------
// MOVE_SIDE (valeur positive signifie à droite)
//-----------------------------------------------------------------------------
void Camera::move_side(float value)
{
x -= value * sinf(yaw);
y -= value * cosf(yaw);
}
//-----------------------------------------------------------------------------
// MOVE_ALTITUDE (valeur positive signifie en haut)
//-----------------------------------------------------------------------------
void Camera::move_altitude(float value)
{
z += value;
}
//-----------------------------------------------------------------------------
// TURN_YAW (valeur positive signifie a gauche)
//-----------------------------------------------------------------------------
void Camera::turn_yaw(float value)
{
yaw += to_rad(value);
}
//-----------------------------------------------------------------------------
// TURN_PITCH (valeur positive signifie en haut)
//-----------------------------------------------------------------------------
void Camera::turn_pitch(float value)
{
pitch += to_rad(value);
}
//-----------------------------------------------------------------------------
// SET_YAW (valeur positive signifie a gauche)
//-----------------------------------------------------------------------------
void Camera::set_yaw(float value)
{
yaw = to_rad(value);
}
//-----------------------------------------------------------------------------
// SET_PITCH (valeur positive signifie en haut)
//-----------------------------------------------------------------------------
void Camera::set_pitch(float value)
{
pitch = to_rad(value);
}
//-----------------------------------------------------------------------------
// LOOK_AT
//-----------------------------------------------------------------------------
void Camera::look_at(float look_at_x, float look_at_y, float look_at_z, float duration)
{
look_at_yaw = 0;//atan2f(look_at_y - y, look_at_x - x);
//look_at_pitch = atan2(look_at_z - z, look_at_x - x);
// calcule de sqrt ??
look_at_duration = duration;
}*/
void Camera::setup_frustrum()
{
float angle_horizontal = to_rad(fov);
float angle_vertical = to_rad(fov / 2);
int sh = sinf(angle_horizontal) * 1000;
int sv = sinf(angle_vertical) * 1000;
int ch = cosf(angle_horizontal) * 1000;
int cv = cosf(angle_vertical) * 1000;
// z-near clipping plane
frustrum.sides[0].normal_x = 0;
frustrum.sides[0].normal_y = 0;
frustrum.sides[0].normal_z = 1;
frustrum.sides[0].distance = -near * 1000;
// right
frustrum.sides[1].normal_x = -ch;
frustrum.sides[1].normal_y = 0;
frustrum.sides[1].normal_z = sh;
frustrum.sides[1].distance = 0;
// top
frustrum.sides[2].normal_x = 0;
frustrum.sides[2].normal_y = cv;
frustrum.sides[2].normal_z = sv;
frustrum.sides[2].distance = 0;
// left
frustrum.sides[3].normal_x = ch;
frustrum.sides[3].normal_y = 0;
frustrum.sides[3].normal_z = sh;
frustrum.sides[3].distance = 0;
// bottom
frustrum.sides[4].normal_x = 0;
frustrum.sides[4].normal_y = -cv;
frustrum.sides[4].normal_z = sv;
frustrum.sides[4].distance = 0;
// z-far clipping plane
//frustrum.z_far.normal.x = 0;
//frustrum.z_far.normal.y = 0;
//frustrum.z_far.normal.z = -1;
//frustrum.z_far.distance = 1000;
}