2018-04-03 23:05:22 +02:00
|
|
|
/*
|
|
|
|
Minetest
|
|
|
|
Copyright (C) 2010-2013 celeron55, Perttu Ahola <celeron55@gmail.com>
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
the Free Software Foundation; either version 2.1 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU Lesser General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Lesser General Public License along
|
|
|
|
with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*/
|
|
|
|
|
2017-05-05 22:07:36 +02:00
|
|
|
#include "l_camera.h"
|
2018-04-03 23:05:22 +02:00
|
|
|
#include <cmath>
|
|
|
|
#include "script/common/c_converter.h"
|
2017-05-05 22:07:36 +02:00
|
|
|
#include "l_internal.h"
|
2018-11-28 20:01:49 +01:00
|
|
|
#include "client/content_cao.h"
|
|
|
|
#include "client/camera.h"
|
|
|
|
#include "client/client.h"
|
2017-05-05 22:07:36 +02:00
|
|
|
|
2017-06-19 23:54:58 +02:00
|
|
|
LuaCamera::LuaCamera(Camera *m) : m_camera(m)
|
2017-05-05 22:07:36 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void LuaCamera::create(lua_State *L, Camera *m)
|
|
|
|
{
|
2019-06-25 21:18:08 +02:00
|
|
|
lua_getglobal(L, "core");
|
|
|
|
luaL_checktype(L, -1, LUA_TTABLE);
|
|
|
|
int objectstable = lua_gettop(L);
|
|
|
|
lua_getfield(L, -1, "camera");
|
|
|
|
|
|
|
|
// Duplication check
|
|
|
|
if (lua_type(L, -1) == LUA_TUSERDATA) {
|
|
|
|
lua_pop(L, 1);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-05-05 22:07:36 +02:00
|
|
|
LuaCamera *o = new LuaCamera(m);
|
2017-05-06 21:30:27 +02:00
|
|
|
*(void **)(lua_newuserdata(L, sizeof(void *))) = o;
|
2017-05-05 22:07:36 +02:00
|
|
|
luaL_getmetatable(L, className);
|
|
|
|
lua_setmetatable(L, -2);
|
2017-05-06 21:30:27 +02:00
|
|
|
|
2019-06-25 21:18:08 +02:00
|
|
|
lua_pushvalue(L, lua_gettop(L));
|
|
|
|
lua_setfield(L, objectstable, "camera");
|
2017-05-05 22:07:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int LuaCamera::l_set_camera_mode(lua_State *L)
|
|
|
|
{
|
|
|
|
Camera *camera = getobject(L, 1);
|
|
|
|
GenericCAO *playercao = getClient(L)->getEnv().getLocalPlayer()->getCAO();
|
|
|
|
if (!camera)
|
|
|
|
return 0;
|
|
|
|
sanity_check(playercao);
|
|
|
|
if (!lua_isnumber(L, 2))
|
|
|
|
return 0;
|
2017-05-06 21:30:27 +02:00
|
|
|
|
2017-05-05 22:07:36 +02:00
|
|
|
camera->setCameraMode((CameraMode)((int)lua_tonumber(L, 2)));
|
|
|
|
playercao->setVisible(camera->getCameraMode() > CAMERA_MODE_FIRST);
|
|
|
|
playercao->setChildrenVisible(camera->getCameraMode() > CAMERA_MODE_FIRST);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int LuaCamera::l_get_camera_mode(lua_State *L)
|
|
|
|
{
|
|
|
|
Camera *camera = getobject(L, 1);
|
|
|
|
if (!camera)
|
|
|
|
return 0;
|
2017-05-06 21:30:27 +02:00
|
|
|
|
2017-05-05 22:07:36 +02:00
|
|
|
lua_pushnumber(L, (int)camera->getCameraMode());
|
2017-05-06 21:30:27 +02:00
|
|
|
|
2017-05-05 22:07:36 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int LuaCamera::l_get_fov(lua_State *L)
|
|
|
|
{
|
|
|
|
Camera *camera = getobject(L, 1);
|
|
|
|
if (!camera)
|
|
|
|
return 0;
|
2017-05-06 21:30:27 +02:00
|
|
|
|
2017-05-05 22:07:36 +02:00
|
|
|
lua_newtable(L);
|
|
|
|
lua_pushnumber(L, camera->getFovX() * core::DEGTORAD);
|
|
|
|
lua_setfield(L, -2, "x");
|
|
|
|
lua_pushnumber(L, camera->getFovY() * core::DEGTORAD);
|
|
|
|
lua_setfield(L, -2, "y");
|
|
|
|
lua_pushnumber(L, camera->getCameraNode()->getFOV() * core::RADTODEG);
|
|
|
|
lua_setfield(L, -2, "actual");
|
|
|
|
lua_pushnumber(L, camera->getFovMax() * core::RADTODEG);
|
|
|
|
lua_setfield(L, -2, "max");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int LuaCamera::l_get_pos(lua_State *L)
|
|
|
|
{
|
|
|
|
Camera *camera = getobject(L, 1);
|
|
|
|
if (!camera)
|
|
|
|
return 0;
|
2017-05-06 21:30:27 +02:00
|
|
|
|
2017-05-05 22:07:36 +02:00
|
|
|
push_v3f(L, camera->getPosition());
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int LuaCamera::l_get_offset(lua_State *L)
|
|
|
|
{
|
2019-11-09 00:27:50 +01:00
|
|
|
LocalPlayer *player = getClient(L)->getEnv().getLocalPlayer();
|
|
|
|
sanity_check(player);
|
2017-05-06 21:30:27 +02:00
|
|
|
|
2019-11-09 00:27:50 +01:00
|
|
|
push_v3f(L, player->getEyeOffset() / BS);
|
2017-05-05 22:07:36 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int LuaCamera::l_get_look_dir(lua_State *L)
|
|
|
|
{
|
|
|
|
LocalPlayer *player = getClient(L)->getEnv().getLocalPlayer();
|
|
|
|
sanity_check(player);
|
|
|
|
|
|
|
|
float pitch = -1.0 * player->getPitch() * core::DEGTORAD;
|
|
|
|
float yaw = (player->getYaw() + 90.) * core::DEGTORAD;
|
2018-04-03 23:05:22 +02:00
|
|
|
v3f v(std::cos(pitch) * std::cos(yaw), std::sin(pitch),
|
|
|
|
std::cos(pitch) * std::sin(yaw));
|
2017-05-05 22:07:36 +02:00
|
|
|
|
|
|
|
push_v3f(L, v);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int LuaCamera::l_get_look_horizontal(lua_State *L)
|
|
|
|
{
|
|
|
|
LocalPlayer *player = getClient(L)->getEnv().getLocalPlayer();
|
|
|
|
sanity_check(player);
|
|
|
|
|
|
|
|
lua_pushnumber(L, (player->getYaw() + 90.) * core::DEGTORAD);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int LuaCamera::l_get_look_vertical(lua_State *L)
|
|
|
|
{
|
|
|
|
LocalPlayer *player = getClient(L)->getEnv().getLocalPlayer();
|
|
|
|
sanity_check(player);
|
|
|
|
|
|
|
|
lua_pushnumber(L, -1.0 * player->getPitch() * core::DEGTORAD);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int LuaCamera::l_get_aspect_ratio(lua_State *L)
|
|
|
|
{
|
|
|
|
Camera *camera = getobject(L, 1);
|
|
|
|
if (!camera)
|
|
|
|
return 0;
|
2017-05-06 21:30:27 +02:00
|
|
|
|
2017-05-05 22:07:36 +02:00
|
|
|
lua_pushnumber(L, camera->getCameraNode()->getAspectRatio());
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
LuaCamera *LuaCamera::checkobject(lua_State *L, int narg)
|
|
|
|
{
|
|
|
|
luaL_checktype(L, narg, LUA_TUSERDATA);
|
2017-05-06 21:30:27 +02:00
|
|
|
|
2017-05-05 22:07:36 +02:00
|
|
|
void *ud = luaL_checkudata(L, narg, className);
|
|
|
|
if (!ud)
|
|
|
|
luaL_typerror(L, narg, className);
|
2017-05-06 21:30:27 +02:00
|
|
|
|
|
|
|
return *(LuaCamera **)ud;
|
2017-05-05 22:07:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Camera *LuaCamera::getobject(LuaCamera *ref)
|
|
|
|
{
|
|
|
|
return ref->m_camera;
|
|
|
|
}
|
|
|
|
|
|
|
|
Camera *LuaCamera::getobject(lua_State *L, int narg)
|
|
|
|
{
|
|
|
|
LuaCamera *ref = checkobject(L, narg);
|
|
|
|
assert(ref);
|
|
|
|
Camera *camera = getobject(ref);
|
|
|
|
if (!camera)
|
|
|
|
return NULL;
|
|
|
|
return camera;
|
|
|
|
}
|
|
|
|
|
|
|
|
int LuaCamera::gc_object(lua_State *L)
|
|
|
|
{
|
2017-05-06 21:30:27 +02:00
|
|
|
LuaCamera *o = *(LuaCamera **)(lua_touserdata(L, 1));
|
2017-05-05 22:07:36 +02:00
|
|
|
delete o;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void LuaCamera::Register(lua_State *L)
|
|
|
|
{
|
|
|
|
lua_newtable(L);
|
|
|
|
int methodtable = lua_gettop(L);
|
|
|
|
luaL_newmetatable(L, className);
|
|
|
|
int metatable = lua_gettop(L);
|
2017-05-06 21:30:27 +02:00
|
|
|
|
2017-05-05 22:07:36 +02:00
|
|
|
lua_pushliteral(L, "__metatable");
|
|
|
|
lua_pushvalue(L, methodtable);
|
|
|
|
lua_settable(L, metatable);
|
2017-05-06 21:30:27 +02:00
|
|
|
|
2017-05-05 22:07:36 +02:00
|
|
|
lua_pushliteral(L, "__index");
|
|
|
|
lua_pushvalue(L, methodtable);
|
|
|
|
lua_settable(L, metatable);
|
2017-05-06 21:30:27 +02:00
|
|
|
|
2017-05-05 22:07:36 +02:00
|
|
|
lua_pushliteral(L, "__gc");
|
|
|
|
lua_pushcfunction(L, gc_object);
|
|
|
|
lua_settable(L, metatable);
|
2017-05-06 21:30:27 +02:00
|
|
|
|
2017-05-05 22:07:36 +02:00
|
|
|
lua_pop(L, 1);
|
2017-05-06 21:30:27 +02:00
|
|
|
|
2017-05-05 22:07:36 +02:00
|
|
|
luaL_openlib(L, 0, methods, 0);
|
|
|
|
lua_pop(L, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
const char LuaCamera::className[] = "Camera";
|
2017-05-07 01:37:37 +02:00
|
|
|
const luaL_Reg LuaCamera::methods[] = {luamethod(LuaCamera, set_camera_mode),
|
2017-05-06 21:30:27 +02:00
|
|
|
luamethod(LuaCamera, get_camera_mode), luamethod(LuaCamera, get_fov),
|
|
|
|
luamethod(LuaCamera, get_pos), luamethod(LuaCamera, get_offset),
|
|
|
|
luamethod(LuaCamera, get_look_dir),
|
|
|
|
luamethod(LuaCamera, get_look_vertical),
|
|
|
|
luamethod(LuaCamera, get_look_horizontal),
|
|
|
|
luamethod(LuaCamera, get_aspect_ratio),
|
|
|
|
|
|
|
|
{0, 0}};
|