240 likes | 499 Views
Terrain Rendering. New Concepts. Loading geometry from file Camera animation Advanced lighting and materials Texture mapping Display lists. Loading geometry from file. Motivations: Built-in geometry is limited (spheres, teapots, cubes, etc)
E N D
New Concepts • Loading geometry from file • Camera animation • Advanced lighting and materials • Texture mapping • Display lists
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
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)
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))
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
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
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
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)
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
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)
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
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
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
Advanced Lighting • Relevant OpenGL functions: • glLight • glMaterial • glLightModel
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)
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
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
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
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
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
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
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
Resources • OpenGL Programming Guide(The Red Book)http://www.glprogramming.com/red/ • Viewinghttp://www.glprogramming.com/red/chapter03.html • Lightinghttp://www.glprogramming.com/red/chapter06.html • PyOpenGL Man Pages (Great OpenGL API doc)http://pyopengl.sourceforge.net/documentation/manual/index.xml • NeHe OpenGL Tutorialshttp://nehe.gamedev.net/