Jelajahi Sumber

- Adding DbgDrawer for a few SceneNode rendering
- Removing SceneNode::render and its reimplementation
- Making the materials more flexible

Panagiotis Christopoulos Charitos 15 tahun lalu
induk
melakukan
bd0d075005

File diff ditekan karena terlalu besar
+ 0 - 0
build/debug/Makefile


+ 52 - 32
src/Renderer/Dbg.cpp

@@ -3,8 +3,10 @@
 #include "App.h"
 #include "Scene.h"
 #include "Camera.h"
-#include "LightData.h"
+#include "Light.h"
 #include "RendererInitializer.h"
+#include "DbgDrawer.h"
+#include "ParticleEmitter.h"
 
 
 //======================================================================================================================
@@ -24,15 +26,11 @@ Dbg::Dbg(Renderer& r_, Object* parent):
 //======================================================================================================================
 void Dbg::drawLine(const Vec3& from, const Vec3& to, const Vec4& color)
 {
-	/*float posBuff [] = {from.x, from.y, from.z, to.x, to.y, to.z};
-
 	setColor(color);
-	setModelMat(Mat4::getIdentity());
-
-	glEnableVertexAttribArray(POSITION_ATTRIBUTE_ID);
-	glVertexAttribPointer(0, 3, GL_FLOAT, false, 0, posBuff);
-	glDrawArrays(GL_LINES, 0, 2);
-	glDisableVertexAttribArray(POSITION_ATTRIBUTE_ID);*/
+	begin();
+		pushBackVertex(from);
+		pushBackVertex(to);
+	end();
 }
 
 
@@ -48,37 +46,35 @@ void Dbg::renderGrid()
 	const float SPACE = 1.0; // space between lines
 	const int NUM = 57;  // lines number. must be odd
 
-	const float OPT = ((NUM - 1) * SPACE / 2);
+	const float GRID_HALF_SIZE = ((NUM - 1) * SPACE / 2);
+
+	setColor(col0);
 
 	begin();
 
-	for(int x = 0; x < NUM; x++)
+	for(int x = - NUM / 2 * SPACE; x < NUM / 2 * SPACE; x += SPACE)
 	{
+		setColor(col0);
+
 		// if the middle line then change color
-		if(x == NUM / 2)
+		if(x == 0)
 		{
 			setColor(col1);
 		}
-		// if the next line after the middle one change back to default col
-		else if(x == (NUM / 2) + 1)
-		{
-			setColor(col0);
-		}
 
-		float opt1 = x * SPACE;
 		// line in z
-		pushBackVertex(Vec3(opt1 - OPT, 0.0, -OPT));
-		pushBackVertex(Vec3(opt1 - OPT, 0.0, OPT));
+		pushBackVertex(Vec3(x, 0.0, -GRID_HALF_SIZE));
+		pushBackVertex(Vec3(x, 0.0, GRID_HALF_SIZE));
 
 		// if middle line change col so you can highlight the x-axis
-		if(x == NUM / 2)
+		if(x == 0)
 		{
 			setColor(col2);
 		}
 
 		// line in the x
-		pushBackVertex(Vec3(-OPT, 0.0, opt1 - OPT));
-		pushBackVertex(Vec3(OPT, 0.0, opt1 - OPT));
+		pushBackVertex(Vec3(-GRID_HALF_SIZE, 0.0, x));
+		pushBackVertex(Vec3(GRID_HALF_SIZE, 0.0, x));
 	}
 
 	// render
@@ -167,10 +163,10 @@ void Dbg::drawSphere(float radius, const Transform& trf, const Vec4& col, int co
 //======================================================================================================================
 void Dbg::drawCube(float size)
 {
-	/*Vec3 maxPos = Vec3(0.5 * size);
+	Vec3 maxPos = Vec3(0.5 * size);
 	Vec3 minPos = Vec3(-0.5 * size);
 
-	Vec3 points [] = {
+	Vec3 points[] = {
 		Vec3(maxPos.x, maxPos.y, maxPos.z),  // right top front
 		Vec3(minPos.x, maxPos.y, maxPos.z),  // left top front
 		Vec3(minPos.x, minPos.y, maxPos.z),  // left bottom front
@@ -181,12 +177,14 @@ void Dbg::drawCube(float size)
 		Vec3(maxPos.x, minPos.y, minPos.z)   // right bottom back
 	};
 
-	const ushort indeces [] = { 0, 1, 2, 3, 4, 0, 3, 7, 1, 5, 6, 2, 5, 4, 7, 6, 0, 4, 5, 1, 3, 2, 6, 7 };
+	const uint indeces[] = {0, 1, 2, 3, 4, 0, 3, 7, 1, 5, 6, 2, 5, 4, 7, 6, 0, 4, 5, 1, 3, 2, 6, 7};
 
-	glEnableVertexAttribArray(0);
-	glVertexAttribPointer(0, 3, GL_FLOAT, false, 0, &(points[0][0]));
-	glDrawElements(GL_QUADS, sizeof(indeces)/sizeof(ushort), GL_UNSIGNED_SHORT, indeces);
-	glDisableVertexAttribArray(0);*/
+	begin();
+		for(const uint* p = indeces; p != indeces + sizeof(indeces); p++)
+		{
+			pushBackVertex(points[*p]);
+		}
+	end();
 }
 
 
@@ -232,6 +230,8 @@ void Dbg::init(const RendererInitializer& initializer)
 	vao = new Vao(this);
 	const int positionAttribLoc = 0;
 	vao->attachArrayBufferVbo(*positionsVbo, positionAttribLoc, 3, GL_FLOAT, GL_FALSE, 0, NULL);
+	const int colorAttribLoc = 1;
+	vao->attachArrayBufferVbo(*colorsVbo, colorAttribLoc, 3, GL_FLOAT, GL_FALSE, 0, NULL);
 
 	//
 	// Rest
@@ -240,6 +240,7 @@ void Dbg::init(const RendererInitializer& initializer)
 	ON_GL_FAIL_THROW_EXCEPTION();
 	modelMat.setIdentity();
 	crntCol = Vec3(1.0, 0.0, 0.0);
+	dbgDrawer = new DbgDrawer(*this, this);
 }
 
 
@@ -253,8 +254,6 @@ void Dbg::run()
 		return;
 	}
 
-	const Camera& cam = r.getCamera();
-
 	fbo.bind();
 	sProg->bind();
 
@@ -265,9 +264,30 @@ void Dbg::run()
 
 	renderGrid();
 	/// @todo Uncomment
+	Vec<SceneNode*>::const_iterator it = app->getScene().nodes.begin();
+	for(; it != app->getScene().nodes.end(); ++it)
+	{
+		const SceneNode& node = *(*it);
+
+		switch(node.type)
+		{
+			case SceneNode::SNT_CAMERA:
+				dbgDrawer->drawCamera(static_cast<const Camera&>(node));
+				break;
+			case SceneNode::SNT_LIGHT:
+				dbgDrawer->drawLight(static_cast<const Light&>(node));
+				break;
+			case SceneNode::SNT_PARTICLE_EMITTER:
+				dbgDrawer->drawParticleEmitter(static_cast<const ParticleEmitter&>(node));
+				break;
+			default:
+				break;
+		}
+	}
 	/*for(uint i=0; i<app->getScene().nodes.size(); i++)
 	{
 		SceneNode* node = app->getScene().nodes[i];
+
 		if
 		(
 			(node->type == SceneNode::SNT_LIGHT && showLightsEnabled) ||

+ 2 - 0
src/Renderer/Dbg.h

@@ -11,6 +11,7 @@
 
 class Vbo;
 class Vao;
+class DbgDrawer;
 
 
 /// Debugging stage
@@ -62,6 +63,7 @@ class Dbg: public RenderingPass
 		Vbo* positionsVbo;
 		Vbo* colorsVbo;
 		Vao* vao;
+		DbgDrawer* dbgDrawer;
 };
 
 

+ 56 - 0
src/Renderer/DbgDrawer.cpp

@@ -0,0 +1,56 @@
+#include "DbgDrawer.h"
+#include "Dbg.h"
+#include "Camera.h"
+#include "Light.h"
+#include "ParticleEmitter.h"
+
+
+//======================================================================================================================
+// drawCamera                                                                                                          =
+//======================================================================================================================
+void DbgDrawer::drawCamera(const Camera& cam) const
+{
+	dbg.setColor(Vec4(1.0, 0.0, 1.0, 1.0));
+	dbg.setModelMat(Mat4(cam.getWorldTransform()));
+
+	const float camLen = 1.0;
+	float tmp0 = camLen / tan((M::PI - cam.getFovX()) * 0.5) + 0.001;
+	float tmp1 = camLen * tan(cam.getFovY() * 0.5) + 0.001;
+
+	Vec3 points[] = {
+		Vec3(0.0, 0.0, 0.0), // 0: eye point
+		Vec3(-tmp0, tmp1, -camLen), // 1: top left
+		Vec3(-tmp0, -tmp1, -camLen), // 2: bottom left
+		Vec3(tmp0, -tmp1, -camLen), // 3: bottom right
+		Vec3(tmp0, tmp1, -camLen) // 4: top right
+	};
+
+	const uint indeces[] = {0, 1, 0, 2, 0, 3, 0, 4, 1, 2, 2, 3, 3, 4, 4, 1};
+
+	dbg.begin();
+		for(uint i = 0; i < sizeof(indeces) / sizeof(uint); i++)
+		{
+			dbg.pushBackVertex(points[indeces[i]]);
+		}
+	dbg.end();
+}
+
+
+//======================================================================================================================
+// drawLight                                                                                                           =
+//======================================================================================================================
+void DbgDrawer::drawLight(const Light& light) const
+{
+	dbg.drawSphere(0.1, light.getWorldTransform(), Vec4(light.getDiffuseCol(), 1.0));
+}
+
+
+//======================================================================================================================
+// drawParticleEmitter                                                                                                 =
+//======================================================================================================================
+void DbgDrawer::drawParticleEmitter(const ParticleEmitter& pe) const
+{
+	dbg.setColor(Vec4(1.0));
+	dbg.setModelMat(Mat4(pe.getWorldTransform()));
+	dbg.drawCube();
+}

+ 40 - 0
src/Renderer/DbgDrawer.h

@@ -0,0 +1,40 @@
+#ifndef DBG_DRAWER_H
+#define DBG_DRAWER_H
+
+#include "Object.h"
+
+
+class Dbg;
+class Camera;
+class Light;
+class ParticleEmitter;
+
+
+/// This is a drawer for some scene nodes that need debug
+class DbgDrawer: public Object
+{
+	public:
+		/// Constructor
+		DbgDrawer(Dbg& dbg_, Object* parent);
+
+		/// Draw a Camera
+		virtual void drawCamera(const Camera& cam) const;
+
+		/// Draw a Light
+		virtual void drawLight(const Light& light) const;
+
+		/// Draw a ParticleEmitter
+		virtual void drawParticleEmitter(const ParticleEmitter& pe) const;
+
+	private:
+		Dbg& dbg; ///< The debug stage
+};
+
+
+inline DbgDrawer::DbgDrawer(Dbg& dbg_, Object* parent):
+	Object(parent),
+	dbg(dbg_)
+{}
+
+
+#endif

+ 49 - 1
src/Renderer/Renderer.cpp

@@ -253,7 +253,55 @@ void Renderer::setupShaderProg(const Material& mtl, const ModelNode& modelNode,
 	boost::ptr_vector<Material::UserDefinedUniVar>::const_iterator it = mtl.getUserDefinedVars().begin();
 	for(; it !=  mtl.getUserDefinedVars().end(); it++)
 	{
-		(*it).set(textureUnit);
+		const Material::UserDefinedUniVar& udv = *it;
+
+		switch(udv.getUniVar().getGlDataType())
+		{
+			// texture or FAI
+			case GL_SAMPLER_2D:
+				if(udv.getTexture() != NULL)
+				{
+					udv.getUniVar().setTexture(*udv.getTexture(), textureUnit);
+				}
+				else
+				{
+					switch(udv.getFai())
+					{
+						case Material::MS_DEPTH_FAI:
+							udv.getUniVar().setTexture(ms->getDepthFai(), textureUnit);
+							break;
+						case Material::IS_FAI:
+							udv.getUniVar().setTexture(is->getFai(), textureUnit);
+							break;
+						case Material::PPS_PRE_PASS_FAI:
+							udv.getUniVar().setTexture(pps->getPrePassFai(), textureUnit);
+							break;
+						case Material::PPS_POST_PASS_FAI:
+							udv.getUniVar().setTexture(pps->getPostPassFai(), textureUnit);
+							break;
+						default:
+							RASSERT_THROW_EXCEPTION("WTF?");
+					}
+				}
+				++textureUnit;
+				break;
+			// float
+			case GL_FLOAT:
+				udv.getUniVar().setFloat(udv.getFloat());
+				break;
+			// vec2
+			case GL_FLOAT_VEC2:
+				udv.getUniVar().setVec2(&udv.getVec2());
+				break;
+			// vec3
+			case GL_FLOAT_VEC3:
+				udv.getUniVar().setVec3(&udv.getVec3());
+				break;
+			// vec4
+			case GL_FLOAT_VEC4:
+				udv.getUniVar().setVec4(&udv.getVec4());
+				break;
+		}
 	}
 
 	//

+ 38 - 34
src/Resources/Material.cpp

@@ -23,37 +23,6 @@ Material::UserDefinedUniVar::UserDefinedUniVar(const ShaderProg::UniVar& sProgVa
 }
 
 
-//======================================================================================================================
-// set                                                                                                                 =
-//======================================================================================================================
-void Material::UserDefinedUniVar::set(uint& textureUnit) const
-{
-	switch(sProgVar.getGlDataType())
-	{
-		// texture
-		case GL_SAMPLER_2D:
-			sProgVar.setTexture(*texture, textureUnit++);
-			break;
-		// float
-		case GL_FLOAT:
-			sProgVar.setFloat(float_);
-			break;
-		// vec2
-		case GL_FLOAT_VEC2:
-			sProgVar.setVec2(&vec2);
-			break;
-		// vec3
-		case GL_FLOAT_VEC3:
-			sProgVar.setVec3(&vec3);
-			break;
-		// vec4
-		case GL_FLOAT_VEC4:
-			sProgVar.setVec4(&vec4);
-			break;
-	}
-}
-
-
 //======================================================================================================================
 // Statics                                                                                                             =
 //======================================================================================================================
@@ -312,10 +281,45 @@ void Material::load(const char* filename)
 				{
 					// texture
 					case GL_SAMPLER_2D:
-						userDefinedVars.push_back(new UserDefinedUniVar(uni,
-						                                                valueTree.get<std::string>("texture").c_str()));
+					{
+						boost::optional<std::string> texture = valueTree.get_optional<std::string>("texture");
+						boost::optional<std::string> fai = valueTree.get_optional<std::string>("fai");
+
+						if(texture)
+						{
+							userDefinedVars.push_back(new UserDefinedUniVar(uni, texture.get().c_str()));
+						}
+						else if(fai)
+						{
+							if(fai.get() == "msDepthFai")
+							{
+								userDefinedVars.push_back(new UserDefinedUniVar(uni, MS_DEPTH_FAI));
+							}
+							else if(fai.get() == "isFai")
+							{
+								userDefinedVars.push_back(new UserDefinedUniVar(uni, IS_FAI));
+							}
+							else if(fai.get() == "ppsPrePassFai")
+							{
+								userDefinedVars.push_back(new UserDefinedUniVar(uni, PPS_PRE_PASS_FAI));
+							}
+							else if(fai.get() == "ppsPostPassFai")
+							{
+								userDefinedVars.push_back(new UserDefinedUniVar(uni, PPS_POST_PASS_FAI));
+							}
+							else
+							{
+								throw EXCEPTION("incorrect FAI");
+							}
+						}
+						else
+						{
+							throw EXCEPTION("texture or fai expected");
+						}
+
 						break;
-						// float
+					}
+					// float
 					case GL_FLOAT:
 						userDefinedVars.push_back(new UserDefinedUniVar(uni, PropertyTree::getFloat(valueTree)));
 						break;

+ 22 - 3
src/Resources/Material.h

@@ -52,6 +52,7 @@
 /// 			<name>varNameInShader</name>
 /// 			<value> **
 /// 				<texture>path/tex.png</texture> |
+/// 				<fai>msDepthFai | isFai | ppsPrePassFai | ppsPostPassFai</fai> |
 /// 				<float>0.0</float> |
 /// 				<vec2><x>0.0</x><y>0.0</y></vec2> |
 /// 				<vec3><x>0.0</x><y>0.0</y><z>0.0</z></vec3> |
@@ -88,7 +89,7 @@ class Material: public Resource
 		/// Standard uniform variables. The Renderer sees what are applicable and sets them
 		/// After changing the enum update also:
 		/// - Some statics in Material.cpp
-		/// - Renderer::setupMaterial
+		/// - Renderer::setupShaderProg
 		/// - The generic material GLSL shader (maybe)
 		enum StdUniVars
 		{
@@ -118,6 +119,15 @@ class Material: public Resource
 			SUV_NUM ///< The number of standard uniform variables
 		};
 
+		/// The renderer's FAIs
+		enum Fai
+		{
+			MS_DEPTH_FAI, ///< Avoid it in MS
+			IS_FAI, ///< Use it anywhere
+			PPS_PRE_PASS_FAI, ///< Avoid it in BS
+			PPS_POST_PASS_FAI ///< Use it anywhere
+		};
+
 		/// Class for user defined material variables that will be passes in to the shader
 		class UserDefinedUniVar
 		{
@@ -125,15 +135,18 @@ class Material: public Resource
 			PROPERTY_R(Vec2, vec2, getVec2)
 			PROPERTY_R(Vec3, vec3, getVec3)
 			PROPERTY_R(Vec4, vec4, getVec4)
+			PROPERTY_R(Fai, fai, getFai)
 
 			public:
 				UserDefinedUniVar(const ShaderProg::UniVar& sProgVar, const char* texFilename);
+				UserDefinedUniVar(const ShaderProg::UniVar& sProgVar, Fai fai);
 				UserDefinedUniVar(const ShaderProg::UniVar& sProgVar, float f);
 				UserDefinedUniVar(const ShaderProg::UniVar& sProgVar, const Vec2& v);
 				UserDefinedUniVar(const ShaderProg::UniVar& sProgVar, const Vec3& v);
 				UserDefinedUniVar(const ShaderProg::UniVar& sProgVar, const Vec4& v);
 
-				void set(uint& textureUnit) const;
+				const ShaderProg::UniVar& getUniVar() const {return sProgVar;}
+				const Texture* getTexture() const {return texture.get();}
 
 			private:
 				const ShaderProg::UniVar& sProgVar;
@@ -226,8 +239,14 @@ class Material: public Resource
 // Inlines                                                                                                             =
 //======================================================================================================================
 
+inline Material::UserDefinedUniVar::UserDefinedUniVar(const ShaderProg::UniVar& sProgVar, Fai fai_):
+	fai(fai_),
+	sProgVar(sProgVar)
+{}
+
+
 inline Material::UserDefinedUniVar::UserDefinedUniVar(const ShaderProg::UniVar& sProgVar, float f):
-	float_ (f),
+	float_(f),
 	sProgVar(sProgVar)
 {}
 

+ 0 - 32
src/Scene/Camera.cpp

@@ -1,6 +1,4 @@
 #include "Camera.h"
-#include "MainRenderer.h"
-#include "App.h"
 
 
 //======================================================================================================================
@@ -17,36 +15,6 @@ void Camera::setAll(float fovx_, float fovy_, float znear_, float zfar_)
 }
 
 
-//======================================================================================================================
-// render                                                                                                              =
-//======================================================================================================================
-void Camera::render()
-{
-	app->getMainRenderer().getDbg().setColor(Vec4(1.0, 0.0, 1.0, 1.0));
-	app->getMainRenderer().getDbg().setModelMat(Mat4(getWorldTransform()));
-
-	const float camLen = 1.0;
-	float tmp0 = camLen / tan((M::PI - fovX)*0.5) + 0.001;
-	float tmp1 = camLen * tan(fovY*0.5) + 0.001;
-
-	float points [][3] = {
-		{0.0, 0.0, 0.0}, // 0: eye point
-		{-tmp0, tmp1, -camLen}, // 1: top left
-		{-tmp0, -tmp1, -camLen}, // 2: bottom left
-		{tmp0, -tmp1, -camLen}, // 3: bottom right
-		{tmp0, tmp1, -camLen}, // 4: top right
-	};
-
-	const ushort indeces [] = { 0, 1, 0, 2, 0, 3, 0, 4, 1, 2, 2, 3, 3, 4, 4, 1 };
-
-
-	glEnableVertexAttribArray(0);
-	glVertexAttribPointer(0, 3, GL_FLOAT, false, 0, points);
-	glDrawElements(GL_LINES, 16, GL_UNSIGNED_SHORT, indeces);
-	glDisableVertexAttribArray(0);
-}
-
-
 //======================================================================================================================
 // lookAtPoint                                                                                                         =
 //======================================================================================================================

+ 0 - 1
src/Scene/Camera.h

@@ -44,7 +44,6 @@ class Camera: public SceneNode
 
 		void lookAtPoint(const Vec3& point);
 		void updateTrf();
-		void render();
 		void init(const char*) {}
 
 		/// @name Frustum checks

+ 0 - 1
src/Scene/GhostNode.h

@@ -12,7 +12,6 @@ class GhostNode: public SceneNode
 		GhostNode(): SceneNode(SNT_GHOST) {}
 		~GhostNode() {}
 		void init(const char*) {}
-		void render() {}
 };
 
 

+ 0 - 12
src/Scene/Light.cpp

@@ -1,7 +1,5 @@
 #include "Light.h"
 #include "LightData.h"
-#include "App.h"
-#include "MainRenderer.h"
 
 
 //======================================================================================================================
@@ -15,13 +13,3 @@ void Light::init(const char* filename)
 	specularCol = lightData->getSpecularCol();
 	castsShadow_ = lightData->castsShadow();
 }
-
-
-//======================================================================================================================
-// render                                                                                                              =
-//======================================================================================================================
-void Light::render()
-{
-	app->getMainRenderer().getDbg().drawSphere(0.1, getWorldTransform(), Vec4(lightData->getDiffuseCol(), 1.0));
-}
-

+ 0 - 1
src/Scene/Light.h

@@ -48,7 +48,6 @@ class Light: public SceneNode
 	
 		Light(LightType type, SceneNode* parent = NULL);
 		~Light() {}
-		void render();
 		void init(const char* filename);
 };
 

+ 0 - 13
src/Scene/ParticleEmitter.cpp

@@ -209,16 +209,3 @@ void ParticleEmitter::update()
 
 	timeOfPrevUpdate = crntTime;
 }
-
-
-//======================================================================================================================
-// render                                                                                                              =
-//======================================================================================================================
-void ParticleEmitter::render()
-{
-	glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
-	app->getMainRenderer().getDbg().setColor(Vec4(1.0));
-	app->getMainRenderer().getDbg().setModelMat(Mat4(getWorldTransform()));
-	app->getMainRenderer().getDbg().drawCube();
-	glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
-}

+ 2 - 1
src/Scene/ParticleEmitter.h

@@ -2,6 +2,7 @@
 #define PARTICLEEMITTER_H
 
 #include <boost/ptr_container/ptr_vector.hpp>
+#include <btBulletCollisionCommon.h>
 #include <memory>
 #include "SceneNode.h"
 #include "GhostNode.h"
@@ -10,6 +11,7 @@
 
 
 class RigidBody;
+class btCollisionShape;
 
 
 /// The particle emitter scene node. This scene node emitts @ref ParticleEmitter:Particle particle nodes in space.
@@ -29,7 +31,6 @@ class ParticleEmitter: public SceneNode, public ParticleEmitterPropsStruct
 
 	public:
 		ParticleEmitter();
-		void render();
 		void init(const char* filename);
 
 	private:

+ 0 - 1
src/Scene/SceneNode.h

@@ -38,7 +38,6 @@ class SceneNode: public Object
 		
 		SceneNode(SceneNodeType type_, SceneNode* parent = NULL);
 		virtual ~SceneNode();
-		virtual void render() {}
 		virtual void init(const char*) = 0; ///< init using a script
 
 		/// @name Updates

+ 1 - 1
src/Scene/SpotLight.h

@@ -12,7 +12,7 @@ class SpotLight: public Light
 		~SpotLight() {}
 		void init(const char* filename);
 
-		/// @name Setters & getters
+		/// @name Accessors
 		/// @{
 		float getDistance() const {return camera->getZFar();}
 		void setDistance(float d) {camera->setZFar(d);}

Beberapa file tidak ditampilkan karena terlalu banyak file yang berubah dalam diff ini