View Full Version : Liquid Effect with OpenGl

02-19-2014, 01:31 PM
Hi there,

I`m completely new in OpenGl and I need to implement a kind of fluideffect like in Portal 2 for my study.
The task is to:

-first: implement a particlesystem (I did that)
-second: implement a geometryshader to do the billboarding stuff (I did that either)
-third: implement a raycasting in the fragmentshader from camera to every fragment (every fragment on billboards passed from geometryshader) to find out if the ray is within an isosurface.

with that it should look like a kind of metaballeffect.

my fragmentshader looks like this:

vec4 color;
vec4 ndcPos;
ndcPos.xy = ((2.0 * gl_FragCoord.xy) - (2.0 * viewport.xy)) / (viewport.zw) - 1;
ndcPos.z = (2.0 * gl_FragCoord.z - gl_DepthRange.near - gl_DepthRange.far) /
(gl_DepthRange.far - gl_DepthRange.near);
ndcPos.w = 1.0;

vec4 clipPos = ndcPos / gl_FragCoord.w;
vec4 eyePos = invPersMatrix * clipPos;

vec3 ray_origin = vec3(0,0,0);
vec3 ray_direction = normalize(eyePos).xyz;

vec3 ray_pos = ray_origin;

for(int i = 1; i <= 100; i++)
float stepDistance = 0.4f;

ray_pos += ray_direction * stepDistance;


color = vec4(ray_pos, 1.0f);
color = vec4(ray_pos, 0.0f);


outputColor = color;

the isInScalarField() function tests if the ray intersects with the implicitsurface (i don`t know if that is the right vocabulary for that in english :D )
it looks like this:

bool isInScalarField(vec3 x)

float isoWert = 0.5f;
float k = 7;
float s = 0.0f;

for(int j = 0; j < 8; j++)
float distance = length(x - neighbours[j]);

s += exp( -k * distance);


if(s > isoWert)
return true;

return false;


neighbours are the neighbourparticles for each particle which has been passed to vertexshader. I pass them to fragmentshader as a uniform array.

My problem ist, that it doesnt look like it should. With this code the billboards, which are quads created by geometryshader, are weird cutted. I think that my vectors are messed up or the neighbours are wrong. I googled hours and hours and tried everything to get this working, but I failed ;( and now I need some help to get this working.

I made a quick video to show my problem:


thank you in advance

Dark Photon
02-19-2014, 02:24 PM
My problem ... With this code the billboards, which are quads created by geometryshader, are weird cutted.

Would check to see if your quads are intersecting the near (or far clip planes). Might try changing the clip planes on your frustum and see if you get different results.

02-19-2014, 11:10 PM
Hello Dark Photon,

I checked it, but near is set to 0.1 and far to 100 (I changed the values, but with the same results). When I zoom in and move the camera, the only changes are that the quads are cutted differently.
Are the vectors correct? When they are ok, the only reason why the result is so weird is that the neighbours are wrong. And with wrong neighbours the isosurface cant be computed right.