From ecab558124772c2d815244378b3e1ad18875038c Mon Sep 17 00:00:00 2001 From: Mikulas Florek Date: Mon, 14 Dec 2015 01:39:37 +0100 Subject: [PATCH] lua api refactor --- src/engine/core/lua_wrapper.h | 35 ++++++++++++ src/lua_script/lua_engine_api.cpp | 89 ++++++++++++++++++++----------- src/physics/physics_scene.cpp | 70 ++++++++++++++++-------- src/renderer/render_scene.cpp | 9 ++-- 4 files changed, 146 insertions(+), 57 deletions(-) diff --git a/src/engine/core/lua_wrapper.h b/src/engine/core/lua_wrapper.h index 372d1a01a..7f30faefc 100644 --- a/src/engine/core/lua_wrapper.h +++ b/src/engine/core/lua_wrapper.h @@ -2,6 +2,7 @@ #include "core/log.h" +#include "core/vec.h" #include #include #include @@ -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(lua_State* L, int index) { return lua_isinteger(L, index) != 0; } +template <> inline bool isType(lua_State* L, int index) +{ + return lua_istable(L, index) != 0; +} template <> inline bool isType(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); diff --git a/src/lua_script/lua_engine_api.cpp b/src/lua_script/lua_engine_api.cpp index 706dbdcf0..2272d2a15 100644 --- a/src/lua_script/lua_engine_api.cpp +++ b/src/lua_script/lua_engine_api.cpp @@ -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(L, 1) || + !LuaWrapper::checkParameterType(L, 2)) + { + lua_pushnil(L); + return 1; + } + + auto* universe = LuaWrapper::toType(L, 1); + Entity entity = LuaWrapper::toType(L, 2); + Vec3 pos = universe->getPosition(entity); + LuaWrapper::pushLua(L, pos); + return 1; +} + +static int getEntityDirection(lua_State* L) +{ + if (!LuaWrapper::checkParameterType(L, 1) || + !LuaWrapper::checkParameterType(L, 2)) + { + lua_pushnil(L); + return 1; + } + + auto* universe = LuaWrapper::toType(L, 1); + Entity entity = LuaWrapper::toType(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(L, 1) || + !LuaWrapper::checkParameterType(L, 2) || + !LuaWrapper::checkParameterType(L, 3)) + { + lua_pushnil(L); + return 1; + } + Vec3 v = LuaWrapper::toType(L, 1); + Vec3 axis = LuaWrapper::toType(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)->setLocalRotation(entity, Quat(Vec3(x, y, z), angle)); + static_cast(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 } diff --git a/src/physics/physics_scene.cpp b/src/physics/physics_scene.cpp index 1011322dd..f297bc09e 100644 --- a/src/physics/physics_scene.cpp +++ b/src/physics/physics_scene.cpp @@ -36,6 +36,38 @@ namespace LuaAPI { +static int raycast(lua_State* L) +{ + if (!LuaWrapper::checkParameterType(L, 1) || + !LuaWrapper::checkParameterType(L, 2) || + !LuaWrapper::checkParameterType(L, 3) || + !LuaWrapper::checkParameterType(L, 4) || + !LuaWrapper::checkParameterType(L, 5) || + !LuaWrapper::checkParameterType(L, 6) || + !LuaWrapper::checkParameterType(L, 7)) + { + lua_pushnil(L); + return 1; + } + + auto* scene = (PhysicsScene*)LuaWrapper::toType(L, 1); + Vec3 origin; + origin.x = LuaWrapper::toType(L, 2); + origin.y = LuaWrapper::toType(L, 3); + origin.z = LuaWrapper::toType(L, 4); + Vec3 dir; + dir.x = LuaWrapper::toType(L, 5); + dir.y = LuaWrapper::toType(L, 6); + dir.z = LuaWrapper::toType(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(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(scene)->moveController(component, Vec3(x, y, z), time_delta); + static_cast(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(scene)->applyForceToActor(component, Vec3(x, y, z)); + static_cast(scene)->applyForceToActor(component, force); } @@ -729,21 +759,19 @@ struct PhysicsSceneImpl : public PhysicsScene if (!scene) return; m_script_scene = static_cast(scene); - m_script_scene->registerFunction("Physics", - "moveController", - LuaWrapper::wrap); - m_script_scene->registerFunction("Physics", - "applyForceToActor", - LuaWrapper::wrap); - m_script_scene->registerFunction("Physics", - "getActorComponent", - LuaWrapper::wrap); - m_script_scene->registerFunction("Physics", - "putToSleep", - LuaWrapper::wrap); - m_script_scene->registerFunction("Physics", - "getActorSpeed", - LuaWrapper::wrap); + +#define REGISTER_FUNCTION(name) \ + m_script_scene->registerFunction( \ + "Physics", #name, LuaWrapper::wrap) + + 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 } diff --git a/src/renderer/render_scene.cpp b/src/renderer/render_scene.cpp index efadf89fd..7d092c0c6 100644 --- a/src/renderer/render_scene.cpp +++ b/src/renderer/render_scene.cpp @@ -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); }