CIS 350 – I
Game Programming
Instructor: Rolf Lakaemper
FRACTALLANDSCAPES
What ?
In this lecture we will learn howto generate
Fractal Heightfield Terrains
What ?
What are we talking about ?
What ?
Heightfield Terrain:
A 2D grid system of height values, defining a 3d landscape
What ?
Fractal:extremely irregular curves or shapes for which
any suitably chosen part is similar in shape to a given larger or smaller part when
magnified or reduced to the same size
Main characteristic:
Self Similarity
What ?
Self Similarity
What ?
Self Identity: Von Koch Snowflake
Fractals
Von Koch Snowflake
Iterating the snowflake algorithm to infinity, the boundary of the 1d
snowflake becomes part of the 2d AREA of the plane it is constructed
in (take it intuitively !)
Fractals
Von Koch Snowflake
It therefore makes sense to define its dimensionality
BETWEEN
one and two !
Fractals
The idea of defining the dimensionality is straightforward:
Example: 1 dimensional object, a lineA line is self similar: it can be divided into N parts, each of which is scaled down by the ratio r = 1/N from the whole.
Example: 2 dimensional object, a rectangle
Also self similar, can be divided into N similar parts of scale r = 1/sqrt(N)
Fractals
We can also state:
N = 1 / (r^D)= (1/r)^D
Hence D results from given r,N as:
D = log(N) / log(1/r)
Fractals
D = log(N) / log(1/r)
Is the fractal dimensionality of a self similar object.
What’s the dimensionality of the Koch Snowflake ?
Fractals
N = 4Scale r = 1/3
D = log(4) / log(3)
D = 1.2619
Intuitive ?
Fractals
The interesting news:
There’s a lot of math behind fractals !
Fractals
The good news:
To use them, an understanding of math is not necessary. They
are algorithmically SIMPLE.
Fractals
Since we only want to use fractals, let’s go straight to algorithms
Algorithms for Random Fractals
Fractals
Random fractals:
In contrast to exact self similar fractals (e.g. the Koch snowflake), also termed as
deterministic fractals, an additional element of randomness is added to
simulate natural phenomena.
An exact computation of fractals is impossible, since their level of detail is
infinite ! Hence we approximate.
Random Fractals
First Case Study: Brownian Motion
As the botanist R. Brown 1827 discovered, small particles of solid matter suspended in a
liquid can be seen under a microscope to move in an irregular and erratic way. The
modeling of this movement is one of the great topics of statistical mechanics.
Random Fractals
The motion of these particles has certain properties, which match the properties of
shapes in nature.
Fractals
There are two main classes of algorithms simulating random fractals, e.g. brownian
motion:
• Recursive approximation: input fractal is recursively improved in resolution,
example: midpoint displacement
• Only one approximation, namely the final resolution, is computed. Example: Fourier
filtering
Fractals
We will use
MIDPOINT DISPLACEMENT
Fractals
A 1D example to draw a mountain :
Start with a single horizontal line segment. Repeat for a sufficiently large number of times {
Repeat over each line segment in the scene { Find the midpoint of the line segment.
Displace the midpoint in Y by a random amount. Reduce the range for random numbers. }
}
Fractals
Result:
Fractals
Result:
Fractals
Note:
To apply the mathematics of fractals to the algorithm and to determine the correct fractal dimension, the random number
generator must have a certain property, i.e. it must deliver random numbers with
Gaussian distribution.
This does not seem to be crucial for the application of creating landscapes. We can
use the C++ / JAVA random numbers.
Fractals
Extension to 2 dimensions:
The Diamond – Square Algorithm
(by Fournier, Fussel, Carpenter)
Fractals
Data Structure: Square Grid
Store data (efficiently) in 2D Array.
Modification is very trivial. Not possible to define all
terrain features. Good for Collision detection
Fractals
Data Structure: Square Grid
Diamond Square
The basic idea:Start with an empty 2D array of points. To
make it easy, it should be square, and the dimension should be a power of two, plus one (e.g. 33x33).
Set the four corner points to the same height value. You've got a square.
Diamond Square
This is the starting-point for the iterative subdivision routine, which is in two steps:
The diamond step: Take the square of four points, generate a random value at the square midpoint, where the two diagonals meet. The midpoint value is calculated by averaging the four corner values, plus a random amount. This gives you diamonds when you have multiple squares arranged in a grid.
Diamond Square
Step 2: The square step:
Taking each diamond of four points, generate a random value at the center of the diamond. Calculate the midpoint value by averaging the corner values, plus a random amount generated in the same range as used for the diamond step. This gives you squares again.
This is done repeatedly, but the next pass is different from the previous one in two ways. First, there are now four squares instead of one. Second, and this is main point: the range for generating random numbers has been reduced by a scaling factor r, e.g. r = 1/4 (remember the fractal dimension ?)
Diamond Square
Diamond Square
Again:
Diamond Square
Some steps: taken from http://www.gameprogrammer.com/fractal.html#midpoint
Diamond Square
The scaling factor r, determining the range of random displacement R, defines the roughness ( => fractal
dimension !) of the landscape.
Some examples for diff. r and R
R(n+1) = R(n) * 1 / (2^H),0 < H < 1
Diamond Square
H=0, 1/(2^H) = 1
Diamond Square
H=0.2, 1/(2^H) = 0.87
Diamond Square
H=0.4, 1/(2^H) = 0.76
Diamond Square
H=0.6, 1/(2^H) = 0.65
Diamond Square
H=0.8, 1/(2^H) = 0.57
Diamond Square
H=1, 1/(2^H) = 0.5
Diamond Square
Let’s have a look ata MATLAB implementation…
Open GL
But how toDISPLAY
a heightfield ?
Open GL
For now we just need a tool that can visualize 3D data in connection with C++ / JAVA.
The math behind 3D scenes will not be part of today’s class.
We will also not render or give a deep introduction into the display tool, but will
only describe and use …
Open GL
OPEN GL
Open GL
What is OpenGL ?
• OpenGL provides the programmer with an interface to graphics hardware
• Powerful rendering and modeling library• Available on all major platforms
• OPEN SOURCE (FREE)• Designed for use in all graphic applications,
from CAD to games• EASY to use
• E.g. used in the core engine of Quake3
Open GL
OpenGL• Developed by Silicon Graphics (SGI)
• Adopted by Microsoft (of course there’s now a microsoft and SGI version for PCs…)• Provides graphic functionality only, NO
window handling (this is done by an additionol module, GLUT)
• Collection of several hundred functions providing access to all features offered by
the graphic hardware
Open GL
Internally OpenGL works as a state machine (A collection of states tells OpenGL what to do). The OpenGL API offers the interface to set various aspects of the state machine,
e.g. line color, thickness, texture etc.
An example for state parameters of the API:
GL_DOUBLEBUFFER, GL_FOG, GL_FOG_COLOR, GL_POINTSIZE, GL_CURRENT_COLOR, etc. …
Open GL
OpenGL primitives
• The basic geometric entities
• OpenGL offers points, lines, line_strip, line_loop, triangles, triangle_strip,
triangle_fan, quads, quad_strip, polygon
• (notation: GL_POINTS, GL_LINES, …)
• These are the tools you are finally drawing with !
Open GL
An example:
The drawing operation is started by the function
void glBegin(Glenum mode)
And ends with
void glEnd()
Open GL
• The ‘mode’ tells openGL WHAT to draw, e.g. GL_POINTS, GL_LINES
• Now we only have to define WHERE to draw, this is done by submitting VERTICES
We’ll use the function
void glVertex3f(float x,float y,float z)
Open GL
• Here it is: draw a pixel !
glBegin(GL_POINTS);glVertex3f(0.0, 0.0, 0.0);
glEnd();
Open GL
• Now draw three pixels !
glBegin(GL_POINTS);glVertex3f(0.0, 0.0, 0.0);glVertex3f(0.0, 0.5, 0.0);glVertex3f(0.5, 0.5, 0.0);
glEnd();
Open GL
• Now draw a triangle
glBegin(GL_TRIANGLE);glVertex3f(-0.5, -0.5, 0.0);glVertex3f(0.5, 0.0, 0.0);glVertex3f(0.0, 0.5, 0.0);
glEnd();
Open GL
Draw a green triangle
glBegin(GL_TRIANGLE);glColor3f(0.0, 1.0, 0.0);glVertex3f(-0.5, -0.5, 0.0);glVertex3f(0.5, 0.0, 0.0);glVertex3f(0.0, 0.5, 0.0);
glEnd();
Open GL
Draw a nice triangle
glBegin(GL_TRIANGLE);glColor3f(0.0, 1.0, 0.0);glVertex3f(-0.5, -0.5, 0.0);glColor3f(1.0, 0.0, 0.0);glVertex3f(0.5, 0.0, 0.0);glColor3f(0.0, 0.0, 1.0);glVertex3f(0.0, 0.5, 0.0);
glEnd();
Open GL
Result:
Open GL
Unfortunately that’s not all. OpenGL does NOT handle the window itself.
Fortunately there’s GLut, the GL utilities library.
Once again we won’t describe it deeply here, but just use it.
Open GL
GLUT: Handling windows I/O
Here’s the GLUT part that handledthe previous output
CallbackFunction !
Open GL
And the registered rendering function:
Open GL
That’s all to create great graphics !!!
Open GL
Now:
Output of a heightfield(see source on seminar’s homepage)
Open GL
Result:
In the LAB you will learn how to install the OpenGL files and how to
alter the application to show a fractal surface !
That’s it for today.
Open GL