add camera:look_at optional interpolation parameter

This commit is contained in:
MihailRis 2024-07-22 19:32:05 +03:00
parent e0cb57a10a
commit 058daf6117
4 changed files with 15 additions and 2 deletions

View File

@ -61,6 +61,9 @@ cam:get_up() -> vec3
-- makes camera look to a given point
cam:look_at(point:vec3)
-- makes camera look to a given point with given interpolation factor
cam:look_at(point: vec3, t: number)
```
Use player.set_camera to switch cameras.

View File

@ -61,6 +61,9 @@ cam:get_up() -> vec3
-- направляет камеру на заданную точку
cam:look_at(point: vec3)
-- направляет камеру на заданную точку с фактором интерполяции
cam:look_at(point: vec3, t: number)
```
Переключение камеры возможно через функцию player.set_camera.

View File

@ -16,7 +16,7 @@ local Camera = {__index={
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,
look_at=function(self, v, f) return cameras.look_at(self.cid, v, f) end,
}}
local wrappers = {}

View File

@ -92,7 +92,14 @@ static int l_look_at(lua::State* L) {
size_t index = static_cast<size_t>(lua::tointeger(L, 1));
auto& camera = *level->cameras.at(index);
auto center = lua::tovec<3>(L, 2);
camera.rotation = glm::inverse(glm::lookAt(glm::vec3(), center-camera.position, glm::vec3(0, 1, 0)));
auto matrix = glm::inverse(glm::lookAt(glm::vec3(), center-camera.position, glm::vec3(0, 1, 0)));
if (lua::isnumber(L, 3)) {
matrix = glm::mat4_cast(glm::slerp(
glm::quat(camera.rotation),
glm::quat(matrix),
static_cast<float>(lua::tonumber(L, 3))));
}
camera.rotation = matrix;
camera.updateVectors();
return 0;
}