1 / 75

Texture Mapping

Texture Mapping. LIN Tao. Lecture Objectives. Apply a texture by assigning texture coordinates to a polygon explicitly Modify texture parameters Identify the advantages and disadvantages of the different filters available for textures Create textures with multiple levels of detail (mipmaps)

Télécharger la présentation

Texture Mapping

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Texture Mapping LIN Tao

  2. Lecture Objectives Apply a texture by assigning texture coordinates to a polygon explicitly Modify texture parameters Identify the advantages and disadvantages of the different filters available for textures Create textures with multiple levels of detail (mipmaps) Use texture objects to improve performance Modify the texture environment Use OpenGL and GLU to generate texture coordinates automatically

  3. The Limits of Geometric Modeling Although graphics cards can render over 10 million polygons per second, that number is insufficient for many phenomena Clouds Grass Terrain Skin

  4. Modeling an Orange(1) Consider the problem of modeling an orange (the fruit) Start with an orange-colored sphere Too simple Replace sphere with a more complex shape Does not capture surface characteristics (small dimples) Takes too many polygons to model all the dimples

  5. Modeling an Orange (2) Take a picture of a real orange, scan it, and “paste” onto simple geometric model This process is known as texture mapping Still might not be sufficient because resulting surface will be smooth Need to change local shape Bump mapping

  6. Three Types of Mapping Texture Mapping Uses images to fill inside of polygons Environment (reflection mapping) Uses a picture of the environment for texture maps Allows simulation of highly specular surfaces Bump mapping Emulates altering normal vectors during the rendering process

  7. Texture Mapping geometric model texture mapped

  8. Environment Mapping

  9. Bump Mapping

  10. OpenGL as Pipeline Architecture Per Vertex Operations & Primitive Assembly Polynomial Evaluator DisplayList Per Fragment Operations Frame Buffer CPU Rasterization Texture Memory Pixel Operations

  11. Where does mapping take place? Mapping techniques are implemented at the end of the rendering pipeline Very efficient because few polygons make it past the clipper

  12. Is it simple? Although the idea is simple---map an image to a surface---there are 3 or 4 coordinate systems involved 2D image 3D surface

  13. Coordinate Systems Parametric coordinates May be used to model curves and surfaces Texture coordinates Used to identify points in the image to be mapped Object or World Coordinates Conceptually, where the mapping takes place Window Coordinates Where the final image is really produced

  14. What is a Texture? • A texture image is a rectangular array of pixel data • Usually a 2D array, but can be 1D or 3D • A "texture pixel" is called a "texel” • Can contain color, luminance and/or alpha information

  15. What Information Does a Texture Contain? struct texel{ unsigned char lumin; }; A one component texture contains Luminance, Intensity, or Alpha data. Examples: Wood, grass, sand

  16. struct texel{ unsighed char lumin; unsigned char alpha; }; A two component texture contains Luminance and Alpha (transparency) data. Examples: Trees, clouds

  17. struct texel{ unsighed char b;//blue unsigned char g;//green unsigned char r;//red }; A three component texture contains Red, Green, and Blue values. Examples: Fabrics, bricks

  18. struct texel{ unsigned char a;//alpha unsigned char b;//blue unsigned char g;//green unsigned char r;//red }; A four component texture contains Red, Green, Blue, and Alpha values. Examples: Objects

  19. What is Texture Mapping? //left top glTexcoord2f(..) glVertex3f(...); //left bottom glTexcoord2f(..) glVertex3f(...); //right top glTexcoord2f(..) glVertex3f(...); //right bottom glTexcoord2f(..) glVertex3f(...); Applying textures (images) to polygons.

  20. Based on parametric texture coordinates glTexCoord*() specified at each vertex Mapping a Texture Texture Space Object Space t 1, 1 (s, t) = (0.2, 0.8) 0, 1 A a c (0.4, 0.2) b B C (0.8, 0.4) s 0, 0 1, 0

  21. The basic texture mapping steps are: 记住: 纹理坐标必须在RGBA模式下才可以使用,在颜色索引模式下使用纹理 体贴的结果难以预料 • Specify the texture. • glTexImage ;gluScaleImage • Indicate how the texture is to be applied to each pixel. • glTexEnv ;glTexParameter • Enable texture mapping. • glEnable • Draw the scene, providing geometric and texture coordinates. • glTexCoord

  22. Define Image as a Texture glTexImage2D( target, level, components, w, h, border, format, type, texels ); target: type of texture, e.g. GL_TEXTURE_2D level: used for mipmapping (discussed later) components: elements per texel w, h: width and height of texels in pixels border: used for smoothing (discussed later) format and type: describe texels texels: pointer to texel array glTexImage2D(GL_TEXTURE_2D, 0, 3, 512, 512, 0, GL_RGB, GL_UNSIGNED_BYTE, my_texels);

  23. Specifying a 2D Texture • void glTexImage2D ( GLenum target, GLint level, GLint internalformat, GLsizei width, GLsizei height, GLint border, GLenum format, GLenum type, const GLvoid *pixels) • target- target texture (it must be GL_TEXTURE_2D for OpenGL release 1.0) • level - resolution level (it is used for specifying multiple levels-of-detail) • internalformat- internal storage format of the texture (it must be 1, 2, 3, 4, or one of a number of symbolic constants;

  24. Specifying a 2D Texture width - width of the texture image (it must be 2m+2*(border) for some integer m) height - height of the texture image (it must be 2m+2*(border) for some integer m) border - the width of the border (either 0 or 1) format - format of the pixel data (GL_RGBA) type - data type of the pixel data (GL_UNSIGNED_BYTE) pixels - pointer to the image data in memory

  25. What if the Texture is the Wrong Size? glTexImage2D() requires a texture whose pixel dimensions are powers of two If necessary, use the OpenGL utility routine gluScaleImage() to scale the image Pass the scaled image to glTexImage2D()

  26. Converting 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_inis source image data_outis for destination image Image interpolated and filtered during scaling

  27. Scaling Texture Images • GLint gluScaleImage( GLenum format, GLsizei widthin, GLsizei heightin, GLenum typein, const void *datain, GLsizei widthout, GLsizei heightout, GLenum typeout, void *dataout ) • format specifies the format of the pixel data • widthin, heightin specify the dimensions of the source image to be scaled • datain specifies a pointer to the source image • typeinand typeout specify the data type for datain and dataout, respectively

  28. Texture Coordinates • Texture coordinates are part of the data that is associated with each vertex. • A 2D texture is treated as a 1x1 square whose texture coordinates go from 0.0 to 1.0 in each dimension. • Texture coordinates are homogeneous (s, t, r, q) • r is unused and q is 1.0

  29. Texture coordinates are assigned to each vertex of a polygon. • Assigned explicitly or generated automatically • Texture coordinates are interpolated as a polygon is filled. • Filters control how the interpolation is performed

  30. Setting Texture Coordinates Explicitly • void glTexCoord2fv( const GLfloat *v ) • v Specifies a pointer to an array of two, which in turn specify the s, t, r, and q texture coordinates

  31. Typical Code glBegin(GL_POLYGON); glColor3f(r0, g0, b0); //if no shading used glNormal3f(u0, v0, w0); // if shading used glTexCoord2f(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 efficiency

  32. Texture Parameters OpenGL has a variety of parameters that determine how texture is applied Wrapping parameters determine what happens if s and t are outside the (0,1) range Filter modes allow us to use area averaging instead of point samples Mipmapping allows us to use textures at multiple resolutions Environment parameters determine how texture mapping interacts with shading

  33. Wrapping Mode Clamping: if s,t > 1 use 1, if s,t <0 use 0 Wrapping: use s,t modulo 1 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP ) glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT ) t s GL_CLAMP wrapping GL_REPEAT wrapping texture

  34. 重复(wrap)和截断(clamp) OpenGL中,指定的纹理坐标可以在(0,1)区间之外 两种方式可以处理:GL_REPEAT and gl_CLAMP GL_REPEAT 如果该值为正,则直接使用小数部分,若该值为负,则取相加和为正的最小整数。例如,纹理坐标(1.3,-0.4),将采样? (0.3,0.6). GL_CLAMP 如果坐标值为负,强制取0 若坐标值超过1,则强制取1 例如,纹理坐标(1.3, -0.4),采样? (1.0,0.0)

  35. Magnification and Minification • One texel rarely corresponds to one pixel on the final screen image. • Magnification and Minification Filter • Texture information must be magnified or minified

  36. Magnification and Minification Texture Polygon Texture Polygon Magnification Minification More than one texel can cover a pixel (minification) or more than one pixel can cover a texel (magnification) Can use point sampling (nearest texel) or linear filtering ( 2 x 2 filter) to obtain texture values

  37. Filter Modes P254 Modes determined by glTexParameteri( 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)

  38. Mipmapped Textures (后面详细讨论) Mipmapping allows for prefiltered texture maps of decreasing resolutions Lessens interpolation errors for smaller textured objects Declare mipmap level during texture definition glTexImage2D( GL_TEXTURE_*D, level,… ) GLU mipmap builder routines will build all the textures from a given image gluBuild*DMipmaps( … )

  39. Specifying Filters • void glTexParameterf( GLenum target, GLenum pname, const GLfloat param ) • target specifies the type of the target texture • GL_TEXTURE_1D or GL_TEXTURE_2D • pname specifies which filter to set • GL_TEXTURE_MAG_FILTER or GL_TEXTURE_MIN_FILTER

  40. param specifies whether speed or image quality is more important • GL_NEAREST to choose the texel nearest to the texture coordinate computed for the pixel. • GL_LINEAR to use the weighted average of the four texels nearest to the texture coordinate computed for the pixel.

  41. How Filters Work(1) • Magnification • Pixels map to less than one texel. • If GL_NEAREST is specified, then texel 10 is applied to the pixel. • If GL_LINEAR is specified, the weighted average of texels 9, 10, 13 and 14 is applied to the pixel.

  42. How Filters Work(2) • Minification • Pixels map to more than one texel. • If GL_NEAREST is specified, then texel 17 is applied to the pixel. • If GL_LINEAR is specified, the weighted average of texels 12, 13, 17 and 18 is applied to the pixel.

  43. Enabling Texture Mapping(注意!) • void glEnable( GLenum mode ) • Set mode to GL_TEXTURE_1D if one-dimensional texturing is performed using glTexImage1D() • Set mode to GL_TEXTURE_2D if two-dimensional texturing is performed using glTexImage2D() • Set mode to GL_TEXTURE_3D_EXT if three-dimensional texturing is performed using glTexImage3DEXT()

  44. Texture.C This program reads in a single image from a .rgb file, and uses it as a texture. It applies the image to a polygon using explicit texture coordinates. The polygon is then made to "swim" across the window. The name of the file containing the image can be specified on the command line. If no file is specified, the program attempts to open the file fish.rgba in the current directory. After reading in the image, the program initializes the texture parameters and loads the texture in initTexture(). Before loading the texture, initTexture() determines whether the image is a power of two in each dimension. If not, it uses the utility routine gluScaleImage() to scale the image. Next it sets both the minification and magnification filter to perform nearest neighbor interpolation. Lastly, it calls glTexImage2D() to load the image into texture memory, and enables 2D texturing. It then applies the texture to a rectangular polygon using explicit texture coordinates. The function drawScene() simply uses glTexCoord2fv(), to explicitly assign the texture coordinates to the polygon.

  45. 纹理对象(Texture Object)的提出 在默认运算模式下,当前纹理是OpenGL状态的一部分,在执行glTexImage()时,当前纹理从系统内存转移到texture memory ,如果只有一个单独纹理图,上述移动需要一次。 如果程序中有多个纹理,性能会急剧下降! 比如,场景中的每个对象都有各自相应的纹理,就像材质属性一样,每次绘制一个不同的对象时,都必须加载相应的纹理。

  46. Texture Objects 纹理对象 纹理对象是纹理内存中的数据结构,用于存储纹理数据,便于随时使用。 主要思想:一次性加载多个纹理到纹理内存。首先,根据纹理和相应的参数值创建一个纹理对象,然后用多个对象来填充纹理内存,并令OpenGL使用一个对象标识符来标识纹理。只要内存足够容纳这些纹理对象,就可以避免纹理重复加载。如果内存不足以容纳所有纹理,则按照优先级对纹理对象进行排序以减少从处理器到纹理内存的数据移动量。

  47. How to Use Texture Object • Generate texture names. • glGenTextures() • Initially bind (create) texture objects to texture data, including the image arrays and texture properties. • glBindTexture() • Bind and rebind texture objects, making their data currently available for rendering textured models. • glBindTexture()

  48. void glGenTextures (GLsize n, GLuint *texnames) Returns n currently unused names for texture objects in the array textureNames. The names returned in textureNames do not have to be a contiguous set of integers. The names in textureNames are marked as used, but they acquire texture state and dimensionality (1D or 2D) only when they are first bound. Zero is a reserved texture name and is never returned as a texture name by glGenTextures().

  49. void glBindTexture (GLenum target, GLuint texname) • glBindTexture() does three things • When using textureName of an unsigned integer other than zero for the first time, a new texture object is created and assigned that name. • When binding to a previously created texture object, that texture object becomes active. • When binding to a textureName value of zero, OpenGL stops using texture objects and returns to the unnamed default texture.

More Related