CS414 Project 3 Greg Kempe (89366033), kempe@cs.ubc.ca 5 December 2003 -------------------------------------- 1. Introduction This tutorial demonstrates the use of particle systems and simulated force fields in OpenGL. The scene consists of three particle systems and a number of spherical force fields. The fields affect the direction and velocity of the particles and various effects resulting from these interactions are demonstrated. The user can move the viewpoint around the scene and change the properties of each particle system (such as the number of particles, their initial speed, particle lifetime etc.) and each force field (the strength and "steepness" of each field). User interaction with the program and a description of how to navigate through the scene is given in Section 2. The particle systems and fields are described in more detail in Section 3. A description of the architecture of the program is given in Section 4 followed by a discussion of how it meets the project requirements in Section 5. 2. User Interaction and Navigation The program can be in one of two modes: design or render. Design mode allows the particle systems and fields to be selected by clicking on the wire frame spheres that represent them. Once selected, their properties can be modified using the sliders on the right side of the screen. Many visual effects (such as texturing and blending) are turned off in design mode to make changes to the selected object's properties more visible. Render mode disables object selection and turns on all visual effects. This mode is used to display the programmer's skills to full effect! In both modes, the user can move the viewpoint around the scene by using the mouse. Left-clicking and dragging controls rotation about the origin, right- clicking and dragging vertically controls the distance from the origin -- the zoom. 3. Particle Systems and Force Fields 3.1 Particle Systems Each particle system models a collection of independent particle based on basic Newtonian physics. That is, each particle has a position, mass and velocity (which is a vector and is therefore directional). Approximately 30 times a second the system simulates a discrete step in time for the particles. That is, each particle is moved (ie. its position p is updated) according to its velocity (V) and the simulated change in time (dt): p = p + V * dt (3.1) Note that uppercase letters are used to represent vectors (ie. they have both direction and magnitude) and lowercase letters represent scalars (magnitude alone). This model assumes the particles have no acceleration. We drop this assumption by modeling the effect of gravity (G) as well as introducing external forces in the form of force fields. Each field exerts a force Fi on a particle in some direction. The resultant force is simply the sum of all forces acting on the particle, yielding F = F1 + F2 + ... + G From the equation F = m*A, where m is mass and A is acceleration, we get A = F / m And since velocity is the product of acceleration and time, we get the new velocity for each particle over the simulated time period as V = V + (f1 + f2 + ... + g) / m * dt (3.2) Therefore, for each step in the simulation we can update the particle's velocity using Equation (3.2) and then calculate its new position using Equation (3.1). 3.2 Force Fields The individual forces acting on each particle still need to be calculated before the equations in Section 3.1 can be used. The model is simplified by assuming point masses that exert the same force (either attractive or repellent) with the same magnitude in all directions -- these are the spherical force fields. For any particle, the force exerted on it by a field is exerted along the vector connecting the particle and the centre of the field. According to Newtonian physics, the magnitude of the force is inversely proportional to the square of the distance (d) between the particle and the field. However, to make the model effective without requiring extremely large masses or extremely powerful fields we model the strength of the field as F = s / exp(1/t * d^2) (3.3) Where s is the strength of the field. The parameter t allows the falloff or steepness of the field to be controlled (it is analogous to the variance of a Gaussian distribution): if t is large then the field has a wider sphere of influence and its force is still reasonably strong quite far away from its enter. If t is small, then the force exerted on particles drops off more rapidly the further away the particle is. By tweaking the s and t parameters (the strength and steepness in the user interface) a wide range of force fields can be attained. It is assumed that gravity is exerted on all particles equally -- that is, the effect of distance between the particles and the Earth is assumed to be negligible. 4. Architecture 4.1 Files The program was implemented in Delphi 7 using the OpenGL, GLU and GLUT APIs for Delphi from www.delphi3d.net. The source code files can be grouped as follows (those marked with a * are required by Delphi and probably aren't all that interesting): Component and files Description ----------------------------------------------------------------------------- Main program and GUI main.pas Main window, creates the OpenGL viewport initialises the scene, responds to UI events proj3.dpr Main entry point *proj3.[cfg, dpr, Support files required by Delphi dof, res] *main.dfm Describes the main window OpenGL tie-in OpenGLBox.pas A placeholder component that acts as an OpenGL window Scene glscene.pas Describes and draws the entire scene, creates the particle systems, force fields etc Particle systems and force fields particlesys.pas Contains the classes for particle systems and force fields and allows them to interact Helper routines maths.pas Some basic vector and matrix maths routines textures.pas Some routines to load textures Textures trough.bmp Various texture and alpha-channel files. wall.bmp dot_alpha.bmp 4.2 Scene and Program Architecture The bulk of the scene-related routines, such as creating the particle systems and force fields, drawing the scene objects etc. are handled by the TScene object. The object is created by the program when the main window is created and both drawing and physics simulation are controlled by the object. Additionally, various parameters such as whether the scene is in design mode and whether force fields are drawn as spheres in the scene, are set on the TScene object. The TScene object creates and initialises the particle systems and force fields in its Init() method. Note that not all force fields are applied to all particle systems -- this allows us to achieve effects for a single particle system without interfering with the other systems. When drawing the scene, TScene draws the basic scene objects and then asks the particle systems and force fields to draw themselves, if necessary. In a similar manner, the main program asks the scene to simulate all forces approximately 30 times a second. The scene then asks each particle system to perform its simulation, which involves iterating through each particle, applying the various forces, and updating particle positions and velocities. In this way, adding new particle systems or force fields to the scene and making them interact with each other and be simulated correctly is trivial. 5. Project Requirements The project includes the following required features: 1. 3D viewing and objects, perspective viewing, smooth rotation and zooming. 2. User input via mouse clicking and dragging. 3. Lighting: there is a single light source and both the cone and cube have materials that use the lighting. 4. Texturing: the walls of the scene are textured as are the particles when not drawn as points. 5. Picking: particle systems and force fields can be selected in design mode using OpenGL's picking support. 6. Two pieces of functionality: physics-based particle systems and constraints on those systems from on spherical forces. 6. Resources The following resources were used as references and inspiration for the program: 1. OpenGL, GLU and GLUT APIs for Delphi from Delphi 3D www.delphi3d.net 2. NeHe's OpenGL Tutorial 19 on Particle Systems nehe.gamedev.net/data/lessons/lesson.asp?lesson=19 3. NeHe's OpenGL Tutorial 39 on Physical Simulation nehe.gamedev.net/data/lessons/lesson.asp?lesson=39 4. Textures from David Gurrea www.davegh.com/blade/davegh.htm