Files
retro3d/camera.cm
2025-12-14 01:36:35 -06:00

91 lines
1.9 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(ex, ey, ez, tx, ty, tz, upx, upy, upz) {
upx = upx != null ? upx : 0
upy = upy != null ? upy : 1
upz = upz != null ? upz : 0
_eye = {x: ex, y: ey, z: ez}
_target = {x: tx, y: ty, z: tz}
_up = {x: upx, y: upy, z: upz}
_view_matrix = model_c.compute_view_matrix(ex, ey, ez, tx, ty, tz, upx, upy, upz)
}
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(left, right, bottom, top, near, far) {
near = near != null ? near : -1
far = far != null ? far : 1
_projection_type = "ortho"
_proj_matrix = model_c.compute_ortho(left, right, bottom, 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
}