Presentation is loading. Please wait.

Presentation is loading. Please wait.

Terrain Rendering. New Concepts Loading geometry from file Camera animation Advanced lighting and materials Texture mapping Display lists.

Similar presentations


Presentation on theme: "Terrain Rendering. New Concepts Loading geometry from file Camera animation Advanced lighting and materials Texture mapping Display lists."— Presentation transcript:

1 Terrain Rendering

2 New Concepts Loading geometry from file Camera animation Advanced lighting and materials Texture mapping Display lists

3 Loading geometry from file Motivations: Built-in geometry is limited (spheres, teapots, cubes, etc) Artists want to experiment with new visual designs without recompiling program Fancy editors can be used to work with data if it is in a common file format

4 Loading geometry from a file For this assignment: 2D height-field stored as image Each pixel represents the height of the terrain at that point (only 1 channel used) Stored in ppm format Lines, triangles, quads must be generated from this before rendering (as well as the normals if lighting is required)

5 Loading geometry from a file Interpreting a pixel as a 3D point: Let s = {units per index} Let h = {units at max height} For each pixel at i,j in image: (x,y,z) = (s*i, s*j, h*intensity(i,j))

6 Loading geometry from file Generating wire-frame geometry For each pixel not on top or left border –Emit a line from current pixel to upper neighbor –Emit a line from current pixel to left neighbor

7 Loading geometry from file Generating surfaces (GL_QUADS) For each pixel not on top or left border –A = current point –B = upper neighbor –C = upper-left neighbor –D = left neighbor –Emit quad of A,B,C,D

8 Loading geometry from file Generating surface with normals for proper lighting: (overview) Build 3D point representations Calculate normals per triangle Average all normals sharing a vertex Normalize averages

9 Loading geometry from file Calculate a triangle’s normal Let the triangle be the points A,B,C Let u = (B-A) -- vector subtraction Let v = (C-A) Normal = normalize(u cross v)

10 Loading geometry from file Shortcut math for height-fields: All points are evenly spaced in the x-y plane. They only differ in their z value For triangle A,B,C in question (a point and its upper and left neighbors) –Let a = A.z –Let b = B.z –Let c = C.z –Let u = b-a –Let v = c-a –N = Normalize(u,v,1) This is just for triangle normals, must still average and renormalize to get vertex normals

11 Loading geometry from file To use your normals call glNormal3f before the glVertex3f corresponding vertex If modelview matrix never has any scaling and your normals are always unit length glEnable(GL_NORMALIZE) is unnecessary (better performance)

12 Camera animation Motivation: Motion is a change over time that can help convey an extra dimension of detail 3D geometric details more visible in motion Let user decide what view they want

13 Camera animation Helicopter camera –gluLookAt Eye point is helicopter (on some fixed path) LookAt point is target (user controlled?) Up vector is always up (for most worlds) First-person-shooter camera –glTranslate using x,y,z of player location –glRotate Rotate about z (up vector) by player heading Rotate about y (horizontal axis) by player tilt –Keyboard/mouse events Update player parameters

14 Advanced lighting/materials Scene may have several lights Each light has ambient,diffuse,specular emmision coefficients Scene may have many objects Each object has ambient,diffuse,specular reflection coefficients

15 Advanced Lighting Relevant OpenGL functions: –glLight –glMaterial –glLightModel

16 Texture Mapping Motivation: Real objects are not all one color We can make geometry look more detailed if we glue a picture onto it Geometry can be re-used with different textures Real world image data can be sampled for use in 3D to gain more realism (photographs)

17 Texture Mapping A texture is an image with a different color at each pixel (RGB/RGBA) No matter the size, the texture is indexed using u,v in the range 0-1 These are texture coodinates Geometry can be augmented with texture coordinates to describe how image should be stretched over the surface

18 Texture Mapping An artist can pick the u,v for each vertex (manual mapping) A formula can compute u,v for each vertex (automatic mapping) –(u,v) = (x,y) -- planar mapping –(u,v) = (theta,phi) -- spherical mapping –(u,v) = (something, t) -- temporal mapping

19 Texture Mapping Loading a texture for use –glEnable GL_TEXTURE_2D -- turn on text –glGenTextures -- request an unused texture id –glBindTexture -- make a certain texture id the active one –gluBuildMaps Target = GL_TEXTURE_2D Internal format = GL_RGB Width = canvas width Height = canvas height Format = GL_RGBA (ppmLoadCanvas make 4-byte pixels) Type = GL_UNSIGNED_BYTE (one byte per channel, 0-255) Data = cavas.pixels (the raw image data) –glTexCoord2f Specify a u,v before calling the corresponding glVertex3f

20 Display lists Motivation Usually we draw the same scene over and over with only minimal changes GPU is idle much of the time waiting for the CPU to feed it the scene again By submitting the non-changing parts of the scene once excess processing can be avoid and parallelism can be improved

21 Display lists Using a display list // before main loop list = glGenList(1) -- get an unused list id glNewList(list,GL_COMPILE) –Lots of (almost any) GL commands and expensive geometry and texture coordinate generation code go here –Resulting geometry will be saved on device (if possible) glEndList(list) // at rendering time glCallList(list) // returns almost instantly

22 Requirements Load a map.ppm and render it as a height field (given on command line) Load a skin.ppm and use it to texture the height field (given on command line) Light height field realistically (at least one directional light and correct normals) Use a display list to cache hieght field geometry Animate viewpoint using either helicopter or FPS style camera

23 Impressive extensions Shoreline –render another height field with a fixed height and different material properties, possibly use difference between heights to decide a color Distant Haze –use glFog and friends to lighten distant terrain Dune Buggy –allow user to move an object around the height field with arcade-style driving controls and track it with the animated camera Lazy Camera –soften camera movements –lookAt = lookAt_last + k*(target-lookAt_last) -- k in 0-1 Creative maps –edit map and skin to design your own terrain Accurate maps –find height field and satellite picture for a real-world location

24 Resources OpenGL Programming Guide (The Red Book) http://www.glprogramming.com/red/OpenGL Programming Guide Viewing http://www.glprogramming.com/red/chapter03.htmlViewing Lighting http://www.glprogramming.com/red/chapter06.htmlLighting PyOpenGL Man Pages (Great OpenGL API doc) http://pyopengl.sourceforge.net/documentation/manual/index.xmlPyOpenGL Man Pages NeHe OpenGL Tutorials http://nehe.gamedev.net/NeHe OpenGL Tutorials


Download ppt "Terrain Rendering. New Concepts Loading geometry from file Camera animation Advanced lighting and materials Texture mapping Display lists."

Similar presentations


Ads by Google