瀏覽代碼

New visibility shapes

Panagiotis Christopoulos Charitos 14 年之前
父節點
當前提交
177a00450e

+ 5 - 3
src/cln/CollisionShape.h

@@ -20,7 +20,8 @@ class CollisionShape
 			CST_PLANE,
 			CST_PLANE,
 			CST_SPHERE,
 			CST_SPHERE,
 			CST_AABB,
 			CST_AABB,
-			CST_OBB
+			CST_OBB,
+			CST_PROJECTION_CAMERA_FRUSTRUM
 		};
 		};
 
 
 		CollisionShape(CollisionShapeType cid_)
 		CollisionShape(CollisionShapeType cid_)
@@ -34,8 +35,9 @@ class CollisionShape
 
 
 		/// If the bounding volume intersects with the plane then the func
 		/// If the bounding volume intersects with the plane then the func
 		/// returns 0, else it returns the distance. If the distance is < 0
 		/// returns 0, else it returns the distance. If the distance is < 0
-		/// then the b.v. lies behind the plane and if > 0 then in front of it
-		virtual float testPlane(const Plane&) const = 0;
+		/// then the collision shape lies behind the plane and if > 0 then in
+		/// front of it
+		virtual float testPlane(const Plane& p) const = 0;
 
 
 	private:
 	private:
 		CollisionShapeType cid;
 		CollisionShapeType cid;

+ 1 - 0
src/cln/Obb.h

@@ -5,6 +5,7 @@
 #include "m/Math.h"
 #include "m/Math.h"
 #include <boost/array.hpp>
 #include <boost/array.hpp>
 
 
+
 /// @addtogroup Collision
 /// @addtogroup Collision
 /// @{
 /// @{
 
 

+ 68 - 0
src/cln/ProjectionCameraShape.cpp

@@ -0,0 +1,68 @@
+#include "ProjectionCameraShape.h"
+#include "LineSegment.h"
+
+
+//==============================================================================
+void ProjectionCameraShape::setAll(float fovX, float fovY,
+	float zNear, float zFar, const Transform& trf)
+{
+	eye = Vec3(0.0, 0.0, -zNear);
+
+	float x = zFar / tan((Math::PI - fovX) / 2.0);
+	float y = tan(fovY / 2.0) * zFar;
+	float z = -zFar;
+
+	dirs[0] = Vec3(x, y, z - zNear); // top right
+	dirs[1] = Vec3(-x, y, z - zNear); // top left
+	dirs[2] = Vec3(-x, -y, z - zNear); // bottom left
+	dirs[3] = Vec3(x, -y, z - zNear); // bottom right
+
+	eye.transform(trf);
+	for(uint i = 0; i < 4; i++)
+	{
+		dirs[i] = trf.getRotation() * dirs[i];
+	}
+}
+
+
+//==============================================================================
+ProjectionCameraShape ProjectionCameraShape::getTransformed(
+	const Transform& trf) const
+{
+	ProjectionCameraShape o;
+	o.eye = eye.getTransformed(trf);
+
+	for(uint i = 0; i < 4; i++)
+	{
+		o.dirs[i] = trf.getRotation() * dirs[i];
+	}
+
+	return o;
+}
+
+
+//==============================================================================
+float ProjectionCameraShape::testPlane(const Plane& p) const
+{
+	float o = 0.0;
+
+	for(uint i = 0; i < 4; i++)
+	{
+		float t = LineSegment(eye, dirs[i]).testPlane(p);
+
+		if(t == 0)
+		{
+			return 0.0;
+		}
+		else if(t < 0.0)
+		{
+			o = std::max(o, t);
+		}
+		else
+		{
+			o = std::min(o, t);
+		}
+	}
+
+	return o;
+}

+ 44 - 0
src/cln/ProjectionCameraShape.h

@@ -0,0 +1,44 @@
+#ifndef PROJECTION_CAMERA_FRUSTUM_H
+#define PROJECTION_CAMERA_FRUSTUM_H
+
+#include "CollisionShape.h"
+#include "m/Math.h"
+
+
+/// @addtogroup Collision
+/// @{
+
+/// Collision shape for the projection cameras
+class ProjectionCameraShape: public CollisionShape
+{
+	public:
+		/// Default constructor
+		ProjectionCameraShape()
+		:	CollisionShape(CST_PROJECTION_CAMERA_FRUSTRUM)
+		{}
+
+		ProjectionCameraShape(float fovX, float fovY, float zNear,
+			float zFar, const Transform& trf)
+		:	CollisionShape(CST_PROJECTION_CAMERA_FRUSTRUM)
+		{
+			setAll(fovX, fovY, zNear, zFar, trf);
+		}
+
+		/// @copydoc CollisionShape::testPlane
+		float testPlane(const Plane& p) const;
+
+		ProjectionCameraShape getTransformed(const Transform& trf) const;
+
+		/// Set all
+		void setAll(float fovX, float fovY, float zNear,
+			float zFar, const Transform& trf);
+
+
+	private:
+		Vec3 eye; ///< The eye point
+		boost::array<Vec3, 4> dirs; ///< Directions
+};
+///@}
+
+
+#endif

+ 1 - 0
src/rsrc/ResourceManager.cpp

@@ -15,6 +15,7 @@
 
 
 #include "rsrc/Image.h"
 #include "rsrc/Image.h"
 #include "core/Logger.h"
 #include "core/Logger.h"
+#include "core/Globals.h"
 
 
 
 
 //==============================================================================
 //==============================================================================

+ 5 - 2
src/scene/ModelPatchNode.h

@@ -15,9 +15,12 @@ class ModelPatchNode: public PatchNode
 		ModelPatchNode(const ModelPatch& modelPatch, ModelNode& parent);
 		ModelPatchNode(const ModelPatch& modelPatch, ModelNode& parent);
 
 
 		const Obb& getVisibilityShapeWSpace() const
 		const Obb& getVisibilityShapeWSpace() const
-			{return visibilityShapeWSpace;}
+		{
+			return visibilityShapeWSpace;
+		}
 
 
-		void init(const char*) {}
+		void init(const char*)
+		{}
 
 
 		virtual void moveUpdate(); ///< Update the visibility shape
 		virtual void moveUpdate(); ///< Update the visibility shape
 
 

+ 77 - 20
src/scene/SceneNode.h

@@ -20,8 +20,6 @@ class Scene;
 class SceneNode
 class SceneNode
 {
 {
 	public:
 	public:
-		typedef Obb VisibilityCollisionShape;
-		
 		/// Class ID for scene nodes
 		/// Class ID for scene nodes
 		enum SceneNodeType
 		enum SceneNodeType
 		{
 		{
@@ -58,36 +56,94 @@ class SceneNode
 
 
 		/// @name Accessors
 		/// @name Accessors
 		/// @{
 		/// @{
-		SceneNodeType getSceneNodeType() const {return type;}
+		SceneNodeType getSceneNodeType() const
+		{
+			return type;
+		}
 
 
-		const Scene& getScene() const {return scene;}
-		Scene& getScene() {return scene;}
+		const Scene& getScene() const
+		{
+			return scene;
+		}
+		Scene& getScene()
+		{
+			return scene;
+		}
 
 
-		const Transform& getLocalTransform() const {return lTrf;}
-		Transform& getLocalTransform() {return lTrf;}
-		void setLocalTransform(const Transform& x) {lTrf = x;}
+		const Transform& getLocalTransform() const
+		{
+			return lTrf;
+		}
+		Transform& getLocalTransform()
+		{
+			return lTrf;
+		}
+		void setLocalTransform(const Transform& x)
+		{
+			lTrf = x;
+		}
 
 
-		const Transform& getWorldTransform() const {return wTrf;}
-		Transform& getWorldTransform() {return wTrf;}
-		void setWorldTransform(const Transform& x) {wTrf = x;}
+		const Transform& getWorldTransform() const
+		{
+			return wTrf;
+		}
+		Transform& getWorldTransform()
+		{
+			return wTrf;
+		}
+		void setWorldTransform(const Transform& x)
+		{
+			wTrf = x;
+		}
 
 
-		const Transform& getPrevWorldTransform() const {return prevWTrf;}
+		const Transform& getPrevWorldTransform() const
+		{
+			return prevWTrf;
+		}
 
 
-		const SceneNode* getParent() const {return parent;}
-		SceneNode* getParent() {return parent;}
+		const SceneNode* getParent() const
+		{
+			return parent;
+		}
+		SceneNode* getParent()
+		{
+			return parent;
+		}
 
 
-		const std::string& getSceneNodeName() const {return name;}
+		const std::string& getSceneNodeName() const
+		{
+			return name;
+		}
 
 
-		ulong getFlags() const {return flags;}
+		ulong getFlags() const
+		{
+			return flags;
+		}
 
 
-		const std::vector<SceneNode*>& getChildren() const {return children;}
+		const std::vector<SceneNode*>& getChildren() const
+		{
+			return children;
+		}
+
+		/// Get the collision shape used for visibility testing
+		virtual const CollisionShape*
+			getVisibilityCollisionShapeWorldSpace() const
+		{
+			return NULL;
+		}
 		/// @}
 		/// @}
 
 
 		/// @name Flag manipulation
 		/// @name Flag manipulation
 		/// @{
 		/// @{
 		void enableFlag(SceneNodeFlags flag, bool enable = true);
 		void enableFlag(SceneNodeFlags flag, bool enable = true);
-		void disableFlag(SceneNodeFlags flag) {enableFlag(flag, false);}
-		bool isFlagEnabled(SceneNodeFlags flag) const {return flags & flag;}
+		void disableFlag(SceneNodeFlags flag)
+		{
+			enableFlag(flag, false);
+		}
+		bool isFlagEnabled(SceneNodeFlags flag) const
+		{
+			return flags & flag;
+		}
 		/// @}
 		/// @}
 
 
 		/// @name Updates
 		/// @name Updates
@@ -99,7 +155,8 @@ class SceneNode
 		virtual void frameUpdate(float prevUpdateTime, float crntTime);
 		virtual void frameUpdate(float prevUpdateTime, float crntTime);
 
 
 		/// This is called if the node moved
 		/// This is called if the node moved
-		virtual void moveUpdate() {}
+		virtual void moveUpdate()
+		{}
 		/// @}
 		/// @}
 
 
 		/// @name Mess with the local transform
 		/// @name Mess with the local transform