Sunday, November 27, 2011

Systems now have interaction

Before I could have interacting rigid body/fluid systems I first had to create a rigid body class where the rigid bodies are composed of particles. To have a rigid body system, I needed to discritize the body into a set of particles. To do this I utilized a method from [2], which uses opengl to create a 3d texture representing voxels inside and outside the rigid-body. From the resulting 3D texture, I can then generate a particle field. For each of the particles, we need position, velocity, and force for each particle that is contained in the rigid body. Also, for each rigid body we should track the linear and torque forces on the center of mass, velocity of center of mass, angular velocity, and the center of mass position.

To calculate the total linear force on a single rigid body, simply sum all the forces of each particle that belongs to that rigid body. [1]

$F_{total} = \displaystyle \sum_{i=0}^N f_i$

And the total torque can be calculated by summing up the cross product of the linear force with the radius from the center of mass for each particle. [1]

$F_{torque} = \displaystyle \sum_{i=0}^N ((pos_i-pos_{com}) \times f_i)$

Currently I don't have any constraints implemented. Also, I still need to implement the segmented scan to improve the calculation of force totals on the rigid body. Currently I naively loop and sum for each rigid body which is inefficient. I also need to create a renderer which will render rigid body meshes at the appropriate locations.

Here is a video of my progress so far.


Rigid-body and Fluid interaction with velocity vectors from Andrew Young on Vimeo.

In the video above, I render the velocity vectors of each particle. This helps to visualize the flow of the fluid. I plan to create a separate blog to discuss my implementation of the rendering code for the velocity vectors.

The interaction between fluid and rigid-body is modeled by a spring/dampening system. The calculations are based on the following two equations as described in [1].

$f_s = -k(d-r_{ij})\frac{r_{ij}}{\left|r_{ij}\right|}$

$f_d = \eta(v_{j}-v_{i})$

Where k is the spring constant and $\eta$ is the dampening coefficient. I need to experiment with these coefficients to get more accurate results.

I also focused a lot of time to refactoring the code around the System class. The System class was abstract and had very little functionality. When creating the rigid body class, I noticed I was just copying and pasting a lot of code. That was a perfect reason to push things up to the parent class.

I moved a lot of generic functionality as well as several buffers (position, velocity, force) that were common to all systems up to the System class. I then added some rudimentary code to all for interaction between systems. Currently this only works between SPH and Rigidbody systems. Eventually, I hope to have a better interaction framework where you can define new rules of interaction quickly and easily. Then I could add interaction between Flocks and SPH which could provide some very interesting simulations.

Todo:

  1. Verify spacing of the rigid-body system is correct.
  2. Experiment with coefficients for interaction.
  3. Explore the alternative formulations for system interaction.
  4. Continue refactoring the code.
  5. Create a class to better handle particle shapes.



References

[1] Harada, T., Tanaka, M., Koshizuka, S., & Kawaguchi, Y. (2007). Real-time Coupling of Fluids and Rigid Bodies. Proc of the APCOM, 1-13. Retrieved from http://individuals.iii.u-tokyo.ac.jp/~yoichiro/report/report-pdf/harada/international/2007apcom.pdf

[2] Fang, S., & Chen, H. (2000). Hardware accelerated voxelization. Computers & Graphics, 24(3), 433-442. Elsevier. Retrieved from http://www.sciencedirect.com/science/article/pii/S0097849300000388

Tuesday, October 25, 2011

Fluid and Rigid Body Interaction

I have finally settled on a topic for my master's thesis. I will implement some techniques discussed in GPU Gems 3 and "Two-way rigid-fluid interaction Heterogeneous Architectures"  , into Ian's EnjaParticles library to allow for rigid-fluid interaction. I will now be the primary maintainer of the library and thus I have forked it to my github account.

The original goal of Ian's project was to integrate the fluid library into Blender's Game Engine(BGE). He has a working build of blender with the fluids integrated into the BGE. The fluid simulation works quite nicely. However, it currently can't handle two-way coupling between rigidbody-fluid systems.

My project will involve upgrading EnjaParticles library to include the coupling physics. Also, I plan to integrate this library into Bullet Physics Library. If I integrate it into the Bullet library, I can then more naturally integrate the library into the BGE. The BGE already uses Bullet for rigidbody and softbody simulations. With the addition of our library into Bullet, the BGE could then have rigidbody, softbody, and fluid simulations.

Goals for the project:

  • Create OpenCL and C++ infrastructure for rigidbodies in Enjaparticles
    • Voxelize an arbitrary [closed] triangular mesh. Already Completed
    • Create data structures to hold particles position, velocity, force, torque... and a related structure which holds these values for the center of mass of the rigid body.
    • Create OpenCL kernels to perform Segmented scan. This is necessary to sum the total forces for each rigid body from its particles.
  • Restructure and optimize the EnjaParticle library.
    • Do not force Opencl/gl interoperability. Could run on CPU's in addition to GPUS.
      • This will also allow for offline simulations that could be serialized.
    • Ensure all kernels are optimized.
    • Implement Morton ordering. This can speed up the SPH simulations substantially.
    • Implement Radix Sorting.
    • General refactoring
      • Some of the methods/attributes of each system class can be abstracted.
      • Enforce encapsulation and utilize inheritance.
      • Add Extensive Documentation.
      • Consider Making renderer part of the main file or at least seperate it from the simulation. Thus giving flexability to the caller on how they wish to render the VBO.
  • Integrate the Upgraded library into Bullet.
  • Upgrade BGE with the latest Bullet library.
  • Other Features that are wishes and not requirements.
    • Improve Screenspace rendering.
      • reflection
      • refraction
      • curvature flow
    • Implement Histopyramid marching cubes into the library for fast surface reconstruction.

Saturday, April 2, 2011

Screen Space Fluid rendering Phase 1

As I mentioned in my previous post, I have currently coded three of the necessary phases for rendering a set of points as fluid surface. The technique used is referred to as "Screen Space Fluid Rendering." The three phases are "Render points as Spheres", "Smooth the depth buffer", and finally "Calculate normals from the Smoothed Depth Buffer". In this blog, I will elaborate more on the screen space fluid rendering technique and I will discuss the first step.

Introduction to Screen Space Fluid Rendering(SSFR)

Before going into the details of each phase, I would like to introduce a little theory behind SSFR. The primary goal behind SSFR is to reconstruct the fluid surface in the viewer's (camera's) space. Concerning ourselves with only the viewer's perspective have potential speed up over methods like marching cubes which attempt to reconstruct the fluids entire surface. SSFR is not without limitations but for generating real time fluid animations it is among the fastest and highest quality currently available. An improvement in the smoothing phase, was developed and titled SSFR with Curvature flow.

Figure 1: The viewer typically can only see a subset of the particles. Also, they can almost never see the opposite surface. These factors motivate the need for creating the surface in user's perspective only.
Figure 2: Points outside the viewer's perspective are clipped. This is another way to save time.
Figure 3: The green line represents the surface we hope to obtain by the end phase.
Creating Spheres from points

The first phase is to create spheres from a collection  of points. If we choose to actually render sphere meshes at each point the algorithm would become very expensive as the number of particles grows. Instead we choose to employ a GLSL shader similar to the one found in Nvidia's oclParticle demo. We only need to use a shader because after this phase we no longer use vertex information. All subsequent phases take output from previous phases and process the image.

Vertex Shader: 

uniform float pointRadius;
uniform float pointScale;   // scale to calculate size in pixels

varying vec3 posEye;        // position of center in eye space

void main()
{

    posEye = vec3(gl_ModelViewMatrix * vec4(gl_Vertex.xyz, 1.0));
    float dist = length(posEye);
    gl_PointSize = pointRadius * (pointScale / dist);

    gl_TexCoord[0] = gl_MultiTexCoord0;
    gl_Position = gl_ModelViewProjectionMatrix * vec4(gl_Vertex.xyz, 1.0);

    gl_FrontColor = gl_Color;
}


Fragment Shader:

uniform float pointRadius;  // point size in world space
uniform float near;
uniform float far;
varying vec3 posEye;        // position of center in eye space

void main()
{
    // calculate normal from texture coordinates
    vec3 n;
    n.xy = gl_TexCoord[0].st*vec2(2.0, -2.0) + vec2(-1.0, 1.0);
    //This is a more compatible version which works on ATI and Nvidia hardware
    //However, This does not work on Apple computers. :/
    //n.xy = gl_PointCoord.st*vec2(2.0, -2.0) + vec2(-1.0, 1.0);

    float mag = dot(n.xy, n.xy);
    if (mag > 1.0) discard;   // kill pixels outside circle
    n.z = sqrt(1.0-mag);

    // point on surface of sphere in eye space
    vec4 spherePosEye =vec4(posEye+n*pointRadius,1.0);

    vec4 clipSpacePos = gl_ProjectionMatrix*spherePosEye;
    float normDepth = clipSpacePos.z/clipSpacePos.w;

    // Transform into window coordinates coordinates
    gl_FragDepth = (((far-near)/2.)*normDepth)+((far+near)/2.);
    gl_FragData[0] = gl_Color;
}


NOTE: The fragment shader above has some compatibility issues with ATI cards. Apparently the appropriate way to handle a point sprites texture coordinates is through gl_PointCoord. However this is not compatible with Apples OpenGL implementation.
Figure 4: Turn the points from the Figure 2 into point sprites. Point sprites are useful because they always face the viewer.


Figure 5: Point sprites which are "below" the surface do not need to be rendered.
The main goal of this shader is to modify the depth values of the rasterized image. To do this we must determine the z value from a 2D texture coordinate. First, take a look at the formula for a sphere.

$
x^2+y^2+z^2 = 1
$

Notice that we are outside the sphere if the following condition occurs:

$
x^2 + y^2 > 0
$


...
    // calculate normal from texture coordinates
    vec3 n;
    n.xy = gl_TexCoord[0].st*vec2(2.0, -2.0) + vec2(-1.0, 1.0);
    float mag = dot(n.xy, n.xy);
    if (mag > 1.0) discard;   // kill pixels outside circle
...

If we are in the sphere then the following calculation will give us the z value of the point on the sphere.

$
z = \sqrt{1-x^2-y^2}
$


...
    n.z = sqrt(1.0-mag);
...


The z value from this calculation is normalized on the interval [0,1]. The next step is to project this back into 3D camera coordinates. After projecting it into camera coordinates we must manually transform it back into window coordinates.



...
    // point on surface of sphere in camera space
    vec4 spherePosEye =vec4(posEye+n*pointRadius,1.0);

    vec4 clipSpacePos = gl_ProjectionMatrix*spherePosEye;
    float normDepth = clipSpacePos.z/clipSpacePos.w;

    // Transform into window coordinates coordinates
    gl_FragDepth = (((far-near)/2.)*normDepth)+((far+near)/2.);
...


Figure 6: Morph the point sprites into hemispheres.


In my next post, I plan to explain how to modify the depth values in a way to make these bumpy spheres look more like a continuous surface.

All shader code and c++ code are available from enjalot's github repository. Rendering code can be found in rtps/rtpslib/Render/.

Thursday, February 10, 2011

Particles to fluid

I have been working with Ian Johnson in the Department of Scientific Computing at FSU on implementing fluid rendering into his RTPS library. So far, I have had success in rendering the fluid surface using only a few steps out of the presentation given at GDC '10. The technique is referred to as Screen Space Fluid Rendering.


[RTPS] Fluid Surface and Collisions in the works from Ian Johnson on Vimeo.

The three phases I have implemented so far are "Render points as spheres", "Gaussian Blur on Depth", and "Calculate Normals from Depth".  The end result of adding this rendering technique to Ian's RTPS library which actually simulates the fluid is demoed in the video above. Next week, I will be posting code snippets along with explanations of how these three steps are combined to create this "surface" like effect.

Hello World

Hello everyone,
My name is Andrew Young. I am creating this blog in particular to detail progress in my research in the Department of Scientific Computing (DSC) at FSU. I may also add post about other interesting adventures I have throughout my time in graduate school.

My next post will be about my struggles as a beginner in OpenGL programming. I rushed head first into the project and quickly became buried in the complexities of Graphics programming.

Thanks,
Andrew Young