Panagiotis Christopoulos Charitos 14 лет назад
Родитель
Сommit
db5225c174

+ 2 - 0
src/Resources/MaterialVariable.h

@@ -28,7 +28,9 @@ class MaterialVariable
 		/// @name Accessors
 		/// @{
 		GETTER_R_BY_VAL(Type, type, getType)
+		/// @exception If the material variable is for depth only
 		const ShaderProgramVariable& getColorPassShaderProgramVariable() const;
+		/// @exception If the material variable is for color only
 		const ShaderProgramVariable& getDepthPassShaderProgramVariable() const;
 		/// Applies to the color pass shader program
 		bool isColorPass() const {return cpSProgVar != NULL;}

+ 19 - 33
src/Resources/UserMaterialVariable.cpp

@@ -4,7 +4,7 @@
 
 
 //==============================================================================
-// Constructors                                                                =
+// Constructors & destructor                                                   =
 //==============================================================================
 
 UserMaterialVariable::UserMaterialVariable(
@@ -14,7 +14,7 @@ UserMaterialVariable::UserMaterialVariable(
 :	MaterialVariable(T_USER, cpUni, dpUni)
 {
 	ASSERT(getGlDataType() == GL_FLOAT);
-	data.scalar = val;
+	data = val;
 }
 
 
@@ -25,7 +25,7 @@ UserMaterialVariable::UserMaterialVariable(
 :	MaterialVariable(T_USER, cpUni, dpUni)
 {
 	ASSERT(getGlDataType() == GL_FLOAT_VEC2);
-	data.vec2 = val;
+	data = val;
 }
 
 
@@ -36,7 +36,7 @@ UserMaterialVariable::UserMaterialVariable(
 :	MaterialVariable(T_USER, cpUni, dpUni)
 {
 	ASSERT(getGlDataType() == GL_FLOAT_VEC3);
-	data.vec3 = val;
+	data = val;
 }
 
 
@@ -47,7 +47,7 @@ UserMaterialVariable::UserMaterialVariable(
 :	MaterialVariable(T_USER, cpUni, dpUni)
 {
 	ASSERT(getGlDataType() == GL_FLOAT_VEC4);
-	data.vec4 = val;
+	data = val;
 }
 
 
@@ -58,44 +58,30 @@ UserMaterialVariable::UserMaterialVariable(
 :	MaterialVariable(T_USER, cpUni, dpUni)
 {
 	ASSERT(getGlDataType() == GL_SAMPLER_2D);
-	data.texture.loadRsrc(texFilename);
+	data = RsrcPtr<Texture>();
+	boost::get<RsrcPtr<Texture> >(data).loadRsrc(texFilename);
 }
 
 
+UserMaterialVariable::~UserMaterialVariable()
+{}
+
+
 //==============================================================================
 // Accessors                                                                   =
 //==============================================================================
 
-float UserMaterialVariable::getFloat() const
-{
-	ASSERT(getGlDataType() == GL_FLOAT);
-	return data.scalar;
-}
-
-
-const Vec2& UserMaterialVariable::getVec2() const
+const UniformShaderProgramVariable&
+	UserMaterialVariable::getColorPassUniformShaderProgramVariable() const
 {
-	ASSERT(getGlDataType() == GL_FLOAT_VEC2);
-	return data.vec2;
+	return static_cast<const UniformShaderProgramVariable&>(
+		getColorPassShaderProgramVariable());
 }
 
 
-const Vec3& UserMaterialVariable::getVec3() const
+const UniformShaderProgramVariable&
+	UserMaterialVariable::getDepthPassUniformShaderProgramVariable() const
 {
-	ASSERT(getGlDataType() == GL_FLOAT_VEC3);
-	return data.vec3;
-}
-
-
-const Vec4& UserMaterialVariable::getVec4() const
-{
-	ASSERT(getGlDataType() == GL_FLOAT_VEC4);
-	return data.vec4;
-}
-
-
-const Texture& UserMaterialVariable::getTexture() const
-{
-	ASSERT(getGlDataType() == GL_SAMPLER_2D);
-	return *data.texture;
+	return static_cast<const UniformShaderProgramVariable&>(
+		getDepthPassShaderProgramVariable());
 }

+ 22 - 17
src/Resources/UserMaterialVariable.h

@@ -4,6 +4,7 @@
 #include "MaterialVariable.h"
 #include "Math/Math.h"
 #include "RsrcPtr.h"
+#include <boost/variant.hpp>
 
 
 class Texture;
@@ -14,7 +15,11 @@ class UniformShaderProgramVariable;
 class UserMaterialVariable: public MaterialVariable
 {
 	public:
-		/// @name Constructors
+		/// The data union
+		typedef boost::variant<float, Vec2, Vec3, Vec4, RsrcPtr<Texture> >
+			DataVariant;
+
+		/// @name Constructors & destructor
 		/// @{
 		UserMaterialVariable(const UniformShaderProgramVariable* cpUni,
 			const UniformShaderProgramVariable* dpUni, float val);
@@ -26,29 +31,29 @@ class UserMaterialVariable: public MaterialVariable
 			const UniformShaderProgramVariable* dpUni, const Vec4& val);
 		UserMaterialVariable(const UniformShaderProgramVariable* cpUni,
 			const UniformShaderProgramVariable* dpUni, const char* texFilename);
+
+		~UserMaterialVariable();
 		/// @}
 
 		/// @name Accessors
 		/// @{
-		float getFloat() const;
-		const Vec2& getVec2() const;
-		const Vec3& getVec3() const;
-		const Vec4& getVec4() const;
-		const Texture& getTexture() const;
+		const DataVariant& getDataVariant() const {return data;}
+
+		/// Get the value of the variant
+		/// @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 UniformShaderProgramVariable&
+			getColorPassUniformShaderProgramVariable() const;
+
+		const UniformShaderProgramVariable&
+			getDepthPassUniformShaderProgramVariable() const;
 		/// @}
 
 	private:
-		/// XXX
-		struct Data
-		{
-			float scalar;
-			Vec2 vec2;
-			Vec3 vec3;
-			Vec4 vec4;
-			RsrcPtr<Texture> texture;
-		};
-
-		Data data;
+		DataVariant data;
 };
 
 

+ 24 - 1
src/Scene/MaterialRuntime2.h

@@ -2,11 +2,34 @@
 #define MATERIAL_RUNTIME_2_H
 
 #include "Resources/Material.h"
+#include "Util/Accessors.h"
+#include <boost/ptr_container/ptr_vector.hpp>
 
 
-/// XXX
+class UserMaterialVariableRuntime;
+
+
+/// One layer above material resource
 class MaterialRuntime2: private MaterialProps
 {
+	public:
+		/// A type
+		typedef boost::ptr_vector<UserMaterialVariableRuntime>
+			VariablesContainer;
+
+		/// @name Constructors & destructors
+		/// @{
+		MaterialRuntime2(const Material2& mtl);
+		~MaterialRuntime2();
+		/// @}
+
+		/// @name Accessors
+		/// @{
+		GETTER_RW(VariablesContainer, vars, getUserMaterialVariablesRuntime)
+		/// @}
+
+	private:
+		VariablesContainer vars;
 };
 
 

+ 1 - 20
src/Scene/UserMaterialVariableRuntime.cpp

@@ -9,24 +9,5 @@ UserMaterialVariableRuntime::UserMaterialVariableRuntime(
 	const UserMaterialVariable& umv_)
 :	umv(umv_)
 {
-	switch(umv.getGlDataType())
-	{
-		case GL_FLOAT:
-			data.scalar = umv.getFloat();
-			break;
-		case GL_FLOAT_VEC2:
-			data.vec2 = umv.getVec2();
-			break;
-		case GL_FLOAT_VEC3:
-			data.vec3 = umv.getVec3();
-			break;
-		case GL_FLOAT_VEC4:
-			data.vec4 = umv.getVec4();
-			break;
-		case GL_SAMPLER_2D:
-			data.tex = &umv.getTexture();
-			break;
-		default:
-			ASSERT(1);
-	}
+	/// XXX
 }

+ 6 - 16
src/Scene/UserMaterialVariableRuntime.h

@@ -3,6 +3,7 @@
 
 #include "Util/Accessors.h"
 #include "Math/Math.h"
+#include <boost/variant.hpp>
 
 
 class UserMaterialVariable;
@@ -13,6 +14,10 @@ class Texture;
 class UserMaterialVariableRuntime
 {
 	public:
+		/// The data union. The Texture resource is read-only at runtime
+		typedef boost::variant<float, Vec2, Vec3, Vec4,
+			const Texture*> DataVariant;
+
 		/// Constructor
 		UserMaterialVariableRuntime(const UserMaterialVariable& umv);
 
@@ -20,26 +25,11 @@ class UserMaterialVariableRuntime
 		/// @{
 		GETTER_R(UserMaterialVariable, umv, getUserMaterialVariable)
 
-		GETTER_SETTER_BY_VAL(float, data.scalar, getFloat, setFloat)
-		GETTER_SETTER(Vec2, data.vec2, getVec2, setVec2)
-		GETTER_SETTER(Vec3, data.vec3, getVec3, setVec3)
-		GETTER_SETTER(Vec4, data.vec4, getVec4, setVec4)
-		const Texture& getTexture() const {return *data.tex;}
 		/// @}
 
 	private:
-		/// XXX
-		struct Data
-		{
-			float scalar;
-			Vec2 vec2;
-			Vec3 vec3;
-			Vec4 vec4;
-			const Texture* tex;
-		};
-
 		const UserMaterialVariable& umv; ///< Know the resource
-		Data data;
+		DataVariant data;
 };