#include <stdio.h>
#include <stdlib.h>
#include "glut.h"
Go to the source code of this file.
Defines | |
#define | IX(i, j) ((i)+(N+2)*(j)) |
Convert 2 dimension array location to a 1 dimensional array index. | |
#define | SWAP(x0, x) {float *tmp = x0; x0 = x; x = tmp;} |
Swap two arrays. | |
#define | DIM 128 |
Dimension of the fluid matrix. | |
#define | SIZE DIM*DIM |
Size of the fluid matrix. | |
#define | max(x, y) x>y?x:y |
Return the maximum value. | |
Functions | |
void | printArray (int N, float *x) |
Prints out the values of the passed in array. | |
void | init (void) |
Creates the OpenGL display list. | |
void | display (void) |
Main program loop. | |
void | idle (void) |
Called when window sytem events aren't being received. | |
void | reshape (int x, int y) |
Called when the window is resized. | |
void | keyboard (unsigned char key, int x, int y) |
Processes keyboard input. | |
void | click (int button, int state, int x, int y) |
Processes the mouse click event. | |
void | motion (int x, int y) |
Processes the mouse moved event. | |
void | timer (int value) |
Timer to control the frame rate. | |
void | updateFluid (int button, int mouseMove, int state) |
Adds density or velocity to the current fluid. | |
void | add_source (int N, float *x, float *s, float dt) |
Adds external sources to the fluid. | |
void | diffuse (int N, int b, float *x, float *x0, float diff, float dt) |
The diffusion step. | |
void | advect (int N, int b, float *d, float *d0, float *u, float *v, float dt) |
The advection step. | |
void | project (int N, float *u, float *v, float *p, float *div) |
The projection step. | |
void | dens_step (int N, float *x, float *x0, float *u, float *v, float diff, float dt) |
The density step. | |
void | vel_step (int N, float *u, float *v, float *u0, float *v0, float visc, float dt) |
The velocity step. | |
void | set_bnd (int N, int b, float *x) |
Set the values of the invisible boundary. | |
int | main (int argc, char *argv[]) |
Variables | |
float | u [SIZE] |
Horizontal Velocity Field. | |
float | v [SIZE] |
Vertical Velocity Field. | |
float | u_prev [SIZE] |
Horizontal Velocity Field for previos step. | |
float | v_prev [SIZE] |
Vertical Velocity Field for previous step. | |
float | dens [SIZE] |
Density Field. | |
float | dens_prev [SIZE] |
Density Field for previous step. | |
int | boundary [SIZE] |
Contains which cells are boundarys. | |
int | wWidth = max(512,DIM) |
GUI window width. | |
int | wHeight = max(512,DIM) |
GUI window height. | |
float | cellSize |
Size of each cell that is drawn. | |
float | diff = 0.5f |
Rate of diffusion of the fluid. | |
float | dt = .2f |
Time step. | |
float | visc = 0.0f |
Viscosity of the fluid. | |
int | stepToggle = 0 |
Steps the program by 1 time step. | |
int | runToggle = 1 |
Starts or stops the program from running in a loop. | |
int | consoleToggle = 0 |
Shows or hide console debug output. | |
int | fluidToggle = 0 |
Injects fluid into simulation. | |
int | densityToggle = 1 |
Show or hide the denisity field drawing. | |
int | velocityToggle = 0 |
Show or hide the velocity field drawing. | |
int | gridToggle = 0 |
Show or hide the grid drawing. | |
int | boundaryToggle = 1 |
Show or hide the boundarys. | |
int | drawBoundaryToggle = 0 |
Turn drawing of boundarys on or off. | |
int | id |
OpenGL display list. | |
int | lastX = 0 |
The last x position the mouse was in. | |
int | lastY = 0 |
The last y position the mouse was in. | |
int | currentX = 0 |
The current x position the mouse is in. | |
int | currentY = 0 |
The current y position the mouse is in. | |
int | clicked = 0 |
A mouse button was clicked. | |
int | leftClick = 0 |
The left mouse button was clicked. | |
int | N = DIM-2 |
The dimension of the fluid array without the boundary walls. | |
int | emitterLoc = DIM/2 |
Location of the fluid injection. | |
int | frameRate = 60 |
Frame Rate. | |
int | updown = 0 |
State of the mouse button. | |
int | lastCellX |
The x coordinate of the last touched boundary cell. | |
int | lastCellY |
The y coordinate of the last touched boundary cell. | |
float | totalDensity = 0 |
The total density in the simulation. |
Definition in file Fluid.c.
#define IX | ( | i, | |||
j | ) | ((i)+(N+2)*(j)) |
#define SWAP | ( | x0, | |||
x | ) | {float *tmp = x0; x0 = x; x = tmp;} |
void add_source | ( | int | N, | |
float * | x, | |||
float * | s, | |||
float | dt | |||
) |
Adds external sources to the fluid.
It adds the external forces that where provided by user input to the current fluid array.
N | Size of the array | |
x | The array that holds the current fluid information | |
s | The array that holds the external forces that where supplied by user input | |
dt | The time step |
void advect | ( | int | N, | |
int | b, | |||
float * | d, | |||
float * | d0, | |||
float * | u, | |||
float * | v, | |||
float | dt | |||
) |
The advection step.
The basic idea of the advection step is: instead of moving the cell centers to forward in time through the velocity field we look for the particles which end up exactly at the cell centers by tracing backwards in time from the cell centers.
N | The size of the array | |
b | Value for set_bnd() | |
d | The current field | |
d0 | The field from the previous step | |
u | Horizontal velocity field | |
v | Vertical velocity field | |
dt | The time step |
void click | ( | int | button, | |
int | state, | |||
int | x, | |||
int | y | |||
) |
Processes the mouse click event.
It processes the mouse click event and calls updateFluid() with the button that was pushed.
button | The mouse button that was clicked | |
state | The state that the clicked mouse button is in, up or down | |
x | The x location of the mouse when the mouse button was clicked | |
y | The y location of the mouse when the mouse button was clicked |
void dens_step | ( | int | N, | |
float * | x, | |||
float * | x0, | |||
float * | u, | |||
float * | v, | |||
float | diff, | |||
float | dt | |||
) |
The density step.
It computes the density step of the fluid simulation.
N | The size of the array | |
x | The current density field | |
x0 | The source densities | |
u | The horizontal velocity field | |
v | The vertical velocity field | |
diff | The rate of diffusion | |
dt | The time step |
void diffuse | ( | int | N, | |
int | b, | |||
float * | x, | |||
float * | x0, | |||
float | diff, | |||
float | dt | |||
) |
The diffusion step.
N | The size of the array | |
b | Value for set_bnd() | |
x | The current field | |
x0 | The field from the previous step | |
diff | The rate of diffusion | |
dt | The time step |
void display | ( | void | ) |
void idle | ( | void | ) |
void init | ( | void | ) |
void keyboard | ( | unsigned char | key, | |
int | x, | |||
int | y | |||
) |
Processes keyboard input.
It processes the keyboard input and sets the various toggle variables based what key was pressed.
key | The ASCII value of the key pressed | |
x | The x location of the mouse when the key was pressed | |
y | The y location of the mouse when the key was pressed |
void motion | ( | int | x, | |
int | y | |||
) |
Processes the mouse moved event.
It processes the mouse moved event and sets the currentX and currentY variables to the current location of the mouse if the left click button is held down.
x | The x position of the mouse | |
y | The y position of the mouse |
void printArray | ( | int | N, | |
float * | x | |||
) |
void project | ( | int | N, | |
float * | u, | |||
float * | v, | |||
float * | p, | |||
float * | div | |||
) |
The projection step.
The projection step is used to make sure the velocity field conserves mass. Every velocity field is the sum of a mass conserving field and a gradient field.
N | The size of the array | |
u | The current horizontal velocity field | |
v | The current vertical velocity field | |
p | The horizontal velocity field from the previous step | |
div | The vertical velocity field from the previous step |
void reshape | ( | int | x, | |
int | y | |||
) |
Called when the window is resized.
x | The new windows width | |
y | The new windows height |
void set_bnd | ( | int | N, | |
int | b, | |||
float * | x | |||
) |
Set the values of the invisible boundary.
N | The size of the array | |
b | If 0 set the value to the value of the cell next to it otherwise set it to 0 | |
x | The array to work on |
void timer | ( | int | value | ) |
Timer to control the frame rate.
The timer calls glutPostRedisplay() every time the timer goes off and creates a new timer since the timer is destroyed everytime it goes off. The timer keeps the program running at a fixed frame rate.
value | A value that is passed along to the timer function when the timer goes off |
void updateFluid | ( | int | button, | |
int | mouseMove, | |||
int | state | |||
) |
Adds density or velocity to the current fluid.
It add density to the fluid at the current mouse position if the user right clicked, add velocity to the fluid at the current mouse position if the user is holding down left click and moves the mouse.
button | The mouse button that was clicked | |
mouseMove | If the mouse moving it equals 1 else it equals 0 | |
state | If the mouse button is pushed down it equals 0 else it equals 1 |
void vel_step | ( | int | N, | |
float * | u, | |||
float * | v, | |||
float * | u0, | |||
float * | v0, | |||
float | visc, | |||
float | dt | |||
) |
The velocity step.
It computes the velocity step of the fluid simulation.
N | The size of the array | |
u | The current horizontal velocity field | |
v | The current vertical velocity field | |
u0 | The horizontal source velocities | |
v0 | The vertical source velocities | |
visc | The amount of viscosity of the fluid | |
dt | The time step |
int boundaryToggle = 1 |
int consoleToggle = 0 |
int densityToggle = 1 |
int drawBoundaryToggle = 0 |
int emitterLoc = DIM/2 |
int fluidToggle = 0 |
int gridToggle = 0 |
int lastCellX |
int lastCellY |
int N = DIM-2 |
int runToggle = 1 |
int stepToggle = 0 |
float totalDensity = 0 |
float u_prev[SIZE] |
float v_prev[SIZE] |
int velocityToggle = 0 |