Browse Source

Bug fixes

Panagiotis Christopoulos Charitos 13 years ago
parent
commit
1d43011f99

+ 5 - 2
include/anki/scene/Renderable.h

@@ -6,6 +6,7 @@
 #include "anki/gl/Ubo.h"
 #include "anki/resource/Material.h"
 #include "anki/resource/Model.h"
+#include <mutex>
 
 namespace anki {
 
@@ -18,8 +19,9 @@ class SceneNode;
 enum BuildinMaterialVariableId
 {
 	BMV_NO_BUILDIN = 0,
-	BMV_MODEL_VIEW_PROJECTION_MATRIX,
-	BMV_MODEL_VIEW_MATRIX,
+	BMV_MVP_MATRIX,
+	BMV_MV_MATRIX,
+	BMV_VP_MATRIX,
 	BMV_NORMAL_MATRIX,
 	BMV_BILLBOARD_MVP_MATRIX,
 	BMV_BLURRING,
@@ -231,6 +233,7 @@ private:
 	RenderableVariables vars;
 	Ubo ubo;
 	PerFrame* perframe;
+	std::mutex mtx;
 };
 /// @}
 

+ 12 - 0
include/anki/scene/Scene.h

@@ -0,0 +1,12 @@
+#ifndef ANKI_SCENE_SCENE_H
+#define ANKI_SCENE_SCENE_H
+
+#include "anki/scene/SceneGraph.h"
+#include "anki/scene/ModelNode.h"
+#include "anki/scene/SkinNode.h"
+#include "anki/scene/StaticGeometryNode.h"
+#include "anki/scene/ParticleEmitter.h"
+#include "anki/scene/Camera.h"
+#include "anki/scene/Light.h"
+
+#endif

+ 33 - 26
src/renderer/Drawer.cpp

@@ -50,7 +50,7 @@ struct SetupRenderableVariableVisitor
 			uniSet<typename TRenderableVariableTemplate::Type>(
 				*uni, x.get(), x.getArraySize());
 			break;
-		case BMV_MODEL_VIEW_PROJECTION_MATRIX:
+		case BMV_MVP_MATRIX:
 			{
 				ANKI_ASSERT(trfs != nullptr);
 				Array<Mat4, maxInstances> mvp;
@@ -63,31 +63,7 @@ struct SetupRenderableVariableVisitor
 				uniSet(*uni, &mvp[0], size);
 			}
 			break;
-		case BMV_BILLBOARD_MVP_MATRIX:
-			{
-				// Calc the billboard rotation matrix
-				Mat3 rot =
-					fr->getViewMatrix().getRotationPart().getTransposed();
-
-				/*Vec3 up(0.0, 1.0, 0.0);
-				Vec3 front = rot.getColumn(2);
-				Vec3 right = up.cross(front);
-				up = front.cross(right);
-				rot.setColumns(right, up, front);*/
-
-				Array<Mat4, maxInstances> bmvp;
-
-				for(U i = 0; i < size; i++)
-				{
-					Transform trf = trfs[i];
-					trf.setRotation(rot);
-					bmvp[i] = vp * Mat4(trf);
-				}
-
-				uniSet(*uni, &bmvp[0], size);
-			}
-			break;
-		case BMV_MODEL_VIEW_MATRIX:
+		case BMV_MV_MATRIX:
 			{
 				ANKI_ASSERT(trfs != nullptr);
 				Array<Mat4, maxInstances> mv;
@@ -100,7 +76,11 @@ struct SetupRenderableVariableVisitor
 				uniSet(*uni, &mv[0], size);
 			}
 			break;
+		case BMV_VP_MATRIX:
+			uniSet(*uni, &vp, 1);
+			break;
 		case BMV_NORMAL_MATRIX:
+			if(trfs)
 			{
 				Array<Mat3, maxInstances> normm;
 
@@ -112,6 +92,33 @@ struct SetupRenderableVariableVisitor
 
 				uniSet(*uni, &normm[0], size);
 			}
+			else
+			{
+				ANKI_ASSERT(uniArrSize == 1
+					&& "Having that instanced doesn't make sense");
+
+				Mat3 norm = v.getRotationPart();
+
+				uniSet(*uni, &norm, 1);
+			}
+			break;
+		case BMV_BILLBOARD_MVP_MATRIX:
+			{
+				// Calc the billboard rotation matrix
+				Mat3 rot =
+					fr->getViewMatrix().getRotationPart().getTransposed();
+
+				Array<Mat4, maxInstances> bmvp;
+
+				for(U i = 0; i < size; i++)
+				{
+					Transform trf = trfs[i];
+					trf.setRotation(rot);
+					bmvp[i] = vp * Mat4(trf);
+				}
+
+				uniSet(*uni, &bmvp[0], size);
+			}
 			break;
 		case BMV_BLURRING:
 			{

+ 6 - 1
src/scene/Renderable.cpp

@@ -38,6 +38,7 @@ struct CreateNewRenderableVariableVisitor
 static Array<const char*, BMV_COUNT - 1> buildinNames = {{
 	"modelViewProjectionMat",
 	"modelViewMat",
+	"viewProjectionMat",
 	"normalMat",
 	"billboardMvpMatrix",
 	"blurring"}};
@@ -135,6 +136,8 @@ void Renderable::init(PropertyMap& pmap)
 //==============================================================================
 void Renderable::setVisibleSubMeshesMask(const SceneNode* frustumable, U64 mask)
 {
+	mtx.lock();
+
 	if(ANKI_UNLIKELY(perframe == nullptr))
 	{
 		perframe = ANKI_NEW(PerFrame, frustumable->getSceneFrameAllocator(),
@@ -142,6 +145,8 @@ void Renderable::setVisibleSubMeshesMask(const SceneNode* frustumable, U64 mask)
 	}
 
 	perframe->pairs.push_back(FrustumableMaskPair{frustumable, mask});
+
+	mtx.unlock();
 }
 
 //==============================================================================
@@ -159,7 +164,7 @@ U64 Renderable::getVisibleSubMeshsMask(const SceneNode& frustumable) const
 		}
 	}
 
-	ANKI_ASSERT("Shouldn't have come to this");
+	ANKI_ASSERT(0 && "Shouldn't have come to this");
 	return 0;
 }
 

+ 2 - 2
src/scene/StaticGeometryNode.cpp

@@ -21,7 +21,7 @@ StaticGeometrySpatial::StaticGeometrySpatial(const Obb& obb,
 StaticGeometryPatchNode::StaticGeometryPatchNode(
 	const ModelPatchBase* modelPatch_, const char* name, SceneGraph* scene)
 	:	SceneNode(name, scene),
-		Spatial(&modelPatch->getBoundingShape(), getSceneAllocator()),
+		Spatial(&modelPatch_->getBoundingShape(), getSceneAllocator()),
 		Renderable(getSceneAllocator()),
 		modelPatch(modelPatch_)
 {
@@ -63,7 +63,7 @@ StaticGeometryPatchNode::~StaticGeometryPatchNode()
 //==============================================================================
 StaticGeometryNode::StaticGeometryNode(const char* filename,
 	const char* name, SceneGraph* scene)
-	: SceneNode(name, scene)
+	: SceneNode(name, scene), patches(getSceneAllocator())
 {
 	model.load(filename);
 

+ 37 - 12
src/scene/Visibility.cpp

@@ -11,7 +11,7 @@ struct VisibilityTestJob: ThreadJob
 {
 	U nodesCount = 0;
 	SceneGraph::Types<SceneNode>::Container::iterator nodes;
-	Frustumable* frustumable = nullptr;
+	SceneNode* frustumableSn = nullptr;
 	Renderer* renderer = nullptr;
 	SceneAllocator<U8> frameAlloc;
 
@@ -25,6 +25,9 @@ struct VisibilityTestJob: ThreadJob
 
 		visible = ANKI_NEW(VisibilityTestResults, frameAlloc, frameAlloc);
 
+		Frustumable* frustumable = frustumableSn->getFrustumable();
+		ANKI_ASSERT(frustumable);
+
 		for(auto it = nodes + start; it != nodes + end; it++)
 		{
 			SceneNode* node = *it;
@@ -49,17 +52,17 @@ struct VisibilityTestJob: ThreadJob
 
 			// Hierarchical spatial => check subspatials
 			U64 subSpatialsMask = 0;
-			for(auto it = sp->getSubSpatialsEnd() - 1; 
-				it >= sp->getSubSpatialsBegin(); --it)
+			U i = 0;
+			for(auto it = sp->getSubSpatialsBegin();
+				it != sp->getSubSpatialsEnd(); ++it)
 			{
 				Spatial* subsp = *it;
-
-				subSpatialsMask <<= 1;
 	
 				if(frustumable->insideFrustum(*subsp))
 				{
-					subSpatialsMask |= 1; 
+					subSpatialsMask |= 1 << i;
 				}
+				++i;
 			}
 
 			// renderable
@@ -77,7 +80,7 @@ struct VisibilityTestJob: ThreadJob
 				if(subSpatialsMask)
 				{
 					ANKI_ASSERT(r->getSubMeshesCount() > 0);
-					r->setVisibleSubMeshesMask(node, subSpatialsMask);
+					r->setVisibleSubMeshesMask(frustumableSn, subSpatialsMask);
 				}
 			}
 			else
@@ -89,7 +92,7 @@ struct VisibilityTestJob: ThreadJob
 
 					if(l->getShadowEnabled() && fr)
 					{
-						testLight(*l);
+						testLight(*node);
 					}
 				}
 			}
@@ -99,10 +102,10 @@ struct VisibilityTestJob: ThreadJob
 	}
 
 	/// Test an individual light
-	void testLight(Light& light)
+	void testLight(SceneNode& lightSn)
 	{
-		Frustumable& ref = *light.getFrustumable();
-		ANKI_ASSERT(&ref != nullptr);
+		ANKI_ASSERT(lightSn.getFrustumable() != nullptr);
+		Frustumable& ref = *lightSn.getFrustumable();
 
 		// Allocate new visibles
 		VisibilityTestResults* lvisible = 
@@ -132,12 +135,34 @@ struct VisibilityTestJob: ThreadJob
 				continue;
 			}
 
+			// Hierarchical spatial => check subspatials
+			U64 subSpatialsMask = 0;
+			U i = 0;
+			for(auto it = sp->getSubSpatialsBegin();
+				it != sp->getSubSpatialsEnd(); ++it)
+			{
+				Spatial* subsp = *it;
+
+				if(frustumableSn->getFrustumable()->insideFrustum(*subsp))
+				{
+					subSpatialsMask |= 1 << i;
+				}
+				++i;
+			}
+
 			sp->enableFlags(Spatial::SF_VISIBLE_LIGHT);
 
 			Renderable* r = node->getRenderable();
 			if(r)
 			{
 				lvisible->renderables.push_back(node);
+
+				// Inform the renderable for the mask
+				if(subSpatialsMask)
+				{
+					ANKI_ASSERT(r->getSubMeshesCount() > 0);
+					r->setVisibleSubMeshesMask(&lightSn, subSpatialsMask);
+				}
 			}
 		}
 	}
@@ -159,7 +184,7 @@ void doVisibilityTests(SceneNode& fsn, SceneGraph& scene,
 	{
 		jobs[i].nodesCount = scene.getSceneNodesCount();
 		jobs[i].nodes = scene.getSceneNodesBegin();
-		jobs[i].frustumable = fr;
+		jobs[i].frustumableSn = &fsn;
 		jobs[i].renderer = &r;
 		jobs[i].frameAlloc = scene.getFrameAllocator();
 

+ 3 - 3
testapp/Main.cpp

@@ -237,9 +237,9 @@ void init()
 		0.7));
 
 #if 1
-	ModelNode* sponzaModel = new ModelNode(
-		"data/maps/sponza/sponza.mdl",
-		"sponza", &scene, Movable::MF_NONE, nullptr);
+	StaticGeometryNode* sponzaModel = new StaticGeometryNode(
+		"data/maps/sponza/sponza_new.mdl",
+		"sponza", &scene);
 
 	(void)sponzaModel;
 #endif