Browse Source

material realtime

Panagiotis Christopoulos Charitos 15 years ago
parent
commit
88a0f35dbf

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


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


+ 1 - 1
docs/manual.rst

@@ -2,7 +2,7 @@
 
 Copyright (C) 2009-2011 Panayiotis Christopoulos-Charitos
 
-http://www.ancient-ritual.com
+http://www.anki3d.org
 
 [email protected]
 

+ 2 - 5
src/Resources/Material/Material.h

@@ -70,7 +70,7 @@
 class Material
 {
 	//==================================================================================================================
-	// Nested                                                                                                          =
+	// Public                                                                                                          =
 	//==================================================================================================================
 	public:
 		/// Standard attribute variables that are acceptable inside the @ref ShaderProg
@@ -113,10 +113,7 @@ class Material
 			SUV_NUM ///< The number of standard uniform variables
 		};
 
-	//==================================================================================================================
-	// Public                                                                                                          =
-	//==================================================================================================================
-	public:
+
 		/// Initialize with default values
 		Material();
 

+ 42 - 1
src/Resources/Material/MtlUserDefinedVar.cpp

@@ -3,8 +3,49 @@
 
 
 //======================================================================================================================
-// Constructor                                                                                                         =
+// Constructors                                                                                                        =
 //======================================================================================================================
+
+MtlUserDefinedVar::MtlUserDefinedVar(const SProgUniVar& sProgVar, Fai fai_):
+	sProgVar(sProgVar)
+{
+	ASSERT(sProgVar.getGlDataType() == GL_SAMPLER_2D);
+	data = fai_;
+}
+
+
+MtlUserDefinedVar::MtlUserDefinedVar(const SProgUniVar& sProgVar, float f):
+	sProgVar(sProgVar)
+{
+	ASSERT(sProgVar.getGlDataType() == GL_FLOAT);
+	data = f;
+}
+
+
+MtlUserDefinedVar::MtlUserDefinedVar(const SProgUniVar& sProgVar, const Vec2& v):
+	sProgVar(sProgVar)
+{
+	ASSERT(sProgVar.getGlDataType() == GL_FLOAT_VEC2);
+	data = v;
+}
+
+
+MtlUserDefinedVar::MtlUserDefinedVar(const SProgUniVar& sProgVar, const Vec3& v):
+	sProgVar(sProgVar)
+{
+	ASSERT(sProgVar.getGlDataType() == GL_FLOAT_VEC3);
+	data = v;
+}
+
+
+MtlUserDefinedVar::MtlUserDefinedVar(const SProgUniVar& sProgVar, const Vec4& v):
+	sProgVar(sProgVar)
+{
+	ASSERT(sProgVar.getGlDataType() == GL_FLOAT_VEC4);
+	data = v;
+}
+
+
 MtlUserDefinedVar::MtlUserDefinedVar(const SProgUniVar& sProgVar, const char* texFilename):
 	sProgVar(sProgVar)
 {

+ 6 - 41
src/Resources/Material/MtlUserDefinedVar.h

@@ -27,12 +27,15 @@ class MtlUserDefinedVar
 		/// The data union
 		typedef boost::variant<float, Vec2, Vec3, Vec4, RsrcPtr<Texture>, Fai> DataVariant;
 
+		/// @name Contructors
+		/// @{
 		MtlUserDefinedVar(const SProgUniVar& sProgVar, const char* texFilename);
 		MtlUserDefinedVar(const SProgUniVar& sProgVar, Fai fai);
 		MtlUserDefinedVar(const SProgUniVar& sProgVar, float f);
 		MtlUserDefinedVar(const SProgUniVar& sProgVar, const Vec2& v);
 		MtlUserDefinedVar(const SProgUniVar& sProgVar, const Vec3& v);
 		MtlUserDefinedVar(const SProgUniVar& sProgVar, const Vec4& v);
+		/// @}
 
 		/// @name Accessors
 		/// @{
@@ -40,54 +43,16 @@ class MtlUserDefinedVar
 
 		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);}
 		/// @}
 
 	private:
 		DataVariant data;
-		const SProgUniVar& sProgVar; ///< Know the other resource
+		const SProgUniVar& sProgVar; ///< Know a part of the ShaderProg resource
 };
 
 
-inline MtlUserDefinedVar::MtlUserDefinedVar(const SProgUniVar& sProgVar, Fai fai_):
-	sProgVar(sProgVar)
-{
-	ASSERT(sProgVar.getGlDataType() == GL_SAMPLER_2D);
-	data = fai_;
-}
-
-
-inline MtlUserDefinedVar::MtlUserDefinedVar(const SProgUniVar& sProgVar, float f):
-	sProgVar(sProgVar)
-{
-	ASSERT(sProgVar.getGlDataType() == GL_FLOAT);
-	data = f;
-}
-
-
-inline MtlUserDefinedVar::MtlUserDefinedVar(const SProgUniVar& sProgVar, const Vec2& v):
-	sProgVar(sProgVar)
-{
-	ASSERT(sProgVar.getGlDataType() == GL_FLOAT_VEC2);
-	data = v;
-}
-
-
-inline MtlUserDefinedVar::MtlUserDefinedVar(const SProgUniVar& sProgVar, const Vec3& v):
-	sProgVar(sProgVar)
-{
-	ASSERT(sProgVar.getGlDataType() == GL_FLOAT_VEC3);
-	data = v;
-}
-
-
-inline MtlUserDefinedVar::MtlUserDefinedVar(const SProgUniVar& sProgVar, const Vec4& v):
-	sProgVar(sProgVar)
-{
-	ASSERT(sProgVar.getGlDataType() == GL_FLOAT_VEC4);
-	data = v;
-}
-
-
 #endif

+ 21 - 9
src/Resources/Mesh/MeshData.h

@@ -13,17 +13,29 @@
 /// Binary file format:
 ///
 /// @code
+/// // Header
 /// <magic:ANKIMESH>
 /// <string:meshName>
-/// <uint:vertsNum>
-/// <float:vert[0].x> <float:vert[0].y> <float:vert[0].z> ...
-/// <float:vert[vertsNum-1].x> <float:vert[vertsNum-1].y> <float:vert[vertsNum-1].z>
-/// <uint:facesNum>
-/// <uint:tri[0].vertIds[0]> <uint:tri[0].vertIds[1]> <uint:tri[0].vertIds[2]> ...
-/// <uint:tri[facesNum-1].vertIds[0]> <uint:tri[facesNum-1].vertIds[1]> <uint:tri[facesNum-1].vertIds[2]>
-/// <uint:texCoordsNum>
-/// <float:texCoord[0].x><float:texCoord[0].y> ...
-/// <float:texCoord[texCoordsNum-1].x><float:texCoord[texCoordsNum-1].y>
+///
+/// // Verts
+/// uint: verts number
+/// float: vert 0 x, float vert 0 y, float: vert 0 z
+/// ...
+///
+/// // Faces
+/// uint: faces number
+/// uint: tri 0 vert ID 0, uint: tri 0 vert ID 1, uint: tri 0 vert ID 2
+/// ...
+///
+/// // Tex coords
+/// uint: tex coords number
+/// float: tex coord for vert 0 x, float: tex coord for vert 0 y
+/// ...
+///
+/// // Bone weights
+/// uint: bone weights number (equal to verts number)
+/// uint: bones number for vert 0, uint: bone id for vert 0 and weight 0, float: weight for vert 0 and weight 0, ...
+/// ...
 /// @endcode
 class MeshData
 {

+ 15 - 8
src/Resources/ShaderProg/SProgVar.h

@@ -20,18 +20,25 @@ class SProgVar
 			SVT_UNIFORM    ///< SVT_UNIFORM
 		};
 
-	PROPERTY_R(GLint, loc, getLoc) ///< GL location
-	PROPERTY_R(std::string, name, getName) ///< The name inside the shader program
-	/// GL_FLOAT, GL_FLOAT_VEC2 etc. See http://www.opengl.org/sdk/docs/man/xhtml/glGetActiveUniform.xml
-	PROPERTY_R(GLenum, glDataType, getGlDataType)
-	PROPERTY_R(ShaderVarType, type, getType) ///< @ref SVT_ATTRIBUTE or @ref SVT_UNIFORM
-
-	public:
-		SProgVar(GLint loc_, const char* name_, GLenum glDataType_, ShaderVarType type_, const ShaderProg* fatherSProg_);
+		SProgVar(GLint loc_, const char* name_, GLenum glDataType_, ShaderVarType type_,
+		         const ShaderProg* fatherSProg_);
 		SProgVar(const SProgVar& var);
+
+		/// @name Accessors
+		/// @{
 		const ShaderProg& getFatherSProg() const {return *fatherSProg;}
+		GETTER_R(GLint, loc, getLoc)
+		GETTER_R(std::string, name, getName)
+		GETTER_R(GLenum, glDataType, getGlDataType)
+		GETTER_R(ShaderVarType, type, getType)
+		/// @}
 
 	private:
+		GLint loc; ///< GL location
+		std::string name; ///< The name inside the shader program
+		/// 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
 		const ShaderProg* fatherSProg; ///< We need the ShaderProg of this variable mainly for sanity checks
 };
 

+ 16 - 13
src/Resources/ShaderProg/ShaderProg.cpp

@@ -109,7 +109,7 @@ void ShaderProg::link() const
 void ShaderProg::getUniAndAttribVars()
 {
 	int num;
-	char name_[256];
+	boost::array<char, 256> name_;
 	GLsizei length;
 	GLint size;
 	GLenum type;
@@ -117,22 +117,23 @@ void ShaderProg::getUniAndAttribVars()
 
 	// attrib locations
 	glGetProgramiv(glId, GL_ACTIVE_ATTRIBUTES, &num);
-	attribVars.reserve(num);
-	for(int i=0; i<num; i++) // loop all attributes
+	for(int i = 0; i < num; i++) // loop all attributes
 	{
-		glGetActiveAttrib(glId, i, sizeof(name_) / sizeof(char), &length, &size, &type, name_);
+		glGetActiveAttrib(glId, i, sizeof(name_) / sizeof(char), &length, &size, &type, &name_[0]);
 		name_[length] = '\0';
 
 		// check if its FFP location
-		int loc = glGetAttribLocation(glId, name_);
+		int loc = glGetAttribLocation(glId, &name_[0]);
 		if(loc == -1) // if -1 it means that its an FFP var
 		{
-			WARNING("Shader prog: \"" << rsrcFilename << "\": You are using FFP vertex attributes (\"" << name_ << "\")");
+			WARNING("Shader prog: \"" << rsrcFilename << "\": You are using FFP vertex attributes (\"" <<
+			        &name_[0] << "\")");
 			continue;
 		}
 
-		attribVars.push_back(SProgAttribVar(loc, name_, type, this));
-		attribNameToVar[attribVars.back().getName().c_str()] = &attribVars.back();
+		SProgAttribVar* var = new SProgAttribVar(loc, &name_[0], type, this);
+		attribVars.push_back(var);
+		attribNameToVar[attribVars.back().getName().c_str()] = var;
 	}
 
 
@@ -141,19 +142,21 @@ void ShaderProg::getUniAndAttribVars()
 	uniVars.reserve(num);
 	for(int i=0; i<num; i++) // loop all uniforms
 	{
-		glGetActiveUniform(glId, i, sizeof(name_) / sizeof(char), &length, &size, &type, name_);
+		glGetActiveUniform(glId, i, sizeof(name_) / sizeof(char), &length, &size, &type, &name_[0]);
 		name_[length] = '\0';
 
 		// check if its FFP location
-		int loc = glGetUniformLocation(glId, name_);
+		int loc = glGetUniformLocation(glId, &name_[0]);
 		if(loc == -1) // if -1 it means that its an FFP var
 		{
-			WARNING("Shader prog: \"" << rsrcFilename << "\": You are using FFP vertex uniforms (\"" << name_ << "\")");
+			WARNING("Shader prog: \"" << rsrcFilename << "\": You are using FFP vertex uniforms (\"" <<
+			        &name_[0] << "\")");
 			continue;
 		}
 
-		uniVars.push_back(SProgUniVar(loc, name_, type, this));
-		uniNameToVar[uniVars.back().getName().c_str()] = &uniVars.back();
+		SProgUniVar* var = new SProgUniVar(loc, &name_[0], type, this);
+		uniVars.push_back(var);
+		uniNameToVar[uniVars.back().getName().c_str()] = var;
 	}
 }
 

+ 18 - 18
src/Resources/ShaderProg/ShaderProg.h

@@ -1,6 +1,7 @@
 #ifndef SHADER_PROG_H
 #define SHADER_PROG_H
 
+#include <boost/ptr_container/ptr_vector.hpp>
 #include <GL/glew.h>
 #include <limits>
 #include "CharPtrHashMap.h"
@@ -17,15 +18,9 @@
 /// location, OpenGL data type and if it is a uniform or an attribute var.
 class ShaderProg
 {
-	private:
-		/// Uniform variable name to variable iterator
-		typedef CharPtrHashMap<SProgUniVar*>::const_iterator NameToSProgUniVarIterator;
-		/// Attribute variable name to variable iterator
-		typedef CharPtrHashMap<SProgAttribVar*>::const_iterator NameToSProgAttribVarIterator;
-
-	//====================================================================================================================
-	// Public                                                                                                            =
-	//====================================================================================================================
+	//==================================================================================================================
+	// Public                                                                                                          =
+	//==================================================================================================================
 	public:
 		ShaderProg();
 		~ShaderProg() {/** @todo add code */}
@@ -47,10 +42,10 @@ class ShaderProg
 		static uint getCurrentProgramGlId();
 
 		/// Accessor to uniform vars vector
-		const Vec<SProgUniVar>& getUniVars() const {return uniVars;}
+		const boost::ptr_vector<SProgUniVar>& getUniVars() const {return uniVars;}
 
 		/// Accessor to attribute vars vector
-		const Vec<SProgAttribVar>& getAttribVars() const {return attribVars;}
+		const boost::ptr_vector<SProgAttribVar>& getAttribVars() const {return attribVars;}
 
 		/// Find uniform variable. On failure it throws an exception so use @ref uniVarExists to check if var exists
 		/// @param varName The name of the var
@@ -81,23 +76,28 @@ class ShaderProg
 		/// Reling the program. Used in transform feedback
 		void relink() const {link();}
 
-	//====================================================================================================================
-	// Private                                                                                                           =
-	//====================================================================================================================
+	//==================================================================================================================
+	// Private                                                                                                         =
+	//==================================================================================================================
 	private:
+		/// Uniform variable name to variable iterator
+		typedef CharPtrHashMap<SProgUniVar*>::const_iterator NameToSProgUniVarIterator;
+		/// Attribute variable name to variable iterator
+		typedef CharPtrHashMap<SProgAttribVar*>::const_iterator NameToSProgAttribVarIterator;
+
 		std::string rsrcFilename;
 		GLuint glId; ///< The OpenGL ID of the shader program
 		GLuint vertShaderGlId; ///< Vertex shader OpenGL id
 		GLuint geomShaderGlId; ///< Geometry shader OpenGL id
 		GLuint fragShaderGlId; ///< Fragment shader OpenGL id
 		static std::string stdSourceCode; ///< Shader source that is used in ALL shader programs
-		Vec<SProgUniVar> uniVars; ///< All the uniform variables
-		Vec<SProgAttribVar> attribVars; ///< All the attribute variables
+		boost::ptr_vector<SProgUniVar> uniVars; ///< All the uniform variables
+		boost::ptr_vector<SProgAttribVar> attribVars; ///< All the attribute variables
 		CharPtrHashMap<SProgUniVar*> uniNameToVar;  ///< A UnorderedMap for fast variable searching
 		CharPtrHashMap<SProgAttribVar*> attribNameToVar; ///< @see uniNameToVar
 
-		/// Query the driver to get the vars. After the linking of the shader prog is done gather all the vars in custom
-		/// containers
+		/// Query the driver to get the vars. After the linking of the shader prog is done gather all the vars in
+		/// custom containers
 		void getUniAndAttribVars();
 
 		/// Uses glBindAttribLocation for every parser attrib location

+ 0 - 25
src/Scene/MaterialRuntime.cpp

@@ -1,25 +0,0 @@
-#include "MaterialRuntime.h"
-
-
-//======================================================================================================================
-// Constructor                                                                                                         =
-//======================================================================================================================
-MtlUserDefinedVarRuntime::MtlUserDefinedVarRuntime(const MtlUserDefinedVar& rsrc_):
-	rsrc(rsrc_)
-{
-	switch(rsrc.getUniVar().getGlDataType())
-	{
-		case GL_FLOAT:
-			break;
-		case GL_FLOAT_VEC2:
-			break;
-		case GL_FLOAT_VEC3:
-			break;
-		case GL_FLOAT_VEC4:
-			break;
-		case GL_SAMPLER_2D:
-			break;
-		default:
-			ASSERT(0);
-	}
-}

+ 0 - 42
src/Scene/MaterialRuntime.h

@@ -1,42 +0,0 @@
-#ifndef MATERIAL_RUNTIME_H
-#define MATERIAL_RUNTIME_H
-
-#include "MtlUserDefinedVar.h"
-
-
-/// @todo
-class MtlUserDefinedVarRuntime
-{
-	public:
-		MtlUserDefinedVarRuntime(const MtlUserDefinedVar& rsrc);
-
-		/// @name Accessors
-		/// @{
-		const MtlUserDefinedVar& getMtlUserDefinedVar() const {return rsrc;}
-		/// @}
-
-	private:
-		struct
-		{
-			float float_;
-			Vec2 vec2;
-			Vec3 vec3;
-			Vec4 vec4;
-		} data;
-
-		const MtlUserDefinedVar& rsrc;
-};
-
-
-/// @todo
-class MaterialRuntime
-{
-	public:
-		//MaterialRuntime
-
-	private:
-
-};
-
-
-#endif

+ 16 - 0
src/Scene/MaterialRuntime/MaterialRuntime.cpp

@@ -0,0 +1,16 @@
+#include <boost/foreach.hpp>
+#include "MaterialRuntime.h"
+#include "Material.h"
+
+
+//======================================================================================================================
+//                                                                                                                     =
+//======================================================================================================================
+MaterialRuntime::MaterialRuntime(const Material& mtl_):
+	mtl(mtl_)
+{
+	BOOST_FOREACH(const MtlUserDefinedVar& udv, mtl.getUserDefinedVars())
+	{
+		userDefVars.push_back(new MtlUserDefinedVarRuntime(udv));
+	}
+}

+ 23 - 0
src/Scene/MaterialRuntime/MaterialRuntime.h

@@ -0,0 +1,23 @@
+#ifndef MATERIAL_RUNTIME_H
+#define MATERIAL_RUNTIME_H
+
+#include <boost/ptr_container/ptr_vector.hpp>
+#include "MtlUserDefinedVarRuntime.h"
+
+
+class Material;
+
+
+/// @todo
+class MaterialRuntime
+{
+	public:
+		MaterialRuntime(const Material& mtl);
+
+	private:
+		const Material& mtl; ///< The resource
+		boost::ptr_vector<MtlUserDefinedVarRuntime> userDefVars;
+};
+
+
+#endif

+ 47 - 0
src/Scene/MaterialRuntime/MtlUserDefinedVarRuntime.cpp

@@ -0,0 +1,47 @@
+#include "MtlUserDefinedVarRuntime.h"
+
+
+//======================================================================================================================
+// Visitor functors                                                                                                    =
+//======================================================================================================================
+
+void MtlUserDefinedVarRuntime::ConstructVisitor::operator()(float x) const
+{
+	udvr.data = x;
+}
+
+void MtlUserDefinedVarRuntime::ConstructVisitor::operator()(const Vec2& x) const
+{
+	udvr.data = x;
+}
+
+void MtlUserDefinedVarRuntime::ConstructVisitor::operator()(const Vec3& x) const
+{
+	udvr.data = x;
+}
+
+void MtlUserDefinedVarRuntime::ConstructVisitor::operator()(const Vec4& x) const
+{
+	udvr.data = x;
+}
+
+void MtlUserDefinedVarRuntime::ConstructVisitor::operator()(const RsrcPtr<Texture>& x) const
+{
+	udvr.data = &x;
+}
+
+void MtlUserDefinedVarRuntime::ConstructVisitor::operator()(MtlUserDefinedVar::Fai x) const
+{
+	udvr.data = x;
+}
+
+
+//======================================================================================================================
+// Constructor                                                                                                         =
+//======================================================================================================================
+MtlUserDefinedVarRuntime::MtlUserDefinedVarRuntime(const MtlUserDefinedVar& rsrc_):
+	rsrc(rsrc_)
+{
+	// Initialize the data using a visitor
+	boost::apply_visitor(ConstructVisitor(*this), rsrc.getDataVariant());
+}

+ 55 - 0
src/Scene/MaterialRuntime/MtlUserDefinedVarRuntime.h

@@ -0,0 +1,55 @@
+#ifndef MTL_USER_DEFINED_VAR_RUNTIME_H
+#define MTL_USER_DEFINED_VAR_RUNTIME_H
+
+#include "MtlUserDefinedVar.h"
+
+
+/// @todo
+class MtlUserDefinedVarRuntime
+{
+	friend class ConstructVisitor;
+
+	public:
+		/// The data union. The Texture resource is readonly at runtime
+		typedef boost::variant<float, Vec2, Vec3, Vec4, const RsrcPtr<Texture>*, MtlUserDefinedVar::Fai> DataVariant;
+
+		MtlUserDefinedVarRuntime(const MtlUserDefinedVar& rsrc);
+
+		/// @name Accessors
+		/// @{
+		const MtlUserDefinedVar& getMtlUserDefinedVar() const {return rsrc;}
+
+		/// 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);}
+
+		/// Get the value of the variant
+		/// @exception boost::exception when you try to get the incorrect data type
+		template<typename Type>
+		Type& get() {return boost::get<Type>(data);}
+		/// @}
+
+	private:
+		/// @todo
+		class ConstructVisitor: public boost::static_visitor<void>
+		{
+			public:
+				MtlUserDefinedVarRuntime& udvr;
+
+				ConstructVisitor(MtlUserDefinedVarRuntime& udvr_): udvr(udvr_) {}
+
+				void operator()(float x) const;
+				void operator()(const Vec2& x) const;
+				void operator()(const Vec3& x) const;
+				void operator()(const Vec4& x) const;
+				void operator()(const RsrcPtr<Texture>& x) const;
+				void operator()(MtlUserDefinedVar::Fai x) const;
+		};
+
+		DataVariant data;
+		const MtlUserDefinedVar& rsrc;
+};
+
+
+#endif

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