Jelajahi Sumber

Scene updates. WILL NOT COMPILE

Panagiotis Christopoulos Charitos 14 tahun lalu
induk
melakukan
acaacab3d7

+ 1 - 1
src/Main.cpp

@@ -492,7 +492,7 @@ void initSubsystems(int argc, char* argv[])
 	// Scripting engine
 	const char* commonPythonCode =
 		"import sys\n"
-		"from Anki import *\n"
+		"from anki import *\n"
 		"\n"
 		"class StdoutCatcher:\n"
 		"    def write(self, str_):\n"

+ 7 - 2
src/cln/CollisionShape.h

@@ -23,9 +23,14 @@ class CollisionShape
 			CID_OBB
 		};
 
-		CollisionShape(ClassId cid_): cid(cid_) {}
+		CollisionShape(ClassId cid_)
+		:	cid(cid_)
+		{}
 
-		ClassId getClassId() const {return cid;}
+		ClassId getClassId() const
+		{
+			return cid;
+		}
 
 		/// If the bounding volume intersects with the plane then the func
 		/// returns 0, else it returns the distance. If the distance is < 0

+ 1 - 1
src/cln/LineSegment.h

@@ -2,7 +2,7 @@
 #define LINE_SEGMENT_H
 
 #include "CollisionShape.h"
-#include "m/Math.h"
+#include "m/Vec3.h"
 #include "util/Accessors.h"
 
 

+ 19 - 17
src/scene/Camera.h

@@ -14,17 +14,13 @@ class SpotLight;
 class PointLight;
 
 
-/// Camera SceneNode
+/// @addtogroup Scene
+/// @{
+
+/// Camera SceneNode interface class
 class Camera: public SceneNode, public VisibilityInfo
 {
 	public:
-		enum CameraType
-		{
-			CT_PERSPECTIVE,
-			CT_ORTHOGRAPHIC,
-			CT_NUM
-		};
-
 		enum FrustrumPlanes
 		{
 			FP_LEFT = 0,
@@ -36,13 +32,13 @@ class Camera: public SceneNode, public VisibilityInfo
 			FP_NUM
 		};
 
-		Camera(CameraType camType, bool inheritParentTrfFlag,
+		Camera(ClassId cid, Scene& scene, ulong flags,
 			SceneNode* parent);
 
+		static bool classof(const SceneNode* x);
+
 		/// @name Accessors
 		/// @{
-		GETTER_R(CameraType, type, getType)
-
 		float getZNear() const {return zNear;}
 		void setZNear(float znear);
 
@@ -81,8 +77,6 @@ class Camera: public SceneNode, public VisibilityInfo
 		/// @}
 
 	protected:
-		CameraType type;
-
 		float zNear, zFar;
 
 		/// @name The frustum planes in local and world space
@@ -115,18 +109,26 @@ class Camera: public SceneNode, public VisibilityInfo
 		virtual void getExtremePoints(Vec3* pointsArr,
 			uint& pointsNum) const = 0;
 };
+/// @}
 
 
 
-inline Camera::Camera(CameraType camType, bool inheritParentTrfFlag,
-	SceneNode* parent)
-:	SceneNode(SNT_CAMERA, inheritParentTrfFlag, parent),
-	type(camType)
+inline Camera::Camera(ClassId cid, Scene& scene, ulong flags,
+			SceneNode* parent)
+:	SceneNode(cid, scene, flags, parent)
 {
 	name = "Camera:" + name;
 }
 
 
+inline bool Camera::classof(const SceneNode* x)
+{
+	return x->getClassId() == CID_CAMERA ||
+		x->getClassId() == CID_PERSPECTIVE_CAMERA ||
+		x->getClassId() == CID_ORTHOGRAPHIC_CAMERA;
+}
+
+
 inline const Plane& Camera::getWSpaceFrustumPlane(FrustrumPlanes id) const
 {
 	return wspaceFrustumPlanes[id];

+ 1 - 3
src/scene/GhostNode.h

@@ -9,11 +9,9 @@
 class GhostNode: public SceneNode
 {
 	public:
-		GhostNode(): SceneNode(SNT_GHOST, false, NULL) {}
+		GhostNode(Scene& scene): SceneNode(SNT_GHOST, scene, SNF_NONE, NULL) {}
 		virtual ~GhostNode() {}
 		void init(const char*) {}
-
-		void frameUpdate(float /*prevUpdateTime*/, float /*crntTime*/) {}
 };
 
 

+ 13 - 17
src/scene/Light.h

@@ -27,20 +27,13 @@
 class Light: public SceneNode, public VisibilityInfo
 {
 	public:
-		enum LightType
-		{
-			LT_POINT,
-			LT_SPOT
-		};
-
-		Light(LightType type, bool inheritParentTrfFlag,
-			SceneNode* parent);
+		Light(ClassId cid, Scene& scene, ulong flags, SceneNode* parent);
 		~Light();
 
+		static bool classof(const SceneNode* x);
+
 		/// @name Accessors
 		/// @{
-		GETTER_R(LightType, type, getType)
-
 		GETTER_SETTER(Vec3, diffuseCol, getDiffuseCol, setDiffuseCol)
 		GETTER_SETTER(Vec3, specularCol, getSpecularCol, setSpecularCol)
 		GETTER_SETTER_BY_VAL(bool, castsShadowFlag, castsShadow, setCastsShadow)
@@ -55,17 +48,20 @@ class Light: public SceneNode, public VisibilityInfo
 		Vec3 diffuseCol; ///< Diffuse color
 		Vec3 specularCol; ///< Specular color
 		bool castsShadowFlag; ///< Casts shadow
-
-	private:
-		LightType type; ///< Light type
 };
 
 
-inline Light::Light(LightType type_, bool inheritParentTrfFlag,
-	SceneNode* parent)
-:	SceneNode(SNT_LIGHT, inheritParentTrfFlag, parent),
-	type(type_)
+inline Light::Light(ClassId cid, Scene& scene, ulong flags, SceneNode* parent)
+:	SceneNode(cid, scene, flags, parent)
 {}
 
 
+inline bool Light::classof(const SceneNode* x)
+{
+	return x->getClassId() == CID_LIGHT ||
+		x->getClassId() == CID_SPOT_LIGHT ||
+		x->getClassId() == CID_POINT_LIGHT;
+}
+
+
 #endif

+ 80 - 28
src/scene/SceneNode.cpp

@@ -1,8 +1,8 @@
-#include <algorithm>
-#include <boost/lexical_cast.hpp>
 #include "SceneNode.h"
 #include "Scene.h"
-#include "core/Globals.h"
+#include "util/Exception.h"
+#include <algorithm>
+#include <boost/lexical_cast.hpp>
 
 
 //==============================================================================
@@ -15,22 +15,29 @@ uint SceneNode::uid = 0;
 //==============================================================================
 // Constructor                                                                 =
 //==============================================================================
-SceneNode::SceneNode(SceneNodeType type_, bool inheritParentTrfFlag_,
-	SceneNode* parent)
-:	Object(parent),
-	type(type_),
-	inheritParentTrfFlag(inheritParentTrfFlag_)
+SceneNode::SceneNode(ClassId cid_, Scene& scene_, ulong flags_,
+	SceneNode* parent_)
+:	cid(cid_),
+ 	flags(flags_),
+ 	parent(parent_),
+ 	scene(scene_)
 {
+	++uid;
+
 	getWorldTransform().setIdentity();
 	getLocalTransform().setIdentity();
-	SceneSingleton::get().registerNode(this);
 
 	name = boost::lexical_cast<std::string>(uid);
 
-	++uid;
-
-	flags = SNF_NONE;
 	enableFlag(SNF_ACTIVE);
+
+	if(parent != NULL)
+	{
+		parent->addChild(*this);
+	}
+
+	// This goes last
+	scene.registerNode(this);
 }
 
 
@@ -39,7 +46,27 @@ SceneNode::SceneNode(SceneNodeType type_, bool inheritParentTrfFlag_,
 //==============================================================================
 SceneNode::~SceneNode()
 {
-	SceneSingleton::get().unregisterNode(this);
+	scene.unregisterNode(this);
+
+	if(parent != NULL)
+	{
+		parent->removeChild(*this);
+	}
+
+	if(isFlagEnabled(SNF_ON_DELETE_DELETE_CHILDREN))
+	{
+		BOOST_REVERSE_FOREACH(SceneNode* child, children)
+		{
+			delete child;
+		}
+	}
+	else
+	{
+		BOOST_REVERSE_FOREACH(SceneNode* child, children)
+		{
+			child->parent = NULL;
+		}
+	}
 }
 
 
@@ -48,53 +75,78 @@ SceneNode::~SceneNode()
 //==============================================================================
 void SceneNode::updateWorldTransform()
 {
-	prevWorldTransform = worldTransform;
+	prevWTrf = wTrf;
 
-	if(getObjParent())
+	if(parent)
 	{
-		const SceneNode* parent = static_cast<const SceneNode*>(getObjParent());
-
-		if(inheritParentTrfFlag)
+		if(isFlagEnabled(SNF_INHERIT_PARENT_TRANSFORM))
 		{
-			worldTransform = parent->getWorldTransform();
+			wTrf = parent->getWorldTransform();
 		}
 		else
 		{
-			worldTransform = Transform::combineTransformations(
-				parent->getWorldTransform(), localTransform);
+			wTrf = Transform::combineTransformations(
+				parent->getWorldTransform(), lTrf);
 		}
 	}
 	else // else copy
 	{
-		worldTransform = localTransform;
+		wTrf = lTrf;
 	}
 }
 
 
 //==============================================================================
-// Move(s)                                                                     =
+// addChild                                                                    =
 //==============================================================================
+void SceneNode::addChild(SceneNode& child)
+{
+	ASSERT(child.parent == NULL); // Child already has parent
+
+	child.parent = this;
+	children.push_back(&child);
+}
+
+
+//==============================================================================
+// removeChild                                                                 =
+//==============================================================================
+void SceneNode::removeChild(SceneNode& child)
+{
+	Vec<SceneNode*>::iterator it = std::find(children.begin(), children.end(),
+		&child);
+
+	if(it == children.end())
+	{
+		throw EXCEPTION("Child not found");
+	}
+
+	children.erase(it);
+	child.parent = NULL;
+}
+
 
 //==============================================================================
+// Move(s)                                                                     =
+//==============================================================================
+
 void SceneNode::moveLocalX(float distance)
 {
-	Vec3 x_axis = localTransform.getRotation().getColumn(0);
+	Vec3 x_axis = lTrf.getRotation().getColumn(0);
 	getLocalTransform().getOrigin() += x_axis * distance;
 }
 
 
-//==============================================================================
 void SceneNode::moveLocalY(float distance)
 {
-	Vec3 y_axis = localTransform.getRotation().getColumn(1);
+	Vec3 y_axis = lTrf.getRotation().getColumn(1);
 	getLocalTransform().getOrigin() += y_axis * distance;
 }
 
 
-//==============================================================================
 void SceneNode::moveLocalZ(float distance)
 {
-	Vec3 z_axis = localTransform.getRotation().getColumn(2);
+	Vec3 z_axis = lTrf.getRotation().getColumn(2);
 	getLocalTransform().getOrigin() += z_axis * distance;
 }
 

+ 88 - 57
src/scene/SceneNode.h

@@ -1,63 +1,82 @@
 #ifndef SCENE_NODE_H
 #define SCENE_NODE_H
 
-#include <memory>
-#include <string>
 #include "m/Math.h"
 #include "core/Object.h"
 #include "cln/Obb.h"
 #include "util/Accessors.h"
+#include "util/Vec.h"
+#include <memory>
+#include <string>
 
 
 class Material;
 class Controller;
+class Scene;
 
 
-/// The backbone of scene. It is also an Object for memory management reasons
-class SceneNode: public Object
+/// @addtogroup Scene
+/// @{
+
+/// Interface class backbone of scene
+class SceneNode
 {
 	public:
 		typedef Obb VisibilityCollisionShape;
-
-		enum SceneNodeType
+		
+		/// Class ID for scene nodes
+		enum ClassId
 		{
-			SNT_GHOST,
-			SNT_LIGHT,
-			SNT_CAMERA,
-			SNT_PARTICLE_EMITTER,
-			SNT_MODEL,
-			SNT_SKIN,
-			SNT_RENDERABLE
+			CID_CAMERA,
+			CID_CHOST_NODE,
+			CID_MODEL_NODE,
+			CID_LIGHT,
+			CID_MODEL_PATCH_NODE,
+			CID_ORTHOGRAPHIC_CAMERA,
+			CID_PARTICLE,
+			CID_PARTICLE_EMITTER_NODE,
+			CID_PATCH_NODE,
+			CID_PERSPECTIVE_CAMERA,
+			CID_POINT_LIGHT,
+			CID_RENDERABLE_NODE,
+			CID_SKIN_NODE,
+			CID_SKIN_PATCH_NODE,
+			CID_SPOT_LIGHT
 		};
-		
+
 		enum SceneNodeFlags
 		{
 			SNF_NONE = 0,
 			SNF_VISIBLE = 1,
 			SNF_ACTIVE = 2,
-			SNF_MOVED = 4
+			SNF_MOVED = 4,
+			SNF_INHERIT_PARENT_TRANSFORM = 8,
+			SNF_ON_DELETE_DELETE_CHILDREN = 16
 		};
 
-		explicit SceneNode(SceneNodeType type, bool inheritParentTrfFlag,
+		/// The one and only constructor
+		/// @param cid The class ID
+		/// @param scene The scene to register the node
+		/// @param flags The flags with the node properties
+		/// @param parent The nods's parent. Its nulptr only for the root node
+		explicit SceneNode(ClassId cid, Scene& scene, ulong flags,
 			SceneNode* parent);
+
 		virtual ~SceneNode();
+
+		static bool classof(const SceneNode*) {return true;}
+
 		virtual void init(const char*) = 0; ///< init using a script file
 
 		/// @name Accessors
 		/// @{
-		GETTER_SETTER(Transform, localTransform, getLocalTransform,
-			setLocalTransform)
-		GETTER_SETTER(Transform, worldTransform, getWorldTransform,
-			setWorldTransform)
-		GETTER_R(Transform, prevWorldTransform, getPrevWorldTransform)
-
-		GETTER_R_BY_VAL(SceneNodeType, type, getSceneNodeType)
-
-		const Object* getParent() const {return getObjParent();}
-		Object* getParent() {return getObjParent();}
-		const Object::Container& getChildren() const {return getObjChildren();}
-		Object::Container& getChildren() {return getObjChildren();}
-
+		GETTER_R_BY_VAL(ClassId, cid, getClassId)
+		GETTER_SETTER(Transform, lTrf, getLocalTransform, setLocalTransform)
+		GETTER_SETTER(Transform, wTrf, getWorldTransform, setWorldTransform)
+		GETTER_R(Transform, prevWTrf, getPrevWorldTransform)
+		const SceneNode* getParent() const {return parent;}
+		SceneNode* getParent() {return parent;}
+		GETTER_RW(Vec<SceneNode*>, children, getChildren)
 		GETTER_R(std::string, name, getSceneNodeName)
 		/// @}
 
@@ -65,8 +84,8 @@ class SceneNode: public Object
 		/// @{
 		void enableFlag(SceneNodeFlags flag, bool enable = true);
 		void disableFlag(SceneNodeFlags flag) {enableFlag(flag, false);}
-		bool isFlagEnabled(SceneNodeFlags flag) const;
-		ulong getFlags() const {return flags;}
+		bool isFlagEnabled(SceneNodeFlags flag) const {return flags & flag;}
+		GETTER_R_BY_VAL(ulong, flags, getFlags)
 		/// @}
 
 		/// @name Updates
@@ -75,8 +94,7 @@ class SceneNode: public Object
 		/// @{
 
 		/// This is called every frame
-		virtual void frameUpdate(float /*prevUpdateTime*/,
-			float /*crntTime*/) {}
+		virtual void frameUpdate(float prevUpdateTime, float crntTime);
 
 		/// This is called if the node moved
 		virtual void moveUpdate() {}
@@ -96,64 +114,77 @@ class SceneNode: public Object
 		/// the Scene
 		void updateWorldTransform();
 
-		void addChild(SceneNode& node) {Object::addChild(&node);}
+		/// XXX
+		void addChild(SceneNode& node);
+
+		/// XXX
+		void removeChild(SceneNode& node);
 
 	protected:
 		std::string name;
 
 	private:
-		Transform localTransform; ///< The transformation in local space
+		static uint uid; ///< Unique identifier
+
+		ClassId cid; ///< Class ID
+
+		Transform lTrf; ///< The transformation in local space
+
 		/// The transformation in world space (local combined with parent's
 		/// transformation)
-		Transform worldTransform;
-		Transform prevWorldTransform;
+		Transform wTrf;
+
+		/// Keep the previous transformation for blurring calculations
+		Transform prevWTrf;
 
-		SceneNodeType type;
 		/// This means that the the node will inherit the world transform of
 		/// its parent (if there is one) and it will not take into account its
 		/// local transform at all
 		bool inheritParentTrfFlag;
 
-		static uint uid; ///< Unique identifier
-
 		ulong flags; ///< The state flags
+
+		SceneNode* parent; ///< Could not be nullptr
+		Vec<SceneNode*> children;
+
+		Scene& scene;
 };
+/// @}
 
 
-inline void SceneNode::rotateLocalX(float angDegrees)
+inline void SceneNode::enableFlag(SceneNodeFlags flag, bool enable)
 {
-	getLocalTransform().getRotation().rotateXAxis(angDegrees);
+	if(enable)
+	{
+		flags |= flag;
+	}
+	else
+	{
+		flags &= ~flag;
+	}
 }
 
 
-inline void SceneNode::rotateLocalY(float angDegrees)
+inline void SceneNode::frameUpdate(float , float)
 {
-	getLocalTransform().getRotation().rotateYAxis(angDegrees);
 }
 
 
-inline void SceneNode::rotateLocalZ(float angDegrees)
+inline void SceneNode::rotateLocalX(float angDegrees)
 {
-	getLocalTransform().getRotation().rotateZAxis(angDegrees);
+	lTrf.getRotation().rotateXAxis(angDegrees);
 }
 
 
-inline void SceneNode::enableFlag(SceneNodeFlags flag, bool enable)
+inline void SceneNode::rotateLocalY(float angDegrees)
 {
-	if(enable)
-	{
-		flags |= flag;
-	}
-	else
-	{
-		flags &= ~flag;
-	}
+	lTrf.getRotation().rotateYAxis(angDegrees);
 }
 
 
-inline bool SceneNode::isFlagEnabled(SceneNodeFlags flag) const
+inline void SceneNode::rotateLocalZ(float angDegrees)
 {
-	return flags & flag;
+	lTrf.getRotation().rotateZAxis(angDegrees);
 }
 
 

+ 1 - 1
src/scene/SkinNode.cpp

@@ -55,7 +55,7 @@ void SkinNode::moveUpdate()
 //==============================================================================
 // frameUpdate                                                                 =
 //==============================================================================
-void SkinNode::frameUpdate(float prevUpdateTime, float crntTime)
+void SkinNode::frameUpdate(float prevUpdateTime, float /*crntTime*/)
 {
 	frame += step;
 

+ 5 - 5
src/script/ScriptManager.cpp

@@ -6,6 +6,9 @@
 #include <Python.h>
 
 
+using namespace boost::python;
+
+
 /// Define the classes
 BOOST_PYTHON_MODULE(Anki)
 {
@@ -45,9 +48,6 @@ BOOST_PYTHON_MODULE(Anki)
 }
 
 
-using namespace boost::python;
-
-
 //==============================================================================
 // init                                                                        =
 //==============================================================================
@@ -55,11 +55,11 @@ void ScriptManager::init()
 {
 	INFO("Initializing scripting engine...");
 
-	PyImport_AppendInittab((char*)("Anki"), &initAnki);
+	PyImport_AppendInittab((char*)("anki"), &initAnki);
 	Py_Initialize();
 	mainModule = object(handle<>(borrowed(PyImport_AddModule("__main__"))));
 	mainNamespace = mainModule.attr("__dict__");
-	ankiModule = object(handle<>(PyImport_ImportModule("Anki")));
+	ankiModule = object(handle<>(PyImport_ImportModule("anki")));
 
 	INFO("Scripting engine initialized");
 }