Top Banner
Image Synthesis Rabie A. Ramadan, PhD 6
22

Image Synthesis Rabie A. Ramadan, PhD 6. 2 3D Images.

Jan 01, 2016

Download

Documents

Harriet Pearson
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Image Synthesis Rabie A. Ramadan, PhD 6. 2 3D Images.

Image Synthesis

Rabie A. Ramadan, PhD

6

Page 2: Image Synthesis Rabie A. Ramadan, PhD 6. 2 3D Images.

2

3D Images

Page 3: 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.

Page 4: Image Synthesis Rabie A. Ramadan, PhD 6. 2 3D Images.

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

Page 5: Image Synthesis Rabie A. Ramadan, PhD 6. 2 3D Images.

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

Page 6: Image Synthesis Rabie A. Ramadan, PhD 6. 2 3D Images.

6

How do we represent a terrain?

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

Page 7: Image Synthesis Rabie A. Ramadan, PhD 6. 2 3D Images.

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.

Page 8: Image Synthesis Rabie A. Ramadan, PhD 6. 2 3D Images.

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.

Page 9: Image Synthesis Rabie A. Ramadan, PhD 6. 2 3D Images.

9

constructor for the Terrain class

It initializes all of our variables.

Page 10: Image Synthesis Rabie A. Ramadan, PhD 6. 2 3D Images.

10

Destructor for the Terrain class

The destructor deletes the two-dimensional arrays hs and normals

Page 11: Image Synthesis Rabie A. Ramadan, PhD 6. 2 3D Images.

11

Get the width and length of the terrain

Page 12: Image Synthesis Rabie A. Ramadan, PhD 6. 2 3D Images.

12

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

Page 13: Image Synthesis Rabie A. Ramadan, PhD 6. 2 3D Images.

13

Computes and returns the normal at each/some point

Page 14: Image Synthesis Rabie A. Ramadan, PhD 6. 2 3D Images.

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.

Page 15: Image Synthesis Rabie A. Ramadan, PhD 6. 2 3D Images.

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.

Page 16: Image Synthesis Rabie A. Ramadan, PhD 6. 2 3D Images.

16

scale our terrain

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

Page 17: Image Synthesis Rabie A. Ramadan, PhD 6. 2 3D Images.

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

Page 18: Image Synthesis Rabie A. Ramadan, PhD 6. 2 3D Images.

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:

Page 19: Image Synthesis Rabie A. Ramadan, PhD 6. 2 3D Images.

19

Main

Page 20: Image Synthesis Rabie A. Ramadan, PhD 6. 2 3D Images.

20

Page 21: Image Synthesis Rabie A. Ramadan, PhD 6. 2 3D Images.

21

Rough and Smoothed

Page 22: Image Synthesis Rabie A. Ramadan, PhD 6. 2 3D Images.

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.