Files
retro3d/camera.cm
2026-02-17 16:03:07 -06:00

89 lines
2.0 KiB
Plaintext

// Camera module for lance3d
var model_c = use('model')
// Private camera state
var _view_matrix = null
var _proj_matrix = null
var _eye = {x: 0, y: 0, z: 5}
var _target = {x: 0, y: 0, z: 0}
var _up = {x: 0, y: 1, z: 0}
var _fov = 60
var _near = 0.1
var _far = 1000
var _aspect = 4/3
var _projection_type = "perspective"
function set_aspect(aspect) {
_aspect = aspect
}
function look_at(eye, target, up) {
var up_val = up || {x: 0, y: 1, z: 0}
_eye = {x: eye.x, y: eye.y, z: eye.z}
_target = {x: target.x, y: target.y, z: target.z}
_up = {x: up_val.x, y: up_val.y, z: up_val.z}
_view_matrix = model_c.compute_view_matrix(eye.x, eye.y, eye.z, target.x, target.y, target.z, up_val.x, up_val.y, up_val.z)
}
function perspective(fov_deg, near, far) {
_fov = fov_deg || 60
_near = near || 0.1
_far = far || 1000
_projection_type = "perspective"
_proj_matrix = model_c.compute_perspective(_fov, _aspect, _near, _far)
}
function ortho(bounds, depth) {
var near = depth && depth.near != null ? depth.near : -1
var far = depth && depth.far != null ? depth.far : 1
_projection_type = "ortho"
_proj_matrix = model_c.compute_ortho(bounds.left, bounds.right, bounds.bottom, bounds.top, near, far)
}
function get_view_matrix() {
return _view_matrix || model_c.mat4_identity()
}
function get_proj_matrix() {
return _proj_matrix || model_c.mat4_identity()
}
function get_eye() {
return {x: _eye.x, y: _eye.y, z: _eye.z}
}
function get_target() {
return {x: _target.x, y: _target.y, z: _target.z}
}
function get_state() {
return {
view_matrix: _view_matrix,
proj_matrix: _proj_matrix,
eye: _eye,
target: _target,
up: _up,
fov: _fov,
near: _near,
far: _far,
aspect: _aspect,
projection_type: _projection_type
}
}
return {
set_aspect: set_aspect,
look_at: look_at,
perspective: perspective,
ortho: ortho,
get_view_matrix: get_view_matrix,
get_proj_matrix: get_proj_matrix,
get_eye: get_eye,
get_target: get_target,
get_state: get_state
}