|
Introduction
This program simulates a snowstorm. The world consists of the sky and the ground stretching to the horizon.
The user can rotate the camera to view the world from different angles. The snowflakes are modeled by a particle system.
They are randomly generated at a plane above the camera view and fall downwards towards the ground. The flakes are represented
by either white dots or a snowflake image. The user can control how hard the snow falls and also the direction of wind along
the X axis. The snowflakes will drift in the direction of the wind when applied. There is also a built-in periodic wind function.
The buildup of snow on the ground can be toggled on and off. The longer it snows, the more snowcover will appear on the ground.
User's Guide
CLICK AND DRAG MOUSE |
Moves camera around world to change viewing position |
DOWN ARROW |
Increases rate of snowfall |
UP ARROW |
Decreases rate of snowfall |
LEFT ARROW |
Pulls wind to negative X direction |
RIGHT ARROW |
Pulls wind to positive X direction |
W |
Toggle between user wind direction and automatic periodic wind |
F |
Toggle between snowflake representation |
G |
Turns falled snow on ground on and off |
P |
Pauses snowfall |
R |
Resets camera position and ground snow |
Q |
Quit the program |
|
The world is made up of the ground and the sky. The ground is a large texture mapped circle. The sky is the inside of a large cylinder, texture mapped with an image of clouds. |
|
The snow is represented with a particle system. A particle has a position, a velocity, a color, a rotation matrix, a rotation axis, a specified amount of life, and a rate of fade. At each iteration, the position is updated according to the directional velocities. If the life expires or the flake reaches the ground, the particle is killed and then respawned again at the top. |
|
An alternative depiction of the particles is using texture mapping. A snowflake image is texture mapped to each particle. A large number of the flakes are randomly rotated so there is not a flat plane of textures seen when the camera is rotated. In order to keep the background of the snowflake image transparent, a technique known as masking is used. First, a mask is texture mapped to the particle. This is an exact copy of the desired texture, except that the parts we would like to see are colored black and the parts we would not like to see are colored white. Blending is used to turn the destination color black if that part of the mask is black. The section of the screen covered by the white part of the mask does not change. Then the actual texture is drawn on top of the mask. Blending modes are changed so any part of the colored texture that is not black is copied to the screen. As a result, the only parts of the texture that get drawn are the ones that land on the black part of the mask. |
|
The ground can also buildup snow as the snowfall progresses. A 2D array is used to represent a grid mapped to positions across the ground. The height of the snow is kept in this array, and the height map is updated as snow falls in that area of the grid. The ground snow is drawn as a square with a slowly expanding width. The way I designed this was to add these squares to an ever expanding vector. This obviously slows things down as it goes to draw each square in the list as the vector continues to grow. So in effect, the longer it snows, the slower the simulation gets. I did not have time to devise a more elegant solution to deal with the speed issue. |
The rate of snowfall is changed by adding a gravity factor to the particle's velocity in the Y direction. Increasing this gravity factor will cause the snow rate to increase.
The wind affects the particles by adding or subtracting from their velocities in the X direction. The trick here was to detect when the particle leaves the viewing window on either the left or right, and then redrawing it on the opposite side of the screen. This ensures that there are no spaces empty of snow when the wind is blowing. A built-in periodic wind is an added feature. This is modeled with a half-rectified sine wave which creates the effect of a periodic wind gust.
References
NeHe Productions
Information on particle systems and masking
Texture Manager
Help with loading textures
Download Executable
Program Files
© Dan Fake, 2004
|