townengine/src/twn_camera.c

53 lines
1.4 KiB
C

#include "twn_camera.h"
#include "twn_engine_context_c.h"
#include <math.h>
#define CAMERA_NEAR_Z 0.1f
#define CAMERA_FAR_Z 100.0f
t_matrix4 camera_look_at(const t_camera *const camera) {
/* from cglm */
const t_fvec3 r = m_vec_norm(m_vec_cross(camera->target, camera->up));
const t_fvec3 u = m_vec_cross(r, camera->target);
t_matrix4 result;
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;
return result;
}