add doc/**/cameras.md & update cameras library

This commit is contained in:
MihailRis 2024-07-22 16:50:03 +03:00
parent 2fe25f5dd5
commit 012e81d4c9
6 changed files with 169 additions and 2 deletions

View File

@ -0,0 +1,66 @@
# *cameras* library
Cameras manipulation library.
## Main functions
```lua
cameras.get(name: str) -> int
```
Returns a camera by name.
## Camera methods
```lua
-- accessing camera
local cam = cameras.get("pack:camera-name")
-- returns camera index
cam:get_index() -> int
-- returns camera name
cam:get_name() -> str
-- returns camera position
cam:get_pos() -> vec3
-- sets camera position
cam:set_pos(pos:vec3)
-- returns camera rotation
cam:get_rot() -> mat4
-- sets camera rotation
cam:set_rot(rot:mat4)
-- returns camera zoom value
cam:get_zoom() -> number
-- sets camera zoom value
cam:set_zoom(zoom: number)
-- returns angle of the camera's field of view in Y (degrees)
cam:get_fov() -> number
-- sets camera field of view angle in Y (degrees)
cam:set_fov(fov: number)
-- returns true if the Y axis is flipped
cam:is_flipped() -> bool
-- reflects the Y axis when true
cam:set_flipped(flipped: bool)
-- checks if perspective mode is enabled
cam:is_perspective() -> bool
-- turns on/off perspective mode
cam:set_perspective(perspective: bool)
-- returns camera direction vector
cam:get_front() -> vec3
-- returns camera right vector
cam:get_right() -> vec3
-- returns camera up vector
cam:get_up() -> vec3
-- makes camera look to a given point
cam:look_at(point:vec3)
```
Use player.set_camera to switch cameras.

View File

@ -0,0 +1,66 @@
# Библиотека *cameras*
Библиотека предназначена для работы с камерами.
## Основные функции
```lua
cameras.get(name: str) -> int
```
Возвращает камеру по имени.
## Методы камеры
```lua
-- получаем камеру
local cam = cameras.get("пак:имя-камеры")
-- возвращает индекс камеры
cam:get_index() -> int
-- возвращает имя камеры
cam:get_name() -> str
-- возвращает позицию камеры
cam:get_pos() -> vec3
-- устанавливает позицию камеры
cam:set_pos(pos: vec3)
-- возращает вращение камеры
cam:get_rot() -> mat4
-- устанавливает вращение камеры
cam:set_rot(rot: mat4)
-- возвращает значение приближения камеры
cam:get_zoom() -> number
-- устанавливает значение приближения камеры
cam:set_zoom(zoom: number)
-- возвращает угол поля зрения камеры по Y (в градусах)
cam:get_fov() -> number
-- устанавливает угол поля зрения камеры по Y (в градусах)
cam:set_fov(fov: number)
-- возвращает true если ось Y отражена
cam:is_flipped() -> bool
-- отражает ось Y при значении true
cam:set_flipped(flipped: bool)
-- проверяет, включен ли режим перспективы
cam:is_perspective() -> bool
-- включает/выключает режим перспективы
cam:set_perspective(perspective: bool)
-- возвращает вектор направления камеры
cam:get_front() -> vec3
-- возвращает вектор направления направо
cam:get_right() -> vec3
-- возвращает вектор направления вверх
cam:get_up() -> vec3
-- направляет камеру на заданную точку
cam:look_at(point: vec3)
```
Переключение камеры возможно через функцию player.set_camera.

33
res/scripts/classes.lua Normal file
View File

@ -0,0 +1,33 @@
local Camera = {__index={
get_pos=function(self) return cameras.get_pos(self.cid) end,
set_pos=function(self, v) return cameras.set_pos(self.cid, v) end,
get_name=function(self) return cameras.name(self.cid) end,
get_index=function(self) return self.cid end,
get_rot=function(self) return cameras.get_rot(self.cid) end,
set_rot=function(self, m) return cameras.set_rot(self.cid, m) end,
get_zoom=function(self) return cameras.get_zoom(self.cid) end,
set_zoom=function(self, f) return cameras.set_zoom(self.cid, f) end,
get_fov=function(self) return cameras.get_fov(self.cid) end,
set_fov=function(self, f) return cameras.set_fov(self.cid, f) end,
is_perspective=function(self) return cameras.is_perspective(self.cid) end,
set_perspective=function(self, b) return cameras.set_perspective(self.cid, b) end,
is_flipped=function(self) return cameras.is_flipped(self.cid) end,
set_flipped=function(self, b) return cameras.set_flipped(self.cid, b) end,
get_front=function(self) return cameras.get_front(self.cid) end,
get_right=function(self) return cameras.get_right(self.cid) end,
get_up=function(self) return cameras.get_up(self.cid) end,
look_at=function(self, v) return cameras.look_at(self.cid, v) end,
}}
local wrappers = {}
cameras.get = function(name)
local wrapper = wrappers[name]
if wrapper ~= nil then
return wrapper
end
local cid = cameras.index(name)
wrapper = setmetatable({cid=cid}, Camera)
wrappers[name] = wrapper
return wrapper
end

View File

@ -58,10 +58,10 @@ static void setter_zoom(lua::State* L, Camera& camera, int idx) {
}
static int getter_fov(lua::State* L, const Camera& camera) {
return lua::pushnumber(L, camera.getFov());
return lua::pushnumber(L, glm::degrees(camera.getFov()));
}
static void setter_fov(lua::State* L, Camera& camera, int idx) {
camera.setFov(lua::tonumber(L, idx));
camera.setFov(glm::radians(lua::tonumber(L, idx)));
}
static int getter_perspective(lua::State* L, const Camera& camera) {

View File

@ -58,6 +58,7 @@ void scripting::initialize(Engine* engine) {
load_script(fs::path("stdlib.lua"), true);
load_script(fs::path("stdcmd.lua"), true);
load_script(fs::path("classes.lua"), true);
}
[[nodiscard]]

View File

@ -1,6 +1,7 @@
#ifndef WINDOW_CAMERA_HPP_
#define WINDOW_CAMERA_HPP_
#define GLM_FORCE_RADIANS
#include <glm/glm.hpp>
class Camera {