Sign in to follow this  
Gobbla

Advection and bilinear interpolation in glsl

Recommended Posts

Hello everyone ! I am in the process of converting my 2d fluid simulation to 3d and i cant get i to work properly. The main problem is that i dont know exactly where the problem is. I suspect that it is my advection implementation that is incorrect because it seems like it behaving a bit strange. I have been trying to solve this for a while now and i am getting quite frustrated :S. So i hope some of you can spot the error.

uniform sampler3D velocitySource;
uniform sampler3D quantityToAdvect;

uniform float dt;
uniform float rdx;


void main(void) 
{
	// trace velocity backwards in time, 	
	vec3 pos = gl_TexCoord[0].stp - dt * rdx * texture3D(velocitySource, gl_TexCoord[0].stp).xyz;    

	vec3 coords1;
	coords1  = floor(pos*66.0 - 0.5)+ 0.5 ; // 66 is the texture size , the 0.5 is to get the correct texel center 
	
	vec3 coords2;
	coords2 = coords1 + 1.0;
	
	// converts back to range 0..1
	coords1 /= 66.0;
	coords2 /= 66.0;
	
		
	// get values from texture
	// all points in slice z
	
	vec4 value111 =  texture3D(quantityToAdvect, vec3(coords1.x, coords1.y, coords1.z)); 
	vec4 value211 =  texture3D(quantityToAdvect, vec3(coords2.x, coords1.y, coords1.z)); 
	vec4 value121 =  texture3D(quantityToAdvect, vec3(coords1.x, coords2.y, coords1.z));
	vec4 value221 =  texture3D(quantityToAdvect, vec3(coords2.x, coords2.y, coords1.z)); 

	// all points in slice z+1
	vec4 value112 =  texture3D(quantityToAdvect, vec3(coords1.x, coords1.y, coords2.z)); 
	vec4 value212 =  texture3D(quantityToAdvect, vec3(coords2.x, coords1.y, coords2.z)); 
	vec4 value122 =  texture3D(quantityToAdvect, vec3(coords1.x, coords2.y, coords2.z));
	vec4 value222 =  texture3D(quantityToAdvect, vec3(coords2.x, coords2.y, coords2.z));  	


	// interpolation amount 
	vec3 amount;
	amount = pos - coords1;
	
	// bilinear interpolation
	vec4 value11 = mix(value111 , value112 , amount.z);
	vec4 value12 = mix(value121 , value122 , amount.z);
	vec4 value21 = mix(value211 , value212 , amount.z);
	vec4 value22 = mix(value221 , value222 , amount.z);	
	
	vec4 finalValue = mix(mix(value11, value21, amount.x) , mix(value12 , value22, amount.x) , amount.y);

	gl_FragColor = finalValue;

	
}




I know i can be hard find problems in random code like this, but i hope someone can figure it out =)

Share this post


Link to post
Share on other sites
I solved it !

I did not convertet my position to global coordinates before stepping back in time.

Here is the updated code if someone is interested:


uniform sampler3D velocitySource;
uniform sampler3D quantityToAdvect;

uniform float dt;
uniform float rdx;


void main(void)
{
// trace velocity backwards in time,
vec3 pos = gl_TexCoord[0].stp*66.0 - dt * rdx * texture3D(velocitySource, gl_TexCoord[0].stp).xyz;

vec3 coords1;
coords1 = floor(pos - 0.5)+ 0.5 ; // 66 is the texture size , the 0.5 is to get the correct texel center

vec3 coords2;
coords2 = coords1 + 1.0;

// interpolation amount
vec3 amount;
amount = pos - coords1;

// converts back to range 0..1
coords1 /= 66.0;
coords2 /= 66.0;


// get values from texture
// all points in slice z

vec4 value111 = texture3D(quantityToAdvect, vec3(coords1.x, coords1.y, coords1.z));
vec4 value211 = texture3D(quantityToAdvect, vec3(coords2.x, coords1.y, coords1.z));
vec4 value121 = texture3D(quantityToAdvect, vec3(coords1.x, coords2.y, coords1.z));
vec4 value221 = texture3D(quantityToAdvect, vec3(coords2.x, coords2.y, coords1.z));

// all points in slice z+1
vec4 value112 = texture3D(quantityToAdvect, vec3(coords1.x, coords1.y, coords2.z));
vec4 value212 = texture3D(quantityToAdvect, vec3(coords2.x, coords1.y, coords2.z));
vec4 value122 = texture3D(quantityToAdvect, vec3(coords1.x, coords2.y, coords2.z));
vec4 value222 = texture3D(quantityToAdvect, vec3(coords2.x, coords2.y, coords2.z));




// bilinear interpolation
vec4 value11 = mix(value111 , value112 , amount.z);
vec4 value12 = mix(value121 , value122 , amount.z);
vec4 value21 = mix(value211 , value212 , amount.z);
vec4 value22 = mix(value221 , value222 , amount.z);

vec4 finalValue = mix(mix(value11, value21, amount.x) , mix(value12 , value22, amount.x) , amount.y);

gl_FragColor = finalValue;
}





Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this