瀏覽代碼

Scene WIP

Panagiotis Christopoulos Charitos 14 年之前
父節點
當前提交
c3e819b4b2
共有 10 個文件被更改,包括 159 次插入102 次删除
  1. 12 8
      anki/scene/Camera.cpp
  2. 11 3
      anki/scene/Camera.h
  3. 29 6
      anki/scene/Light.cpp
  4. 33 27
      anki/scene/Light.h
  5. 30 25
      anki/scene/Movable.cpp
  6. 6 8
      anki/scene/Movable.h
  7. 13 4
      anki/scene/Property.h
  8. 16 6
      anki/scene/Renderable.cpp
  9. 0 14
      anki/scene/SceneNode.cpp
  10. 9 1
      anki/scene/SceneNode.h

+ 12 - 8
anki/scene/Camera.cpp

@@ -36,10 +36,12 @@ PerspectiveCamera::PerspectiveCamera(const char* name, Scene* scene,
 	uint movableFlags, Movable* movParent)
 	: Camera(CT_PERSPECTIVE, name, scene, movableFlags, movParent, &frustum)
 {
-	Property<PerspectiveFrustum>& prop =
-		addNewProperty(new ReadWritePointerProperty<PerspectiveFrustum>(
-		"frustum", &frustum));
-	ANKI_CONNECT(&prop, valueChanged, this, updateFrustumSlot);
+	Property<PerspectiveFrustum>* prop =
+		new ReadWritePointerProperty<PerspectiveFrustum>("frustum", &frustum);
+
+	addNewProperty(prop);
+
+	ANKI_CONNECT(prop, valueChanged, this, updateFrustumSlot);
 }
 
 
@@ -52,10 +54,12 @@ OrthographicCamera::OrthographicCamera(const char* name, Scene* scene,
 	uint movableFlags, Movable* movParent)
 	: Camera(CT_ORTHOGRAPHIC, name, scene, movableFlags, movParent, &frustum)
 {
-	Property<OrthographicFrustum>& prop =
-		addNewProperty(new ReadWritePointerProperty<OrthographicFrustum>(
-		"frustum", &frustum));
-	ANKI_CONNECT(&prop, valueChanged, this, updateFrustumSlot);
+	Property<OrthographicFrustum>* prop =
+		new ReadWritePointerProperty<OrthographicFrustum>("frustum", &frustum);
+
+	addNewProperty(prop);
+
+	ANKI_CONNECT(prop, valueChanged, this, updateFrustumSlot);
 }
 
 

+ 11 - 3
anki/scene/Camera.h

@@ -65,23 +65,30 @@ public:
 	/// @name SceneNode virtuals
 	/// @{
 
-	/// Re-implements SceneNode::getMovable()
+	/// Override SceneNode::getMovable()
 	Movable* getMovable()
 	{
 		return this;
 	}
 
-	/// Re-implements SceneNode::getFrustumable()
+	/// Override SceneNode::getFrustumable()
 	Frustumable* getFrustumable()
 	{
 		return this;
 	}
 
-	/// Re-implements SceneNode::getSpatial()
+	/// Override SceneNode::getSpatial()
 	Spatial* getSpatial()
 	{
 		return this;
 	}
+
+	/// Override SceneNode::frameUpdate
+	void frameUpdate(float prevUpdateTime, float crntTime, int frame)
+	{
+		SceneNode::frameUpdate(prevUpdateTime, crntTime, frame);
+		Movable::update();
+	}
 	/// @}
 
 	/// @name Movable virtuals
@@ -183,6 +190,7 @@ public:
 private:
 	PerspectiveFrustum frustum;
 
+	/// Called when the property changes
 	void updateFrustumSlot(const PerspectiveFrustum&)
 	{
 		frustumUpdate();

+ 29 - 6
anki/scene/Light.cpp

@@ -5,21 +5,44 @@
 namespace anki {
 
 
+//==============================================================================
+// Light                                                                       =
+//==============================================================================
+
+//==============================================================================
+Light::Light(LightType t, const char* fmtl, // Light
+	const char* name, Scene* scene, // Scene
+	uint movableFlags, Movable* movParent, // Movable
+	CollisionShape* cs) // Spatial
+	: SceneNode(name, scene), Movable(movableFlags, movParent),
+		Spatial(cs), type(t)
+{
+	mtl.load(fmtl);
+	Renderable::init(*this);
+}
+
+
 //==============================================================================
 Light::~Light()
 {}
 
 
 //==============================================================================
-// init                                                                        =
+// PointLight                                                                  =
 //==============================================================================
-void Light::init(const char* filename)
+
+//==============================================================================
+PointLight::PointLight(const char* fmtl,
+	const char* name, Scene* scene,
+	uint movableFlags, Movable* movParent)
+	: Light(LT_POINT, fmtl, name, scene, movableFlags, movParent, &sphereW)
 {
-	lightData.load(filename);
+	Property<PerspectiveFrustum>* prop =
+		new ReadWritePointerProperty<PerspectiveFrustum>("frustum", &frustum);
+
+	addNewProperty(prop);
 
-	diffuseCol = lightData->getDiffuseColor();
-	specularCol = lightData->getSpecularColor();
-	castsShadowFlag = lightData->getCastShadow();
+	ANKI_CONNECT(prop, valueChanged, this, updateFrustumSlot);
 }
 
 

+ 33 - 27
anki/scene/Light.h

@@ -40,15 +40,10 @@ public:
 
 	/// @name Constructors
 	/// @{
-	Light(LightType t,
+	Light(LightType t, const char* mtl, // Light
 		const char* name, Scene* scene, // Scene
 		uint movableFlags, Movable* movParent, // Movable
-		CollisionShape* cs) // Spatial
-		: SceneNode(name, scene), Movable(movableFlags, movParent),
-			Spatial(cs), type(t)
-	{
-		/// XXX mtlr
-	}
+		CollisionShape* cs); // Spatial
 	/// @}
 
 	virtual ~Light();
@@ -77,36 +72,32 @@ public:
 	{
 		return this;
 	}
+
+	/// Override SceneNode::frameUpdate
+	void frameUpdate(float prevUpdateTime, float crntTime, int frame)
+	{
+		SceneNode::frameUpdate(prevUpdateTime, crntTime, frame);
+		Movable::update();
+	}
 	/// @}
 
 	/// @name Renderable virtuals
 	/// @{
-	const ModelPatchBase* getModelPatchBase() const
+	const ModelPatchBase& getModelPatchBase() const
 	{
 		ANKI_ASSERT(0 && "Lights don't support it");
 		return NULL;
 	}
 
-	MaterialRuntime& getMaterialRuntime()
+	MaterialRuntime& getMaterial()
 	{
-		return *mtlr;
-	}
-
-	const Transform* getWorldTransform(const PassLevelKey&)
-	{
-		return &getWorldTranform();
-	}
-
-	const Transform* getPreviousWorldTransform(
-		const PassLevelKey&)
-	{
-		return &getPreviousWorldTranform();
+		return *mtl;
 	}
 	/// @}
 
 private:
 	LightType type;
-	boost::scoped_ptr<MaterialRuntime> mtlr;
+	MaterialResourcePointer mtl;
 };
 
 
@@ -114,13 +105,28 @@ private:
 class PointLight: public Light
 {
 public:
-	PointLight(const char* name, Scene* scene, uint movableFlags,
-		Movable* movParent)
-		: Light(LT_POINT, name, scene, movableFlags, movParent, &sphere)
-	{}
+	/// @name Constructors/Destructor
+	/// @{
+	PointLight(const char* fmtl,
+		const char* name, Scene* scene,
+		uint movableFlags, Movable* movParent);
+	/// @}
+
+	/// @name Movable virtuals
+	/// @{
+
+	/// Overrides Movable::moveUpdate(). This does:
+	/// - Update the collision shape
+	void moveUpdate()
+	{
+		Movable::moveUpdate();
+		sphereW = sphereL.getTransformed();
+	}
+	/// @}
 
 public:
-	Sphere sphere;
+	Sphere sphereW;
+	Sphere sphereL;
 };
 
 

+ 30 - 25
anki/scene/Movable.cpp

@@ -18,41 +18,46 @@ Movable::Movable(uint flags_, Movable* parent, PropertyMap& pmap)
 
 
 //==============================================================================
-void Movable::updateWorldTransform()
+void Movable::update()
 {
-	if(shouldUpdateWTrf)
+	if(getParent() == NULL)
 	{
-		prevWTrf = wTrf;
+		updateWorldTransform();
+	}
+}
 
-		if(getParent())
-		{
-			if(isFlagEnabled(MF_IGNORE_LOCAL_TRANSFORM))
-			{
-				wTrf = getParent()->getWorldTransform();
-			}
-			else
-			{
-				wTrf = Transform::combineTransformations(
-					getParent()->getWorldTransform(), lTrf);
-			}
-		}
-		else // else copy
-		{
-			wTrf = lTrf;
-		}
 
-		// Moved?
-		if(prevWTrf != wTrf)
+//==============================================================================
+void Movable::updateWorldTransform()
+{
+	prevWTrf = wTrf;
+
+	if(getParent())
+	{
+		if(isFlagEnabled(MF_IGNORE_LOCAL_TRANSFORM))
 		{
-			enableFlag(MF_MOVED);
-			moveUpdate();
+			wTrf = getParent()->getWorldTransform();
 		}
 		else
 		{
-			disableFlag(MF_MOVED);
+			wTrf = Transform::combineTransformations(
+				getParent()->getWorldTransform(), lTrf);
 		}
+	}
+	else // else copy
+	{
+		wTrf = lTrf;
+	}
 
-		shouldUpdateWTrf = false;
+	// Moved?
+	if(prevWTrf != wTrf)
+	{
+		enableFlag(MF_MOVED);
+		moveUpdate();
+	}
+	else
+	{
+		disableFlag(MF_MOVED);
 	}
 
 	BOOST_FOREACH(Movable* child, getChildren())

+ 6 - 8
anki/scene/Movable.h

@@ -144,14 +144,12 @@ public:
 	virtual void moveUpdate()
 	{}
 
-	/// Update self and children world transform, if root node
-	void update()
-	{
-		if(getParent() == NULL)
-		{
-			updateWorldTransform();
-		}
-	}
+	/// Update self and children world transform, if root node. Called every
+	/// frame.
+	/// @note Don't update if child because we start from roots and go to
+	///       children and we don't want a child to be updated before the
+	///       parent
+	void update();
 
 protected:
 	bool shouldUpdateWTrf; ///< Its true when we change the local transform

+ 13 - 4
anki/scene/Property.h

@@ -49,26 +49,35 @@ public:
 	template<typename T>
 	const T& getValue() const
 	{
-		checkType<T>();
+		checkType<Property<T> >();
 		return static_cast<const Property<T>*>(this)->getValue();
 	}
 
 	template<typename T>
 	void setValue(const T& x)
 	{
-		checkType<T>();
+		checkType<Property<T> >();
 		return static_cast<Property<T>*>(this)->setValue(x);
 	}
+	/// @}
+
+	/// Upcast to property
+	template<typename Prop>
+	Prop& upCast()
+	{
+		checkType<Prop>();
+		return static_cast<Prop&>(*this);
+	}
 
 private:
 	std::string name;
 	uint tid;
 
 	/// Runtime type checking
-	template<typename T>
+	template<typename Prop>
 	void checkType() const
 	{
-		if(Property<T>::TYPE_ID != getTypeId())
+		if(Prop::TYPE_ID != getTypeId())
 		{
 			throw ANKI_EXCEPTION("Types do not match: " + name);
 		}

+ 16 - 6
anki/scene/Renderable.cpp

@@ -7,27 +7,36 @@
 namespace anki {
 
 
-struct XXXVisitor: boost::static_visitor<void>
+//==============================================================================
+// CreateNewPropertyVisitor                                                    =
+//==============================================================================
+
+/// Create a new property given a material variable
+struct CreateNewPropertyVisitor: boost::static_visitor<void>
 {
 	const MaterialVariable* mvar;
 	PropertyMap* pmap;
 
-	XXXVisitor(const MaterialVariable* mvar_, PropertyMap* pmap_)
+	CreateNewPropertyVisitor(const MaterialVariable* mvar_, PropertyMap* pmap_)
 		: mvar(mvar_), pmap(pmap_)
 	{}
 
 	template<typename T>
 	void operator()(const T& x) const
 	{
-		MaterialVariableReadCowPointerProperty<T>* prop =
-			new MaterialVariableReadCowPointerProperty<T>(
-			mvar->getName().c_str(), &(mvar->getValue<T>()));
+		typedef MaterialVariableReadCowPointerProperty<T> Prop;
+
+		Prop* prop = new Prop(mvar->getName().c_str(), &(mvar->getValue<T>()));
 
 		pmap->addNewProperty(prop);
 	}
 };
 
 
+//==============================================================================
+// Renderable                                                                  =
+//==============================================================================
+
 //==============================================================================
 void Renderable::init(PropertyMap& pmap) const
 {
@@ -35,7 +44,8 @@ void Renderable::init(PropertyMap& pmap) const
 
 	BOOST_FOREACH(const MaterialVariable& mv, mtl.getVariables())
 	{
-		boost::apply_visitor(XXXVisitor(&mv, &pmap), mv.getVariant());
+		boost::apply_visitor(CreateNewPropertyVisitor(&mv, &pmap),
+			mv.getVariant());
 	}
 }
 

+ 0 - 14
anki/scene/SceneNode.cpp

@@ -1,6 +1,5 @@
 #include "anki/scene/SceneNode.h"
 #include "anki/scene/Scene.h"
-#include "anki/scene/Movable.h"
 
 
 namespace anki {
@@ -23,17 +22,4 @@ SceneNode::~SceneNode()
 }
 
 
-//==============================================================================
-void SceneNode::frameUpdate(float /*prevUpdateTime*/, float /*crntTime*/,
-	int /*frame*/)
-{
-	// Movable update
-	Movable* m = getMovable();
-	if(m)
-	{
-		m->update();
-	}
-}
-
-
 } // end namespace

+ 9 - 1
anki/scene/SceneNode.h

@@ -70,7 +70,15 @@ public:
 
 	/// This is called by the scene every frame after logic and before
 	/// rendering. By default it does nothing
-	virtual void frameUpdate(float prevUpdateTime, float crntTime, int frame);
+	/// @param[in] prevUpdateTime Timestamp of the previous update
+	/// @param[in] crntTime Timestamp of this update
+	/// @param[in] frame Frame number
+	virtual void frameUpdate(float prevUpdateTime, float crntTime, int frame)
+	{
+		(void)prevUpdateTime;
+		(void)crntTime;
+		(void)frame;
+	}
 
 private:
 	std::string name; ///< A unique name