Просмотр исходного кода

Scene refactoring. WONT COMPILE

Panagiotis Christopoulos Charitos 12 лет назад
Родитель
Сommit
3e19b7bf49
3 измененных файлов с 78 добавлено и 80 удалено
  1. 5 70
      include/anki/scene/Camera.h
  2. 35 2
      include/anki/scene/SceneNode.h
  3. 38 8
      src/scene/Camera.cpp

+ 5 - 70
include/anki/scene/Camera.h

@@ -12,8 +12,7 @@ namespace anki {
 /// @{
 
 /// Camera SceneNode interface class
-class Camera: public SceneNode, public MoveComponent, public SpatialComponent, 
-	public FrustumComponent
+class Camera: public SceneNode
 {
 public:
 	/// @note Don't EVER change the order
@@ -47,30 +46,14 @@ public:
 	virtual F32 getFar() const = 0;
 	/// @}
 
-	/// @name Spatial virtuals
-	/// @{
-
-	/// Override Spatial::getSpatialOrigin
-	const Vec3& getSpatialOrigin() const
-	{
-		return getWorldTransform().getOrigin();
-	}
-	/// @}
-
 	void lookAtPoint(const Vec3& point);
 
 protected:
-	/// Calculate the @a viewMat. The view matrix is the inverse world 
-	/// transformation
-	void updateViewMatrix()
-	{
-		viewMat = Mat4(getWorldTransform().getInverse());
-	}
+	/// Called when something changes in the frustum
+	void frustumUpdate();
 
-	void updateViewProjectionMatrix()
-	{
-		viewProjectionMat = projectionMat * viewMat;
-	}
+	/// Called when the world transform got updated
+	void moveUpdate(MoveComponent& move);
 
 private:
 	CameraType type;
@@ -124,37 +107,8 @@ public:
 	}
 	/// @}
 
-	/// @name Movable virtuals
-	/// @{
-
-	/// Overrides Movable::moveUpdate(). This does:
-	/// @li Update view matrix
-	/// @li Update view-projection matrix
-	/// @li Move the frustum
-	void moveUpdate()
-	{
-		updateViewMatrix();
-		updateViewProjectionMatrix();
-		frustum.setTransform(getWorldTransform());
-		FrustumComponent::setOrigin(getWorldTransform().getOrigin());
-		FrustumComponent::markForUpdate();
-
-		SpatialComponent::markForUpdate();
-	}
-	/// @}
-
 private:
 	PerspectiveFrustum frustum;
-
-	/// Called when something changes in the frustum
-	void frustumUpdate()
-	{
-		projectionMat = frustum.calculateProjectionMatrix();
-		updateViewProjectionMatrix();
-		FrustumComponent::markForUpdate();
-
-		SpatialComponent::markForUpdate();
-	}
 };
 
 /// Orthographic camera
@@ -205,27 +159,8 @@ public:
 	}
 	/// @}
 
-	/// @name Movable virtuals
-	/// @{
-
-	/// Overrides Movable::moveUpdate(). This does:
-	/// @li Update view matrix
-	/// @li Update view-projection matrix
-	/// @li Update frustum
-	void moveUpdate()
-	{
-		ANKI_ASSERT(0 && "TODO");
-	}
-	/// @}
-
 private:
 	OrthographicFrustum frustum;
-
-	/// Called when something changes in the frustum
-	void frustumUpdate()
-	{
-		ANKI_ASSERT(0 && "TODO");
-	}
 };
 /// @}
 

+ 35 - 2
include/anki/scene/SceneNode.h

@@ -182,9 +182,24 @@ public:
 		}
 	}
 
-	/// Get a pointer to the first component of the requested type
+	/// Try geting a pointer to the first component of the requested type
+	template<typename Component>
+	Component* tryGetComponent()
+	{
+		I id = SceneComponent::getVariadicTypeId<Component>();
+		for(auto comp : components)
+		{
+			if(comp->getVisitableTypeId() == id)
+			{
+				return comp;
+			}
+		}
+		return nullptr;
+	}
+
+	/// Try geting a pointer to the first component of the requested type
 	template<typename Component>
-	Component* getComponent()
+	const Component* tryGetComponent() const
 	{
 		I id = SceneComponent::getVariadicTypeId<Component>();
 		for(auto comp : components)
@@ -197,6 +212,24 @@ public:
 		return nullptr;
 	}
 
+	/// Get a pointer to the first component of the requested type
+	template<typename Component>
+	Component& getComponent()
+	{
+		Component* out = tryGetComponent<Component>();
+		ANKI_ASSERT(out != nullptr);
+		return *out;
+	}
+
+	/// Get a pointer to the first component of the requested type
+	template<typename Component>
+	const Component& getComponent() const
+	{
+		Component* out = tryGetComponent<Component>();
+		ANKI_ASSERT(out != nullptr);
+		return *out;
+	}
+
 protected:
 	struct
 	{

+ 38 - 8
src/scene/Camera.cpp

@@ -9,7 +9,7 @@ namespace anki {
 //==============================================================================
 Camera::Camera(
 	const char* name, SceneGraph* scene, // SceneNode
-	Frustum* frustum, // Spatial & Frustumable
+	Frustum* frustum, // SpatialComponent & FrustumComponent
 	CameraType type_) 
 	:	SceneNode(name, scene),
 		MoveComponent(this),
@@ -17,13 +17,10 @@ Camera::Camera(
 		FrustumComponent(frustum),
 		type(type_)
 {
-	sceneNodeProtected.moveC = this;
-	sceneNodeProtected.spatialC = this;
-	sceneNodeProtected.frustumC = this;
-
 	// Init components
 	newComponent<MoveComponent>(this);
 	newComponent<SpatialComponent>(this, frustum);
+	newComponent<FrustumComponent>(this, frustum);
 }
 
 //==============================================================================
@@ -33,14 +30,47 @@ Camera::~Camera()
 //==============================================================================
 void Camera::lookAtPoint(const Vec3& point)
 {
+	MoveComponent& move = getComponent<MoveComponent>();
+
 	const Vec3& j = Vec3(0.0, 1.0, 0.0);
-	Vec3 vdir = (point - getLocalTransform().getOrigin()).getNormalized();
+	Vec3 vdir = (point - move.getLocalTransform().getOrigin()).getNormalized();
 	Vec3 vup = j - vdir * j.dot(vdir);
 	Vec3 vside = vdir.cross(vup);
 
-	Mat3 rot = getLocalTransform().getRotation();
+	Mat3 rot = move.getLocalTransform().getRotation();
 	rot.setColumns(vside, vup, -vdir);
-	setLocalRotation(rot);
+	move.setLocalRotation(rot);
+}
+
+//==============================================================================
+void Camera::frustumUpdate()
+{
+	// Frustum
+	FrustumComponent& fr = getComponent<FrustumComponent>();
+	fr.setProjectionMatrix(fr.getFrustum().calculateProjectionMatrix());
+	fr.setViewProjectionMatrix(fr.getProjectionMatrix() * fr.getViewMatrix());
+	fr.markForUpdate();
+
+	// Spatial
+	SpatialComponent& sp = getComponent<SpatialComponent>();
+	sp.markForUpdate();
+}
+
+//==============================================================================
+void Camera::moveUpdate(MoveComponent& move)
+{
+	// Frustum
+	FrustumComponent& fr = getComponent<FrustumComponent>();
+	fr.setViewMatrix(move.getWorldTransform().getInverse());
+	fr.setViewProjectionMatrix(fr.getProjectionMatrix() * fr.getViewMatrix());
+	fr.setOrigin(move.getWorldTransform().getOrigin());
+	fr.markForUpdate();
+	fr.getFrustum().setTransform(move.getWorldTransform());
+
+	// Spatial
+	SpatialComponent& sp = getComponent<SpatialComponent>();
+	sp.setOrigin(move.getWorldTransform().getOrigin());
+	sp.markForUpdate();
 }
 
 //==============================================================================