lua api refactor

This commit is contained in:
Mikulas Florek 2015-12-14 01:39:37 +01:00
parent b1f3325154
commit ecab558124
4 changed files with 146 additions and 57 deletions

View file

@ -2,6 +2,7 @@
#include "core/log.h"
#include "core/vec.h"
#include <lua.hpp>
#include <lauxlib.h>
#include <tuple>
@ -21,6 +22,20 @@ template <> inline int toType(lua_State* L, int index)
{
return (int)lua_tointeger(L, index);
}
template <> inline Vec3 toType(lua_State* L, int index)
{
Vec3 v;
lua_rawgeti(L, index, 1);
v.x = (float)lua_tonumber(L, -1);
lua_pop(L, 1);
lua_rawgeti(L, index, 2);
v.y = (float)lua_tonumber(L, -1);
lua_pop(L, 1);
lua_rawgeti(L, index, 3);
v.z = (float)lua_tonumber(L, -1);
lua_pop(L, 1);
return v;
}
template <> inline int64 toType(lua_State* L, int index)
{
return (int64)lua_tointeger(L, index);
@ -73,6 +88,10 @@ template <> inline bool isType<int>(lua_State* L, int index)
{
return lua_isinteger(L, index) != 0;
}
template <> inline bool isType<Vec3>(lua_State* L, int index)
{
return lua_istable(L, index) != 0;
}
template <> inline bool isType<uint32>(lua_State* L, int index)
{
return lua_isinteger(L, index) != 0;
@ -107,6 +126,22 @@ template <> inline void pushLua(lua_State* L, float value)
{
lua_pushnumber(L, value);
}
inline void pushLua(lua_State* L, const Vec3& value)
{
lua_createtable(L, 3, 0);
lua_pushvalue(L, -1);
lua_pushnumber(L, value.x);
lua_rawseti(L, -2, 1);
lua_pushvalue(L, -1);
lua_pushnumber(L, value.y);
lua_rawseti(L, -2, 2);
lua_pushvalue(L, -1);
lua_pushnumber(L, value.z);
lua_rawseti(L, -2, 3);
}
template <> inline void pushLua(lua_State* L, bool value)
{
lua_pushboolean(L, value);

View file

@ -45,6 +45,12 @@ static int getEnvironment(lua_State* L)
}
static void addDebugCross(RenderScene* scene, Vec3 pos, float size, int color, float life)
{
scene->addDebugCross(pos, size, color, life);
}
static Texture* getMaterialTexture(Material* material, int texture_index)
{
if (!material) return nullptr;
@ -77,32 +83,55 @@ static int createComponent(IScene* scene, const char* type, int entity_idx)
}
static int getEntityPosition(lua_State* L)
{
if (!LuaWrapper::checkParameterType<void*>(L, 1) ||
!LuaWrapper::checkParameterType<Entity>(L, 2))
{
lua_pushnil(L);
return 1;
}
auto* universe = LuaWrapper::toType<Universe*>(L, 1);
Entity entity = LuaWrapper::toType<Entity>(L, 2);
Vec3 pos = universe->getPosition(entity);
LuaWrapper::pushLua(L, pos);
return 1;
}
static int getEntityDirection(lua_State* L)
{
if (!LuaWrapper::checkParameterType<void*>(L, 1) ||
!LuaWrapper::checkParameterType<float>(L, 2))
{
lua_pushnil(L);
return 1;
}
auto* universe = LuaWrapper::toType<Universe*>(L, 1);
Entity entity = LuaWrapper::toType<Entity>(L, 2);
Quat rot = universe->getRotation(entity);
LuaWrapper::pushLua(L, rot * Vec3(0, 0, 1));
return 1;
}
static int multVecQuat(lua_State* L)
{
Vec3 v;
v.x = (float)lua_tonumber(L, 1);
v.y = (float)lua_tonumber(L, 2);
v.z = (float)lua_tonumber(L, 3);
Vec3 axis;
axis.x = (float)lua_tonumber(L, 4);
axis.y = (float)lua_tonumber(L, 5);
axis.z = (float)lua_tonumber(L, 6);
if (!LuaWrapper::checkParameterType<Vec3>(L, 1) ||
!LuaWrapper::checkParameterType<Vec3>(L, 2) ||
!LuaWrapper::checkParameterType<float>(L, 3))
{
lua_pushnil(L);
return 1;
}
Vec3 v = LuaWrapper::toType<Vec3>(L, 1);
Vec3 axis = LuaWrapper::toType<Vec3>(L, 2);
Quat q(axis, (float)lua_tonumber(L, 7));
Vec3 res = q * v;
lua_createtable(L, 3, 0);
int table_idx = lua_gettop(L);
lua_pushnumber(L, res.x);
lua_rawseti(L, table_idx, 0);
lua_pushnumber(L, res.y);
lua_rawseti(L, table_idx, 1);
lua_pushnumber(L, res.z);
lua_rawseti(L, table_idx, 2);
LuaWrapper::pushLua(L, res);
return 1;
}
@ -119,35 +148,31 @@ static void logInfo(const char* text)
}
static void setEntityPosition(Universe* univ, int entity_index, float x, float y, float z)
static void setEntityPosition(Universe* univ, int entity_index, Vec3 pos)
{
univ->setPosition(entity_index, x, y, z);
univ->setPosition(entity_index, pos);
}
static void setEntityRotation(Universe* univ,
int entity_index,
float x,
float y,
float z,
Vec3 axis,
float angle)
{
if (entity_index < 0 || entity_index > univ->getEntityCount()) return;
univ->setRotation(entity_index, Quat(Vec3(x, y, z), angle));
univ->setRotation(entity_index, Quat(axis, angle));
}
static void setEntityLocalRotation(IScene* hierarchy,
Entity entity,
float x,
float y,
float z,
Vec3 axis,
float angle)
{
if (entity == INVALID_ENTITY) return;
static_cast<Hierarchy*>(hierarchy)->setLocalRotation(entity, Quat(Vec3(x, y, z), angle));
static_cast<Hierarchy*>(hierarchy)->setLocalRotation(entity, Quat(axis, angle));
}
@ -216,9 +241,11 @@ void registerEngineLuaAPI(LuaScriptScene& scene, Engine& engine, lua_State* L)
REGISTER_FUNCTION(addInputAction);
REGISTER_FUNCTION(logError);
REGISTER_FUNCTION(logInfo);
REGISTER_FUNCTION(logInfo);
REGISTER_FUNCTION(addDebugCross);
scene.registerFunction("Engine", "multVecQuat", &LuaAPI::multVecQuat);
scene.registerFunction("Engine", "getEntityPosition", &LuaAPI::getEntityPosition);
scene.registerFunction("Engine", "getEntityDirection", &LuaAPI::getEntityDirection);
#undef REGISTER_FUNCTION
}

View file

@ -36,6 +36,38 @@ namespace LuaAPI
{
static int raycast(lua_State* L)
{
if (!LuaWrapper::checkParameterType<void*>(L, 1) ||
!LuaWrapper::checkParameterType<float>(L, 2) ||
!LuaWrapper::checkParameterType<float>(L, 3) ||
!LuaWrapper::checkParameterType<float>(L, 4) ||
!LuaWrapper::checkParameterType<float>(L, 5) ||
!LuaWrapper::checkParameterType<float>(L, 6) ||
!LuaWrapper::checkParameterType<float>(L, 7))
{
lua_pushnil(L);
return 1;
}
auto* scene = (PhysicsScene*)LuaWrapper::toType<void*>(L, 1);
Vec3 origin;
origin.x = LuaWrapper::toType<float>(L, 2);
origin.y = LuaWrapper::toType<float>(L, 3);
origin.z = LuaWrapper::toType<float>(L, 4);
Vec3 dir;
dir.x = LuaWrapper::toType<float>(L, 5);
dir.y = LuaWrapper::toType<float>(L, 6);
dir.z = LuaWrapper::toType<float>(L, 7);
RaycastHit hit;
scene->raycast(origin, dir, FLT_MAX, hit);
LuaWrapper::pushLua(L, hit.entity);
return 1;
}
static float getActorSpeed(IScene* scene, ComponentIndex component)
{
return static_cast<PhysicsScene*>(scene)->getActorSpeed(component);
@ -44,12 +76,10 @@ static float getActorSpeed(IScene* scene, ComponentIndex component)
static void moveController(IScene* scene,
ComponentIndex component,
float x,
float y,
float z,
Vec3 dir,
float time_delta)
{
static_cast<PhysicsScene*>(scene)->moveController(component, Vec3(x, y, z), time_delta);
static_cast<PhysicsScene*>(scene)->moveController(component, dir, time_delta);
}
@ -65,9 +95,9 @@ static void putToSleep(IScene* scene, Entity entity)
}
static void applyForceToActor(IScene* scene, int component, float x, float y, float z)
static void applyForceToActor(IScene* scene, int component, Vec3 force)
{
static_cast<PhysicsScene*>(scene)->applyForceToActor(component, Vec3(x, y, z));
static_cast<PhysicsScene*>(scene)->applyForceToActor(component, force);
}
@ -729,21 +759,19 @@ struct PhysicsSceneImpl : public PhysicsScene
if (!scene) return;
m_script_scene = static_cast<LuaScriptScene*>(scene);
m_script_scene->registerFunction("Physics",
"moveController",
LuaWrapper::wrap<decltype(&LuaAPI::moveController), LuaAPI::moveController>);
m_script_scene->registerFunction("Physics",
"applyForceToActor",
LuaWrapper::wrap<decltype(&LuaAPI::applyForceToActor), LuaAPI::applyForceToActor>);
m_script_scene->registerFunction("Physics",
"getActorComponent",
LuaWrapper::wrap<decltype(&LuaAPI::getActorComponent), LuaAPI::getActorComponent>);
m_script_scene->registerFunction("Physics",
"putToSleep",
LuaWrapper::wrap<decltype(&LuaAPI::putToSleep), LuaAPI::putToSleep>);
m_script_scene->registerFunction("Physics",
"getActorSpeed",
LuaWrapper::wrap<decltype(&LuaAPI::getActorSpeed), LuaAPI::getActorSpeed>);
#define REGISTER_FUNCTION(name) \
m_script_scene->registerFunction( \
"Physics", #name, LuaWrapper::wrap<decltype(&LuaAPI::name), LuaAPI::name>)
REGISTER_FUNCTION(moveController);
REGISTER_FUNCTION(applyForceToActor);
REGISTER_FUNCTION(getActorComponent);
REGISTER_FUNCTION(putToSleep);
REGISTER_FUNCTION(getActorSpeed);
m_script_scene->registerFunction("Physics", "raycast", LuaAPI::raycast);
#undef REGISTER_FUNCTION
}

View file

@ -1692,9 +1692,7 @@ public:
}
void fillTemporaryInfos(const CullingSystem::Results& results,
const Frustum& frustum,
int64 layer_mask)
void fillTemporaryInfos(const CullingSystem::Results& results, const Frustum& frustum)
{
PROFILE_FUNCTION();
m_jobs.clear();
@ -1714,9 +1712,10 @@ public:
if (results[subresult_index].empty()) continue;
MTJD::Job* job = MTJD::makeJob(m_engine.getMTJDManager(),
[&subinfos, layer_mask, this, &results, subresult_index, &frustum]()
[&subinfos, this, &results, subresult_index, &frustum]()
{
PROFILE_BLOCK("Temporary Info Job");
PROFILE_INT("Renderable count", results[subresult_index].size());
Vec3 frustum_position = frustum.getPosition();
const int* LUMIX_RESTRICT raw_subresults = &results[subresult_index][0];
Renderable* LUMIX_RESTRICT renderables = &m_renderables[0];
@ -1921,7 +1920,7 @@ public:
const CullingSystem::Results* results = cull(frustum, layer_mask);
if (!results) return;
fillTemporaryInfos(*results, frustum, layer_mask);
fillTemporaryInfos(*results, frustum);
mergeTemporaryInfos(meshes);
}