Переглянути джерело

Geting ready for the generic depth pass materials

Panagiotis Christopoulos Charitos 16 роки тому
батько
коміт
e17497c956
3 змінених файлів з 82 додано та 119 видалено
  1. 23 55
      src/resources/material.cpp
  2. 24 30
      src/resources/material.h
  3. 35 34
      src/scene/mesh_node.cpp

+ 23 - 55
src/resources/material.cpp

@@ -78,30 +78,6 @@ bool material_t::Load( const char* filename )
 			}
 			shader_prog = rsrc::shaders.Load( token->value.string );
 		}
-		/*//** DIFFUSE_COL **
-		else if( token->code == scanner_t::TC_IDENTIFIER && !strcmp( token->value.string, "DIFFUSE_COL" ) )
-		{
-			ParseArrOfNumbers<float>( scanner, true, true, 4, &diffuse_color[0] );
-		}
-		//** SPECULAR_COL **
-		else if( token->code == scanner_t::TC_IDENTIFIER && !strcmp( token->value.string, "SPECULAR_COL" ) )
-		{
-			ParseArrOfNumbers<float>( scanner, true, true, 4, &specular_color[0] );
-		}
-		//** SHININESS **
-		else if( token->code == scanner_t::TC_IDENTIFIER && !strcmp( token->value.string, "SHININESS" ) )
-		{
-			token = &scanner.GetNextToken();
-			if( token->code != scanner_t::TC_NUMBER )
-			{
-				PARSE_ERR_EXPECTED( "number" );
-				return false;
-			}
-			if( token->type == scanner_t::DT_FLOAT )
-				shininess = token->value.float_;
-			else
-				shininess = (float)token->value.int_;
-		}*/
 		//** BLENDS **
 		else if( token->code == scanner_t::TC_IDENTIFIER && !strcmp( token->value.string, "BLENDS" ) )
 		{
@@ -221,13 +197,13 @@ bool material_t::Load( const char* filename )
 				if( token->code == scanner_t::TC_RBRACKET )
 					break;
 				else if( token->code == scanner_t::TC_IDENTIFIER && !strcmp( token->value.string, "TEXTURE" ) )
-					type = user_defined_var_t::TEXTURE;
+					type = user_defined_var_t::VT_TEXTURE;
 				else if( token->code == scanner_t::TC_IDENTIFIER && !strcmp( token->value.string, "FLOAT" ) )
-					type = user_defined_var_t::FLOAT;
+					type = user_defined_var_t::VT_FLOAT;
 				else if( token->code == scanner_t::TC_IDENTIFIER && !strcmp( token->value.string, "VEC3" ) )
-					type = user_defined_var_t::VEC3;
+					type = user_defined_var_t::VT_VEC3;
 				else if( token->code == scanner_t::TC_IDENTIFIER && !strcmp( token->value.string, "VEC4" ) )
-					type = user_defined_var_t::VEC4;
+					type = user_defined_var_t::VT_VEC4;
 				else
 				{
 					PARSE_ERR_EXPECTED( "TEXTURE or FLOAT or VEC3 or VEC4 or }" );
@@ -252,7 +228,7 @@ bool material_t::Load( const char* filename )
 				switch( type )
 				{
 					// texture
-					case user_defined_var_t::TEXTURE:
+					case user_defined_var_t::VT_TEXTURE:
 						token = &scanner.GetNextToken();
 						if( token->code == scanner_t::TC_STRING )
 						{
@@ -277,7 +253,7 @@ bool material_t::Load( const char* filename )
 						}
 						break;
 					// float
-					case user_defined_var_t::FLOAT:
+					case user_defined_var_t::VT_FLOAT:
 						token = &scanner.GetNextToken();
 						if( token->code == scanner_t::TC_NUMBER && token->type == scanner_t::DT_FLOAT )
 							var.value.float_ = token->value.float_;
@@ -288,11 +264,11 @@ bool material_t::Load( const char* filename )
 						}
 						break;
 					// vec3
-					case user_defined_var_t::VEC3:
+					case user_defined_var_t::VT_VEC3:
 						if( !ParseArrOfNumbers<float>( scanner, true, true, 3, &var.value.vec3[0] ) ) return false;
 						break;
 					// vec4
-					case user_defined_var_t::VEC4:
+					case user_defined_var_t::VT_VEC4:
 						if( !ParseArrOfNumbers<float>( scanner, true, true, 4, &var.value.vec4[0] ) ) return false;
 						break;
 				};
@@ -346,19 +322,19 @@ bool material_t::InitTheOther()
 	shader_prog->Unbind();
 
 	// init the attribute locations
-	attribute_locs.tanget = shader_prog->GetAttributeLocationSilently( "tangent" );
-	attribute_locs.position = shader_prog->GetAttributeLocationSilently( "position" );
-	attribute_locs.normal = shader_prog->GetAttributeLocationSilently( "normal" );
-	attribute_locs.tex_coords = shader_prog->GetAttributeLocationSilently( "tex_coords" );
+	attrib_locs.tanget = shader_prog->GetAttributeLocationSilently( "tangent" );
+	attrib_locs.position = shader_prog->GetAttributeLocationSilently( "position" );
+	attrib_locs.normal = shader_prog->GetAttributeLocationSilently( "normal" );
+	attrib_locs.tex_coords = shader_prog->GetAttributeLocationSilently( "tex_coords" );
 
 	// vertex weights
-	attribute_locs.vert_weight_bones_num = shader_prog->GetAttributeLocationSilently( "vert_weight_bones_num" );
-	if( attribute_locs.vert_weight_bones_num != -1 )
+	attrib_locs.vert_weight_bones_num = shader_prog->GetAttributeLocationSilently( "vert_weight_bones_num" );
+	if( attrib_locs.vert_weight_bones_num != -1 )
 	{
-		attribute_locs.vert_weight_bone_ids = shader_prog->GetAttributeLocation( "vert_weight_bone_ids" );
-		attribute_locs.vert_weight_weights = shader_prog->GetAttributeLocation( "vert_weight_weights" );
-		uniform_locs.skinning_rotations = shader_prog->GetUniformLocation( "skinning_rotations" );
-		uniform_locs.skinning_translations = shader_prog->GetUniformLocation( "skinning_translations" );
+		attrib_locs.vert_weight_bone_ids = shader_prog->GetAttributeLocation( "vert_weight_bone_ids" );
+		attrib_locs.vert_weight_weights = shader_prog->GetAttributeLocation( "vert_weight_weights" );
+		uni_locs.skinning_rotations = shader_prog->GetUniformLocation( "skinning_rotations" );
+		uni_locs.skinning_translations = shader_prog->GetUniformLocation( "skinning_translations" );
 	}
 
 	return true;
@@ -377,7 +353,7 @@ void material_t::Unload()
 	// loop all user defined vars and unload the textures
 	for( uint i=0; i<user_defined_vars.size(); i++ )
 	{
-		if( user_defined_vars[i].type == user_defined_var_t::TEXTURE )
+		if( user_defined_vars[i].type == user_defined_var_t::VT_TEXTURE )
 			rsrc::textures.Unload( user_defined_vars[i].value.texture );
 	}
 
@@ -399,9 +375,6 @@ void material_t::SetToDefault()
 	blending_dfactor = GL_ZERO;
 	depth_testing = true;
 	wireframe = false;
-	specular_color = vec4_t(1.0);
-	diffuse_color = vec4_t(1.0);
-	shininess = 0.0;
 	grass_map = NULL;
 	casts_shadow = true;
 	refracts = false;
@@ -417,11 +390,6 @@ void material_t::Setup()
 {
 	shader_prog->Bind();
 
-	// first set the standard vars
-	glMaterialfv( GL_FRONT, GL_DIFFUSE, &diffuse_color[0] );
-	glMaterialfv( GL_FRONT, GL_SPECULAR, &specular_color[0] );
-	glMaterialf( GL_FRONT, GL_SHININESS, shininess );
-
 	if( blends )
 	{
 		glEnable( GL_BLEND );
@@ -447,19 +415,19 @@ void material_t::Setup()
 		switch( udv->type )
 		{
 			// texture
-			case user_defined_var_t::TEXTURE:
+			case user_defined_var_t::VT_TEXTURE:
 				shader_prog->LocTexUnit( udv->uniform_location, *udv->value.texture, texture_unit++ );
 				break;
 			// float
-			case user_defined_var_t::FLOAT:
+			case user_defined_var_t::VT_FLOAT:
 				glUniform1f( udv->uniform_location, udv->value.float_ );
 				break;
 			// vec3
-			case user_defined_var_t::VEC3:
+			case user_defined_var_t::VT_VEC3:
 				glUniform3fv( udv->uniform_location, 1, &udv->value.vec3[0] );
 				break;
 			// vec4
-			case user_defined_var_t::VEC4:
+			case user_defined_var_t::VT_VEC4:
 				glUniform4fv( udv->uniform_location, 1, &udv->value.vec4[0] );
 				break;
 		}

+ 24 - 30
src/resources/material.h

@@ -5,7 +5,7 @@
 #include "gmath.h"
 #include "resource.h"
 
-/// The material class
+/// Mesh material resource
 class material_t: public resource_t
 {
 	//===================================================================================================================================
@@ -18,13 +18,14 @@ class material_t: public resource_t
 			public:
 				enum type_e
 				{
-					TEXTURE,
-					FLOAT,
-					VEC3,
-					VEC4
+					VT_TEXTURE,
+					VT_FLOAT,
+					VT_VEC2, // not used yet
+					VT_VEC3,
+					VT_VEC4
 				};
 
-				struct value_t       // unfortunately we cannot use union with vec3_t and vec4_t
+				struct value_t       // unfortunately we cannot use union because of vec3_t and vec4_t
 				{
 					texture_t* texture;
 					float      float_;
@@ -39,27 +40,23 @@ class material_t: public resource_t
 				string name;
 		}; // end class user_defined_var_t
 
-		vec_t< user_defined_var_t > user_defined_vars;
+		vec_t<user_defined_var_t> user_defined_vars;
 
 
 	//===================================================================================================================================
-	// Misc                                                                                                                             =
+	// data                                                                                                                             =
 	//===================================================================================================================================
-	protected:
-		void SetToDefault();
-		bool InitTheOther(); ///< The func is for not poluting Load with extra code
-
 	public:
-		shader_prog_t* shader_prog;
-
-		vec4_t diffuse_color;
-		vec4_t specular_color;
-		float shininess;
+		shader_prog_t* shader_prog; ///< The most imortant asspect of materials
 
 		bool blends; ///< The entities with blending are being rendered in blending stage and those without in material stage
 		bool refracts;
 		int  blending_sfactor;
 		int  blending_dfactor;
+		bool depth_testing;
+		bool wireframe;
+		bool casts_shadow; ///< Used in shadowmapping passes but not in EarlyZ
+		texture_t* grass_map; // ToDo remove this
 
 		// vertex attributes
 		struct
@@ -73,18 +70,14 @@ class material_t: public resource_t
 			int vert_weight_bones_num;
 			int vert_weight_bone_ids;
 			int vert_weight_weights;
-		} attribute_locs;
+		} attrib_locs;
 
 		// uniforms
 		struct
 		{
 			int skinning_rotations;
 			int skinning_translations;
-		} uniform_locs;
-
-		bool depth_testing;
-		bool wireframe;
-		bool casts_shadow; ///< Used in EarlyZ and in shadowmapping passes
+		} uni_locs;
 
 		// for depth passing
 		/*struct
@@ -109,13 +102,14 @@ class material_t: public resource_t
 			} uni_locs;
 		} dp;*/
 
-		/**
-		 * Used mainly in depth passes. If the grass_map pointer is != NULL then the entity is "grass like".
-		 * Most of the time the grass_map is the same as the diffuse map
-		 */
-		texture_t* grass_map;
-
-		// funcs
+	//===================================================================================================================================
+	// funcs                                                                                                                            =
+	//===================================================================================================================================
+	protected:
+		void SetToDefault();
+		bool InitTheOther(); ///< The func is for not poluting Load with extra code
+		
+	public:
 		material_t() { SetToDefault(); }
 		void Setup();
 		bool Load( const char* filename );

+ 35 - 34
src/scene/mesh_node.cpp

@@ -32,6 +32,7 @@ void mesh_node_t::Deinit()
 //=====================================================================================================================================
 // Render                                                                                                                             =
 //=====================================================================================================================================
+/// Called in material or blending stages
 void mesh_node_t::Render()
 {
 	glPushMatrix();
@@ -41,49 +42,49 @@ void mesh_node_t::Render()
 	if( skel_controller )
 	{
 		// first the uniforms
-		glUniformMatrix3fv( material->uniform_locs.skinning_rotations, skel_controller->skel_node->skeleton->bones.size(), 1,
+		glUniformMatrix3fv( material->uni_locs.skinning_rotations, skel_controller->skel_node->skeleton->bones.size(), 1,
 		                    &(skel_controller->skel_node->skel_anim_controller->bone_rotations[0])[0] );
-		glUniform3fv( material->uniform_locs.skinning_translations, skel_controller->skel_node->skeleton->bones.size(),
+		glUniform3fv( material->uni_locs.skinning_translations, skel_controller->skel_node->skeleton->bones.size(),
 		              &(skel_controller->skel_node->skel_anim_controller->bone_translations[0])[0] );
 
 		// then the attributes
-		DEBUG_ERR( material->attribute_locs.vert_weight_bones_num == -1 );
+		DEBUG_ERR( material->attrib_locs.vert_weight_bones_num == -1 );
 
 		mesh->vbos.vert_weights.Bind();
-		glEnableVertexAttribArray( material->attribute_locs.vert_weight_bones_num );
-		glVertexAttribPointer( material->attribute_locs.vert_weight_bones_num, 1, GL_FLOAT, GL_FALSE, sizeof(mesh_t::vertex_weight_t), BUFFER_OFFSET(0) );
-		glEnableVertexAttribArray( material->attribute_locs.vert_weight_bone_ids );
-		glVertexAttribPointer( material->attribute_locs.vert_weight_bone_ids, 4, GL_FLOAT, GL_FALSE, sizeof(mesh_t::vertex_weight_t), BUFFER_OFFSET(4) );
-		glEnableVertexAttribArray( material->attribute_locs.vert_weight_weights );
-		glVertexAttribPointer( material->attribute_locs.vert_weight_weights, 4, GL_FLOAT, GL_FALSE, sizeof(mesh_t::vertex_weight_t), BUFFER_OFFSET(20) );
+		glEnableVertexAttribArray( material->attrib_locs.vert_weight_bones_num );
+		glVertexAttribPointer( material->attrib_locs.vert_weight_bones_num, 1, GL_FLOAT, GL_FALSE, sizeof(mesh_t::vertex_weight_t), BUFFER_OFFSET(0) );
+		glEnableVertexAttribArray( material->attrib_locs.vert_weight_bone_ids );
+		glVertexAttribPointer( material->attrib_locs.vert_weight_bone_ids, 4, GL_FLOAT, GL_FALSE, sizeof(mesh_t::vertex_weight_t), BUFFER_OFFSET(4) );
+		glEnableVertexAttribArray( material->attrib_locs.vert_weight_weights );
+		glVertexAttribPointer( material->attrib_locs.vert_weight_weights, 4, GL_FLOAT, GL_FALSE, sizeof(mesh_t::vertex_weight_t), BUFFER_OFFSET(20) );
 	}
 
-	if( material->attribute_locs.position != -1 )
+	if( material->attrib_locs.position != -1 )
 	{
 		mesh->vbos.vert_coords.Bind();
-		glVertexAttribPointer( material->attribute_locs.position, 3, GL_FLOAT, false, 0, NULL );
-		glEnableVertexAttribArray( material->attribute_locs.position );
+		glVertexAttribPointer( material->attrib_locs.position, 3, GL_FLOAT, false, 0, NULL );
+		glEnableVertexAttribArray( material->attrib_locs.position );
 	}
 
-	if( material->attribute_locs.normal != -1 )
+	if( material->attrib_locs.normal != -1 )
 	{
 		mesh->vbos.vert_normals.Bind();
-		glVertexAttribPointer( material->attribute_locs.normal, 3, GL_FLOAT, false, 0, NULL );
-		glEnableVertexAttribArray( material->attribute_locs.normal );
+		glVertexAttribPointer( material->attrib_locs.normal, 3, GL_FLOAT, false, 0, NULL );
+		glEnableVertexAttribArray( material->attrib_locs.normal );
 	}
 
-	if( material->attribute_locs.tex_coords != -1 )
+	if( material->attrib_locs.tex_coords != -1 )
 	{
 		mesh->vbos.tex_coords.Bind();
-		glVertexAttribPointer( material->attribute_locs.tex_coords, 2, GL_FLOAT, false, 0, NULL );
-		glEnableVertexAttribArray( material->attribute_locs.tex_coords );
+		glVertexAttribPointer( material->attrib_locs.tex_coords, 2, GL_FLOAT, false, 0, NULL );
+		glEnableVertexAttribArray( material->attrib_locs.tex_coords );
 	}
 
-	if( material->attribute_locs.tanget != -1 )
+	if( material->attrib_locs.tanget != -1 )
 	{
 		mesh->vbos.vert_tangents.Bind();
-		glVertexAttribPointer( material->attribute_locs.tanget, 4, GL_FLOAT, false, 0, NULL );
-		glEnableVertexAttribArray( material->attribute_locs.tanget );
+		glVertexAttribPointer( material->attrib_locs.tanget, 4, GL_FLOAT, false, 0, NULL );
+		glEnableVertexAttribArray( material->attrib_locs.tanget );
 	}
 
 	mesh->vbos.vert_indeces.Bind();
@@ -91,16 +92,16 @@ void mesh_node_t::Render()
 	glDrawElements( GL_TRIANGLES, mesh->vert_indeces.size(), GL_UNSIGNED_SHORT, 0 );
 
 	// disable
-	if( material->attribute_locs.position != -1 ) glDisableVertexAttribArray( material->attribute_locs.position );
-	if( material->attribute_locs.normal != -1 ) glDisableVertexAttribArray( material->attribute_locs.normal );
-	if( material->attribute_locs.tex_coords != -1 ) glDisableVertexAttribArray( material->attribute_locs.tex_coords );
-	if( material->attribute_locs.tanget != -1 ) glDisableVertexAttribArray( material->attribute_locs.tanget );
+	if( material->attrib_locs.position != -1 ) glDisableVertexAttribArray( material->attrib_locs.position );
+	if( material->attrib_locs.normal != -1 ) glDisableVertexAttribArray( material->attrib_locs.normal );
+	if( material->attrib_locs.tex_coords != -1 ) glDisableVertexAttribArray( material->attrib_locs.tex_coords );
+	if( material->attrib_locs.tanget != -1 ) glDisableVertexAttribArray( material->attrib_locs.tanget );
 
 	if( skel_controller )
 	{
-		glDisableVertexAttribArray( material->attribute_locs.vert_weight_bones_num );
-		glDisableVertexAttribArray( material->attribute_locs.vert_weight_bone_ids );
-		glDisableVertexAttribArray( material->attribute_locs.vert_weight_weights );
+		glDisableVertexAttribArray( material->attrib_locs.vert_weight_bones_num );
+		glDisableVertexAttribArray( material->attrib_locs.vert_weight_bone_ids );
+		glDisableVertexAttribArray( material->attrib_locs.vert_weight_weights );
 	}
 
 	vbo_t::UnbindAllTargets();
@@ -130,13 +131,13 @@ void mesh_node_t::RenderDepth()
 	if( skel_controller )
 	{
 		// first the uniforms
-		glUniformMatrix3fv( material->uniform_locs.skinning_rotations, skel_controller->skel_node->skeleton->bones.size(), 1,
+		glUniformMatrix3fv( material->uni_locs.skinning_rotations, skel_controller->skel_node->skeleton->bones.size(), 1,
 		                    &(skel_controller->skel_node->skel_anim_controller->bone_rotations[0])[0] );
-		glUniform3fv( material->uniform_locs.skinning_translations, skel_controller->skel_node->skeleton->bones.size(),
+		glUniform3fv( material->uni_locs.skinning_translations, skel_controller->skel_node->skeleton->bones.size(),
 		              &(skel_controller->skel_node->skel_anim_controller->bone_translations[0])[0] );
 
 		// then the attributes
-		DEBUG_ERR( material->attribute_locs.vert_weight_bones_num == -1 );
+		DEBUG_ERR( material->attrib_locs.vert_weight_bones_num == -1 );
 
 		mesh->vbos.vert_weights.Bind();
 		glEnableVertexAttribArray( r::is::shadows::shdr_depth_hw_skinning->GetAttributeLocation(0) );
@@ -159,9 +160,9 @@ void mesh_node_t::RenderDepth()
 
 	if( skel_controller )
 	{
-		glDisableVertexAttribArray( material->attribute_locs.vert_weight_bones_num );
-		glDisableVertexAttribArray( material->attribute_locs.vert_weight_bone_ids );
-		glDisableVertexAttribArray( material->attribute_locs.vert_weight_weights );
+		glDisableVertexAttribArray( material->attrib_locs.vert_weight_bones_num );
+		glDisableVertexAttribArray( material->attrib_locs.vert_weight_bone_ids );
+		glDisableVertexAttribArray( material->attrib_locs.vert_weight_weights );
 	}
 
 	vbo_t::UnbindAllTargets();