Browse Source

Reorgranizing cameras

Panagiotis Christopoulos Charitos 14 years ago
parent
commit
56e9c33cc2

File diff suppressed because it is too large
+ 0 - 1
build/debug/Makefile


+ 2 - 2
src/Main.cpp

@@ -3,7 +3,7 @@
 #include <fstream>
 
 #include "Input.h"
-#include "Camera.h"
+#include "PerspectiveCamera.h"
 #include "Math.h"
 #include "Renderer.h"
 #include "Ui.h"
@@ -121,7 +121,7 @@ void init()
 	//Ui::init();
 
 	// camera
-	Camera* cam = new Camera(false, NULL);
+	PerspectiveCamera* cam = new PerspectiveCamera(false, NULL);
 	cam->setAll(MainRendererSingleton::getInstance().getAspectRatio()*toRad(60.0), toRad(60.0), 0.5, 200.0);
 	cam->moveLocalY(3.0);
 	cam->moveLocalZ(5.7);

+ 27 - 0
src/Renderer/Drawers/SceneDbgDrawer.cpp

@@ -4,12 +4,39 @@
 #include "Light.h"
 #include "ParticleEmitter.h"
 #include "SkinNode.h"
+#include "PerspectiveCamera.h"
 
 
 //======================================================================================================================
 // drawCamera                                                                                                          =
 //======================================================================================================================
 void SceneDbgDrawer::drawCamera(const Camera& cam) const
+{
+	switch(cam.getType())
+	{
+		case Camera::CT_PERSPECTIVE:
+		{
+			const PerspectiveCamera& pcam = static_cast<const PerspectiveCamera&>(cam);
+			drawPerspectiveCamera(pcam);
+			break;
+		}
+
+		case Camera::CT_ORTHOGRAPHIC:
+			/// @todo
+			ASSERT(false && "todo");
+			break;
+
+		case Camera::CT_NUM:
+			ASSERT(false && "WTF?");
+			break;
+	}
+}
+
+
+//======================================================================================================================
+// drawPerspectiveCamera                                                                                               =
+//======================================================================================================================
+void SceneDbgDrawer::drawPerspectiveCamera(const PerspectiveCamera& cam) const
 {
 	dbg.setColor(Vec4(1.0, 0.0, 1.0, 1.0));
 	dbg.setModelMat(Mat4(cam.getWorldTransform()));

+ 3 - 0
src/Renderer/Drawers/SceneDbgDrawer.h

@@ -7,6 +7,7 @@ class Camera;
 class Light;
 class ParticleEmitter;
 class SkinNode;
+class PerspectiveCamera;
 
 
 /// This is a drawer for some scene nodes that need debug
@@ -30,6 +31,8 @@ class SceneDbgDrawer
 
 	private:
 		Dbg& dbg; ///< The debug stage
+
+		virtual void drawPerspectiveCamera(const PerspectiveCamera& cam) const;
 };
 
 

File diff suppressed because it is too large
+ 1 - 0
src/Renderer/Smo.cpp


+ 12 - 4
src/Renderer/Smo.h

@@ -7,6 +7,7 @@
 #include "RsrcPtr.h"
 #include "Vbo.h"
 #include "Vao.h"
+#include "Camera.h"
 
 
 class PointLight;
@@ -32,13 +33,20 @@ class Smo: public RenderingPass
 
 		/// @name Camera shape stuff
 		/// @{
-		static float camPositions[];
-		Vbo cameraPositionsVbo; ///< A camera shape
-		Vbo cameraVertIndecesVbo; ///< The vertex indeces
-		Vao cameraVao; ///< And another VAO
+
+		struct CameraGeom
+		{
+			Vbo positionsVbo; ///< A camera shape
+			Vbo vertIndecesVbo; ///< The vertex indeces
+			Vao vao; ///< And another VAO
+		};
+
+		boost::array<CameraGeom, Camera::CT_NUM> camGeom;
 		/// @}
 
 		RsrcPtr<ShaderProg> sProg;
+
+		void initCamGeom();
 };
 
 

+ 10 - 91
src/Scene/Camera.cpp → src/Scene/Cameras/Camera.cpp

@@ -2,20 +2,6 @@
 #include "Camera.h"
 
 
-//======================================================================================================================
-// setAll                                                                                                              =
-//======================================================================================================================
-void Camera::setAll(float fovx_, float fovy_, float znear_, float zfar_)
-{
-	fovX = fovx_;
-	fovY = fovy_;
-	zNear = znear_;
-	zFar = zfar_;
-	calcProjectionMatrix();
-	calcLSpaceFrustumPlanes();
-}
-
-
 //======================================================================================================================
 // lookAtPoint                                                                                                         =
 //======================================================================================================================
@@ -29,32 +15,6 @@ void Camera::lookAtPoint(const Vec3& point)
 }
 
 
-//======================================================================================================================
-// calcLSpaceFrustumPlanes                                                                                             =
-//======================================================================================================================
-void Camera::calcLSpaceFrustumPlanes()
-{
-	float c, s; // cos & sine
-
-	sinCos(PI + fovX / 2, s, c);
-	// right
-	lspaceFrustumPlanes[FP_RIGHT] = Plane(Vec3(c, 0.0, s), 0.0);
-	// left
-	lspaceFrustumPlanes[FP_LEFT] = Plane(Vec3(-c, 0.0, s), 0.0);
-
-	sinCos((3 * PI - fovY) * 0.5, s, c);
-	// top
-	lspaceFrustumPlanes[FP_TOP] = Plane(Vec3(0.0, s, c), 0.0);
-	// bottom
-	lspaceFrustumPlanes[FP_BOTTOM] = Plane(Vec3(0.0, -s, c), 0.0);
-
-	// near
-	lspaceFrustumPlanes[FP_NEAR] = Plane(Vec3(0.0, 0.0, -1.0), zNear);
-	// far
-	lspaceFrustumPlanes[FP_FAR] = Plane(Vec3(0.0, 0.0, 1.0), -zFar);
-}
-
-
 //======================================================================================================================
 // updateWSpaceFrustumPlanes                                                                                           =
 //======================================================================================================================
@@ -91,40 +51,27 @@ bool Camera::insideFrustum(const CollisionShape& bvol) const
 //======================================================================================================================
 bool Camera::insideFrustum(const Camera& cam) const
 {
-	// get five points. These points are the tips of the given camera
-	boost::array<Vec3, 5> points;
-
-	// get 3 sample floats
-	float x = cam.getZFar() / tan((PI - cam.getFovX()) / 2.0);
-	float y = tan(cam.getFovY() / 2.0) * cam.getZFar();
-	float z = -cam.getZFar();
-
-	// the actual points in local space
-	points[0] = Vec3(x, y, z); // top right
-	points[1] = Vec3(-x, y, z); // top left
-	points[2] = Vec3(-x, -y, z); // bottom left
-	points[3] = Vec3(x, -y, z); // bottom right
-	points[4] = cam.getWorldTransform().getOrigin(); // eye (already in world space)
-
-	// transform them to the given camera's world space (exept the eye)
-	for(uint i = 0; i < 4; i++)
-	{
-		points[i].transform(cam.getWorldTransform());
-	}
+	const uint MAX_EXTREME_POINTS_NUM = 10;
+	Vec3 points[MAX_EXTREME_POINTS_NUM];
+	uint pointsNum;
+
+	cam.getExtremePoints(points, pointsNum);
+
+	ASSERT(pointsNum < MAX_EXTREME_POINTS_NUM);
 
 	// the collision code
 	for(uint i = 0; i < 6; i++) // for the 6 planes
 	{
-		int failed = 0;
+		uint failed = 0;
 
-		for(uint j = 0; j < 5; j++) // for the 5 points
+		for(uint j = 0; j < pointsNum; j++) // for the n points
 		{
 			if(wspaceFrustumPlanes[i].test(points[j]) < 0.0)
 			{
 				++failed;
 			}
 		}
-		if(failed == 5)
+		if(failed == pointsNum)
 		{
 			return false; // if all points are behind the plane then the cam is not in frustum
 		}
@@ -134,34 +81,6 @@ bool Camera::insideFrustum(const Camera& cam) const
 }
 
 
-//======================================================================================================================
-// calcProjectionMatrix                                                                                                =
-//======================================================================================================================
-void Camera::calcProjectionMatrix()
-{
-	float f = 1.0 / tan(fovY * 0.5); // f = cot(fovY/2)
-
-	projectionMat(0, 0) = f * fovY / fovX; // = f/aspectRatio;
-	projectionMat(0, 1) = 0.0;
-	projectionMat(0, 2) = 0.0;
-	projectionMat(0, 3) = 0.0;
-	projectionMat(1, 0) = 0.0;
-	projectionMat(1, 1) = f;
-	projectionMat(1, 2) = 0.0;
-	projectionMat(1, 3) = 0.0;
-	projectionMat(2, 0) = 0.0;
-	projectionMat(2, 1) = 0.0;
-	projectionMat(2, 2) = (zFar + zNear) / ( zNear - zFar);
-	projectionMat(2, 3) = (2.0 * zFar * zNear) / (zNear - zFar);
-	projectionMat(3, 0) = 0.0;
-	projectionMat(3, 1) = 0.0;
-	projectionMat(3, 2) = -1.0;
-	projectionMat(3, 3) = 0.0;
-
-	invProjectionMat = projectionMat.getInverse();
-}
-
-
 //======================================================================================================================
 // updateViewMatrix                                                                                                    =
 //======================================================================================================================

+ 16 - 32
src/Scene/Camera.h → src/Scene/Cameras/Camera.h

@@ -22,7 +22,8 @@ class Camera: public SceneNode
 		enum CameraType
 		{
 			CT_PERSPECTIVE,
-			CT_ORTHOGRAPHIC
+			CT_ORTHOGRAPHIC,
+			CT_NUM
 		};
 
 		enum FrustrumPlanes
@@ -36,16 +37,11 @@ class Camera: public SceneNode
 			FP_NUM
 		};
 
-		Camera(bool compoundFlag, SceneNode* parent): SceneNode(SNT_CAMERA, compoundFlag, parent) {}
-		~Camera() {}
+		Camera(CameraType camType, bool compoundFlag, SceneNode* parent);
 
 		/// @name Accessors
 		/// @{
-		float getFovX() const {return fovX;}
-		void setFovX(float fovx);
-
-		float getFovY() const {return fovY;}
-		void setFovY(float fovy);
+		GETTER_R(CameraType, type, getType)
 
 		float getZNear() const {return zNear;}
 		void setZNear(float znear);
@@ -53,8 +49,6 @@ class Camera: public SceneNode
 		float getZFar() const {return zFar;}
 		void setZFar(float zfar);
 
-		void setAll(float fovx, float fovy, float znear, float zfar);
-
 		const Mat4& getProjectionMatrix() const {return projectionMat;}
 		const Mat4& getViewMatrix() const {return viewMat;}
 
@@ -91,13 +85,8 @@ class Camera: public SceneNode
 		bool insideFrustum(const Camera& cam) const;
 		/// @}
 
-	public:
-		/// @name Angles
-		/// fovX is the angle in the y axis (imagine the cam positioned in the default OGL pos) Note that fovX > fovY
-		/// (most of the time) and aspectRatio = fovX/fovY
-		/// @{
-		float fovX, fovY;
-		/// @}
+	protected:
+		CameraType type;
 
 		float zNear, zFar;
 
@@ -127,27 +116,22 @@ class Camera: public SceneNode
 		Vec<SpotLight*> spotLights;
 		/// @}
 
-		void calcProjectionMatrix();
+		/// Calculate projectionMat and invProjectionMat
+		virtual void calcProjectionMatrix() = 0;
+		virtual void calcLSpaceFrustumPlanes() = 0;
 		void updateViewMatrix();
-		void calcLSpaceFrustumPlanes();
 		void updateWSpaceFrustumPlanes();
+
+		/// @todo
+		virtual void getExtremePoints(Vec3* pointsArr, uint& pointsNum) const = 0;
 };
 
 
-inline void Camera::setFovX(float fovx_)
-{
-	fovX = fovx_;
-	calcProjectionMatrix();
-	calcLSpaceFrustumPlanes();
-}
 
-
-inline void Camera::setFovY(float fovy_)
-{
-	fovY = fovy_;
-	calcProjectionMatrix();
-	calcLSpaceFrustumPlanes();
-}
+inline Camera::Camera(CameraType camType, bool compoundFlag, SceneNode* parent):
+	SceneNode(SNT_CAMERA, compoundFlag, parent),
+	type(camType)
+{}
 
 
 inline void Camera::setZNear(float znear_)

+ 60 - 0
src/Scene/Cameras/PerspectiveCamera.h

@@ -0,0 +1,60 @@
+#ifndef PERSPECTIVE_CAMERA_H
+#define PERSPECTIVE_CAMERA_H
+
+#include "Camera.h"
+
+
+/// @todo
+class PerspectiveCamera: public Camera
+{
+	public:
+		PerspectiveCamera(bool compoundFlag, SceneNode* parent): Camera(CT_PERSPECTIVE, compoundFlag, parent) {}
+
+		/// @name Accessors
+		/// @{
+		float getFovX() const {return fovX;}
+		void setFovX(float fovx);
+
+		float getFovY() const {return fovY;}
+		void setFovY(float fovy);
+		/// @}
+
+		void setAll(float fovx, float fovy, float znear, float zfar);
+
+		bool insideFrustum(const PerspectiveCamera& cam) const;
+
+	private:
+		/// @name Data
+		/// @{
+
+		/// fovX is the angle in the y axis (imagine the cam positioned in the default OGL pos) Note that fovX > fovY
+		/// (most of the time) and aspectRatio = fovX/fovY
+		float fovX;
+		float fovY; /// @see fovX
+		/// @}
+
+		void calcLSpaceFrustumPlanes();
+		void calcProjectionMatrix();
+
+		/// Implements Camera::getExtremePoints
+		void getExtremePoints(Vec3* pointsArr, uint& pointsNum) const;
+};
+
+
+inline void PerspectiveCamera::setFovX(float fovx_)
+{
+	fovX = fovx_;
+	calcProjectionMatrix();
+	calcLSpaceFrustumPlanes();
+}
+
+
+inline void PerspectiveCamera::setFovY(float fovy_)
+{
+	fovY = fovy_;
+	calcProjectionMatrix();
+	calcLSpaceFrustumPlanes();
+}
+
+
+#endif

+ 0 - 0
src/Scene/Light.cpp → src/Scene/Lights/Light.cpp


+ 0 - 1
src/Scene/Light.h → src/Scene/Lights/Light.h

@@ -19,7 +19,6 @@ Specular intensity of material: Sm
 
 #include "Texture.h"
 #include "SceneNode.h"
-#include "Camera.h"
 #include "RsrcPtr.h"
 #include "LightRsrc.h"
 

+ 0 - 0
src/Scene/PointLight.h → src/Scene/Lights/PointLight.h


+ 1 - 1
src/Scene/SpotLight.cpp → src/Scene/Lights/SpotLight.cpp

@@ -11,6 +11,6 @@ void SpotLight::init(const char* filename)
 	{
 		throw EXCEPTION("Light data is wrong type");
 	}
-	camera = new Camera(true, this);
+	camera = new PerspectiveCamera(true, this);
 	camera->setAll(lightData->getFovX(), lightData->getFovY(), 0.02, lightData->getDistance());
 }

+ 2 - 1
src/Scene/SpotLight.h → src/Scene/Lights/SpotLight.h

@@ -2,6 +2,7 @@
 #define SPOT_LIGHT_H
 
 #include "Light.h"
+#include "PerspectiveCamera.h"
 
 
 /// Spot light
@@ -26,7 +27,7 @@ class SpotLight: public Light
 		/// @}
 
 	private:
-		Camera* camera;
+		PerspectiveCamera* camera;
 };
 
 

+ 0 - 14
src/Scene/PerspectiveCamera.h

@@ -1,14 +0,0 @@
-#ifndef PERSPECTIVE_CAMERA_H
-#define PERSPECTIVE_CAMERA_H
-
-#include "Camera.h"
-
-
-class PerspectiveCamera: public Camera
-{
-	public:
-	private:
-};
-
-
-#endif

+ 2 - 2
src/Scripting/Scene/Camera.bpi.cpp

@@ -5,8 +5,8 @@
 WRAP(Camera)
 {
 	class_<Camera, bases<SceneNode>, noncopyable>("Camera", no_init)
-		.def("setFovX", &Camera::setFovX)
-		.def("getFovX", &Camera::getFovX)
+		/*.def("setFovX", &Camera::setFovX)
+		.def("getFovX", &Camera::getFovX)*/
 	;
 }
 

Some files were not shown because too many files changed in this diff