Browse Source

- Material 2

Panagiotis Christopoulos Charitos 14 năm trước cách đây
mục cha
commit
87b787c672

+ 10 - 5
license

@@ -1,11 +1,16 @@
-AnKi 3D Engine
-Copyright (c) 2009, 2011 Panagiotis Christopoulos-Charitos
+AnKi 3D Engine Project
+Copyright (c) 2009 - 2011 Panagiotis Christopoulos Charitos
 All rights reserved.
 
-AnKi 3D Engine source code is available under dual license. GPLv3 and commercial
-license. The commercial license applies in cases where this source code or parts
+AnKi 3D Engine source code is available under multiple licences. The source 
+files under the directory src/Util/Scanner are licensed under MIT license and
+the above copyright notice and this permission notice shall be included in all 
+copies or substantial portions of the software. 
+
+Every other source file is under dual license. GPLv3 and commercial license. 
+The commercial license applies in cases where this source code or parts
 of this source code will be used in software that won't be licensed under GPLv3.
-To use the commercial license contact Panagiotis Christopoulos-Charitos
+To use the commercial license contact Panagiotis Christopoulos Charitos
 
 THIS SOFTWARE IS PROVIDED BY PANAGIOTIS CHRISTOPOULOS-CHARITOS ''AS IS'' AND ANY
 EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED

+ 0 - 11
shaders/MaterialFragmentVariables.glsl

@@ -1,14 +1,3 @@
-/// @name Varyings
-/// @{
-in vec2 vTexCoords;
-#if !defined(DEPTH_PASS)
-in vec3 vNormal;
-in vec3 vTangent;
-in float vTangentW;
-in vec3 vVertPosViewSpace;
-#endif
-/// @}
-
 /// @name Fragment out
 /// @{
 #if !defined(DEPTH_PASS)

+ 24 - 8
shaders/MaterialVertex.glsl

@@ -4,9 +4,16 @@
 /// @name Attributes
 /// @{
 in vec3 position;
+
+#if defined(USING_TEX_COORDS_ATTRIB)
 in vec2 texCoords;
-#if !defined(DEPTH_PASS)
+#endif
+
+#if defined(USING_NORMAL_ATTRIB)
 in vec3 normal;
+#endif
+
+#if defined(USING_TANGENT_ATTRIB)
 in vec4 tangent;
 #endif
 /// @}
@@ -14,21 +21,26 @@ in vec4 tangent;
 /// @name Uniforms
 /// @{
 uniform mat4 modelViewProjectionMat;
-#if !defined(DEPTH_PASS)
 uniform mat3 normalMat;
 uniform mat4 modelViewMat;
-#endif
 /// @}
 
 /// @name Varyings
 /// @{
+#if defined(USING_TEX_COORDS_ATTRIB)
 out vec2 vTexCoords;
-#if !defined(DEPTH_PASS)
+#endif
+
+#if defined(USING_NORMAL_ATTRIB)
 out vec3 vNormal;
+#endif
+
+#if defined(USING_TANGENT_ATTRIB)
 out vec3 vTangent;
 out float vTangentW;
-out vec3 vVertPosViewSpace; ///< For env mapping. AKA view vector
 #endif
+
+out vec3 vVertPosViewSpace; ///< For env mapping. AKA view vector
 /// @}
 
 
@@ -38,16 +50,20 @@ out vec3 vVertPosViewSpace; ///< For env mapping. AKA view vector
 //==============================================================================
 void main()
 {
-#if !defined(DEPTH_PASS)
-	// calculate the vert pos, normal and tangent
+#if defined(USING_NORMAL_ATTRIB)
 	vNormal = normalMat * normal;
+#endif
 
+#if defined(USING_TANGENT_ATTRIB)
 	vTangent = normalMat * vec3(tangent);
 	vTangentW = tangent.w;
+#endif
 	
 	vVertPosViewSpace = vec3(modelViewMat * vec4(position, 1.0));
-#endif
 
+#if defined(USING_TEX_COORDS_ATTRIB)
 	vTexCoords = texCoords;
+#endif
+
 	gl_Position = modelViewProjectionMat * vec4(position, 1.0);
 }

+ 2 - 2
src/Resources/Material2.cpp

@@ -181,7 +181,7 @@ void Material2::parseMaterialTag(const boost::property_tree::ptree& pt)
 	INFO(dpShaderProg->getShaderInfoString());
 
 	//boost::optional<>
-	getVariables(pt.get_child("shaderProgram.ins"));
+	getVariables(pt.get_child("shaderProgram.inputs"));
 }
 
 
@@ -268,7 +268,7 @@ void Material2::getVariables(const boost::property_tree::ptree& pt)
 			const ptree* valuePt = NULL;
 			BOOST_FOREACH(const ptree::value_type& v, pt)
 			{
-				if(v.first != "in")
+				if(v.first != "input")
 				{
 					throw EXCEPTION("XXX"); // XXX
 				}

+ 6 - 5
src/Resources/Material2.h

@@ -70,8 +70,8 @@ struct MaterialProperties
 /// 			<include>file2.glsl</include>
 /// 		</includes>
 ///
-/// 		<ins>
-/// 			<in> *
+/// 		<inputs>
+/// 			<input> *
 /// 				<name>xx</name>
 /// 				<value>
 /// 					<float>0.0</float> |
@@ -80,8 +80,8 @@ struct MaterialProperties
 /// 					<vec4><x>0.0</x><y>0.0</y><z>0.0</z><w>0.0</w></vec4> |
 /// 					<sampler2D>path/to/image.tga</sampler2D>
 /// 				</value>
-/// 			</in>
-/// 		</ins>
+/// 			</input>
+/// 		</inputs>
 ///
 /// 		<operations>
 /// 			<operation>
@@ -97,7 +97,8 @@ struct MaterialProperties
 /// 	</shaderProgram>
 /// </material>
 /// @endcode
-/// *: For "in" if the value is not set then the in variable will be build in
+/// *: For if the value is not set then the in variable will be build in or
+///    standard varying
 class Material2: private MaterialProperties
 {
 	public:

+ 69 - 23
src/Resources/MaterialShaderProgramCreator.cpp

@@ -49,6 +49,16 @@ ConstCharPtrHashMap<MaterialShaderProgramCreator::ArgQualifier>::Type
 	("inout", AQ_INOUT);
 
 
+ConstCharPtrHashMap<GLenum>::Type
+	MaterialShaderProgramCreator::varyingNameToGlType =
+	boost::assign::map_list_of
+	("vTexCoords", GL_FLOAT_VEC2)
+	("vNormal", GL_FLOAT_VEC3)
+	("vTangent", GL_FLOAT_VEC3)
+	("vTangentW", GL_FLOAT)
+	("vVertPosViewSpace", GL_FLOAT_VEC3);
+
+
 //==============================================================================
 // Constructor                                                                 =
 //==============================================================================
@@ -312,6 +322,8 @@ void MaterialShaderProgramCreator::getNextTokenAndSkipNewlines(
 void MaterialShaderProgramCreator::parseShaderProgramTag(
 	const boost::property_tree::ptree& pt)
 {
+	usingTexCoordsAttrib = usingNormalAttrib = usingTangentAttrib = false;
+
 	using namespace boost::property_tree;
 
 	srcLines.push_back(
@@ -348,19 +360,19 @@ void MaterialShaderProgramCreator::parseShaderProgramTag(
 	//
 	Vec<std::string> uniformsLines; // Store the source of the uniform vars
 
-	boost::optional<const ptree&> insPt = pt.get_child_optional("ins");
+	boost::optional<const ptree&> insPt = pt.get_child_optional("inputs");
 	if(insPt)
 	{
 		BOOST_FOREACH(const ptree::value_type& v, insPt.get())
 		{
-			if(v.first != "in")
+			if(v.first != "input")
 			{
 				throw EXCEPTION("Expected in and not: " + v.first);
 			}
 
 			const ptree& inPt = v.second;
 			std::string line;
-			parseInTag(inPt, line);
+			parseInputTag(inPt, line);
 			uniformsLines.push_back(line);
 		} // end for all ins
 
@@ -393,6 +405,21 @@ void MaterialShaderProgramCreator::parseShaderProgramTag(
 	//
 	// Create the output
 	//
+	if(usingTexCoordsAttrib)
+	{
+		srcLines.insert(srcLines.begin(), "#define USING_TEX_COORDS_ATTRIB");
+	}
+
+	if(usingNormalAttrib)
+	{
+		srcLines.insert(srcLines.begin(), "#define USING_NORMAL_ATTRIB");
+	}
+
+	if(usingTangentAttrib)
+	{
+		srcLines.insert(srcLines.begin(), "#define USING_TANGENT_ATTRIB");
+	}
+
 	BOOST_FOREACH(const std::string& line, srcLines)
 	{
 		source += line + "\n";
@@ -403,9 +430,9 @@ void MaterialShaderProgramCreator::parseShaderProgramTag(
 
 
 //==============================================================================
-// parseInTag                                                                  =
+// parseInputTag                                                               =
 //==============================================================================
-void MaterialShaderProgramCreator::parseInTag(
+void MaterialShaderProgramCreator::parseInputTag(
 	const boost::property_tree::ptree& pt, std::string& line)
 {
 	using namespace boost::property_tree;
@@ -414,25 +441,44 @@ void MaterialShaderProgramCreator::parseInTag(
 	boost::optional<const ptree&> valuePt = pt.get_child_optional("value");
 	GLenum glType;
 
-	line = "uniform ";
-
-	// Buildin
+	// Buildin or varying
 	if(!valuePt)
 	{
 		BuildinMaterialVariable::BuildinVariable tmp;
 
-		if(!BuildinMaterialVariable::isBuildin(name.c_str(), &tmp, &glType))
+		if(BuildinMaterialVariable::isBuildin(name.c_str(), &tmp, &glType))
 		{
-			throw EXCEPTION("The variable is not build in: " + name);
+			const char* glTypeTxt = glTypeToTxt.at(glType);
+			line += "uniform ";
+			line += glTypeTxt;
 		}
+		else if(varyingNameToGlType.find(name.c_str()) !=
+			varyingNameToGlType.end())
+		{
+			glType = varyingNameToGlType.at(name.c_str());
+			const char* glTypeTxt = glTypeToTxt.at(glType);
 
-		boost::unordered_map<GLenum, const char*>::const_iterator it =
-			glTypeToTxt.find(glType);
-
-		ASSERT(it != glTypeToTxt.end() &&
-			"Buildin's type is not registered");
+			line += "in ";
+			line += glTypeTxt;
 
-		line += it->second;
+			// Set a few flags
+			if(name == "vTexCoords")
+			{
+				usingTexCoordsAttrib = true;
+			}
+			else if(name == "vNormal")
+			{
+				usingNormalAttrib = true;
+			}
+			else if(name == "vTangent" || name == "vTangentW")
+			{
+				usingTangentAttrib = true;
+			}
+		}
+		else
+		{
+			throw EXCEPTION("The variable is not build-in or varying: " + name);
+		}
 	}
 	else
 	{
@@ -452,7 +498,7 @@ void MaterialShaderProgramCreator::parseInTag(
 
 		const std::string& typeTxt = v.first;
 
-		line += typeTxt;
+		line += "uniform " + typeTxt;
 		glType = txtToGlType.at(typeTxt.c_str());
 	}
 
@@ -474,17 +520,17 @@ void MaterialShaderProgramCreator::parseOperatorTag(
 	std::stringstream line;
 
 	// Find func def
-	ConstCharPtrHashMap<FuncDefinition*>::Type::const_iterator it =
-		funcNameToDef.find(funcName.c_str());
-
-	if(it == funcNameToDef.end())
+	const FuncDefinition* def = NULL;
+	try
+	{
+		def = funcNameToDef.at(funcName.c_str());
+	}
+	catch(std::exception& e)
 	{
 		throw EXCEPTION("Function is not defined in any include file: " +
 			funcName);
 	}
 
-	const FuncDefinition* def = it->second;
-
 	// Check args size
 	if(argsPt.size() != def->argDefinitions.size())
 	{

+ 20 - 2
src/Resources/MaterialShaderProgramCreator.h

@@ -74,6 +74,24 @@ class MaterialShaderProgramCreator
 		/// Go from "AQ_SOMETHING" string to AQ_SOMETHING enum
 		static ConstCharPtrHashMap<ArgQualifier>::Type txtToArgQualifier;
 
+		/// This holds the varyings that come from the vertex shader to the
+		/// fragment. These varyings can be used in the ins
+		/// - vTexCoords
+		/// - vNormal
+		/// - vTangent
+		/// - vTangentW
+		/// - vVertPosViewSpace
+		static ConstCharPtrHashMap<GLenum>::Type varyingNameToGlType;
+
+		/// @name Using attribute flag
+		/// Keep a few flags here to set a few defines in the shader program
+		/// source. The parseInputTag sets them
+		/// @{
+		bool usingTexCoordsAttrib;
+		bool usingNormalAttrib;
+		bool usingTangentAttrib;
+		/// @}
+
 		/// Container that holds the function definitions
 		boost::ptr_vector<FuncDefinition> funcDefs;
 
@@ -113,8 +131,8 @@ class MaterialShaderProgramCreator
 		/// @code <shaderProgram></shaderProgram> @endcode
 		void parseShaderProgramTag(const boost::property_tree::ptree& pt);
 
-		/// Parse what is within the @code <in></in> @endcode
-		void parseInTag(const boost::property_tree::ptree& pt,
+		/// Parse what is within the @code <input></input> @endcode
+		void parseInputTag(const boost::property_tree::ptree& pt,
 			std::string& line);
 
 		/// Parse what is within the @code <operation></operation> @endcode

+ 10 - 10
src/Resources/UserMaterialVariable.h

@@ -14,16 +14,6 @@ class UniformShaderProgramVariable;
 class UserMaterialVariable: public MaterialVariable
 {
 	public:
-		/// XXX
-		struct Data
-		{
-			float scalar;
-			Vec2 vec2;
-			Vec3 vec3;
-			Vec4 vec4;
-			RsrcPtr<Texture> texture;
-		};
-
 		/// @name Constructors
 		/// @{
 		UserMaterialVariable(const UniformShaderProgramVariable* cpUni,
@@ -48,6 +38,16 @@ class UserMaterialVariable: public MaterialVariable
 		/// @}
 
 	private:
+		/// XXX
+		struct Data
+		{
+			float scalar;
+			Vec2 vec2;
+			Vec3 vec3;
+			Vec4 vec4;
+			RsrcPtr<Texture> texture;
+		};
+
 		Data data;
 };
 

+ 13 - 0
src/Scene/MaterialRuntime2.h

@@ -0,0 +1,13 @@
+#ifndef MATERIAL_RUNTIME_2_H
+#define MATERIAL_RUNTIME_2_H
+
+#include "Resources/Material.h"
+
+
+/// XXX
+class MaterialRuntime2: private MaterialProps
+{
+};
+
+
+#endif

+ 32 - 0
src/Scene/UserMaterialVariableRuntime.cpp

@@ -0,0 +1,32 @@
+#include "UserMaterialVariableRuntime.h"
+#include "Resources/UserMaterialVariable.h"
+
+
+//==============================================================================
+// Constructor                                                                =
+//==============================================================================
+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);
+	}
+}

+ 46 - 0
src/Scene/UserMaterialVariableRuntime.h

@@ -0,0 +1,46 @@
+#ifndef USER_MATERIAL_VARIABLE_RUNTIME_H
+#define USER_MATERIAL_VARIABLE_RUNTIME_H
+
+#include "Util/Accessors.h"
+#include "Math/Math.h"
+
+
+class UserMaterialVariable;
+class Texture;
+
+
+/// XXX
+class UserMaterialVariableRuntime
+{
+	public:
+		/// Constructor
+		UserMaterialVariableRuntime(const UserMaterialVariable& umv);
+
+		/// @name Accessors
+		/// @{
+		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;
+};
+
+
+#endif