[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [18674] branches/sim_physics/source/ blender: Volume rendering: multiple scattering

Matt Ebb matt at mke3.net
Mon Jan 26 03:42:29 CET 2009


Revision: 18674
          http://projects.blender.org/plugins/scmsvn/viewcvs.php?view=rev&root=bf-blender&revision=18674
Author:   broken
Date:     2009-01-26 03:42:17 +0100 (Mon, 26 Jan 2009)

Log Message:
-----------
Volume rendering: multiple scattering

This is mostly a contribution from Raul 'farsthary' Hernandez - an approximation for 
multiple scattering within volumes. Thanks, Raul! Where single scattering considers 
the path from the light to a point in the volume, and to the eye, multiple scattering 
approximates the interactions of light as it bounces around randomly within the 
volume, before eventually reaching the eye.

It works as a diffusion process that effectively blurs the lighting information 
that's already stored within the light cache.

A cloudy sky setup, with single scattering, and multiple scattering:
http://mke3.net/blender/devel/rendering/volumetrics/vol_sky_ss_ms.jpg
http://mke3.net/blender/devel/rendering/volumetrics/sky_ms.blend

To enable it, there is a menu in the volume panel (which needs a bit of cleanup, for 
later), that lets you choose between self-shading methods:

* None: No attenuation of the light source by the volume - light passes straight 
through at full strength
* Single Scattering: (same as previously, with 'self-shading' enabled)
* Multiple Scattering: Uses multiple scattering only for shading information
* Single + Multiple: Adds the multiple scattering lighting on top of the existing 
single scattered light - this can be useful to tweak the strength of the effect, 
while still retaining details in the lighting.

An example of how the different scattering methods affect the visual result:
http://mke3.net/blender/devel/rendering/volumetrics/ss_ms_comparison.jpg
http://mke3.net/blender/devel/rendering/volumetrics/ss_ms_comparison.blend


The multiple scattering methods introduce 3 new controls when enabled:
* Blur: A factor blending between fully diffuse/blurred lighting, and sharper
* Spread: The range that the diffuse blurred lighting spreads over - similar to a 
blur width. The higher the spread, the slower the processing time.
* Intensity: A multiplier for the multiple scattering light brightness

Here's the effect of multiple scattering on a tight beam (similar to a laser). The 
effect of the 'spread' value is pretty clear here:
http://mke3.net/blender/devel/rendering/volumetrics/ms_spread_laser.jpg

Unlike the rest of the system so far, this part of the volume rendering engine isn't 
physically based, and currently it's not unusual to get non-physical results (i.e. 
much more light being scattered out then goes in via lamps or emit). To counter this, 
you can use the intensity slider to tweak the brightness - on the todo, perhaps there is a more automatic method we can work on for this later on. I'd also like to check 
on speeding this up further with threading too.

Modified Paths:
--------------
    branches/sim_physics/source/blender/blenloader/intern/readfile.c
    branches/sim_physics/source/blender/makesdna/DNA_material_types.h
    branches/sim_physics/source/blender/render/intern/include/volume_precache.h
    branches/sim_physics/source/blender/render/intern/source/volume_precache.c
    branches/sim_physics/source/blender/render/intern/source/volumetric.c
    branches/sim_physics/source/blender/src/buttons_shading.c

Modified: branches/sim_physics/source/blender/blenloader/intern/readfile.c
===================================================================
--- branches/sim_physics/source/blender/blenloader/intern/readfile.c	2009-01-25 21:02:52 UTC (rev 18673)
+++ branches/sim_physics/source/blender/blenloader/intern/readfile.c	2009-01-26 02:42:17 UTC (rev 18674)
@@ -8072,17 +8072,27 @@
 			}
 		}
 	}
-	
+
 	if (main->versionfile < 248 || (main->versionfile == 248 && main->subversionfile < 3)) {
 		Tex *tex;
+		Material *ma;
 		
 		/* blend texture extrapolation */
 		for(tex=main->tex.first; tex; tex= tex->id.next) {
 			if (tex->type == TEX_BLEND)
 				tex->extend = TEX_EXTEND;
 		}
+		
+		for(ma=main->mat.first; ma; ma= ma->id.next) {
+			if (ma->vol_shadeflag & 2) { // old MA_VOL_ATTENUATED
+				ma->vol_shade_type = MA_VOL_SHADE_SINGLE;
+				ma->vol_ms_diff = 0.5f;
+				ma->vol_ms_steps = 5;
+				ma->vol_ms_intensity = 1.0;
+			}
+		}
 	}
-	
+
 	/* WATCH IT!!!: pointers from libdata have not been converted yet here! */
 	/* WATCH IT 2!: Userdef struct init has to be in src/usiblender.c! */
 

Modified: branches/sim_physics/source/blender/makesdna/DNA_material_types.h
===================================================================
--- branches/sim_physics/source/blender/makesdna/DNA_material_types.h	2009-01-25 21:02:52 UTC (rev 18673)
+++ branches/sim_physics/source/blender/makesdna/DNA_material_types.h	2009-01-26 02:42:17 UTC (rev 18674)
@@ -70,13 +70,18 @@
 	short vol_precache_resolution;
 	float vol_stepsize, vol_shade_stepsize;
 	float vol_depth_cutoff;
-	float vpad;
+	short vol_shade_type;
+	short vpad;
 	float vol_density_scale;
 	float vol_absorption, vol_scattering;
 	float vol_absorption_col[3];
 	short vol_shadeflag;
 	short vol_phasefunc_type;
 	float vol_phasefunc_g;
+	float vpad2;
+	
+	float vol_ms_diff, vol_ms_intensity;
+	int vol_ms_steps;
 		
 	float fresnel_mir, fresnel_mir_i;
 	float fresnel_tra, fresnel_tra_i;
@@ -358,11 +363,16 @@
 
 /* vol_shadeflag */
 #define MA_VOL_SHADED		1
-#define MA_VOL_ATTENUATED	2
 #define MA_VOL_RECVSHADOW	4
 #define MA_VOL_PRECACHESHADING	8
 #define MA_VOL_USEALPHA		16
 
+/* vol_shading_type */
+#define MA_VOL_SHADE_NONE					0
+#define MA_VOL_SHADE_SINGLE					1
+#define MA_VOL_SHADE_MULTIPLE				2
+#define MA_VOL_SHADE_SINGLEPLUSMULTIPLE		3
+
 /* vol_phasefunc_type */
 #define MA_VOL_PH_ISOTROPIC		0
 #define MA_VOL_PH_MIEHAZY		1

Modified: branches/sim_physics/source/blender/render/intern/include/volume_precache.h
===================================================================
--- branches/sim_physics/source/blender/render/intern/include/volume_precache.h	2009-01-25 21:02:52 UTC (rev 18673)
+++ branches/sim_physics/source/blender/render/intern/include/volume_precache.h	2009-01-26 02:42:17 UTC (rev 18674)
@@ -28,4 +28,6 @@
  
 void volume_precache(Render *re);
 void free_volume_precache(Render *re);
-int point_inside_volume_objectinstance(ObjectInstanceRen *obi, float *co);
\ No newline at end of file
+int point_inside_volume_objectinstance(ObjectInstanceRen *obi, float *co);
+
+#define VOL_MS_TIMESTEP	0.1f
\ No newline at end of file

Modified: branches/sim_physics/source/blender/render/intern/source/volume_precache.c
===================================================================
--- branches/sim_physics/source/blender/render/intern/source/volume_precache.c	2009-01-25 21:02:52 UTC (rev 18673)
+++ branches/sim_physics/source/blender/render/intern/source/volume_precache.c	2009-01-26 02:42:17 UTC (rev 18674)
@@ -47,6 +47,7 @@
 #include "render_types.h"
 #include "renderdatabase.h"
 #include "volumetric.h"
+#include "volume_precache.h"
 
 
 #include "BKE_global.h"
@@ -211,120 +212,141 @@
 	}
 }
 
+static inline int I(int x,int y,int z,int n) //has a pad of 1 voxel surrounding the core for boundary simulation
+{ 
+	return (x*(n+2)+y)*(n+2)+z;
+}
 
-void vol_precache_objectinstance(Render *re, ObjectInstanceRen *obi, Material *ma, float *bbmin, float *bbmax)
+static void ms_diffuse(int b, float* x0, float* x, float diff, int n)
 {
-	int x, y, z;
+	int i, j, k, l;
+	const float dt = VOL_MS_TIMESTEP;
+	const float a = dt*diff*n*n*n;
+	
+	for (l=0; l<20; l++)
+	{
+		for (k=1; k<=n; k++)
+		{
+			for (j=1; j<=n; j++)
+			{
+				for (i=1; i<=n; i++)
+				{
+					x[I(i,j,k,n)] = (x0[I(i,j,k,n)] + a*(
+														 x[I(i-1,j,k,n)]+x[I(i+1,j,k,n)]+
+														 x[I(i,j-1,k,n)]+x[I(i,j+1,k,n)]+
+														 x[I(i,j,k-1,n)]+x[I(i,j,k+1,n)]))/(1+6*a);
+				}
+			}
+		}
+	}
+}
 
-	float co[3], voxel[3], scatter_col[3];
-	ShadeInput shi;
-	float view[3] = {0.0,0.0,-1.0};
-	float density;
-	float stepsize;
+void multiple_scattering_diffusion(Render *re, float *cache, int res, Material *ma)
+{
+	const float diff = ma->vol_ms_diff * 0.001f; 	/* compensate for scaling for a nicer UI range */
+	const float fac = ma->vol_ms_intensity;
+	const float simframes = ma->vol_ms_steps;
+	const int shade_type = ma->vol_shade_type;
+	const float dt = VOL_MS_TIMESTEP;
+
+	int i, j, k, m;
+	int n = res;
+	const int size = (n+2)*(n+2)*(n+2);
+	double time, lasttime= PIL_check_seconds_timer();
+	float total;
+	float c=1.0f;
+	int index;
+	float origf;	/* factor for blending in original light cache */
 	
-	float resf, res_3f;
-	int res_2, res_3;
 	
-	float i = 1.0f;
-	double time, lasttime= PIL_check_seconds_timer();
-	const int res = ma->vol_precache_resolution;
-	RayTree *tree;
+	float *sr0=(float *)MEM_callocN(size*sizeof(float), "temporary multiple scattering buffer");
+	float *sr=(float *)MEM_callocN(size*sizeof(float), "temporary multiple scattering buffer");
+	float *sg0=(float *)MEM_callocN(size*sizeof(float), "temporary multiple scattering buffer");
+	float *sg=(float *)MEM_callocN(size*sizeof(float), "temporary multiple scattering buffer");
+	float *sb0=(float *)MEM_callocN(size*sizeof(float), "temporary multiple scattering buffer");
+	float *sb=(float *)MEM_callocN(size*sizeof(float), "temporary multiple scattering buffer");
+
+	total = (float)(n*n*n*simframes);
 	
-	R = *re;
+	/* Scattering as diffusion pass */
+	for (m=0; m<simframes; m++)
+	{
+		/* add sources */
+		for (k=1; k<=n; k++)
+		{
+			for (j=1; j<=n; j++)
+			{
+				for (i=1; i<=n; i++)
+				{
+					time= PIL_check_seconds_timer();
+					c++;
+					
+					index=(i-1)*n*n + (j-1)*n + k-1;
+					
+					if (cache[index] > 0.0f)
+						sr[I(i,j,k,n)] += cache[index];
+					if (cache[1*n*n*n + index] > 0.0f)
+						sg[I(i,j,k,n)] += cache[1*n*n*n + index];
+					if (cache[2*n*n*n + index] > 0.0f)
+						sb[I(i,j,k,n)] += cache[2*n*n*n + index];
 
-	/* create a raytree with just the faces of the instanced ObjectRen, 
-	 * used for checking if the cached point is inside or outside. */
-	tree = create_raytree_obi(obi, bbmin, bbmax);
-	if (!tree) return;
 
-	/* Need a shadeinput to calculate scattering */
-	memset(&shi, 0, sizeof(ShadeInput)); 
-	shi.depth= 1;
-	shi.mask= 1;
-	shi.mat = ma;
-	shi.vlr = NULL;
-	memcpy(&shi.r, &shi.mat->r, 23*sizeof(float));	// note, keep this synced with render_types.h
-	shi.har= shi.mat->har;
-	shi.obi= obi;
-	shi.obr= obi->obr;
-	shi.lay = re->scene->lay;
-	VECCOPY(shi.view, view);
-	
-	stepsize = vol_get_stepsize(&shi, STEPSIZE_VIEW);
-
-	resf = (float)res;
-	res_2 = res*res;
-	res_3 = res*res*res;
-	res_3f = (float)res_3;
-	
-	VecSubf(voxel, bbmax, bbmin);
-	if ((voxel[0] < FLT_EPSILON) || (voxel[1] < FLT_EPSILON) || (voxel[2] < FLT_EPSILON))
-		return;
-	VecMulf(voxel, 1.0f/res);
-	
-	obi->volume_precache = MEM_callocN(sizeof(float)*res_3*3, "volume light cache");
-	
-	/* Iterate over the 3d voxel grid, and fill the voxels with scattering information
-	 *
-	 * It's stored in memory as 3 big float grids next to each other, one for each RGB channel.
-	 * I'm guessing the memory alignment may work out better this way for the purposes
-	 * of doing linear interpolation, but I haven't actually tested this theory! :)
-	 */
-	for (x=0; x < res; x++) {
-		co[0] = bbmin[0] + (voxel[0] * x);
-		
-		for (y=0; y < res; y++) {
-			co[1] = bbmin[1] + (voxel[1] * y);
-			
-			for (z=0; z < res; z++) {
-				co[2] = bbmin[2] + (voxel[2] * z);
-			
-				time= PIL_check_seconds_timer();
-				i++;
-			
-				/* display progress every second */
-				if(re->test_break()) {
-					if(tree) {
-						RE_ray_tree_free(tree);
-						tree= NULL;
+					/* Displays progress every second */
+					if(time-lasttime>1.0f) {
+						char str[64];
+						sprintf(str, "Simulating multiple scattering: %d%%", (int)
+								(100.0f * (c / total)));
+						re->i.infostr= str;
+						re->stats_draw(&re->i);
+						re->i.infostr= NULL;
+						lasttime= time;
 					}
-					return;
 				}
-				if(time-lasttime>1.0f) {
-					char str[64];
-					sprintf(str, "Precaching volume: %d%%", (int)(100.0f * (i / res_3f)));
-					re->i.infostr= str;
-					re->stats_draw(&re->i);
-					re->i.infostr= NULL;
-					lasttime= time;
-				}
-				
-				/* don't bother if the point is not inside the volume mesh */
-				if (!point_inside_obi(tree, obi, co)) {
-					obi->volume_precache[0*res_3 + x*res_2 + y*res + z] = -1.0f;
-					obi->volume_precache[1*res_3 + x*res_2 + y*res + z] = -1.0f;
-					obi->volume_precache[2*res_3 + x*res_2 + y*res + z] = -1.0f;
-					continue;
-				}
-				density = vol_get_density(&shi, co);
-				vol_get_scattering(&shi, scatter_col, co, stepsize, density);
-			
-				obi->volume_precache[0*res_3 + x*res_2 + y*res + z] = scatter_col[0];
-				obi->volume_precache[1*res_3 + x*res_2 + y*res + z] = scatter_col[1];
-				obi->volume_precache[2*res_3 + x*res_2 + y*res + z] = scatter_col[2];
 			}
 		}
-	}
+		SWAP(float *, sr, sr0);
+		SWAP(float *, sg, sg0);
+		SWAP(float *, sb, sb0);
 
-	if(tree) {
-		RE_ray_tree_free(tree);
-		tree= NULL;
+		/* main diffusion simulation */
+		ms_diffuse(0, sr0, sr, diff, n);
+		ms_diffuse(0, sg0, sg, diff, n);
+		ms_diffuse(0, sb0, sb, diff, n);
+		
+		if (re->test_break()) break;
 	}
 	
-	lightcache_filter(obi->volume_precache, res);
+	/* copy to light cache */
 
+	if (shade_type == MA_VOL_SHADE_SINGLEPLUSMULTIPLE)
+		origf = 1.0f;
+	else
+		origf = 0.0f;
+
+	for (k=1;k<=n;k++)
+	{
+		for (j=1;j<=n;j++)
+		{
+			for (i=1;i<=n;i++)
+			{
+				index=(i-1)*n*n + (j-1)*n + k-1;
+				cache[index]			= origf * cache[index]  + fac * sr[I(i,j,k,n)];
+				cache[1*n*n*n + index]	= origf * cache[1*n*n*n + index] + fac * sg[I(i,j,k,n)];
+				cache[2*n*n*n + index]	= origf * cache[2*n*n*n + index] + fac * sb[I(i,j,k,n)];
+			}
+		}
+	}
+
+	MEM_freeN(sr0);
+	MEM_freeN(sr);
+	MEM_freeN(sg0);
+	MEM_freeN(sg);
+	MEM_freeN(sb0);
+	MEM_freeN(sb);
 }
 
+
+
 #if 0 // debug stuff

@@ Diff output truncated at 10240 characters. @@




More information about the Bf-blender-cvs mailing list