Browse Source

Add the fog density volumes to the physics playground

Panagiotis Christopoulos Charitos 7 years ago
parent
commit
47f807feee

+ 43 - 6
samples/physics_playground/Main.cpp

@@ -29,6 +29,45 @@ end
 	return Error::NONE;
 }
 
+static Error createFogVolumeFadeEvent(SceneNode* node)
+{
+	CString script = R"(
+density = 15
+radius = 3.5
+
+function update(event, prevTime, crntTime)
+	node = event:getAssociatedSceneNodes():getAt(0)
+	fogComponent = node:getFogDensityComponent()
+
+	dt = crntTime - prevTime
+	density = density - 4.0 * dt
+	radius = radius + 0.5 * dt
+
+	pos = node:getMoveComponent():getLocalOrigin()
+	pos:setY(pos:getY() - 1.1 * dt)
+	node:getMoveComponent():setLocalOrigin(pos)
+
+	if density <= 0.0 or radius <= 0.0 then
+		event:getAssociatedSceneNodes():getAt(0):setMarkedForDeletion()
+	else
+		fogComponent:setSphere(radius)
+		fogComponent:setDensity(density)
+	end
+
+	return 1
+end
+
+function onKilled(event, prevTime, crntTime)
+	return 1
+end
+	)";
+	ScriptEvent* event;
+	ANKI_CHECK(node->getSceneGraph().getEventManager().newEvent(event, -1, 10.0, script));
+	event->addAssociatedSceneNode(node);
+
+	return Error::NONE;
+}
+
 class RayCast : public PhysicsWorldRayCastCallback
 {
 public:
@@ -258,7 +297,7 @@ Error MyApp::userMainLoop(Bool& quit)
 
 			createDestructionEvent(monkey);
 
-#if 0
+#if 1
 			// Create some particles
 			ParticleEmitterNode* particles;
 			ANKI_CHECK(getSceneGraph().newSceneNode(
@@ -282,12 +321,10 @@ Error MyApp::userMainLoop(Bool& quit)
 				fogComp.setSphere(2.1f);
 				fogComp.setDensity(15.0f);
 
-				BodyNode* body;
-				name.sprintf("fogbody%u", id++);
-				ANKI_CHECK(getSceneGraph().newSceneNode(name.toCString(), body, "assets/sphere_r2.ankicl"));
-				body->getComponent<BodyComponent>().setTransform(trf);
+				fogNode->getComponent<MoveComponent>().setLocalTransform(trf);
 
-				body->addChild(fogNode);
+				createDestructionEvent(fogNode);
+				createFogVolumeFadeEvent(fogNode);
 			}
 		}
 	}

+ 30 - 5
src/anki/script/LuaBinder.cpp

@@ -10,6 +10,24 @@
 namespace anki
 {
 
+// Forward
+#define ANKI_SCRIPT_CALL_WRAP(x_) void wrapModule##x_(lua_State*)
+ANKI_SCRIPT_CALL_WRAP(Logger);
+ANKI_SCRIPT_CALL_WRAP(Math);
+ANKI_SCRIPT_CALL_WRAP(Renderer);
+ANKI_SCRIPT_CALL_WRAP(Scene);
+#undef ANKI_SCRIPT_CALL_WRAP
+
+static void wrapModules(lua_State* l)
+{
+#define ANKI_SCRIPT_CALL_WRAP(x_) wrapModule##x_(l)
+	ANKI_SCRIPT_CALL_WRAP(Logger);
+	ANKI_SCRIPT_CALL_WRAP(Math);
+	ANKI_SCRIPT_CALL_WRAP(Renderer);
+	ANKI_SCRIPT_CALL_WRAP(Scene);
+#undef ANKI_SCRIPT_CALL_WRAP
+}
+
 static int luaPanic(lua_State* l)
 {
 	ANKI_SCRIPT_LOGE("Lua panic attack: %s", lua_tostring(l, -1));
@@ -37,6 +55,8 @@ Error LuaBinder::create(ScriptAllocator alloc, void* parent)
 	luaL_openlibs(m_l);
 	lua_atpanic(m_l, &luaPanic);
 
+	wrapModules(m_l);
+
 	return Error::NONE;
 }
 
@@ -256,8 +276,15 @@ LuaThread LuaBinder::newLuaThread()
 {
 	LuaThread out;
 
-	out.m_luaState = lua_newthread(m_l);
-	out.m_reference = luaL_ref(m_l, LUA_REGISTRYINDEX);
+	void* ud;
+	auto allocCallback = lua_getallocf(m_l, &ud);
+	ANKI_ASSERT(ud && allocCallback);
+
+	out.m_luaState = lua_newstate(allocCallback, ud);
+	luaL_openlibs(out.m_luaState);
+	lua_atpanic(out.m_luaState, &luaPanic);
+
+	wrapModules(out.m_luaState);
 
 	return out;
 }
@@ -266,12 +293,10 @@ void LuaBinder::destroyLuaThread(LuaThread& luaThread)
 {
 	if(luaThread.m_luaState)
 	{
-		// Unref it, garbage collector will take care of it
-		luaL_unref(m_l, LUA_REGISTRYINDEX, luaThread.m_reference);
+		lua_close(luaThread.m_luaState);
 	}
 
 	luaThread.m_luaState = nullptr;
-	luaThread.m_reference = -1;
 }
 
 void LuaBinder::serializeGlobals(lua_State* l, LuaBinderSerializeGlobalsCallback& callback)

+ 0 - 6
src/anki/script/LuaBinder.h

@@ -154,14 +154,8 @@ public:
 		ANKI_ASSERT(m_luaState == nullptr);
 		m_luaState = b.m_luaState;
 		b.m_luaState = nullptr;
-
-		m_reference = b.m_reference;
-		b.m_reference = -1;
 		return *this;
 	}
-
-private:
-	int m_reference = -1;
 };
 
 /// @memberof LuaBinder

+ 0 - 18
src/anki/script/ScriptManager.cpp

@@ -10,14 +10,6 @@
 namespace anki
 {
 
-// Forward
-#define ANKI_SCRIPT_CALL_WRAP(x_) void wrapModule##x_(lua_State*)
-ANKI_SCRIPT_CALL_WRAP(Logger);
-ANKI_SCRIPT_CALL_WRAP(Math);
-ANKI_SCRIPT_CALL_WRAP(Renderer);
-ANKI_SCRIPT_CALL_WRAP(Scene);
-#undef ANKI_SCRIPT_CALL_WRAP
-
 ScriptManager::ScriptManager()
 {
 }
@@ -35,16 +27,6 @@ Error ScriptManager::init(AllocAlignedCallback allocCb, void* allocCbData)
 
 	ANKI_CHECK(m_lua.create(m_alloc, this));
 
-	// Wrap stuff
-	lua_State* l = m_lua.getLuaState();
-
-#define ANKI_SCRIPT_CALL_WRAP(x_) wrapModule##x_(l)
-	ANKI_SCRIPT_CALL_WRAP(Logger);
-	ANKI_SCRIPT_CALL_WRAP(Math);
-	ANKI_SCRIPT_CALL_WRAP(Renderer);
-	ANKI_SCRIPT_CALL_WRAP(Scene);
-#undef ANKI_SCRIPT_CALL_WRAP
-
 	return Error::NONE;
 }