Panagiotis Christopoulos Charitos преди 14 години
родител
ревизия
c5a57f87d5

+ 2 - 2
src/Resources/AttributeShaderProgramVariable.h

@@ -4,7 +4,7 @@
 #include "ShaderProgramVariable.h"
 
 
-/// Attribute shader variable
+/// Attribute shader program variable
 class AttributeShaderProgramVariable: public ShaderProgramVariable
 {
 	public:
@@ -17,7 +17,7 @@ class AttributeShaderProgramVariable: public ShaderProgramVariable
 inline AttributeShaderProgramVariable::AttributeShaderProgramVariable(
 	int loc_, const char* name_,
 	GLenum glDataType_, const ShaderProgram& fatherSProg_)
-:	ShaderProgramVariable(loc_, name_, glDataType_, SVT_ATTRIBUTE, fatherSProg_)
+:	ShaderProgramVariable(loc_, name_, glDataType_, ATTRIBUTE, fatherSProg_)
 {}
 
 

+ 26 - 39
src/Resources/Material.cpp

@@ -50,7 +50,8 @@ Material::Material()
 	castsShadowFlag = true;
 
 	// Reset tha array
-	std::fill(buildinsArr.begin(), buildinsArr.end(), NULL);
+	std::fill(buildinsArr.begin(), buildinsArr.end(),
+		static_cast<BuildinMaterialVariable*>(NULL));
 }
 
 
@@ -232,33 +233,36 @@ void Material::populateVariables(const boost::property_tree::ptree& pt)
 	//
 	// Get all names of all shader prog vars. Dont duplicate
 	//
-	std::map<std::string, bool> allVarNames;
+	std::map<std::string, GLenum> allVarNames;
 
 	BOOST_FOREACH(const RsrcPtr<ShaderProgram>& sProg, sProgs)
 	{
-		BOOST_FOREACH(const ShaderProgramVariable)
+		BOOST_FOREACH(const ShaderProgramVariable& v, sProg->getVariables())
+		{
+			allVarNames[v.getName()] = v.getGlDataType();
+		}
 	}
 
+	//
 	// Iterate shader program variables
-	BOOST_FOREACH(const ShaderProgramVariable& sv,
-		cpShaderProg->populateVariables())
+	//
+	MaterialVariable::ShaderPrograms sProgs_;
+	for(uint i = 0; i < MaterialVariable::PASS_TYPES_NUM; i++)
 	{
-		const char* svName = sv.getName().c_str();
-
-		// Get (if exists) the depth pass shader variable
-		const ShaderProgramVariable* dpSv = NULL;
-		if(dpShaderProg->variableExists(svName))
-		{
-			dpSv = &dpShaderProg->getVariable(svName);
-		}
+		sProgs_[i] = sProgs[i].get();
+	}
 
-		MaterialVariable* mv = NULL;
+	std::map<std::string, GLenum>::const_iterator it = allVarNames.begin();
+	for(; it != allVarNames.end(); it++)
+	{
+		const char* svName = it->first.c_str();
+		GLenum dataType = it->second;
 
 		// Buildin?
 		if(BuildinMaterialVariable::isBuildin(svName))
 		{
 			BuildinMaterialVariable* v =
-				new BuildinMaterialVariable(&sv, dpSv, svName);
+				new BuildinMaterialVariable(svName, sProgs_);
 
 			mtlVars.push_back(v);
 			buildinsArr[v->getVariableEnum()] = v;
@@ -266,30 +270,13 @@ void Material::populateVariables(const boost::property_tree::ptree& pt)
 		// User defined
 		else
 		{
-			// Get uniforms
-			if(sv.getType() != ShaderProgramVariable::SVT_UNIFORM)
-			{
-				throw EXCEPTION("Variable should be uniform: " + sv.getName());
-			}
-
-			const UniformShaderProgramVariable* uniC =
-				static_cast<const UniformShaderProgramVariable*>(&sv);
-
-			const UniformShaderProgramVariable* uniD = NULL;
-
-			if(dpSv)
-			{
-				const UniformShaderProgramVariable* uniD =
-					static_cast<const UniformShaderProgramVariable*>(dpSv);
-			}
-
 			// Find the ptree that contains the value
 			const ptree* valuePt = NULL;
 			BOOST_FOREACH(const ptree::value_type& v, pt)
 			{
 				if(v.first != "input")
 				{
-					throw EXCEPTION("Error parsing the property_tree");
+					throw EXCEPTION("Expecting <input> and not: " + v.first);
 				}
 
 				if(v.second.get<std::string>("name") == svName)
@@ -307,31 +294,31 @@ void Material::populateVariables(const boost::property_tree::ptree& pt)
 
 			UserMaterialVariable* v = NULL;
 			// Get the value
-			switch(sv.getGlDataType())
+			switch(dataType)
 			{
 				// sampler2D
 				case GL_SAMPLER_2D:
-					v = new UserMaterialVariable(uniC, uniD,
+					v = new UserMaterialVariable(svName, sProgs_,
 						valuePt->get<std::string>("sampler2D").c_str());
 					break;
 				// float
 				case GL_FLOAT:
-					v = new UserMaterialVariable(uniC, uniD,
+					v = new UserMaterialVariable(svName, sProgs_,
 						PropertyTree::getFloat(*valuePt));
 					break;
 				// vec2
 				case GL_FLOAT_VEC2:
-					v = new UserMaterialVariable(uniC, uniD,
+					v = new UserMaterialVariable(svName, sProgs_,
 						PropertyTree::getVec2(*valuePt));
 					break;
 				// vec3
 				case GL_FLOAT_VEC3:
-					v = new UserMaterialVariable(uniC, uniD,
+					v = new UserMaterialVariable(svName, sProgs_,
 						PropertyTree::getVec3(*valuePt));
 					break;
 				// vec4
 				case GL_FLOAT_VEC4:
-					v = new UserMaterialVariable(uniC, uniD,
+					v = new UserMaterialVariable(svName, sProgs_,
 						PropertyTree::getVec4(*valuePt));
 					break;
 				// default is error

+ 31 - 32
src/Resources/Material.h

@@ -24,16 +24,22 @@ class Scanner;
 /// Contains a few properties that other classes may use
 struct MaterialProperties
 {
-	/// Used in depth passes of shadowmapping and not in other depth passes
-	/// like EarlyZ
-	bool castsShadowFlag;
-	/// The entities with blending are being rendered in blending stage and
-	/// those without in material stage
-	bool renderInBlendingStageFlag;
-	int blendingSfactor; ///< Default GL_ONE
-	int blendingDfactor; ///< Default GL_ZERO
-	bool depthTesting;
-	bool wireframe;
+	public:
+		/// Check if blending is enabled
+		bool isBlendingEnabled() const
+			{return blendingSfactor != GL_ONE || blendingDfactor != GL_ZERO;}
+
+	protected:
+		/// Used in depth passes of shadowmapping and not in other depth passes
+		/// like EarlyZ
+		bool castsShadowFlag;
+		/// The entities with blending are being rendered in blending stage and
+		/// those without in material stage
+		bool renderInBlendingStageFlag;
+		int blendingSfactor; ///< Default GL_ONE
+		int blendingDfactor; ///< Default GL_ZERO
+		bool depthTesting;
+		bool wireframe;
 };
 
 
@@ -108,10 +114,11 @@ class Material: private MaterialProperties
 
 		typedef boost::ptr_vector<MaterialVariable> VarsContainer;
 
-		typedef boost::unordered_map<BuildinMaterialVariable::BuildinVariable,
+		typedef boost::unordered_map<BuildinMaterialVariable::BuildinEnum,
 			BuildinMaterialVariable*> BuildinEnumToBuildinHashMap;
 
-		typedef Vec<RsrcPtr<ShaderProgram> > ShaderProgams;
+		typedef boost::array<BuildinMaterialVariable*,
+			BuildinMaterialVariable::BUILDINS_NUM> BuildinsArr;
 
 		//======================================================================
 		// Methods                                                             =
@@ -134,31 +141,30 @@ class Material: private MaterialProperties
 		/// Access the base class just for copying in other classes
 		GETTER_R(MaterialProperties, *this, accessMaterialPropertiesBaseClass)
 
-		const ShaderProgram& getColorPassShaderProgram() const
-			{return *cpShaderProg;}
-
-		const ShaderProgram& getDepthPassShaderProgram() const
-			{return *dpShaderProg;}
+		const ShaderProgram& getShaderProgram(
+			MaterialVariable::PassType p) const {return *sProgs[p];}
 
 		// Variable accessors
 		GETTER_R(VarsContainer, mtlVars, getVariables)
 		GETTER_R(Vec<UserMaterialVariable*>, userMtlVars, getUserVariables)
 		const BuildinMaterialVariable& getBuildinVariable(
-			BuildinMaterialVariable::BuildinVariable e) const;
+			BuildinMaterialVariable::BuildinEnum e) const;
 		/// @}
 
-		/// Return false if blendingSfactor is equal to GL_ONE and
-		/// blendingDfactor to GL_ZERO
-		bool isBlendingEnabled() const;
-
 		/// Load a material file
 		void load(const char* filename);
 
 		/// Check if a buildin variable exists
-		bool buildinVariableExits(BuildinMaterialVariable::BuildinVariable e)
+		bool buildinVariableExits(BuildinMaterialVariable::BuildinEnum e)
 			const {return buildinsArr[e] != NULL;}
 
 	private:
+		//======================================================================
+		// Nested                                                              =
+		//======================================================================
+
+		typedef Vec<RsrcPtr<ShaderProgram> > ShaderPrograms;
+
 		//======================================================================
 		// Members                                                             =
 		//======================================================================
@@ -169,8 +175,7 @@ class Material: private MaterialProperties
 		/// All the material variables. Both buildins and user
 		VarsContainer mtlVars;
 
-		boost::array<BuildinMaterialVariable*, BuildinMaterialVariable::BV_NUM>
-			buildinsArr; ///< To find. Initialize to int
+		BuildinsArr buildinsArr; ///< To find. Initialize to int
 
 		Vec<UserMaterialVariable*> userMtlVars; ///< To iterate
 
@@ -192,14 +197,8 @@ class Material: private MaterialProperties
 };
 
 
-inline bool Material::isBlendingEnabled() const
-{
-	return blendingSfactor != GL_ONE || blendingDfactor != GL_ZERO;
-}
-
-
 inline const BuildinMaterialVariable& Material::getBuildinVariable(
-	BuildinMaterialVariable::BuildinVariable e) const
+	BuildinMaterialVariable::BuildinEnum e) const
 {
 	ASSERT(buildinVariableExits(e));
 	return *buildinsArr[e];

+ 1 - 1
src/Resources/MaterialShaderProgramCreator.h

@@ -113,7 +113,7 @@ class MaterialShaderProgramCreator
 		/// Used by parseShaderFileForFunctionDefinitions to skip preprocessor
 		/// definitions. Takes into account the backslashes. For example for
 		/// @code
-		/// #define lala \
+		/// #define lala \\
 		/// 	10
 		/// @endcode
 		/// it skips from define to 10

+ 4 - 1
src/Resources/MaterialVariable.h

@@ -10,7 +10,6 @@
 
 
 class ShaderProgram;
-class ShaderProgramVariable;
 
 
 /// XXX
@@ -62,6 +61,10 @@ class MaterialVariable
 
 		/// Get the name of all the shader program variables
 		const char* getName() const {return oneSProgVar->getName().c_str();}
+
+		/// Get the type of all the shader program variables
+		ShaderProgramVariable::Type getShaderProgramVariableType() const
+			{return oneSProgVar->getType();}
 		/// @}
 
 	private:

+ 1 - 1
src/Resources/ShaderProgram.cpp

@@ -366,7 +366,7 @@ std::string ShaderProgram::getShaderInfoString() const
 	BOOST_FOREACH(const ShaderProgramVariable& var, vars)
 	{
 		ss << var.getName() << " " << var.getLoc() << " ";
-		if(var.getType() == ShaderProgramVariable::SVT_ATTRIBUTE)
+		if(var.getType() == ShaderProgramVariable::ATTRIBUTE)
 		{
 			ss << "attribute";
 		}

+ 7 - 7
src/Resources/ShaderProgramVariable.h

@@ -15,14 +15,14 @@ class ShaderProgramVariable: public boost::noncopyable
 {
 	public:
 		/// Shader var types
-		enum ShaderVarType
+		enum Type
 		{
-			SVT_ATTRIBUTE, ///< SVT_ATTRIBUTE
-			SVT_UNIFORM    ///< SVT_UNIFORM
+			ATTRIBUTE,
+			UNIFORM
 		};
 
 		ShaderProgramVariable(GLint loc_, const char* name_,
-			GLenum glDataType_, ShaderVarType type_,
+			GLenum glDataType_, Type type_,
 			const ShaderProgram& fatherSProg_);
 
 		/// @name Accessors
@@ -31,7 +31,7 @@ class ShaderProgramVariable: public boost::noncopyable
 		GETTER_R(GLint, loc, getLoc)
 		GETTER_R(std::string, name, getName)
 		GETTER_R(GLenum, glDataType, getGlDataType)
-		GETTER_R(ShaderVarType, type, getType)
+		GETTER_R(Type, type, getType)
 		/// @}
 
 	private:
@@ -40,7 +40,7 @@ class ShaderProgramVariable: public boost::noncopyable
 		/// GL_FLOAT, GL_FLOAT_VEC2 etc. See
 		/// http://www.opengl.org/sdk/docs/man/xhtml/glGetActiveUniform.xml
 		GLenum glDataType;
-		ShaderVarType type; ///< @ref SVT_ATTRIBUTE or @ref SVT_UNIFORM
+		Type type; ///< @ref ATTRIBUTE or @ref UNIFORM
 		/// We need the ShaderProg of this variable mainly for sanity checks
 		const ShaderProgram& fatherSProg;
 };
@@ -48,7 +48,7 @@ class ShaderProgramVariable: public boost::noncopyable
 
 inline ShaderProgramVariable::ShaderProgramVariable(GLint loc_,
 	const char* name_, GLenum glDataType_,
-	ShaderVarType type_, const ShaderProgram& fatherSProg_)
+	Type type_, const ShaderProgram& fatherSProg_)
 :	loc(loc_),
 	name(name_),
 	glDataType(glDataType_),

+ 1 - 1
src/Resources/UniformShaderProgramVariable.h

@@ -32,7 +32,7 @@ class UniformShaderProgramVariable: public ShaderProgramVariable
 inline UniformShaderProgramVariable::UniformShaderProgramVariable(
 	int loc_, const char* name_,
 	GLenum glDataType_, const ShaderProgram& fatherSProg_)
-:	ShaderProgramVariable(loc_, name_, glDataType_, SVT_UNIFORM, fatherSProg_)
+:	ShaderProgramVariable(loc_, name_, glDataType_, UNIFORM, fatherSProg_)
 {}
 
 

+ 5 - 20
src/Resources/UserMaterialVariable.cpp

@@ -14,6 +14,7 @@ UserMaterialVariable::UserMaterialVariable(
 :	MaterialVariable(USER, shaderProgVarName, shaderProgsArr)
 {
 	ASSERT(getGlDataType() == GL_FLOAT);
+	ASSERT(getShaderProgramVariableType() == ShaderProgramVariable::UNIFORM);
 	data = val;
 }
 
@@ -25,6 +26,7 @@ UserMaterialVariable::UserMaterialVariable(
 :	MaterialVariable(USER, shaderProgVarName, shaderProgsArr)
 {
 	ASSERT(getGlDataType() == GL_FLOAT_VEC2);
+	ASSERT(getShaderProgramVariableType() == ShaderProgramVariable::UNIFORM);
 	data = val;
 }
 
@@ -36,6 +38,7 @@ UserMaterialVariable::UserMaterialVariable(
 :	MaterialVariable(USER, shaderProgVarName, shaderProgsArr)
 {
 	ASSERT(getGlDataType() == GL_FLOAT_VEC3);
+	ASSERT(getShaderProgramVariableType() == ShaderProgramVariable::UNIFORM);
 	data = val;
 }
 
@@ -47,6 +50,7 @@ UserMaterialVariable::UserMaterialVariable(
 :	MaterialVariable(USER, shaderProgVarName, shaderProgsArr)
 {
 	ASSERT(getGlDataType() == GL_FLOAT_VEC4);
+	ASSERT(getShaderProgramVariableType() == ShaderProgramVariable::UNIFORM);
 	data = val;
 }
 
@@ -58,6 +62,7 @@ UserMaterialVariable::UserMaterialVariable(
 :	MaterialVariable(USER, shaderProgVarName, shaderProgsArr)
 {
 	ASSERT(getGlDataType() == GL_SAMPLER_2D);
+	ASSERT(getShaderProgramVariableType() == ShaderProgramVariable::UNIFORM);
 	data = RsrcPtr<Texture>();
 	boost::get<RsrcPtr<Texture> >(data).loadRsrc(texFilename);
 }
@@ -65,23 +70,3 @@ UserMaterialVariable::UserMaterialVariable(
 
 UserMaterialVariable::~UserMaterialVariable()
 {}
-
-
-//==============================================================================
-// Accessors                                                                   =
-//==============================================================================
-
-const UniformShaderProgramVariable&
-	UserMaterialVariable::getColorPassUniformShaderProgramVariable() const
-{
-	return static_cast<const UniformShaderProgramVariable&>(
-		getColorPassShaderProgramVariable());
-}
-
-
-const UniformShaderProgramVariable&
-	UserMaterialVariable::getDepthPassUniformShaderProgramVariable() const
-{
-	return static_cast<const UniformShaderProgramVariable&>(
-		getDepthPassShaderProgramVariable());
-}

+ 12 - 6
src/Resources/UserMaterialVariable.h

@@ -4,11 +4,11 @@
 #include "MaterialVariable.h"
 #include "Math/Math.h"
 #include "RsrcPtr.h"
+#include "UniformShaderProgramVariable.h"
 #include <boost/variant.hpp>
 
 
 class Texture;
-class UniformShaderProgramVariable;
 
 
 /// XXX
@@ -57,13 +57,11 @@ class UserMaterialVariable: public MaterialVariable
 		/// @exception boost::exception when you try to get the incorrect data
 		/// type
 		template<typename Type>
-		const Type& get() const {return boost::get<Type>(data);}
+		const Type& getValue() const {return boost::get<Type>(data);}
 
+		/// Uses static cast to get the uniform
 		const UniformShaderProgramVariable&
-			getColorPassUniformShaderProgramVariable() const;
-
-		const UniformShaderProgramVariable&
-			getDepthPassUniformShaderProgramVariable() const;
+			getShaderProgramUniformVariable(PassType p) const;
 		/// @}
 
 	private:
@@ -71,4 +69,12 @@ class UserMaterialVariable: public MaterialVariable
 };
 
 
+inline const UniformShaderProgramVariable&
+	UserMaterialVariable::getShaderProgramUniformVariable(PassType p) const
+{
+	return static_cast<const UniformShaderProgramVariable&>(
+		getShaderProgramVariable(p));
+}
+
+
 #endif