
计算机图形学computer graphics课件18.ppt
59页Texture MappingSoftware College, Shandong University Instructor: Zhou Yuanfeng E-mail: yuanfeng.zhou@23Objectives•Introduce Mapping MethodsTexture MappingEnvironment MappingBump Mapping•Consider basic strategiesForward vs backward mappingPoint sampling vs area averaging4The Limits of Geometric Modeling•Although graphics cards can render over 10 million polygons per second, that number is insufficient for many phenomenaCloudsGrassTerrainSkin, hairWater and fire5Modeling an Orange•Consider the problem of modeling an orange (the fruit)•Start with an orangecolored sphereToo simple•Replace sphere with a more complex shapeDoes not capture surface characteristics (small dimples)Takes too many polygons to model all the dimples6Modeling an Orange (2)•Take a picture of a real orange, scan it, and “paste” onto simple geometric modelThis process is known as texture mapping•Still might not be sufficient because resulting surface will be smoothNeed to change local shapeBump mapping+7Three Types of Mapping•Texture MappingUses images to fill inside of polygons•Environment (reflection mapping)Uses a picture of the environment for texture mapsAllows simulation of highly specular surfaces•Bump mappingEmulates altering normal vectors during the rendering process8Texture Mappinggeometric modeltexture mapped9Environment Mapping 10Bump Mapping+=original surfacebump mapmodified surface11Where does mapping take place?•Mapping techniques are implemented at the end of the rendering pipelineVery efficient because few polygons make it past the clipper 12Is it simple?•Although the idea is simplemap an image to a surfacethere are 3 or 4 coordinate systems involved2D image3D surface13Coordinate Systems•Parametric coordinatesMay be used to model curves and surfaces•Texture coordinatesUsed to identify points in the image to be mapped•Object or World CoordinatesConceptually, where the mapping takes place•Window CoordinatesWhere the final image is really produced14Texture Mappingparametric coordinatestexture coordinatesworld coordinateswindow coordinates15Mapping Functions•Basic problem is how to find the maps•Consider mapping from texture coordinates to a point a surface •Appear to need three functionsx = x(s,t)y = y(s,t)z = z(s,t)•But we really want to go the other wayst(x,y,z)16Backward Mapping•We really want to go backwardsGiven a pixel, we want to know to which point on an object it correspondsGiven a point on an object, we want to know to which point in the texture it corresponds•Need a map of the form s = s(x,y,z)t = t(x,y,z)•Such functions are difficult to find in general Parameterization1718Two-part mapping•One solution to the mapping problem is to first map the texture to a simple intermediate surface•Example: map to cylinder19Cylindrical Mappingparametric cylinderx = r cos (2pi u)y = r sin (2pi u)z = v/hmaps rectangle in u,v space to cylinderof radius r and height h in world coordinatess = ut = vmaps from texture spaceCylindrical Mapping20•Mapping: zz(a) texture(b)cylindrical xyrh(c)mappingxy(d)ba(c)11Cylindrical Mapping21•Map u and v to points a,b,c,d: (1) : (2) : (3) : (4) :Solving:From texture to cylindrical parameters:From cylindrical parameters to texture:22Spherical MappingWe can use a parametric spherex = r cos 2piuy = r sin 2pu cos 2pivz = r sin 2pu sin 2pivin a similar manner to the cylinderbut have to decide where to putthe distortionSpheres are used in environmental maps23Box Mapping•Easy to use with simple orthographic projection•Also used in environment mapsPolycube mapping24Conformal mapping•In mathematics, a conformal map is a functionwhich preserves angles.2526Second Mapping•Map from intermediate object to actual objectNormals from intermediate to actualNormals from actual to intermediateVectors from center of intermediate intermediateactual27Aliasing•Point sampling of the texture can lead to aliasing errorspoint samples in u,v (or x,y,z) spacepoint samples in texture spacemiss blue stripes28Area AveragingA better but slower option is to use area averagingNote that preimage of pixel is curvedpixelpreimageCatmull texture mapping•Surface subdivision29Pixel edSScreenSurfaceContinuous mapping30 (a) Spherical Mapping (b)Cylindrical MappingMip-map (1983, Lance Williams)31Mip-map•Actually mipmap is a lookup table for texture32OpenGL Texture Mapping34Basic StrategyThree steps to applying a texture1.specify the texture•read or generate image•assign to texture•enable texturing2.assign texture coordinates to vertices•Proper mapping function is left to application3.specify texture parameters•wrapping, filtering35Texture Mappingstxyzimagegeometrydisplay36Texture Example•The texture (below) is a 256 x 256 image that has been mapped to a rectangular polygon which is viewed in perspective37Texture Mapping and the OpenGL Pipelinegeometry pipelineverticespixel pipelineimagerasterizer•Images and geometry flow through separate pipelines that join at the rasterizer“complex” textures do not affect geometric complexity38•Define a texture image from an array of texels (texture elements) in CPU memory Glubyte my_texels[512][512];•Define as any other pixel mapScanned imageGenerate by application code•Enable texture mappingglEnable(GL_TEXTURE_2D)OpenGL supports 14 dimensional texture mapsSpecifying a Texture Image1D texture mapping391D texture mapping•unsigned char colorbar[1024];•glTexImage1D(GL_TEXTURE_1D, 0, GL_RGBA, 256, 0, GL_BGRA_EXT, GL_UNSIGNED_BYTE, colorbar);•glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, GL_CLAMP);•Texture coord: [0,1]4041Define Image as a TextureglTexImage2D( target, level, components, w, h, border, format, type, texels );target: type of texture, e.g. GL_TEXTURE_2Dlevel: used for mipmapping (discussed later)components: elements per texelw, h: width and height of texels in pixelsborder: used for smoothing (discussed later)format and type: describe texelstexels: pointer to texel arrayglTexImage2D(GL_TEXTURE_2D, 0, 3, 512, 512, 0, GL_RGB, GL_UNSIGNED_BYTE, my_texels);42Converting A Texture Image•OpenGL requires texture dimensions to be powers of 2•If dimensions of image are not powers of 2•gluScaleImage( format, w_in, h_in, type_in, *data_in, w_out, h_out, type_out, *data_out );data_in is source imagedata_out is for destination image•Image interpolated and filtered during scaling43•Based on parametric texture coordinates•glTexCoord*() specified at each vertexst1, 10, 10, 01, 0(s, t) = (0.2, 0.8)(0.4, 0.2)(0.8, 0.4)ABCabcTexture SpaceObject SpaceMapping a Texture44Typical CodeglBegin(GL_POLYGON);glColor3f(r0, g0, b0); //if no shading usedglNormal3f(u0, v0, w0); // if shading usedglTexCoord2f(s0, t0);glVertex3f(x0, y0, z0);glColor3f(r1, g1, b1);glNormal3f(u1, v1, w1);glTexCoord2f(s1, t1);glVertex3f(x1, y1, z1);..glEnd();Note that we can use vertex arrays to increase efficiency45Interpolation OpenGL uses interpolation to find proper texels from specified texture coordinates Can be distortionsgood selectionof tex coordinatespoor selectionof tex coordinatestexture stretchedover trapezoid showing effects of bilinear interpolation46Texture Parameters•OpenGL has a variety of parameters that determine how texture is appliedWrapping parameters determine what happens if s and t are outside the (0,1) rangeFilter modes allow us to use area averaging instead of point samplesMipmapping allows us to use textures at multiple resolutionsEnvironment parameters determine how texture mapping interacts with shading47Wrapping ModeClamping: if s,t > 1 use 1, if s,t <0 use 0Repeating: use s,t modulo 1glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP )glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT )texturestGL_CLAMPwrappingGL_REPEATwrapping48Magnification and MinificationTexturePolygonMagnificationMinificationPolygonTextureMore than one texel can cover a pixel (minification) ormore than one pixel can cover a texel (magnification)Can use point sampling (nearest texel) or linear filtering( 2 x 2 filter) to obtain texture values49Filter ModesModes determined byglTexParameteri( target, type, mode ) glTexParameteri(GL_TEXTURE_2D, GL_TEXURE_MAG_FILTER, GL_NEAREST);glTexParameteri(GL_TEXTURE_2D, GL_TEXURE_MIN_FILTER, GL_LINEAR);Note that linear filtering requires a border of an extra texel for filtering at edges (border = 1)50Mipmapped Textures•Mipmapping allows for prefiltered texture maps of decreasing resolutions•Lessens interpolation errors for smaller textured objects•Declare mipmap level during texture definitionglTexImage2D( GL_TEXTURE_*D, level, … )•GLU mipmap builder routines will build all the textures from a given imagegluBuild*DMipmaps( … )51Example pointsamplingmipmapped pointsamplingmipmapped linear filtering linear filtering52Texture Functions•Controls how texture is applied•glTexEnv{fi}[v]( GL_TEXTURE_ENV, mode, param ) •GL_TEXTURE_ENV_MODE modesGL_MODULATE: modulates with computed shadeGL_BLEND: blends with an environmental colorGL_REPLACE: use only texture colorGL(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);•Set blend color with GL_TEXTURE_ENV_COLORDemo53Perspective Correction Hint•Texture coordinate and color interpolationeither linearly in screen spaceor using depth/perspective values (slower)•Noticeable for polygons “on edge”•glHint( GL_PERSPECTIVE_CORRECTION_HINT, hint )where hint is one of •GL_DONT_CARE•GL_NICEST•GL_FASTEST54Generating Texture Coordinates•OpenGL can generate texture coordinates automaticallyglTexGen{ifd}[v]()•specify a planegenerate texture coordinates based upon distance from the plane•generation modesGL_OBJECT_LINEARGL_EYE_LINEAR GL_SPHERE_MAP (used for environmental maps)Linear mapping•Texture is “attached” to object:GLfloat params = {A,B,C,D};glTexGenfv(GL_S, GL_OBJECT_PLANE,params);glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);glEnable(GL_TEXTURE_GEN_S);• Default mapping is identityglTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP);glEnable(GL_TEXTURE_GEN_S);5556Texture Objects•Texture is part of the OpenGL state If we have different textures for different objects, OpenGL will be moving large amounts data from processor memory to texture memory•Recent versions of OpenGL have texture objectsone image per texture objectTexture memory can hold multiple texture objects57Applying Textures II1.specify textures in texture objects2.set texture filter 3.set texture function 4.set texture wrap mode5.set optional perspective correction hint6.bind texture object 7.enable texturing8.supply texture coordinates for vertexcoordinates can also be generated58Other Texture Features•Environment MapsStart with image of environment through a wide angle lens •Can be either a real scanned image or an image created in OpenGLUse this texture to generate a spherical mapUse automatic texture coordinate generation•MultitexturingApply a sequence of textures through cascaded texture units3D texture•Cg shader coords(u,v,r)59。
