Explorar el Código

- Making Scene singleton
- SceneDrawer

Panagiotis Christopoulos Charitos hace 15 años
padre
commit
627dd9da19

+ 0 - 3
src/Core/App.cpp

@@ -6,7 +6,6 @@
 #include <boost/filesystem.hpp>
 #include <boost/algorithm/string.hpp>
 #include "App.h"
-#include "Scene.h"
 #include "RendererInitializer.h"
 #include "MainRenderer.h"
 #include "ScriptingEngine.h"
@@ -103,8 +102,6 @@ void App::init(int argc, char* argv[])
 	"sys.stderr = StderrCatcher()\n";
 	ScriptingEngineSingleton::getInstance().execScript(commonPythonCode);
 
-	scene = new Scene(NULL);
-
 	StdinListenerSingleton::getInstance().start();
 
 	initWindow();

+ 0 - 13
src/Core/App.h

@@ -47,7 +47,6 @@ class App
 		/// @name Accessors
 		/// @{
 		bool isTerminalColoringEnabled() const;
-		Scene& getScene();
 		Camera* getActiveCam() {return activeCam;}
 		void setActiveCam(Camera* cam) {activeCam = cam;}
 		/// @}
@@ -64,11 +63,6 @@ class App
 		bool fullScreenFlag;
 		Camera* activeCam; ///< Pointer to the current camera
 
-		/// @name Pointers to serious subsystems
-		/// @{
-		Scene* scene;
-		/// @}
-
 		void parseCommandLineArgs(int argc, char* argv[]);
 
 		/// A slot to handle the messageHandler's signal
@@ -86,13 +80,6 @@ inline bool App::isTerminalColoringEnabled() const
 }
 
 
-inline Scene& App::getScene()
-{
-	RASSERT_THROW_EXCEPTION(scene == NULL);
-	return *scene;
-}
-
-
 typedef Singleton<App> AppSingleton;
 
 

+ 5 - 5
src/Main.cpp

@@ -73,7 +73,7 @@ void initPhysics()
 	init.shape = groundShape;
 	init.startTrf = groundTransform;
 
-	new RigidBody(AppSingleton::getInstance().getScene().getPhysics(), init);
+	new RigidBody(SceneSingleton::getInstance().getPhysics(), init);
 
 
 	/*{
@@ -215,7 +215,7 @@ void init()
 	/*PhyCharacter::Initializer init;
 	init.sceneNode = imp;
 	init.startTrf = Transform(Vec3(0, 40, 0), Mat3::getIdentity(), 1.0);
-	character = new PhyCharacter(AppSingleton::getInstance().getScene().getPhysics(), init);*/
+	character = new PhyCharacter(SceneSingleton::getInstance().getPhysics(), init);*/
 
 	// crate
 	/*crate = new MeshNode;
@@ -310,9 +310,9 @@ void mainLoop()
 		mover->getLocalTransform().rotation.reorthogonalize();
 
 		AppSingleton::getInstance().execStdinScpripts();
-		AppSingleton::getInstance().getScene().getPhysics().update(crntTime);
-		AppSingleton::getInstance().getScene().updateAllControllers();
-		AppSingleton::getInstance().getScene().updateAllWorldStuff();
+		SceneSingleton::getInstance().getPhysics().update(crntTime);
+		SceneSingleton::getInstance().updateAllControllers();
+		SceneSingleton::getInstance().updateAllWorldStuff();
 
 		MainRendererSingleton::getInstance().render(*AppSingleton::getInstance().getActiveCam());
 

+ 1 - 2
src/Physics/Physics.cpp

@@ -7,8 +7,7 @@
 //======================================================================================================================
 // Constructor                                                                                                         =
 //======================================================================================================================
-Physics::Physics(Object* parent):
-	Object(parent),
+Physics::Physics():
 	defaultContactProcessingThreshold(BT_LARGE_FLOAT),
 	time(0.0)
 {

+ 3 - 3
src/Physics/Physics.h

@@ -3,9 +3,9 @@
 
 #include <btBulletCollisionCommon.h>
 #include <btBulletDynamicsCommon.h>
-#include "Object.h"
 #include "BtAndAnkiConvertors.h"
 #include "PhyDbgDrawer.h"
+#include "Vec.h"
 
 
 class PhyCharacter;
@@ -15,7 +15,7 @@ class RigidBody;
 /**
  * The master container for all physics related stuff.
  */
-class Physics: public Object
+class Physics
 {
 	friend class PhyCharacter; ///< For registering and unregistering
 	friend class RigidBody;  ///< For registering and unregistering
@@ -33,7 +33,7 @@ class Physics: public Object
 		};
 
 	public:
-		Physics(Object* parent);
+		Physics();
 		void update(float crntTime);
 		void debugDraw();
 

+ 2 - 2
src/Renderer/Bs.cpp

@@ -87,8 +87,8 @@ void Bs::run()
 	glDepthMask(false);
 
 	// render the models
-	Vec<ModelNode*>::const_iterator it = AppSingleton::getInstance().getScene().modelNodes.begin();
-	for(; it != AppSingleton::getInstance().getScene().modelNodes.end(); ++it)
+	Vec<ModelNode*>::const_iterator it = SceneSingleton::getInstance().modelNodes.begin();
+	for(; it != SceneSingleton::getInstance().modelNodes.end(); ++it)
 	{
 		const ModelNode& mn = *(*it);
 		boost::ptr_vector<ModelNodePatch>::const_iterator it = mn.getModelNodePatches().begin();

+ 2 - 2
src/Renderer/Dbg.cpp

@@ -269,8 +269,8 @@ void Dbg::run()
 	setModelMat(Mat4::getIdentity());
 	renderGrid();
 
-	Vec<SceneNode*>::const_iterator it = AppSingleton::getInstance().getScene().nodes.begin();
-	for(; it != AppSingleton::getInstance().getScene().nodes.end(); ++it)
+	Vec<SceneNode*>::const_iterator it = SceneSingleton::getInstance().nodes.begin();
+	for(; it != SceneSingleton::getInstance().nodes.end(); ++it)
 	{
 		const SceneNode& node = *(*it);
 

+ 3 - 3
src/Renderer/Is.cpp

@@ -334,7 +334,7 @@ void Is::run()
 	glDisable(GL_DEPTH_TEST);
 
 	// ambient pass
-	ambientPass(AppSingleton::getInstance().getScene().getAmbientCol());
+	ambientPass(SceneSingleton::getInstance().getAmbientCol());
 
 	// light passes
 	glEnable(GL_BLEND);
@@ -345,9 +345,9 @@ void Is::run()
 	calcPlanes();
 
 	// for all lights
-	for(uint i=0; i<AppSingleton::getInstance().getScene().lights.size(); i++)
+	for(uint i=0; i<SceneSingleton::getInstance().lights.size(); i++)
 	{
-		const Light& light = *AppSingleton::getInstance().getScene().lights[i];
+		const Light& light = *SceneSingleton::getInstance().lights[i];
 		switch(light.getType())
 		{
 			case Light::LT_POINT:

+ 6 - 6
src/Renderer/Renderer.cpp

@@ -15,12 +15,12 @@
 // Constructor                                                                                                         =
 //======================================================================================================================
 Renderer::Renderer():
-	width(640),
-	height(480),
 	ms(*this),
 	is(*this),
 	pps(*this),
-	bs(*this)
+	bs(*this),
+	width(640),
+	height(480)
 {}
 
 
@@ -241,7 +241,7 @@ void Renderer::setupShaderProg(const Material& mtl, const ModelNode& modelNode,
 
 	if(mtl.getStdUniVar(Material::SUV_SCENE_AMBIENT_COLOR))
 	{
-		Vec3 col(AppSingleton::getInstance().getScene().getAmbientCol());
+		Vec3 col(SceneSingleton::getInstance().getAmbientCol());
 		mtl.getStdUniVar(Material::SUV_SCENE_AMBIENT_COLOR)->setVec3(&col);
 	}
 
@@ -352,8 +352,8 @@ void Renderer::renderModelNode(const ModelNode& modelNode, const Camera& cam, Mo
 //======================================================================================================================
 void Renderer::renderAllModelNodes(const Camera& cam, ModelNodeRenderType type) const
 {
-	Vec<ModelNode*>::const_iterator it = AppSingleton::getInstance().getScene().modelNodes.begin();
-	for(; it != AppSingleton::getInstance().getScene().modelNodes.end(); ++it)
+	Vec<ModelNode*>::const_iterator it = SceneSingleton::getInstance().modelNodes.begin();
+	for(; it != SceneSingleton::getInstance().modelNodes.end(); ++it)
 	{
 		const ModelNode& md = *(*it);
 		renderModelNode(md, cam, type);

+ 15 - 18
src/Renderer/Renderer.h

@@ -27,15 +27,6 @@ class ModelNode;
 /// It is a class and not a namespace because we may need external renderers for security cameras for example
 class Renderer
 {
-	//====================================================================================================================
-	// Properties                                                                                                        =
-	//====================================================================================================================
-	PROPERTY_R(uint, width, getWidth) ///< Width of the rendering. Dont confuse with the window width
-	PROPERTY_R(uint, height, getHeight) ///< Height of the rendering. Dont confuse with the window width
-	PROPERTY_R(float, aspectRatio, getAspectRatio) ///< Just a precalculated value
-	PROPERTY_R(Mat4, viewProjectionMat, getViewProjectionMat) ///< Precalculated in case anyone needs it
-	PROPERTY_R(uint, framesNum, getFramesNum) ///< Frame number
-
 	//====================================================================================================================
 	// Public                                                                                                            =
 	//====================================================================================================================
@@ -49,11 +40,18 @@ class Renderer
 		};
 
 		Renderer();
-
 		~Renderer() throw() {}
 
-		/// @name Setters & getters
+		/// @name Accessors
 		/// @{
+		GETTER_RW(Ms, ms, getMs)
+		GETTER_RW(Is, is, getIs)
+		GETTER_RW(Pps, pps, getPps)
+		uint getWidth() const {return width;}
+		uint getHeight() const {return height;}
+		float getAspectRatio() const {return aspectRatio;}
+		uint getFramesNum() const {return framesNum;}
+		GETTER_R(Mat4, viewProjectionMat, getViewProjectionMat);
 		const Camera& getCamera() const {return *cam;}
 		/// @}
 
@@ -65,13 +63,6 @@ class Renderer
 		/// @param cam The camera from where the rendering will be done
 		void render(Camera& cam);
 
-		/// @name Accessors
-		/// @{
-		GETTER_RW(Ms, ms, getMs)
-		GETTER_RW(Is, is, getIs)
-		GETTER_RW(Pps, pps, getPps)
-		/// @}
-
 		/// My version of gluUnproject
 		/// @param windowCoords Window screen coords
 		/// @param modelViewMat The modelview matrix
@@ -125,6 +116,9 @@ class Renderer
 		Bs bs; ///< Blending stage
 		/// @}
 
+		uint width; ///< Width of the rendering. Dont confuse with the window width
+		uint height; ///< Height of the rendering. Dont confuse with the window width
+		float aspectRatio; ///< Just a precalculated value
 		const Camera* cam; ///< Current camera
 		static int maxColorAtachments; ///< Max color attachments an FBO can accept
 
@@ -132,6 +126,9 @@ class Renderer
 	// Protected                                                                                                         =
 	//====================================================================================================================
 	private:
+		uint framesNum; ///< Frame number
+		Mat4 viewProjectionMat; ///< Precalculated in case anyone needs it
+
 		/// @name For drawing a quad into the active framebuffer
 		/// @{
 		Vbo quadPositionsVbo; ///< The VBO for quad positions

+ 30 - 1
src/Renderer/SceneDrawer.cpp

@@ -159,7 +159,7 @@ void SceneDrawer::setupShaderProg(const Material& mtl, const Transform& nodeWorl
 
 	if(mtl.getStdUniVar(Material::SUV_SCENE_AMBIENT_COLOR))
 	{
-		Vec3 col(AppSingleton::getInstance().getScene().getAmbientCol());
+		Vec3 col(SceneSingleton::getInstance().getAmbientCol());
 		mtl.getStdUniVar(Material::SUV_SCENE_AMBIENT_COLOR)->setVec3(&col);
 	}
 
@@ -223,3 +223,32 @@ void SceneDrawer::setupShaderProg(const Material& mtl, const Transform& nodeWorl
 
 	ON_GL_FAIL_THROW_EXCEPTION();
 }
+
+
+//======================================================================================================================
+// renderSceneNodePatch                                                                                                =
+//======================================================================================================================
+void SceneDrawer::renderSceneNodePatch(const SceneNodePatch& renderable, const Camera& cam, RenderingPassType rtype)
+{
+	const Material* mtl;
+	const Vao* vao;
+
+	switch(rtype)
+	{
+		case RPT_COLOR:
+			mtl = &renderable.getCpMtl();
+			vao = &renderable.getCpVao();
+			break;
+
+		case RPT_DEPTH:
+			mtl = &renderable.getDpMtl();
+			vao = &renderable.getDpVao();
+			break;
+	}
+
+	setupShaderProg(*mtl, renderable.getWorldTransform(), cam, r);
+
+	vao->bind();
+	glDrawElements(GL_TRIANGLES, renderable.getVertIdsNum(), GL_UNSIGNED_SHORT, 0);
+	vao->unbind();
+}

+ 5 - 1
src/Renderer/SceneDrawer.h

@@ -20,9 +20,13 @@ class SceneDrawer
 			RPT_DEPTH
 		};
 
+		/// The one and only contructor
+		SceneDrawer(const Renderer& r_): r(r_) {}
+
+		void renderSceneNodePatch(const SceneNodePatch& renderable, const Camera& cam, RenderingPassType rtype);
 
 	private:
-		//const Renderer& r; ///< Keep it here cause the class wants a few FAIs
+		const Renderer& r; ///< Keep it here cause the class wants a few stuff from it
 
 		/// This function:
 		/// - binds the shader program

+ 2 - 2
src/Scene/Controllers/Controller.cpp

@@ -9,7 +9,7 @@
 Controller::Controller(ControllerType type_):
 	type(type_) 
 {
-	AppSingleton::getInstance().getScene().registerController(this);
+	SceneSingleton::getInstance().registerController(this);
 }
 
 
@@ -18,5 +18,5 @@ Controller::Controller(ControllerType type_):
 //======================================================================================================================
 Controller::~Controller()
 {
-	AppSingleton::getInstance().getScene().unregisterController(this);
+	SceneSingleton::getInstance().unregisterController(this);
 }

+ 1 - 0
src/Scene/ModelNodePatch.h

@@ -31,6 +31,7 @@ class ModelNodePatch: public SceneNodePatch
 		const Material& getCpMtl() const {return modelPatchRsrc.getCpMtl();}
 		const Material& getDpMtl() const {return modelPatchRsrc.getDpMtl();}
 		const ModelPatch& getModelPatchRsrc() const {return modelPatchRsrc;}
+		uint getVertIdsNum() const {return modelPatchRsrc.getMesh().getVertIdsNum();}
 		/// @}
 
 	protected:

+ 1 - 1
src/Scene/ParticleEmitter.cpp

@@ -58,7 +58,7 @@ void ParticleEmitter::init(const char* filename)
 		init.sceneNode = particle;
 		init.group = Physics::CG_PARTICLE;
 		init.mask = Physics::CG_ALL ^ Physics::CG_PARTICLE;
-		RigidBody* body = new RigidBody(AppSingleton::getInstance().getScene().getPhysics(), init);
+		RigidBody* body = new RigidBody(SceneSingleton::getInstance().getPhysics(), init);
 
 		body->forceActivationState(DISABLE_SIMULATION);
 

+ 2 - 3
src/Scene/Scene.cpp

@@ -12,13 +12,12 @@
 //======================================================================================================================
 // Constructor                                                                                                         =
 //======================================================================================================================
-Scene::Scene(Object* parent):
-	Object(parent)
+Scene::Scene()
 {
 	ambientCol = Vec3(0.1, 0.05, 0.05)*4;
 	sunPos = Vec3(0.0, 1.0, -1.0) * 50.0;
 
-	physics = new Physics(this);
+	physics.reset(new Physics);
 }
 
 

+ 13 - 14
src/Scene/Scene.h

@@ -1,10 +1,11 @@
 #ifndef SCENE_H
 #define SCENE_H
 
-#include "Object.h"
-#include "skybox.h"
+#include <memory>
 #include "Physics.h"
 #include "Exception.h"
+#include "Properties.h"
+#include "Singleton.h"
 
 
 class SceneNode;
@@ -16,7 +17,7 @@ class ModelNode;
 
 
 /// The Scene contains all the dynamic entities
-class Scene: public Object
+class Scene
 {
 	//PROPERTY_RW(Vec3, ambientCol, setAmbientCol, getAmbientCol) ///< The global ambient color
 	PROPERTY_RW(Vec3, sunPos, getSunPos, setSunPos)
@@ -34,10 +35,9 @@ class Scene: public Object
 		Container<ParticleEmitter> particleEmitters;
 		Container<ModelNode> modelNodes;
 		Container<Controller> controllers;
-		Skybox skybox; /// @todo to be removed
 
 		// The funcs
-		Scene(Object* parent);
+		Scene();
 		~Scene() throw() {}
 
 		void registerNode(SceneNode* node); ///< Put a node in the appropriate containers
@@ -50,14 +50,14 @@ class Scene: public Object
 
 		/// @name Accessors
 		/// @{
-		Vec3& getAmbientCol() {return ambientCol;}
-		void setAmbientCol(const Vec3& col) {ambientCol = col;}
-		Physics& getPhysics();
+		GETTER_SETTER(Vec3, ambientCol, getAmbientCol, setAmbientCol)
+		Physics& getPhysics() {return *physics;}
+		const Physics& getPhysics() const {return *physics;}
 		/// @}
 
 	private:
 		Vec3 ambientCol; ///< The global ambient color
-		Physics* physics; ///< Connection with Bullet wrapper
+		std::auto_ptr<Physics> physics; ///< Connection with Bullet wrapper
 
 		/// Adds a node in a container
 		template<typename ContainerType, typename Type>
@@ -86,11 +86,10 @@ inline void Scene::eraseNode(ContainerType& container, Type* x)
 }
 
 
-inline Physics& Scene::getPhysics()
-{
-	RASSERT_THROW_EXCEPTION(physics == NULL);
-	return *physics;
-}
+//======================================================================================================================
+// Singleton                                                                                                           =
+//======================================================================================================================
+typedef Singleton<Scene> SceneSingleton;
 
 
 #endif

+ 2 - 2
src/Scene/SceneNode.cpp

@@ -16,7 +16,7 @@ void SceneNode::commonConstructorCode()
 	getWorldTransform().setIdentity();
 	getLocalTransform().setIdentity();
 
-	AppSingleton::getInstance().getScene().registerNode(this);
+	SceneSingleton::getInstance().registerNode(this);
 }
 
 
@@ -25,7 +25,7 @@ void SceneNode::commonConstructorCode()
 //======================================================================================================================
 SceneNode::~SceneNode()
 {
-	AppSingleton::getInstance().getScene().unregisterNode(this);
+	SceneSingleton::getInstance().unregisterNode(this);
 }
 
 

+ 1 - 0
src/Scene/SceneNodePatch.h

@@ -17,6 +17,7 @@ class SceneNodePatch
 
 		virtual const Vao& getCpVao() const = 0; ///< Get color pass VAO
 		virtual const Vao& getDpVao() const = 0; ///< Get depth pass VAO
+		virtual uint getVertIdsNum() const = 0;  ///< Get vert ids number for rendering
 		virtual const Material& getCpMtl() const = 0;  ///< Get color pass material
 		virtual const Material& getDpMtl() const = 0;  ///< Get depth pass material
 		const Transform& getWorldTransform() const {return father.getWorldTransform();}

+ 1 - 0
src/Scripting/BoostPythonInterfaces.cpp

@@ -15,6 +15,7 @@ BOOST_PYTHON_MODULE(Anki)
 	CALL_WRAP(LoggerSingleton);
 
 	CALL_WRAP(Scene);
+	CALL_WRAP(SceneSingleton);
 
 	CALL_WRAP(Hdr);
 	CALL_WRAP(Pps);

+ 0 - 1
src/Scripting/Core/App.bpi.cpp

@@ -7,7 +7,6 @@
 WRAP(App)
 {
 	class_<App, noncopyable>("App", no_init)
-		.def("getScene", &App::getScene, return_value_policy<reference_existing_object>())
 		.def("quit", &App::quit)
 	;
 }

+ 0 - 2
src/Scripting/Renderer/Hdr.bpi.cpp

@@ -4,8 +4,6 @@
 
 WRAP(Hdr)
 {
-	//typedef ;
-
 	class_<Hdr, noncopyable>("Hdr", no_init)
 		.add_property("blurringIterationsNum", (uint (Hdr::*)() const)(&Hdr::getBlurringIterationsNum),
 		              &Hdr::setBlurringIterationsNum)

+ 5 - 1
src/Scripting/Scene/Scene.bpi.cpp

@@ -6,6 +6,10 @@ WRAP(Scene)
 {
 	class_<Scene, noncopyable>("Scene", no_init)
 		.def("setAmbientCol", &Scene::setAmbientCol)
-		.def("getAmbientCol", &Scene::getAmbientCol, return_value_policy<reference_existing_object>())
+		.def("getAmbientCol", (const Vec3& (Scene::*)() const)(&Scene::getAmbientCol),
+		     return_value_policy<reference_existing_object>())
 	;
 }
+
+
+WRAP_SINGLETON(SceneSingleton)