Browse Source

Refactoring

Panagiotis Christopoulos Charitos 14 years ago
parent
commit
6e4cf16c3d

+ 71 - 71
shaders/IsLpGeneric.glsl

@@ -69,11 +69,11 @@ vec3 getFragPosVSpace()
 		discard;
 	}
 
-	vec3 _fragPosVspace_;
-	vec3 _vposn_ = normalize(vPosition);
-	_fragPosVspace_.z = -planes.y / (planes.x + _depth_);
-	_fragPosVspace_.xy = _vposn_.xy * (_fragPosVspace_.z / _vposn_.z);
-	return _fragPosVspace_;
+	vec3 fragPosVspace;
+	vec3 vposn = normalize(vPosition);
+	fragPosVspace.z = -planes.y / (planes.x + _depth_);
+	fragPosVspace.xy = vposn.xy * (fragPosVspace.z / vposn.z);
+	return fragPosVspace;
 }
 
 
@@ -81,10 +81,10 @@ vec3 getFragPosVSpace()
 // getAttenuation                                                                                                      =
 //======================================================================================================================
 /// @return The attenuation factor given the distance from the frag to the light source
-float getAttenuation(in float _fragLightDist_)
+float getAttenuation(in float fragLightDist)
 {
-	return clamp(1.0 - sqrt(_fragLightDist_) / lightRadius, 0.0, 1.0);
-	//return 1.0 - _fragLightDist_ * _inv_light_radius;
+	return clamp(1.0 - sqrt(fragLightDist) / lightRadius, 0.0, 1.0);
+	//return 1.0 - fragLightDist * _inv_light_radius;
 }
 
 
@@ -94,31 +94,31 @@ float getAttenuation(in float _fragLightDist_)
 #if defined(SPOT_LIGHT_ENABLED) && defined(SHADOW_ENABLED)
 
 /// @return The blurred shadow
-float pcfLow(in vec3 _shadowUv_)
+float pcfLow(in vec3 shadowUv)
 {
-	const float _mapScale_ = 1.0 / SHADOWMAP_SIZE;
-	const int _kernelSize_ = 8;
-	const vec2 _kernel_[_kernelSize_] = vec2[]
+	const float MAP_SCALE = 1.0 / SHADOWMAP_SIZE;
+	const int KERNEL_SIZE = 8;
+	const vec2 KERNEL[KERNEL_SIZE] = vec2[]
 	(
-		vec2(_mapScale_, _mapScale_),
-		vec2(_mapScale_, -_mapScale_),
-		vec2(-_mapScale_, _mapScale_),
-		vec2(-_mapScale_, -_mapScale_),
-		vec2(0.0, _mapScale_),
-		vec2(0.0, -_mapScale_),
-		vec2(_mapScale_, 0.0),
-		vec2(-_mapScale_, 0.0)
+		vec2(MAP_SCALE, MAP_SCALE),
+		vec2(MAP_SCALE, -MAP_SCALE),
+		vec2(-MAP_SCALE, MAP_SCALE),
+		vec2(-MAP_SCALE, -MAP_SCALE),
+		vec2(0.0, MAP_SCALE),
+		vec2(0.0, -MAP_SCALE),
+		vec2(MAP_SCALE, 0.0),
+		vec2(-MAP_SCALE, 0.0)
 	);
 	
-	float _shadowCol_ = shadow2D(shadowMap, _shadowUv_).r;
-	for(int i=0; i<_kernelSize_; i++)
+	float shadowCol = shadow2D(shadowMap, shadowUv).r;
+	for(int i=0; i<KERNEL_SIZE; i++)
 	{
-		vec3 _uvCoord_ = vec3(_shadowUv_.xy + _kernel_[i], _shadowUv_.z);
-		_shadowCol_ += shadow2D(shadowMap, _uvCoord_).r;
+		vec3 uv = vec3(shadowUv.xy + KERNEL[i], shadowUv.z);
+		shadowCol += shadow2D(shadowMap, uv).r;
 	}
 	
-	_shadowCol_ *= (1.0 / 9.0);
-	return _shadowCol_;
+	shadowCol *= (1.0 / 9.0);
+	return shadowCol;
 }
 
 #endif
@@ -128,48 +128,48 @@ float pcfLow(in vec3 _shadowUv_)
 // doPhong                                                                                                             =
 //======================================================================================================================
 /// Performs phong lighting using the MS FAIs and a few other things
-/// @param _fragPosVspace_ The fragment position in view space
-/// @param _fragLightDist_ Output needed for the attenuation calculation
+/// @param fragPosVspace The fragment position in view space
+/// @param fragLightDist Output needed for the attenuation calculation
 /// @return The final color
-vec3 doPhong(in vec3 _fragPosVspace_, out float _fragLightDist_)
+vec3 doPhong(in vec3 fragPosVspace, out float fragLightDist)
 {
 	// get the vector from the frag to the light
-	vec3 _frag2LightVec_ = lightPos - _fragPosVspace_;
+	vec3 frag2LightVec = lightPos - fragPosVspace;
 
-	// Instead of using normalize(_frag2LightVec_) we brake the operation because we want _fragLightDist_ for the calc of
+	// Instead of using normalize(frag2LightVec) we brake the operation because we want fragLightDist for the calc of
 	// the attenuation
-	_fragLightDist_ = dot(_frag2LightVec_, _frag2LightVec_);
-	vec3 _lightDir_ = _frag2LightVec_ * inversesqrt(_fragLightDist_);
+	fragLightDist = dot(frag2LightVec, frag2LightVec);
+	vec3 lightDir = frag2LightVec * inversesqrt(fragLightDist);
 
 	// Read the normal
-	vec3 _normal_ = unpackNormal(texture2D(msNormalFai, vTexCoords).rg);
+	vec3 normal = unpackNormal(texture2D(msNormalFai, vTexCoords).rg);
 
 	// Lambert term
-	float _lambertTerm_ = dot(_normal_, _lightDir_);
+	float lambertTerm = dot(normal, lightDir);
 
-	if(_lambertTerm_ < 0.0)
+	if(lambertTerm < 0.0)
 	{
 		discard;
 	}
-	//_lambertTerm_ = max(0.0, _lambertTerm_);
+	//lambertTerm = max(0.0, lambertTerm);
 
 	// Diffuce
-	vec3 _diffuse_ = texture2D(msDiffuseFai, vTexCoords).rgb;
-	_diffuse_ *= lightDiffuseCol;
-	vec3 _color_ = _diffuse_ * _lambertTerm_;
+	vec3 diffuse = texture2D(msDiffuseFai, vTexCoords).rgb;
+	diffuse *= lightDiffuseCol;
+	vec3 color = diffuse * lambertTerm;
 
 	// Specular
-	vec4 _specularMix_ = texture2D(msSpecularFai, vTexCoords); // the MS specular FAI has the color and the shininess
-	vec3 _specular_ = _specularMix_.xyz;
-	float _shininess_ = _specularMix_.w;
-
-	vec3 _eyeVec_ = normalize(-_fragPosVspace_);
-	vec3 _h_ = normalize(_lightDir_ + _eyeVec_);
-	float _specIntensity_ = pow(max(0.0, dot(_normal_, _h_)), _shininess_);
-	_color_ += _specular_ * lightSpecularCol * (_specIntensity_ * _lambertTerm_);
+	vec4 specularMix = texture2D(msSpecularFai, vTexCoords); // the MS specular FAI has the color and the shininess
+	vec3 specular = specularMix.xyz;
+	float shininess = specularMix.w;
+
+	vec3 _eyeVec_ = normalize(-fragPosVspace);
+	vec3 h = normalize(lightDir + _eyeVec_);
+	float specIntensity = pow(max(0.0, dot(normal, h)), shininess);
+	color += specular * lightSpecularCol * (specIntensity * lambertTerm);
 	
 	// end
-	return _color_;
+	return color;
 }
 
 
@@ -185,50 +185,50 @@ void main()
 	// Point light
 	//
 	#if defined(POINT_LIGHT_ENABLED)
-		// The func doPhong calculates the frag to light distance (_fragLightDist_) and be cause we need that distance
+		// The func doPhong calculates the frag to light distance (fragLightDist) and be cause we need that distance
 		// latter for other calculations we export it
-		float _fragLightDist_;
-		vec3 _color_ = doPhong(fragPosVspace, _fragLightDist_);
-		fColor = _color_ * getAttenuation(_fragLightDist_);
+		float fragLightDist;
+		vec3 color = doPhong(fragPosVspace, fragLightDist);
+		fColor = color * getAttenuation(fragLightDist);
 
 	//
 	// Spot light
 	//
 	#elif defined(SPOT_LIGHT_ENABLED)
-		vec4 _vTexCoords2_ = texProjectionMat * vec4(fragPosVspace, 1.0);
-		vec3 _vTexCoords3_ = _vTexCoords2_.xyz / _vTexCoords2_.w;
-
-		if(_vTexCoords2_.w > 0.0 &&
-		   _vTexCoords3_.x > 0.0 &&
-		   _vTexCoords3_.x < 1.0 &&
-		   _vTexCoords3_.y > 0.0 &&
-		   _vTexCoords3_.y < 1.0 &&
-		   _vTexCoords2_.w < lightRadius)
+		vec4 vTexCoords2 = texProjectionMat * vec4(fragPosVspace, 1.0);
+		vec3 vTexCoords3 = vTexCoords2.xyz / vTexCoords2.w;
+
+		if(vTexCoords2.w > 0.0 &&
+		   vTexCoords3.x > 0.0 &&
+		   vTexCoords3.x < 1.0 &&
+		   vTexCoords3.y > 0.0 &&
+		   vTexCoords3.y < 1.0 &&
+		   vTexCoords2.w < lightRadius)
 		{
 			// Get shadow
 			#if defined(SHADOW_ENABLED)
 				#if defined(PCF_ENABLED)
-					float _shadowCol_ = pcfLow(_vTexCoords3_);
+					float shadowCol = pcfLow(vTexCoords3);
 				#else
-					float _shadowCol_ = shadow2D(shadowMap, _vTexCoords3_).r;
+					float shadowCol = shadow2D(shadowMap, vTexCoords3).r;
 				#endif
 
-				if(_shadowCol_ == 0.0)
+				if(shadowCol == 0.0)
 				{
 					discard;
 				}
 			#endif
 
-			float _fragLightDist_;
-			vec3 _color_ = doPhong(fragPosVspace, _fragLightDist_);
+			float fragLightDist;
+			vec3 color = doPhong(fragPosVspace, fragLightDist);
 
-			vec3 _lightTexCol_ = texture2DProj(lightTex, _vTexCoords2_.xyz).rgb;
-			float _att_ = getAttenuation(_fragLightDist_);
+			vec3 lightTexCol = texture2DProj(lightTex, vTexCoords2.xyz).rgb;
+			float att = getAttenuation(fragLightDist);
 
 			#if defined(SHADOW_ENABLED)
-				fColor = _lightTexCol_ * _color_ * (_shadowCol_ * _att_);
+				fColor = lightTexCol * color * (shadowCol * att);
 			#else
-				fColor = _lightTexCol_ * _color_ * _att_;
+				fColor = lightTexCol * color * att;
 			#endif
 		}
 		else

+ 5 - 22
shaders/MsMpGeneric.glsl

@@ -4,8 +4,7 @@
 /// in all the buffers
 /// 
 /// Control defines:
-/// DIFFUSE_MAPPING, NORMAL_MAPPING, SPECULAR_MAPPING, PARALLAX_MAPPING, ENVIRONMENT_MAPPING, ALPHA_TESTING,
-/// STATIC_GEOMETRY
+/// DIFFUSE_MAPPING, NORMAL_MAPPING, SPECULAR_MAPPING, PARALLAX_MAPPING, ENVIRONMENT_MAPPING, ALPHA_TESTING
  
 #if defined(ALPHA_TESTING) && !defined(DIFFUSE_MAPPING)
 	#error "Cannot have ALPHA_TESTING without DIFFUSE_MAPPING"
@@ -66,25 +65,13 @@ out vec3 vVertPosViewSpace; ///< For env mapping. AKA view vector
 void main()
 {
 	// calculate the vert pos, normal and tangent
-	#if defined(STATIC_GEOMETRY)
-		vNormal = normal;
-	#else
-		vNormal = normalMat * normal;
-	#endif
+	vNormal = normalMat * normal;
 
 	#if NEEDS_TANGENT
-		#if defined(STATIC_GEOMETRY)
-			vTangent = vec3(tangent);
-		#else
-			vTangent = normalMat * vec3(tangent);
-		#endif
+		vTangent = normalMat * vec3(tangent);
 	#endif
 
-	#if defined(STATIC_GEOMETRY)
-		gl_Position = vec4(position, 1.0);
-	#else
-		gl_Position = modelViewProjectionMat * vec4(position, 1.0);
-	#endif
+	gl_Position = modelViewProjectionMat * vec4(position, 1.0);
 
 	// calculate the rest
 
@@ -99,11 +86,7 @@ void main()
 
 
 	#if defined(ENVIRONMENT_MAPPING) || defined(PARALLAX_MAPPING)
-		#if defined(STATIC_GEOMETRY)
-			vVertPosViewSpace = vec3(viewMat * vec4(position, 1.0));
-		#else
-			vVertPosViewSpace = vec3(modelViewMat * vec4(position, 1.0));
-		#endif
+		vVertPosViewSpace = vec3(modelViewMat * vec4(position, 1.0));
 	#endif
 }
 

+ 9 - 1
src/Renderer/Drawers/SceneDrawer.cpp

@@ -136,7 +136,15 @@ void SceneDrawer::setupShaderProg(const MaterialRuntime& mtlr, const Transform&
 	   mtlr.getStdUniVar(Material::SUV_MODELVIEWPROJECTION_MAT) ||
 	   mtlr.getStdUniVar(Material::SUV_NORMAL_MAT))
 	{
-		modelViewMat = Mat4::combineTransformations(viewMat, modelMat);
+		// Optimization
+		if(modelMat == Mat4::getIdentity())
+		{
+			modelViewMat = viewMat;
+		}
+		else
+		{
+			modelViewMat = Mat4::combineTransformations(viewMat, modelMat);
+		}
 	}
 
 	// set matrices

+ 3 - 0
src/Resources/Material/Material.h

@@ -145,6 +145,9 @@ class Material: private MaterialProps
 		GETTER_R_BY_VAL(bool, depthTesting, isDepthTestingEnabled)
 		GETTER_R_BY_VAL(bool, wireframe, isWireframeEnabled)
 		GETTER_R(boost::ptr_vector<MtlUserDefinedVar>, userDefinedVars, getUserDefinedVars)
+
+		/// Access the base class just for copying in other classes
+		const MaterialProps& accessMaterialPropsBaseClass() const {return *this;}
 		/// @}
 
 		/// @return Return true if the shader has references to texture coordinates

+ 15 - 16
src/Scene/Lights/Light.h

@@ -1,19 +1,3 @@
-/*
-LIGHTING MODEL
-
-Final intensity:                If = Ia + Id + Is
-Ambient intensity:              Ia = Al * Am
-Ambient intensity of light:     Al
-Ambient intensity of material:  Am
-Defuse intensity:               Id = Dl * Dm * LambertTerm
-Defuse intensity of light:      Dl
-Defuse intensity of material:   Dm
-LambertTerm:                    max(Normal dot Light, 0.0)
-Specular intensity:             Is = Sm x Sl x pow(max(R dot E, 0.0), f)
-Specular intensity of light:    Sl
-Specular intensity of material: Sm
-*/
-
 #ifndef LIGHT_H
 #define LIGHT_H
 
@@ -24,6 +8,21 @@ Specular intensity of material: Sm
 
 
 /// Light scene node. It can be spot or point
+////
+/// Explaining the lighting model:
+/// @code
+/// Final intensity:                If = Ia + Id + Is
+/// Ambient intensity:              Ia = Al * Am
+/// Ambient intensity of light:     Al
+/// Ambient intensity of material:  Am
+/// Defuse intensity:               Id = Dl * Dm * LambertTerm
+/// Defuse intensity of light:      Dl
+/// Defuse intensity of material:   Dm
+/// LambertTerm:                    max(Normal dot Light, 0.0)
+/// Specular intensity:             Is = Sm * Sl * pow(max(R dot E, 0.0), f)
+/// Specular intensity of light:    Sl
+/// Specular intensity of material: Sm
+/// @endcode
 class Light: public SceneNode
 {
 	public:

+ 3 - 6
src/Scene/MaterialRuntime/MaterialRuntime.cpp

@@ -10,12 +10,9 @@
 MaterialRuntime::MaterialRuntime(const Material& mtl_):
 	mtl(mtl_)
 {
-	castsShadowFlag = mtl.castsShadow();
-	renderInBlendingStageFlag = mtl.renderInBlendingStage();
-	blendingSfactor = mtl.getBlendingSfactor();
-	blendingDfactor = mtl.getBlendingDfactor();
-	depthTesting = mtl.isDepthTestingEnabled();
-	wireframe = mtl.isWireframeEnabled();
+	MaterialProps& me = *this;
+	const MaterialProps& he = mtl.accessMaterialPropsBaseClass();
+	me = he;
 
 	BOOST_FOREACH(const MtlUserDefinedVar& udv, mtl.getUserDefinedVars())
 	{

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

@@ -13,8 +13,10 @@
 class MaterialRuntime: private MaterialProps
 {
 	public:
+		/// A type
 		typedef boost::ptr_vector<MaterialRuntimeUserDefinedVar> MaterialRuntimeUserDefinedVarContainer;
 
+		/// The one and only contructor
 		MaterialRuntime(const Material& mtl);
 
 		/// @name Accessors
@@ -31,6 +33,7 @@ class MaterialRuntime: private MaterialProps
 		/// @see getUserDefinedVarByName
 		const MaterialRuntimeUserDefinedVar& getUserDefinedVarByName(const char* name) const;
 
+		/// Get the resource
 		const Material& getMaterial() const {return mtl;}
 
 		/// Find var and set its value

+ 3 - 28
src/Scene/MaterialRuntime/MaterialRuntimeUserDefinedVar.cpp

@@ -2,39 +2,14 @@
 
 
 //======================================================================================================================
-// Visitor functors                                                                                                    =
+// ConstructVisitor::operator() <RsrcPtr<Texture> >                                                                    =
 //======================================================================================================================
-
-void MaterialRuntimeUserDefinedVar::ConstructVisitor::operator()(float x) const
-{
-	udvr.data = x;
-}
-
-void MaterialRuntimeUserDefinedVar::ConstructVisitor::operator()(const Vec2& x) const
-{
-	udvr.data = x;
-}
-
-void MaterialRuntimeUserDefinedVar::ConstructVisitor::operator()(const Vec3& x) const
-{
-	udvr.data = x;
-}
-
-void MaterialRuntimeUserDefinedVar::ConstructVisitor::operator()(const Vec4& x) const
-{
-	udvr.data = x;
-}
-
-void MaterialRuntimeUserDefinedVar::ConstructVisitor::operator()(const RsrcPtr<Texture>& x) const
+template <>
+void MaterialRuntimeUserDefinedVar::ConstructVisitor::operator()<RsrcPtr<Texture> >(const RsrcPtr<Texture>& x) const
 {
 	udvr.data = &x;
 }
 
-void MaterialRuntimeUserDefinedVar::ConstructVisitor::operator()(MtlUserDefinedVar::Fai x) const
-{
-	udvr.data = x;
-}
-
 
 //======================================================================================================================
 // Constructor                                                                                                         =

+ 3 - 6
src/Scene/MaterialRuntime/MaterialRuntimeUserDefinedVar.h

@@ -47,12 +47,9 @@ class MaterialRuntimeUserDefinedVar
 
 				ConstructVisitor(MaterialRuntimeUserDefinedVar& 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;
+				/// Template method that applies to all DataVariant values except texture resource
+				template<typename Type>
+				void operator()(const Type& x) const {udvr.data = x;}
 		};
 
 		DataVariant data;