[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [34699] trunk/blender/source/blender/ render/intern/source/render_texture.c: bump-mapping update to properly support

M.G. Kishalmi lmg at kishalmi.net
Mon Feb 7 22:55:56 CET 2011


Revision: 34699
          http://projects.blender.org/scm/viewvc.php?view=rev&root=bf-blender&revision=34699
Author:   lmg
Date:     2011-02-07 21:55:54 +0000 (Mon, 07 Feb 2011)
Log Message:
-----------
bump-mapping update to properly support
 multiple textures in different bump-spaces.

kudos to M.L.Mikkelsen (sparky) for helping with the math! :)

Modified Paths:
--------------
    trunk/blender/source/blender/render/intern/source/render_texture.c

Modified: trunk/blender/source/blender/render/intern/source/render_texture.c
===================================================================
--- trunk/blender/source/blender/render/intern/source/render_texture.c	2011-02-07 18:42:07 UTC (rev 34698)
+++ trunk/blender/source/blender/render/intern/source/render_texture.c	2011-02-07 21:55:54 UTC (rev 34699)
@@ -1879,12 +1879,14 @@
 /* Improved bump code from later in 2.5 development cycle */
 
 typedef struct NTapBump {
-	int nunvdone;
-
+	int init_done;
+	int iPrevBumpSpace;	// 0: uninitialized, 1: objectspace, 2: texturespace, 4: viewspace
 	// bumpmapping
+	float vNorg[3]; // backup copy of shi->vn
 	float vNacc[3]; // original surface normal minus the surface gradient of every bump map which is encountered
 	float vR1[3], vR2[3]; // cross products (sigma_y, original_normal), (original_normal, sigma_x)
 	float sgn_det; // sign of the determinant of the matrix {sigma_x, sigma_y, original_normal}
+	float fPrevMagnitude; // copy of previous magnitude, used for multiple bumps in different spaces
 } NTapBump;
 
 static void ntap_bump_init(NTapBump *ntap_bump)
@@ -1897,22 +1899,32 @@
 	TexResult ttexr = {0, 0, 0, 0, 0, texres->talpha, NULL};	// temp TexResult
 
 	const int fromrgb = ((tex->type == TEX_IMAGE) || ((tex->flag & TEX_COLORBAND)!=0));
-	float Hscale = 0.1f * Tnor*mtex->norfac; // factor 0.1 proved to look like the previous bump code
+	float Hscale = Tnor*mtex->norfac;
 
 	// 2 channels for 2D texture and 3 for 3D textures.
 	const int nr_channels = (mtex->texco == TEXCO_UV)? 2 : 3;
-	int c, rgbnor;
+	int c, rgbnor, iBumpSpace;
 	float dHdx, dHdy;
 
 	// disable internal bump eval in sampler, save pointer
 	float *nvec = texres->nor;
 	texres->nor = NULL;
 
-	// TODO: solve this Hscale issue more elegantly.
-	if( mtex->texflag & MTEX_BUMP_TEXTURESPACE )
+	if( mtex->texflag & MTEX_BUMP_TEXTURESPACE ) {
 		if(tex->ima)
-			Hscale *= 130.0f;
+			Hscale *= 13.0f; // appears to be a sensible default value
+	} else
+		Hscale *= 0.1f; // factor 0.1 proved to look like the previous bump code
 
+	if( !ntap_bump->init_done ) {
+		VECCOPY(ntap_bump->vNacc, shi->vn);
+		VECCOPY(ntap_bump->vNorg, shi->vn);
+		ntap_bump->fPrevMagnitude = 1.0f;
+		ntap_bump->iPrevBumpSpace = 0;
+		
+		ntap_bump->init_done = 1;
+	}
+	
 	if(!(mtex->texflag & MTEX_5TAP_BUMP)) {
 		// compute height derivatives with respect to output image pixel coordinates x and y
 		float STll[3], STlr[3], STul[3];
@@ -1998,17 +2010,25 @@
 		[Mik10] Mikkelsen M. S.: Bump Mapping Unparametrized Surfaces on the GPU.
 		-> http://jbit.net/~sparky/sfgrad_bump/mm_sfgrad_bump.pdf */
 
-	if(!ntap_bump->nunvdone) {
+	if( mtex->texflag & MTEX_BUMP_OBJECTSPACE )
+		iBumpSpace = 1;
+	else if( mtex->texflag & MTEX_BUMP_TEXTURESPACE )
+		iBumpSpace = 2;
+	else
+		iBumpSpace = 4; // ViewSpace
+	
+	if( ntap_bump->iPrevBumpSpace != iBumpSpace ) {
+		
 		// initialize normal perturbation vectors
 		int xyz;
-		float fDet, abs_fDet;
+		float fDet, abs_fDet, fMagnitude;
 		// object2view and inverted matrix
 		float obj2view[3][3], view2obj[3][3], tmp[4][4];
 		// local copies of derivatives and normal
 		float dPdx[3], dPdy[3], vN[3];
 		VECCOPY(dPdx, shi->dxco);
 		VECCOPY(dPdy, shi->dyco);
-		VECCOPY(vN, shi->vn);
+		VECCOPY(vN, ntap_bump->vNorg);
 		
 		if( mtex->texflag & MTEX_BUMP_OBJECTSPACE ) {
 			// TODO: these calculations happen for every pixel!
@@ -2040,17 +2060,21 @@
 			}
 		}
 		
-		for(xyz=0; xyz<3; xyz++)
-				ntap_bump->vNacc[xyz] = abs_fDet * vN[xyz];
-	
+		fMagnitude = abs_fDet;
 		if( mtex->texflag & MTEX_BUMP_OBJECTSPACE ) {
 			// pre do transform of texres->nor by the inverse transposed of obj2view
-			mul_transposed_m3_v3( view2obj, ntap_bump->vNacc );
+			mul_transposed_m3_v3( view2obj, vN );
 			mul_transposed_m3_v3( view2obj, ntap_bump->vR1 );
 			mul_transposed_m3_v3( view2obj, ntap_bump->vR2 );
+			
+			fMagnitude *= len_v3(vN);
 		}
-
-		ntap_bump->nunvdone= 1;
+		
+		for(xyz=0; xyz<3; xyz++)
+				ntap_bump->vNacc[xyz] *= fMagnitude / ntap_bump->fPrevMagnitude;
+		
+		ntap_bump->fPrevMagnitude = fMagnitude;
+		ntap_bump->iPrevBumpSpace = iBumpSpace;
 	}
 
 	if( mtex->texflag & MTEX_BUMP_TEXTURESPACE ) {




More information about the Bf-blender-cvs mailing list