CS 475/575 -- Spring Quarter 2015

Project #6

OpenCL / OpenGL Particle System

120 Points

Due: June 1 ( = May 32 )


This page was last updated: May 17 ( = June -14 ), 2014


Note: Don't turn in any .zip files for this one!
Submit your .cpp file,, .cl file, executable file, and PDF file individually.
See below for special naming requirements.

Introduction

Particle systems are used in games, movies, etc. to depict phenomena such as clouds, dust, fireworks, fire, explosions, water flow, sand, insects, wildebeests, etc. Once you know what they are, you can't stop seeing them. To make a particle system work, you manipulate a collection of many 3D particles to exhibit some behavior. ( Look here for more information.)

In this project, you will use OpenCL and OpenGL together to make a cool particle system. (The degree of cool-ness is up to you.)

Requirements:

  1. Design your 3D environment. The particles need to start from somewhere. Where should that be? The particles need to start with initial velocities. What should those be?

    Your 3D environment needs to have at least two "bumpers" in it for the particles to bounce off of. Each bumper needs to be geometrically designed such that, given a particle's XYZ, you can quickly tell if that particle is inside or outside the bumper. To get the bounce right, each bumper must be able to know its outward-facing surface normal everywhere.

    Let's face it. Spheres are computationally "nice". In computer graphics, we love spheres. It is fast and easy to tell if something is inside or outside a sphere. Determining a normal vector for a point on the surface of a sphere is even easier.

    It is OK to assume that the two bumpers are separate from each other, that is, a particle cannot be colliding with both at the same time.

  2. Create an OpenGL buffer object to hold the particles' XYZW positions as an array-of-structures.
    Create an OpenGL buffer object to hold the particles' RGBA colors as an array-of-structures.
    Create a C++ array-of-structures to hold the particles' XYZ velocities.
    (OpenGL buffer objects are used for position and color, but not velocity. This is because OpenGL will need to use positions and colors in the drawing, but not the velocities.)

  3. Determine good starting values for all 3 data structures.
    For the position and color buffers, use glMapBuffer( ) to send the values directly to the GPU.
    The velocity array is a C++ array-of-structures, so just fill it using C++ code.

  4. Create two OpenCL buffers from the position and color OpenGL buffers using calls to clCreateFromGLBuffer( ). You don't need to transmit the data to these OpenCL buffers -- it is already there in the OpenGL buffers that they are now linked to.

  5. For the velocity array, create an OpenCL buffer using clCreateBuffer and transmit the C++ array-of-structures to it like you've done before using clEnqueueWriteBuffer( ).

  6. Decide on a time step, DT. Your .cl program will need to know about it. You can pass it in, or hard code it.

  7. Create a .cl OpenCL kernel that will advance all of the particles by the timestep DT. The sample code shows giving the .cl program access to the particles' positions and velocities. You will need to use these, and will need to update them.

  8. Your OpenCL .cl program must also handle the bounces off of your bumpers. Be sure to draw these bumpers in your .cpp program so that you can see where they are. Again, spheres are "nice".

  9. The sample code shows giving the .cl program access to the particles' colors. Joe Parallel didn't do anything with these. But, your .cl kernel needs to dynamically change the color of each particle. You could base this on position, velocity, time, bounce knowledge, etc. But, the color of each particle needs to change during the simulation.

  10. Leave the local work-group size at some fixed value. You can pull this out of your experience with Project #5, or you can experiment with the timing. Vary the total number of particles and measure the performance using units that make sense. Following our class tradition of inventing our own giga-cool-sounding units of measure, Joe Parallel used "Particles Per Second", or "MegaParticles Per Second", or "GigaParticles Per Second".

  11. Make a table and a graph of Performance versus Total Number of Particles

  12. Turn into teach, individually, not in a .zip file:
    1. Your source code (.cpp and .cl). Give your .cl file a name that is some variation on your name or login. (See below.)
    2. Your executable
    3. Your commentary in a PDF file.

  13. Your commentary PDF should include:
    1. What machine you ran this on
    2. What dynamic thing did you do with the particle colors
    3. Include at least one screen capture image of your project in action
    4. Show the table and graph
    5. What patterns are you seeing in the performance curve?
    6. Why do you think the patterns look this way?
    7. What does that mean for the proper use of GPU parallel computing?

Advancing a Particle by DT

In the sample code, Joe Parallel wanted really badly to make the code look cleaner by treating (x,y,z) positions and velocities as single variables. To do this, he typedef'ed new variable types called point, vector, and color and took advantage of OpenCL's float4 variable type. (Unfortunately, there isn't a float3.)
He also stored a sphere definition as a float4: x, y, z, r.


typedef float4 point;		// x, y, z, 1.
typedef float4 vector;		// vx, vy, vz, 0.
typedef float4 color;		// r, g, b, a
typedef float4 sphere;		// x, y, z, r

constant sphere Sphere1 = (sphere)( -100., -800., 0.,  600. );

Joe Parallel also stored the (x,y,z) acceleration of gravity in a float4 and hard-coded a time step:


constant float4 G       = (float4) ( 0., -9.8, 0., 0. );
constant float  DT      = 0.1;

Now, given a particle's position point p and a particle's velocity vector v, here is how you advance it one time step:


kernel
void
Particle( global point *dPobj, global vector *dVel, global color *dCobj )
{
        int gid = get_global_id( 0 );		// particle number

        point  p = dPobj[gid];
        vector v = dVel[gid];
        color  c = dCobj[gid];

        point  pp = p + v*DT + .5*DT*DT*G;	// p'
        vector vp = v + G*DT;			// v'
        pp.w = 1.;
        vp.w = 0.;

Bouncing is handled by changing the velocity vector according to the outward-facing surface normal of the bumper at the point right before an impact:



        if(  IsInsideSphere( pp, Sphere1 )  )
        {
                vp = BounceSphere( p, v, Sphere1 );
                pp = p + vp*DT + .5*DT*DT*G;
        }

Assigning the new positions and velocities back into the global buffers happens like this:


        dPobj[gid] = pp;
        dVel[gid]  = vp;

Some utility functions you might find useful:


bool
IsInsideSphere( point p, sphere s )
{
	float r = fast_length( p.xyz - s.xyz );
	return  ( r < s.w );
}


vector
Bounce( vector in, vector n )
{
	n.w = 0.;
	n = fast_normalize( n );
	vector out = in - 2.*n*dot(in.xyz, n.xyz);
	out.w = 0.;
	return out;
}


vector
BounceSphere( point p, vector in, sphere s )
{
	vector n;
	n.xyz = fast_normalize( p.xyz - s.xyz );
	n.w = 0.;
	return Bounce( in, n );
}

The .zip File for the Template

To make this easier, an entire Visual Studio solution has been zipped up in the file ParticlesTemplate.zip

File Naming Conventions

Please, please, please:

  1. Name your .cl file to be some unique variation on your name or login
  2. Put your name in the graphics window title bar

So, for Joe Parallel, the file name would be something like jparallel.cl The naming of the .cl file must happen during development because that file name is hard-coded into your .cpp program.

There is a constant character string near the top of the program called WINDOWTITLE where you set the string that goes in the graphics window title bar. Change Joe Parallel to your name.

Seeing Joe Parallel's Animation

Click here to see Joe Parallel's animation.
(It might work better to right click on the link and store the mp4 file locally and run it from there.)

Grading:

FeaturePoints
Convincing particle motion10
Bouncing from at least two bumpers30
Dynamic color changes30
Performance table and graph20
Commentary in the PDF file30
Potential Total120