+ All Categories
Home > Documents > CIS 350 – I Game Programming Instructor: Rolf Lakaemper

CIS 350 – I Game Programming Instructor: Rolf Lakaemper

Date post: 12-Feb-2016
Category:
Upload: efrat
View: 52 times
Download: 0 times
Share this document with a friend
Description:
CIS 350 – I Game Programming Instructor: Rolf Lakaemper. FRACTAL LANDSCAPES. What ?. In this lecture we will learn how to generate Fractal Heightfield Terrains. What ?. What are we talking about ?. What ?. Heightfield Terrain: A 2D grid system of height values, - PowerPoint PPT Presentation
65
CIS 350 – I Game Programming Instructor: Rolf Lakaemper
Transcript
Page 1: CIS 350 – I Game Programming Instructor: Rolf Lakaemper

CIS 350 – I

Game Programming

Instructor: Rolf Lakaemper

Page 2: CIS 350 – I Game Programming Instructor: Rolf Lakaemper

FRACTALLANDSCAPES

Page 3: CIS 350 – I Game Programming Instructor: Rolf Lakaemper

What ?

In this lecture we will learn howto generate

Fractal Heightfield Terrains

Page 4: CIS 350 – I Game Programming Instructor: Rolf Lakaemper

What ?

What are we talking about ?

Page 5: CIS 350 – I Game Programming Instructor: Rolf Lakaemper

What ?

Heightfield Terrain:

A 2D grid system of height values, defining a 3d landscape

Page 6: CIS 350 – I Game Programming Instructor: Rolf Lakaemper

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

Page 7: CIS 350 – I Game Programming Instructor: Rolf Lakaemper

What ?

Self Similarity

Page 8: CIS 350 – I Game Programming Instructor: Rolf Lakaemper

What ?

Self Identity: Von Koch Snowflake

Page 9: CIS 350 – I Game Programming Instructor: Rolf Lakaemper

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 !)

Page 10: CIS 350 – I Game Programming Instructor: Rolf Lakaemper

Fractals

Von Koch Snowflake

It therefore makes sense to define its dimensionality

BETWEEN

one and two !

Page 11: CIS 350 – I Game Programming Instructor: Rolf Lakaemper

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)

Page 12: CIS 350 – I Game Programming Instructor: Rolf Lakaemper

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)

Page 13: CIS 350 – I Game Programming Instructor: Rolf Lakaemper

Fractals

D = log(N) / log(1/r)

Is the fractal dimensionality of a self similar object.

What’s the dimensionality of the Koch Snowflake ?

Page 14: CIS 350 – I Game Programming Instructor: Rolf Lakaemper

Fractals

N = 4Scale r = 1/3

D = log(4) / log(3)

D = 1.2619

Intuitive ?

Page 15: CIS 350 – I Game Programming Instructor: Rolf Lakaemper

Fractals

The interesting news:

There’s a lot of math behind fractals !

Page 16: CIS 350 – I Game Programming Instructor: Rolf Lakaemper

Fractals

The good news:

To use them, an understanding of math is not necessary. They

are algorithmically SIMPLE.

Page 17: CIS 350 – I Game Programming Instructor: Rolf Lakaemper

Fractals

Since we only want to use fractals, let’s go straight to algorithms

Algorithms for Random Fractals

Page 18: CIS 350 – I Game Programming Instructor: Rolf Lakaemper

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.

Page 19: CIS 350 – I Game Programming Instructor: Rolf Lakaemper

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.

Page 20: CIS 350 – I Game Programming Instructor: Rolf Lakaemper

Random Fractals

The motion of these particles has certain properties, which match the properties of

shapes in nature.

Page 21: CIS 350 – I Game Programming Instructor: Rolf Lakaemper

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

Page 22: CIS 350 – I Game Programming Instructor: Rolf Lakaemper

Fractals

We will use

MIDPOINT DISPLACEMENT

Page 23: CIS 350 – I Game Programming Instructor: Rolf Lakaemper

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. }

}

Page 24: CIS 350 – I Game Programming Instructor: Rolf Lakaemper

Fractals

Result:

Page 25: CIS 350 – I Game Programming Instructor: Rolf Lakaemper

Fractals

Result:

Page 26: CIS 350 – I Game Programming Instructor: Rolf Lakaemper

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.

Page 27: CIS 350 – I Game Programming Instructor: Rolf Lakaemper

Fractals

Extension to 2 dimensions:

The Diamond – Square Algorithm

(by Fournier, Fussel, Carpenter)

Page 28: CIS 350 – I Game Programming Instructor: Rolf Lakaemper

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

Page 29: CIS 350 – I Game Programming Instructor: Rolf Lakaemper

Fractals

Data Structure: Square Grid

Page 30: CIS 350 – I Game Programming Instructor: Rolf Lakaemper

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.

Page 31: CIS 350 – I Game Programming Instructor: Rolf Lakaemper

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.

Page 32: CIS 350 – I Game Programming Instructor: Rolf Lakaemper

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.

Page 33: CIS 350 – I Game Programming Instructor: Rolf Lakaemper

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

Page 34: CIS 350 – I Game Programming Instructor: Rolf Lakaemper

Diamond Square

Again:

Page 35: CIS 350 – I Game Programming Instructor: Rolf Lakaemper

Diamond Square

Some steps: taken from http://www.gameprogrammer.com/fractal.html#midpoint

Page 36: CIS 350 – I Game Programming Instructor: Rolf Lakaemper

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

Page 37: CIS 350 – I Game Programming Instructor: Rolf Lakaemper

Diamond Square

H=0, 1/(2^H) = 1

Page 38: CIS 350 – I Game Programming Instructor: Rolf Lakaemper

Diamond Square

H=0.2, 1/(2^H) = 0.87

Page 39: CIS 350 – I Game Programming Instructor: Rolf Lakaemper

Diamond Square

H=0.4, 1/(2^H) = 0.76

Page 40: CIS 350 – I Game Programming Instructor: Rolf Lakaemper

Diamond Square

H=0.6, 1/(2^H) = 0.65

Page 41: CIS 350 – I Game Programming Instructor: Rolf Lakaemper

Diamond Square

H=0.8, 1/(2^H) = 0.57

Page 42: CIS 350 – I Game Programming Instructor: Rolf Lakaemper

Diamond Square

H=1, 1/(2^H) = 0.5

Page 43: CIS 350 – I Game Programming Instructor: Rolf Lakaemper

Diamond Square

Let’s have a look ata MATLAB implementation…

Page 44: CIS 350 – I Game Programming Instructor: Rolf Lakaemper

Open GL

But how toDISPLAY

a heightfield ?

Page 45: CIS 350 – I Game Programming Instructor: Rolf Lakaemper

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 …

Page 46: CIS 350 – I Game Programming Instructor: Rolf Lakaemper

Open GL

OPEN GL

Page 47: CIS 350 – I Game Programming Instructor: Rolf Lakaemper

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

Page 48: CIS 350 – I Game Programming Instructor: Rolf Lakaemper

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

Page 49: CIS 350 – I Game Programming Instructor: Rolf Lakaemper

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. …

Page 50: CIS 350 – I Game Programming Instructor: Rolf Lakaemper

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 !

Page 51: CIS 350 – I Game Programming Instructor: Rolf Lakaemper

Open GL

An example:

The drawing operation is started by the function

void glBegin(Glenum mode)

And ends with

void glEnd()

Page 52: CIS 350 – I Game Programming Instructor: Rolf Lakaemper

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)

Page 53: CIS 350 – I Game Programming Instructor: Rolf Lakaemper

Open GL

• Here it is: draw a pixel !

glBegin(GL_POINTS);glVertex3f(0.0, 0.0, 0.0);

glEnd();

Page 54: CIS 350 – I Game Programming Instructor: Rolf Lakaemper

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();

Page 55: CIS 350 – I Game Programming Instructor: Rolf Lakaemper

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();

Page 56: CIS 350 – I Game Programming Instructor: Rolf Lakaemper

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();

Page 57: CIS 350 – I Game Programming Instructor: Rolf Lakaemper

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();

Page 58: CIS 350 – I Game Programming Instructor: Rolf Lakaemper

Open GL

Result:

Page 59: CIS 350 – I Game Programming Instructor: Rolf Lakaemper

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.

Page 60: CIS 350 – I Game Programming Instructor: Rolf Lakaemper

Open GL

GLUT: Handling windows I/O

Here’s the GLUT part that handledthe previous output

CallbackFunction !

Page 61: CIS 350 – I Game Programming Instructor: Rolf Lakaemper

Open GL

And the registered rendering function:

Page 62: CIS 350 – I Game Programming Instructor: Rolf Lakaemper

Open GL

That’s all to create great graphics !!!

Page 63: CIS 350 – I Game Programming Instructor: Rolf Lakaemper

Open GL

Now:

Output of a heightfield(see source on seminar’s homepage)

Page 64: CIS 350 – I Game Programming Instructor: Rolf Lakaemper

Open GL

Result:

Page 65: CIS 350 – I Game Programming Instructor: Rolf Lakaemper

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


Recommended