Bladeren bron

Changes and additions to Material

Panagiotis Christopoulos Charitos 15 jaren geleden
bovenliggende
commit
124abcf45e

File diff suppressed because it is too large
+ 513 - 294
build/debug/Makefile


+ 112 - 4
src/Renderer/Renderer.cpp

@@ -1,15 +1,12 @@
 #include "Renderer.h"
 #include "Renderer.h"
 #include "Camera.h" /// @todo remove this
 #include "Camera.h" /// @todo remove this
 #include "RendererInitializer.h"
 #include "RendererInitializer.h"
+#include "Material.h"
 
 
 
 
 //=====================================================================================================================================
 //=====================================================================================================================================
 // Vars                                                                                                                               =
 // Vars                                                                                                                               =
 //=====================================================================================================================================
 //=====================================================================================================================================
-bool Renderer::textureCompression = false;
-int  Renderer::maxTextureUnits = -1;
-bool Renderer::mipmapping = true;
-int  Renderer::maxAnisotropy = 8;
 float Renderer::quadVertCoords [][2] = { {1.0,1.0}, {0.0,1.0}, {0.0,0.0}, {1.0,0.0} };
 float Renderer::quadVertCoords [][2] = { {1.0,1.0}, {0.0,1.0}, {0.0,0.0}, {1.0,0.0} };
 
 
 
 
@@ -89,6 +86,117 @@ void Renderer::drawQuad( int vertCoordsUniLoc )
 	glDisableVertexAttribArray( vertCoordsUniLoc );
 	glDisableVertexAttribArray( vertCoordsUniLoc );
 }
 }
 
 
+
+//=====================================================================================================================================
+// setupMaterial                                                                                                                      =
+//=====================================================================================================================================
+void Renderer::setupMaterial( const Material& mtl )
+{
+	mtl.shaderProg->bind();
+
+	if( mtl.blends )
+	{
+		glEnable( GL_BLEND );
+		//glDisable( GL_BLEND );
+		glBlendFunc( mtl.blendingSfactor, mtl.blendingDfactor );
+	}
+	else
+		glDisable( GL_BLEND );
+
+
+	if( mtl.depthTesting )
+		glEnable( GL_DEPTH_TEST );
+	else
+		glDisable( GL_DEPTH_TEST );
+
+	if( mtl.wireframe )
+		glPolygonMode( GL_FRONT, GL_LINE );
+	else
+		glPolygonMode( GL_FRONT, GL_FILL );
+
+
+	// now loop all the user defined vars and set them
+	uint textureUnit = 0;
+	for( uint i=0; i<mtl.userDefinedVars.size(); i++ )
+	{
+		const Material::UserDefinedVar* udv = &mtl.userDefinedVars[i];
+		switch( udv->sProgVar->getGlDataType() )
+		{
+			// texture
+			case GL_SAMPLER_2D:
+				if( !udv->specialVariable )
+				{
+					udv->sProgVar->setTexture( *udv->value.texture, textureUnit++ );
+				}
+				else
+				{
+					switch( udv->value.speciaValue )
+					{
+						case Material::UserDefinedVar::SV_MS_NORMAL_FAI:
+							udv->sProgVar->setTexture( ms.normalFai, textureUnit++ );
+							break;
+
+						case Material::UserDefinedVar::SV_MS_DIFFUSE_FAI:
+							udv->sProgVar->setTexture( ms.diffuseFai, textureUnit++ );
+							break;
+
+						case Material::UserDefinedVar::SV_MS_SPECULAR_FAI:
+							udv->sProgVar->setTexture( ms.specularFai, textureUnit++ );
+							break;
+
+						case Material::UserDefinedVar::SV_MS_DEPTH_FAI:
+							udv->sProgVar->setTexture( ms.depthFai, textureUnit++ );
+							break;
+
+						case Material::UserDefinedVar::SV_IS_FAI:
+							udv->sProgVar->setTexture( is.fai, textureUnit++ );
+							break;
+
+						case Material::UserDefinedVar::SV_PPS_FAI:
+							udv->sProgVar->setTexture( pps.fai, textureUnit++ );
+							break;
+
+						default:
+							DEBUG_ERR( 1 );
+					}
+				}
+				break;
+			// float
+			case GL_FLOAT:
+				udv->sProgVar->setFloat( udv->value.float_ );
+				break;
+			// vec2
+			case GL_FLOAT_VEC2:
+				if( !udv->specialVariable )
+				{
+					udv->sProgVar->setVec2( &udv->value.vec2 );
+				}
+				else
+				{
+					switch( udv->value.speciaValue )
+					{
+						case Material::UserDefinedVar::SV_RENDERER_SIZE:
+						{
+							Vec2 v( width, height );
+							udv->sProgVar->setVec2( &v );
+							break;
+						}
+					}
+				}
+				break;
+			// vec3
+			case GL_FLOAT_VEC3:
+				udv->sProgVar->setVec3( &udv->value.vec3 );
+				break;
+			// vec4
+			case GL_FLOAT_VEC4:
+				udv->sProgVar->setVec4( &udv->value.vec4 );
+				break;
+		}
+	}
+}
+
+
 //=====================================================================================================================================
 //=====================================================================================================================================
 // setProjectionMatrix                                                                                                                =
 // setProjectionMatrix                                                                                                                =
 //=====================================================================================================================================
 //=====================================================================================================================================

+ 1 - 5
src/Renderer/Renderer.h

@@ -332,6 +332,7 @@ class Renderer
 		static float quadVertCoords [][2];
 		static float quadVertCoords [][2];
 
 
 		static void drawQuad( int vertCoordsUniLoc );
 		static void drawQuad( int vertCoordsUniLoc );
+		void setupMaterial( const Material& mtl );
 
 
 	public:
 	public:
 		// the stages as data members
 		// the stages as data members
@@ -340,11 +341,6 @@ class Renderer
 		Pps pps; ///< Postprocessing rendering stage
 		Pps pps; ///< Postprocessing rendering stage
 		Dbg dbg; ///< Debugging rendering stage
 		Dbg dbg; ///< Debugging rendering stage
 
 
-		// texture stuff
-		static bool textureCompression; ///< Used in Texture::load to enable texture compression. Decreases video memory usage
-		static int  maxTextureUnits; ///< Used in Texture::bind so we wont bind in a nonexistent texture unit. Readonly
-		static bool mipmapping; ///< Used in Texture::load. Enables mipmapping increases video memory usage
-		static int  maxAnisotropy; ///< Max texture anisotropy. Used in Texture::load
 		// matrices & viewing
 		// matrices & viewing
 		Mat4 modelViewMat; ///< This changes once for every mesh rendering
 		Mat4 modelViewMat; ///< This changes once for every mesh rendering
 		Mat4 projectionMat; ///< This changes once every frame
 		Mat4 projectionMat; ///< This changes once every frame

+ 7 - 6
src/Renderer/Ssao.cpp

@@ -92,18 +92,19 @@ void Renderer::Pps::Ssao::init()
 	// noise map
 	// noise map
 	//
 	//
 
 
+	/// @todo fix this crap
 	// load noise map and disable temporally the texture compression and enable mipmapping
 	// load noise map and disable temporally the texture compression and enable mipmapping
-	bool texCompr = Renderer::textureCompression;
-	bool mipmaping = Renderer::mipmapping;
-	Renderer::textureCompression = false;
-	Renderer::mipmapping = true;
+	bool texCompr = Texture::compressionEnabled;
+	bool mipmaping = Texture::mipmappingEnabled;
+	Texture::compressionEnabled = false;
+	Texture::mipmappingEnabled = true;
 	noiseMap = Rsrc::textures.load( "gfx/noise3.tga" );
 	noiseMap = Rsrc::textures.load( "gfx/noise3.tga" );
 	noiseMap->texParameter( GL_TEXTURE_WRAP_S, GL_REPEAT );
 	noiseMap->texParameter( GL_TEXTURE_WRAP_S, GL_REPEAT );
 	noiseMap->texParameter( GL_TEXTURE_WRAP_T, GL_REPEAT );
 	noiseMap->texParameter( GL_TEXTURE_WRAP_T, GL_REPEAT );
 	//noise_map->texParameter( GL_TEXTURE_MAG_FILTER, GL_NEAREST );
 	//noise_map->texParameter( GL_TEXTURE_MAG_FILTER, GL_NEAREST );
 	//noise_map->texParameter( GL_TEXTURE_MIN_FILTER, GL_NEAREST );
 	//noise_map->texParameter( GL_TEXTURE_MIN_FILTER, GL_NEAREST );
-	Renderer::textureCompression = texCompr;
-	Renderer::mipmapping = mipmaping;
+	Texture::compressionEnabled = texCompr;
+	Texture::mipmappingEnabled = mipmaping;
 
 
 }
 }
 
 

+ 45 - 11
src/Resources/Material.cpp

@@ -237,25 +237,39 @@ bool Material::load( const char* filename )
 						{
 						{
 							var.value.texture = Rsrc::textures.load( token->getValue().getString() );
 							var.value.texture = Rsrc::textures.load( token->getValue().getString() );
 						}
 						}
-						else if( token->getCode() == Scanner::TC_IDENTIFIER && !strcmp( token->getValue().getString(), "IS_FAI" ) )
+						else if( token->getCode() == Scanner::TC_IDENTIFIER && !strcmp( token->getValue().getString(), "MS_NORMAL_FAI" ) )
 						{
 						{
-							var.value.texture = &app->getMainRenderer()->is.fai;
+							var.specialVariable = true;
+							var.value.speciaValue = UserDefinedVar::SV_MS_NORMAL_FAI;
 						}
 						}
-						else if( token->getCode() == Scanner::TC_IDENTIFIER && !strcmp( token->getValue().getString(), "MS_NORMAL_FAI" ) )
+						else if( token->getCode() == Scanner::TC_IDENTIFIER && !strcmp( token->getValue().getString(), "MS_DIFFUSE_FAI" ) )
+						{
+							var.specialVariable = true;
+							var.value.speciaValue = UserDefinedVar::SV_MS_DIFFUSE_FAI;
+						}
+						else if( token->getCode() == Scanner::TC_IDENTIFIER && !strcmp( token->getValue().getString(), "MS_SPECULAR_FAI" ) )
 						{
 						{
-							var.value.texture = &app->getMainRenderer()->ms.normalFai;
+							var.specialVariable = true;
+							var.value.speciaValue = UserDefinedVar::SV_MS_SPECULAR_FAI;
 						}
 						}
 						else if( token->getCode() == Scanner::TC_IDENTIFIER && !strcmp( token->getValue().getString(), "MS_DEPTH_FAI" ) )
 						else if( token->getCode() == Scanner::TC_IDENTIFIER && !strcmp( token->getValue().getString(), "MS_DEPTH_FAI" ) )
 						{
 						{
-							var.value.texture = &app->getMainRenderer()->ms.depthFai;
+							var.specialVariable = true;
+							var.value.speciaValue = UserDefinedVar::SV_MS_DEPTH_FAI;
+						}
+						else if( token->getCode() == Scanner::TC_IDENTIFIER && !strcmp( token->getValue().getString(), "IS_FAI" ) )
+						{
+							var.specialVariable = true;
+							var.value.speciaValue = UserDefinedVar::SV_IS_FAI;
 						}
 						}
 						else if( token->getCode() == Scanner::TC_IDENTIFIER && !strcmp( token->getValue().getString(), "PPS_FAI" ) )
 						else if( token->getCode() == Scanner::TC_IDENTIFIER && !strcmp( token->getValue().getString(), "PPS_FAI" ) )
 						{
 						{
-							var.value.texture = &app->getMainRenderer()->pps.fai;
+							var.specialVariable = true;
+							var.value.speciaValue = UserDefinedVar::SV_PPS_FAI;
 						}
 						}
 						else
 						else
 						{
 						{
-							PARSE_ERR_EXPECTED( "string or IS_FAI or MS_NORMAL_FAI or MS_DEPTH_FAI or PPS_FAI" );
+							PARSE_ERR_EXPECTED( "string or MS_NORMAL_FAI or MS_DIFFUSE_FAI or MS_SPECULAR_FAI or MS_DEPTH_FAI or IS_FAI or PPS_FAI" );
 							return false;
 							return false;
 						}
 						}
 						break;
 						break;
@@ -272,7 +286,29 @@ bool Material::load( const char* filename )
 						break;
 						break;
 					// vec2
 					// vec2
 					case GL_FLOAT_VEC2:
 					case GL_FLOAT_VEC2:
-						ERROR( "Unimplemented" );
+						token = &scanner.getNextToken();
+						if( token->getCode() == Scanner::TC_LBRACKET )
+						{
+							if( !Parser::parseArrOfNumbers<float>( scanner, false, true, 2, &var.value.vec2[0] ) )
+								return false;
+
+							token = &scanner.getNextToken();
+							if( token->getCode() != Scanner::TC_RBRACKET )
+							{
+								PARSE_ERR_EXPECTED( "}" );
+								return false;
+							}
+						}
+						else if( token->getCode() == Scanner::TC_IDENTIFIER && !strcmp( token->getValue().getString(), "RENDERER_SIZE" ) )
+						{
+							var.specialVariable = true;
+							var.value.speciaValue = UserDefinedVar::SV_RENDERER_SIZE;
+						}
+						else
+						{
+							PARSE_ERR_EXPECTED( "{ or RENDERER_SIZE" );
+							return false;
+						}
 						break;
 						break;
 					// vec3
 					// vec3
 					case GL_FLOAT_VEC3:
 					case GL_FLOAT_VEC3:
@@ -352,7 +388,7 @@ void Material::unload()
 	// loop all user defined vars and unload the textures
 	// loop all user defined vars and unload the textures
 	for( uint i=0; i<userDefinedVars.size(); i++ )
 	for( uint i=0; i<userDefinedVars.size(); i++ )
 	{
 	{
-		if( userDefinedVars[i].sProgVar->getGlDataType() == GL_SAMPLER_2D )
+		if( userDefinedVars[i].sProgVar->getGlDataType() == GL_SAMPLER_2D && ! userDefinedVars[i].specialVariable )
 			Rsrc::textures.unload( userDefinedVars[i].value.texture );
 			Rsrc::textures.unload( userDefinedVars[i].value.texture );
 	}
 	}
 }
 }
@@ -372,8 +408,6 @@ void Material::setToDefault()
 	castsShadow = true;
 	castsShadow = true;
 	refracts = false;
 	refracts = false;
 	dpMtl = NULL;
 	dpMtl = NULL;
-	/*depth.shaderProg = NULL;
-	depth.alpha_testing_map = NULL;*/
 }
 }
 
 
 
 

+ 37 - 56
src/Resources/Material.h

@@ -10,50 +10,58 @@
 /// Mesh material @ref Resource resource
 /// Mesh material @ref Resource resource
 class Material: public Resource
 class Material: public Resource
 {
 {
+	friend class Renderer;
+	friend class MeshNode;
+
 	//===================================================================================================================================
 	//===================================================================================================================================
 	// User defined variables                                                                                                           =
 	// User defined variables                                                                                                           =
 	//===================================================================================================================================
 	//===================================================================================================================================
-	public:
-		/// class for user defined material variables that will be passes in to the shader
-		class UserDefinedVar
+	protected:
+		/**
+		 * Class for user defined material variables that will be passes in to the shader
+		 */
+		struct UserDefinedVar
 		{
 		{
-			public:
-				enum SpecialValue
-				{
-					SV_IS_FAI
-				};
-
-				struct Value       // unfortunately we cannot use union because of Vec3 and Vec4
-				{
-					Texture* texture;
-					float float_;
-					Vec2 vec2;
-					Vec3 vec3;
-					Vec4 vec4;
-					SpecialValue speciaValue;
-					Value(): texture(NULL) {}
-				};
-
-				Value  value;
-				const ShaderProg::Var* sProgVar;
-		}; // end class UserDefinedVar
+			enum SpecialValue
+			{
+				// Texture
+				SV_MS_NORMAL_FAI,
+				SV_MS_DIFFUSE_FAI,
+				SV_MS_SPECULAR_FAI,
+				SV_MS_DEPTH_FAI,
+				SV_IS_FAI,
+				SV_PPS_FAI,
+				// Vec2
+				SV_RENDERER_SIZE ///< Active renderer's width and height
+			};
+
+			struct Value  // unfortunately we cannot use union because of Vec3 and Vec4
+			{
+				Texture* texture;
+				float float_;
+				Vec2 vec2;
+				Vec3 vec3;
+				Vec4 vec4;
+				SpecialValue speciaValue;
+				Value(): texture(NULL) {}
+			};
 
 
-		Vec<UserDefinedVar> userDefinedVars;
+			Value value;
+			bool specialVariable;
+			const ShaderProg::UniVar* sProgVar;
+		}; // end UserDefinedVar
 
 
 
 
-	//===================================================================================================================================
-	// data                                                                                                                             =
-	//===================================================================================================================================
-	public:
 		ShaderProg* shaderProg; ///< The most important aspect of materials
 		ShaderProg* shaderProg; ///< The most important aspect of materials
 
 
 		bool blends; ///< The entities with blending are being rendered in blending stage and those without in material stage
 		bool blends; ///< The entities with blending are being rendered in blending stage and those without in material stage
-		bool refracts;
 		int  blendingSfactor;
 		int  blendingSfactor;
 		int  blendingDfactor;
 		int  blendingDfactor;
+		bool refracts;
 		bool depthTesting;
 		bool depthTesting;
 		bool wireframe;
 		bool wireframe;
 		bool castsShadow; ///< Used in shadowmapping passes but not in EarlyZ
 		bool castsShadow; ///< Used in shadowmapping passes but not in EarlyZ
+		Vec<UserDefinedVar> userDefinedVars;
 
 
 		// vertex attributes
 		// vertex attributes
 		struct
 		struct
@@ -76,35 +84,8 @@ class Material: public Resource
 			int skinningTranslations;
 			int skinningTranslations;
 		} uniLocs;
 		} uniLocs;
 
 
-		// for depth passing
-		/*struct
-		{
-			ShaderProg* shaderProg; ///< Depth pass shader program
-			Texture* alpha_testing_map;
-			
-			struct
-			{
-				int position;
-				int texCoords;
-
-				// for hw skinning
-				int vertWeightBonesNum;
-				int vertWeightBoneIds;
-				int vertWeightWeights;
-			} attribute_locs;
-			
-			struct
-			{
-				int alpha_testing_map;
-			} uniLocs;
-		} depth;*/
-
 		Material* dpMtl;
 		Material* dpMtl;
 
 
-	//===================================================================================================================================
-	// funcs                                                                                                                            =
-	//===================================================================================================================================
-	protected:
 		void setToDefault();
 		void setToDefault();
 		bool additionalInit(); ///< The func is for not polluting load with extra code
 		bool additionalInit(); ///< The func is for not polluting load with extra code
 		
 		

+ 6 - 0
src/Resources/ShaderProg.cpp

@@ -47,6 +47,12 @@ void ShaderProg::UniVar::setVec3( const Vec3 v3[], uint size ) const
 	glUniform3fv( getLoc(), size, &( const_cast<Vec3&>(v3[0]) )[0] );
 	glUniform3fv( getLoc(), size, &( const_cast<Vec3&>(v3[0]) )[0] );
 }
 }
 
 
+void ShaderProg::UniVar::setVec4( const Vec4 v4[], uint size ) const
+{
+	STD_SET_UNI_CHECK();
+	glUniform4fv( getLoc(), size, &( const_cast<Vec4&>(v4[0]) )[0] );
+}
+
 void ShaderProg::UniVar::setMat4( const Mat4 m4[], uint size ) const
 void ShaderProg::UniVar::setMat4( const Mat4 m4[], uint size ) const
 {
 {
 	STD_SET_UNI_CHECK();
 	STD_SET_UNI_CHECK();

+ 1 - 0
src/Resources/ShaderProg.h

@@ -73,6 +73,7 @@ class ShaderProg: public Resource
 				void setFloatVec( float f[], uint size = 1 ) const;
 				void setFloatVec( float f[], uint size = 1 ) const;
 				void setVec2( const Vec2 v2[], uint size = 1 ) const;
 				void setVec2( const Vec2 v2[], uint size = 1 ) const;
 				void setVec3( const Vec3 v3[], uint size = 1 ) const;
 				void setVec3( const Vec3 v3[], uint size = 1 ) const;
+				void setVec4( const Vec4 v4[], uint size = 1 ) const;
 				void setMat4( const Mat4 m4[], uint size = 1 ) const;
 				void setMat4( const Mat4 m4[], uint size = 1 ) const;
 				void setTexture( const Texture& tex, uint texUnit ) const;
 				void setTexture( const Texture& tex, uint texUnit ) const;
 		};
 		};

+ 1 - 0
src/Resources/Texture.h

@@ -15,6 +15,7 @@
  */
  */
 class Texture: public Resource
 class Texture: public Resource
 {
 {
+	friend class Renderer; /// @todo Remove this when remove the SSAO load noise map crap
 	friend class MainRenderer;
 	friend class MainRenderer;
 
 
 	protected:
 	protected:

+ 1 - 1
src/Util/Common.h

@@ -84,7 +84,7 @@ extern string getFunctionFromPrettyFunction( const char* pretty_function );
 #define WARNING( x ) GENERAL_ERR( "Warning", x, COL_WARNING )
 #define WARNING( x ) GENERAL_ERR( "Warning", x, COL_WARNING )
 
 
 /// Show an error and exit application
 /// Show an error and exit application
-#define FATAL( x ) { GENERAL_ERR( "Fatal", x << ". Bye!", COL_FATAL ); exit( EXIT_FAILURE ); }
+#define FATAL( x ) { GENERAL_ERR( "Fatal", x << ". Bye!", COL_FATAL ); ::exit( EXIT_FAILURE ); }
 
 
 /// Show an info message
 /// Show an info message
 #define INFO( x ) GENERAL_MSG( "Info", x, COL_INFO )
 #define INFO( x ) GENERAL_MSG( "Info", x, COL_INFO )

+ 1 - 1
src/Util/Tokenizer/Parser.h

@@ -25,7 +25,7 @@ namespace Parser {
 // parseArrOfNumbers                                                                                                                  =
 // parseArrOfNumbers                                                                                                                  =
 //=====================================================================================================================================
 //=====================================================================================================================================
 /**
 /**
- * @brief This template func is used for a common operation of parsing arrays of numbers
+ * This template func is used for a common operation of parsing arrays of numbers
  *
  *
  * It parses expressions like this one: { 10 -0.2 123.e-10 -0x0FF } and stores the result in the arr array. The acceptable types
  * It parses expressions like this one: { 10 -0.2 123.e-10 -0x0FF } and stores the result in the arr array. The acceptable types
  * (typename Type) are integer or floating point types
  * (typename Type) are integer or floating point types

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