2024-09-16 06:07:01 +00:00
|
|
|
#include "twn_camera.h"
|
|
|
|
#include "twn_context.h"
|
2024-07-30 15:05:05 +00:00
|
|
|
|
|
|
|
#include <math.h>
|
|
|
|
|
|
|
|
|
|
|
|
#define CAMERA_NEAR_Z 0.1f
|
|
|
|
#define CAMERA_FAR_Z 100.0f
|
|
|
|
|
2024-07-29 12:21:39 +00:00
|
|
|
|
|
|
|
t_matrix4 camera_look_at(const t_camera *const camera) {
|
|
|
|
/* from cglm */
|
2024-07-30 15:05:05 +00:00
|
|
|
const t_fvec3 r = m_vec_norm(m_vec_cross(camera->target, camera->up));
|
|
|
|
const t_fvec3 u = m_vec_cross(r, camera->target);
|
2024-07-29 12:21:39 +00:00
|
|
|
|
|
|
|
t_matrix4 result;
|
|
|
|
|
2024-07-30 15:05:05 +00:00
|
|
|
result.row[0].x = r.x;
|
|
|
|
result.row[0].y = u.x;
|
|
|
|
result.row[0].z = -camera->target.x;
|
|
|
|
result.row[1].x = r.y;
|
|
|
|
result.row[1].y = u.y;
|
|
|
|
result.row[1].z = -camera->target.y;
|
|
|
|
result.row[2].x = r.z;
|
|
|
|
result.row[2].y = u.z;
|
|
|
|
result.row[2].z = -camera->target.z;
|
|
|
|
result.row[3].x = -m_vec_dot(r, camera->pos);
|
|
|
|
result.row[3].y = -m_vec_dot(u, camera->pos);
|
|
|
|
result.row[3].z = m_vec_dot(camera->target, camera->pos);
|
|
|
|
result.row[0].w = result.row[1].w = result.row[2].w = 0.0f;
|
|
|
|
result.row[3].w = 1.0f;
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
t_matrix4 camera_perspective(const t_camera *const camera) {
|
|
|
|
/* from cglm */
|
|
|
|
t_matrix4 result = {0};
|
|
|
|
|
|
|
|
const float aspect = RENDER_BASE_RATIO;
|
|
|
|
|
|
|
|
const float f = 1.0f / tanf(camera->fov * 0.5f);
|
|
|
|
const float fn = 1.0f / (CAMERA_NEAR_Z - CAMERA_FAR_Z);
|
|
|
|
|
|
|
|
result.row[0].x = f / aspect;
|
|
|
|
result.row[1].y = f;
|
|
|
|
result.row[2].z = (CAMERA_NEAR_Z + CAMERA_FAR_Z) * fn;
|
|
|
|
result.row[2].w = -1.0f;
|
|
|
|
result.row[3].z = 2.0f * CAMERA_NEAR_Z * CAMERA_FAR_Z * fn;
|
2024-07-29 12:21:39 +00:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|