image synthesis rabie a. ramadan, phd 6. 2 3d images

Post on 01-Jan-2016

226 Views

Category:

Documents

3 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Image Synthesis

Rabie A. Ramadan, PhD

6

2

3D Images

3

Representing and Loading 3D Terrains

One common feature in games and other 3D programs is the presence of a 3D terrain.

4

How do we represent a terrain?

The most straightforward approach, and, as it turns out, one of the best approaches, is • To make a 2D grid in the x-z plane and store the height of

the terrain at each grid point.

This does not work with every train

5

How do we represent a terrain? We could hard code every height into the program itself. But it's

better to store the heights in a separate file.

The most straightforward type of file we can use is a grayscale image, where • White represents the maximum allowable height and black represents the

minimum allowable height.

Such an image file is called a "heightmap".

it allows us to see what our terrain looks like, even without rendering it in 3D

6

How do we represent a terrain?

Below is a zoomed-in version of the heightmapheightmap for our program.

7

Going Through The Code

we have #include "vec3f.h“

This includes a special vector class called "Vec3f",

A vector of three floats. It does everything you'd expect vectors to do.

You can add and subtract using + and -, multiply and divide using * and /, get or set the components using vec[0], vec[1], and vec[2], and do some other stuff.

8

Going Through The Code

•It stores a width and length, indicating the number of grid points in the x and z directions respectively.

•It stores all of the heights and the normals at each point using two-dimensional arrays.

•it has a bool that tells us whether the normals array actually has the correct normals.

9

constructor for the Terrain class

It initializes all of our variables.

10

Destructor for the Terrain class

The destructor deletes the two-dimensional arrays hs and normals

11

Get the width and length of the terrain

12

set and get the height of the terrain at a particular grid point

13

Computes and returns the normal at each/some point

14

Load Terrain

load a terrain from an image file. First, we call ' loadBMP function to load the bitmap from file.

Then' we go through the pixels of the array and use them to set the heights of the terrain.

15

Load Terrain

A color of 0 corresponds to a height of -height / 2, and a color of 255 corresponds to a height of height / 2.

It doesn't matter which color component we use . The red component is used for no particular reason.

Then, we delete the image and force the terrain to compute all of the normals.

16

scale our terrain

it is at most 5 units wide and 5 units long. Then, we translate it so it's centered.

17

Here, we draw the terrain. GL_TRIANGLE_STRIP is new. It makes OpenGL draw a triangle at every three consecutive vertices

that you indicate. If your vertices are v1, v2, v3, ..., then OpenGL will draw the triangles

(v1, v2, v3), (v2, v3, v4), (v3, v4, v5), .... To draw the terrain, for each z, we do a triangle strip with vertices (0,

h1, z), (0, h2, z + 1), (1, h3, z), (1, h4, z + 1), (2, h5, z), (2, h6, z + 1),

18

Using triangle strips is not only more convenient than using triangles; it's faster, as there are fewer 3D vertices to send to the graphics card. So, our terrain is drawn as shown below:

19

Main

20

21

Rough and Smoothed

22

Smoothing

How exactly are we going to smooth the normals? For each normal, let's average in a little bit of the surrounding normals.

top related