Browse Source

Fixing bugs

Panagiotis Christopoulos Charitos 7 years ago
parent
commit
4c50493a38

+ 23 - 0
samples/physics_playground/Main.cpp

@@ -258,6 +258,7 @@ Error MyApp::userMainLoop(Bool& quit)
 
 			createDestructionEvent(monkey);
 
+#if 0
 			// Create some particles
 			ParticleEmitterNode* particles;
 			ANKI_CHECK(getSceneGraph().newSceneNode(
@@ -266,6 +267,28 @@ Error MyApp::userMainLoop(Bool& quit)
 				"assets/smoke.ankipart"));
 			particles->getComponent<MoveComponent>().setLocalTransform(trf);
 			createDestructionEvent(particles);
+#endif
+
+			// Create some fog volumes
+			for(U i = 0; i < 1; ++i)
+			{
+				static int id = 0;
+				StringAuto name(getSceneGraph().getFrameAllocator());
+				name.sprintf("fog%u", id++);
+
+				FogDensityNode* fogNode;
+				ANKI_CHECK(getSceneGraph().newSceneNode(name.toCString(), fogNode));
+				FogDensityComponent& fogComp = fogNode->getComponent<FogDensityComponent>();
+				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);
+
+				body->addChild(fogNode);
+			}
 		}
 	}
 

+ 6 - 0
samples/physics_playground/assets/sphere_r2.ankicl

@@ -0,0 +1,6 @@
+<?xml version="1.0" encoding="UTF-8" ?>
+<collisionShape>
+	<type>sphere</type>
+	<value>2.0</value>
+</collisionShape>
+

+ 6 - 1
shaders/ClusteredShadingCommon.glsl

@@ -129,7 +129,7 @@ layout(std430, ANKI_SS_BINDING(LIGHT_SET, LIGHT_SS_BINDING + 1)) readonly buffer
 };
 
 // Debugging function
-Vec3 lightHeatmap(U32 firstIndex, U32 maxLights, Bool decals, Bool plights, Bool slights, Bool probes)
+Vec3 lightHeatmap(U32 firstIndex, U32 maxLights, Bool decals, Bool plights, Bool slights, Bool probes, Bool fogVolumes)
 {
 	U32 count = 0;
 	U32 idx;
@@ -154,6 +154,11 @@ Vec3 lightHeatmap(U32 firstIndex, U32 maxLights, Bool decals, Bool plights, Bool
 		count += (decals) ? 1u : 0u;
 	}
 
+	while((idx = u_lightIndices[firstIndex++]) != MAX_U32)
+	{
+		count += (fogVolumes) ? 1u : 0u;
+	}
+
 	F32 factor = min(1.0, F32(count) / F32(maxLights));
 	return heatmap(factor);
 }

+ 1 - 1
shaders/LightShading.glslp

@@ -130,7 +130,7 @@ void main()
 
 		idxOffset = u_clusters[clusterIdx];
 
-		// out_color = lightHeatmap(idxOffset, 5, false, false, true, false); return;
+		// out_color = lightHeatmap(idxOffset, 5, false, false, false, false, true); return;
 	}
 
 	// Decode GBuffer

+ 4 - 4
shaders/VolumetricLightingAccumulation.glslp

@@ -172,14 +172,14 @@ Vec4 accumulateLightsAndFog(U32 clusterIdx, Vec3 worldPos)
 
 	// Fog density
 	F32 fogDensity = 0.0;
-	idx = u_lightIndices[clusterIdx];
-	idx = u_lightIndices[idx - 1];
+	idxOffset = u_clusters[clusterIdx];
+	idxOffset = u_lightIndices[idxOffset - 1u];
 	ANKI_LOOP while((idx = u_lightIndices[idxOffset++]) != MAX_U32)
 	{
 		FogDensityVolume vol = u_fogDensityVolumes[idx];
 
 		F32 factor;
-		ANKI_BRANCH if(vol.m_isBox != 0u)
+		ANKI_BRANCH if(vol.m_isBox == 1u)
 		{
 			factor =
 				computeProbeBlendWeight(worldPos, vol.m_aabbMinOrSphereCenter, vol.m_aabbMaxOrSphereRadiusSquared, 0.2);
@@ -189,7 +189,7 @@ Vec4 accumulateLightsAndFog(U32 clusterIdx, Vec3 worldPos)
 			Vec3 diff = worldPos - vol.m_aabbMinOrSphereCenter;
 			F32 distSq = dot(diff, diff) / vol.m_aabbMaxOrSphereRadiusSquared.x;
 			distSq = min(1.0, distSq);
-			factor = distSq * distSq;
+			factor = 1.0 - distSq;
 		}
 
 		fogDensity += vol.m_density * factor;

+ 41 - 3
src/anki/scene/FogDensityNode.cpp

@@ -6,6 +6,7 @@
 #include <anki/scene/FogDensityNode.h>
 #include <anki/scene/components/MoveComponent.h>
 #include <anki/scene/components/FogDensityComponent.h>
+#include <anki/scene/components/SpatialComponent.h>
 
 namespace anki
 {
@@ -22,11 +23,10 @@ public:
 	{
 		updated = false;
 
-		MoveComponent& movec = m_node->getComponent<MoveComponent>();
+		const MoveComponent& movec = m_node->getComponent<MoveComponent>();
 		if(movec.getTimestamp() == m_node->getGlobalTimestamp())
 		{
-			FogDensityComponent& fogc = m_node->getComponent<FogDensityComponent>();
-			fogc.updatePosition(movec.getWorldTransform().getOrigin());
+			static_cast<FogDensityNode*>(m_node)->moveUpdated(movec);
 		}
 
 		return Error::NONE;
@@ -40,6 +40,44 @@ FogDensityNode::FogDensityNode(SceneGraph* scene, CString name)
 	newComponent<MoveComponent>(MoveComponentFlag::NONE);
 	newComponent<FeedbackComponent>();
 	newComponent<FogDensityComponent>();
+	newComponent<SpatialComponent>(&m_spatialBox);
+}
+
+FogDensityNode::~FogDensityNode()
+{
+}
+
+void FogDensityNode::moveUpdated(const MoveComponent& movec)
+{
+	// Update the fog component
+	FogDensityComponent& fogc = getComponent<FogDensityComponent>();
+	fogc.updatePosition(movec.getWorldTransform().getOrigin());
+
+	// Update the spatial component
+	SpatialComponent& spatialc = getComponent<SpatialComponent>();
+
+	Vec4 min, max;
+	if(fogc.isAabb())
+	{
+		fogc.getAabb(min, max);
+	}
+	else
+	{
+		F32 radius;
+		fogc.getSphere(radius);
+
+		min = Vec4(-radius, -radius, -radius, 0.0f);
+		max = Vec4(radius, radius, radius, 0.0f);
+	}
+
+	min += movec.getWorldTransform().getOrigin();
+	max += movec.getWorldTransform().getOrigin();
+
+	m_spatialBox.setMin(min);
+	m_spatialBox.setMax(max);
+
+	spatialc.setSpatialOrigin(movec.getWorldTransform().getOrigin());
+	spatialc.markForUpdate();
 }
 
 } // end namespace anki

+ 5 - 0
src/anki/scene/FogDensityNode.h

@@ -6,6 +6,7 @@
 #pragma once
 
 #include <anki/scene/SceneNode.h>
+#include <anki/collision/Aabb.h>
 
 namespace anki
 {
@@ -28,6 +29,10 @@ public:
 
 private:
 	class FeedbackComponent;
+
+	Aabb m_spatialBox;
+
+	void moveUpdated(const MoveComponent& movec);
 };
 /// @}
 

+ 19 - 1
src/anki/scene/components/FogDensityComponent.h

@@ -27,7 +27,7 @@ public:
 	{
 	}
 
-	void setBoundingBox(const Vec4& aabbMin, const Vec4& aabbMax)
+	void setAabb(const Vec4& aabbMin, const Vec4& aabbMax)
 	{
 		m_aabbMin = aabbMin;
 		m_aabbMax = aabbMax;
@@ -40,6 +40,24 @@ public:
 		m_box = false;
 	}
 
+	Bool isAabb() const
+	{
+		return m_box == true;
+	}
+
+	void getAabb(Vec4& aabbMin, Vec4& aabbMax) const
+	{
+		ANKI_ASSERT(isAabb());
+		aabbMin = m_aabbMin;
+		aabbMax = m_aabbMax;
+	}
+
+	void getSphere(F32& radius) const
+	{
+		ANKI_ASSERT(!isAabb());
+		radius = m_sphereRadius;
+	}
+
 	void setDensity(F32 d)
 	{
 		ANKI_ASSERT(d >= 0.0f);

+ 7 - 7
src/anki/script/Scene.cpp

@@ -1646,8 +1646,8 @@ const LuaUserDataTypeInfo& LuaUserData::getDataTypeInfoFor<FogDensityComponent>(
 	return luaUserDataTypeInfoFogDensityComponent;
 }
 
-/// Pre-wrap method FogDensityComponent::setBoundingBox.
-static inline int pwrapFogDensityComponentsetBoundingBox(lua_State* l)
+/// Pre-wrap method FogDensityComponent::setAabb.
+static inline int pwrapFogDensityComponentsetAabb(lua_State* l)
 {
 	LuaUserData* ud;
 	(void)ud;
@@ -1689,15 +1689,15 @@ static inline int pwrapFogDensityComponentsetBoundingBox(lua_State* l)
 	const Vec4& arg1(*iarg1);
 
 	// Call the method
-	self->setBoundingBox(arg0, arg1);
+	self->setAabb(arg0, arg1);
 
 	return 0;
 }
 
-/// Wrap method FogDensityComponent::setBoundingBox.
-static int wrapFogDensityComponentsetBoundingBox(lua_State* l)
+/// Wrap method FogDensityComponent::setAabb.
+static int wrapFogDensityComponentsetAabb(lua_State* l)
 {
-	int res = pwrapFogDensityComponentsetBoundingBox(l);
+	int res = pwrapFogDensityComponentsetAabb(l);
 	if(res >= 0)
 	{
 		return res;
@@ -1854,7 +1854,7 @@ static int wrapFogDensityComponentgetDensity(lua_State* l)
 static inline void wrapFogDensityComponent(lua_State* l)
 {
 	LuaBinder::createClass(l, &luaUserDataTypeInfoFogDensityComponent);
-	LuaBinder::pushLuaCFuncMethod(l, "setBoundingBox", wrapFogDensityComponentsetBoundingBox);
+	LuaBinder::pushLuaCFuncMethod(l, "setAabb", wrapFogDensityComponentsetAabb);
 	LuaBinder::pushLuaCFuncMethod(l, "setSphere", wrapFogDensityComponentsetSphere);
 	LuaBinder::pushLuaCFuncMethod(l, "setDensity", wrapFogDensityComponentsetDensity);
 	LuaBinder::pushLuaCFuncMethod(l, "getDensity", wrapFogDensityComponentgetDensity);

+ 1 - 1
src/anki/script/Scene.xml

@@ -217,7 +217,7 @@ using WeakArraySceneNodePtr = WeakArray<SceneNode*>;
 		</class>
 		<class name="FogDensityComponent">
 			<methods>
-				<method name="setBoundingBox">
+				<method name="setAabb">
 					<args>
 						<arg>const Vec4&amp;</arg>
 						<arg>const Vec4&amp;</arg>