Presentation is loading. Please wait.

Presentation is loading. Please wait.

Texture Mapping + Texture Object = Texture Mapped Object.

Similar presentations


Presentation on theme: "Texture Mapping + Texture Object = Texture Mapped Object."— Presentation transcript:

1 Texture Mapping + Texture Object = Texture Mapped Object

2 Texture Mapping Basics
Basic Concepts Texture (2D Texture): A two-dimensional array of data Texel : An individual data element in a texture Texture Space : A normalized coordinate space in which the texture is specified. t 1 (s, t): Texture Coordinates 0.0  s  1.0, 0.0  t  1.0 s 1

3 Surface-Texture Transformation
Transform from object space (x, y, z) to texture space (s, t) Curved surface Parametric equation P = P(u, v). Usually transform from parametric space (u, v) to texture space (s, t) Example: Sphere Parametric equation: z P = (x, y, z) r y Surface-Texture Transformation x

4 Polygon surface Specify texture coordinates for each vertex. Then linearly interpolate texture coordinates across projected polygon. y 3 1 Scan line y 4 p(x, y) 5 2 x

5 Texture Mapping Procedure
Projected Pixel Area yd t Pixel Area Object Surface s Pixel Center xd Device Space Object Space Texture Space Surface-texture transformation

6 Polygon surface with texture coordinates for four corners
Texture Wrapping Methods Methods to deal with texture coordinates outside [0, 1] range. (0, 3) (3, 3) Texture (0, 0) (3, 0) Polygon surface with texture coordinates for four corners

7 Repeat Mirrored Repeat n  s < n  1 s  s  n n  s < n  1 if (n is even number) s  s  n if (n is odd number) s  n  1  s

8 Clamp Border Clamp if (s < 0) s  0 if (s > 1) s  1 if (s < 0) {s  0; Use border color;} if (s > 1) {s  1; Use border color;} Border Color:

9 One-Dimensional and Three-Dimensional Texture
One-Dimensional Texture (Linear Texture) Three-Dimensional Texture (Volume Texture, Solid Texture) One-dimensional array of data. Three-dimensional array of data. Texture coordinate s. Texture coordinates (s, t, r). r s 1 s t

10 Texture Mapping Examples

11 Texture Filtering Nearest Filtering Linear Filtering
Texture Filtering: Determine screen pixel color from texel color values using projected pixel area in texture space. Nearest Filtering Linear Filtering Mipmap Nearest Filtering Mipmap Linear Filtering Anisotropic Filtering

12 Texture Minification and Magnification
Projected Pixel Area Projected Pixel Area s s Minification: Projected pixel area in texture space is larger than a single texel. Magnification: Projected pixel area in texture space is smaller than a single texel.

13 Projected Pixel Center
Nearest Filtering (Point Sampling) Select the texel whose center lies nearest to the projected pixel center. Use the color of this texel as pixel color. t Projected Pixel Center Nearest Texel Center s Nearest filtering can result in severe aliasing effects and blocky images.

14 Projected Pixel Center
Linear Filtering (Bilinear Filtering) Select 22 array of texels that lie nearest to the projected pixel center. The pixel color is linearly interpolated from these texels. t Projected Pixel Center s Linear filtering produces better and smooth texture mapped images.

15 Texture array mn (s0, t0+t) (s0+s, t0+t) (s, t) (s0, t0)

16 Mipmap Using a single texture, when object surface moves further away from the viewer, texture mapping may result in artifacts such as shimmering and flashing. Mipmap (multum in parvo, many things in a small place): A series of prefiltered texture images with decreasing resolution. Level 0 (8  8) Level 1 (4  4) Level 2 (2  2) Level 3 (1  1) Mipmap is only used for texture minification. Higher mipmap level should be chosen for longer distance between object surface and the viewer.

17 Mipmap Nearest Filtering
Choose the nearest mipmap level. Mipmap Linear Filtering Choose the two nearest mipmap levels. Linearly interpolate between texels sampled from these two levels. Trilinear Filtering: Use mipmap linear filtering for sampling between mipmap levels and use bilinear filtering for sampling within each mipmap level.

18 Without Mipmap With Mipmap

19 Anisotropic Filtering
Texture anisotropy: The projected pixel area in texture space has elongated shape if the object surface forms a steep angle with respect to view plane. yd Projected Pixel Area t Pixel Area Object Surface s xd View Plane

20 yd Object Surface Projected Pixel Area t Pixel Area s xd View Plane Anisotropic Filtering: Use multiple samples in projected pixel area. Different sample patterns are used for different projected pixel area shape.

21 Trilinear Filtering Anisotropic

22

23 OpenGL Texture Mapping
OpenGL Texture Mapping Procedures: Create Texture Object. Specify Texture Image for Texture Object. Specify Texture Parameters (Texture Wrapping and Filtering Modes) for Texture Object. Specify Texture Environment (The way in which texel color is combined with fragment color). Enable Texture Mapping and Bind Texture Object. Render Primitives. Specify Texture Coordinates for Each Vertex.

24 Texture Objects A texture object stores texture image data and texture parameters. Each texture object is identified by a positive integer index number. Procedures to use texture objects: Generate texture object names. Initially bind texture objects and set texture image data and parameters. Bind and rebind texture objects for rendering. Delete texture objects when they are no longer needed.

25 Generate Texture Object Names
void glGenTextures(GLsizei n, GLuint *textureNames) Return n currently unused integer index names for texture objects and store them in array textureNames. GLboolean glIsTexture(GLuint textureName) Return GL_TRUE if textureName is currently used as the name for a texture object. Otherwise return GL_FALSE. Delete Texture Objects void glDeleteTextures(GLsizei n, const GLuint *textureNames) Delete n texture objects whose names are stored in array textureNames. Their data is released and their names are freed.

26 Bind Texture Objects void glBindTexture(GLenum target, GLuint textureName) When using textureName for the first time, a new texture object is created. When binding to a previously created texture object, that texture object becomes active. target: Texture target textureName: Texture object name target Meaning GL_TEXTURE_1D One-dimensional Texture GL_TEXTURE_2D Two-dimensional Texture GL_TEXTURE_3D Three-dimensional Texture GL_TEXTURE_CUBE_MAP Cube Environment Map Use glEnable(target) to enable texture mapping. Use glDisable(target) to disable texture mapping.

27 Specify Texture Image Texture image can be specified from system memory or frame buffer. Texture image of a texture object can be replaced entirely or partly after it is initially specified. Texture image can be 1D, 2D or 3D. 3D texture support was added in OpenGL 1.2. Cube environment map texture consists of six 2D square texture images. Cube map support was added in OpenGL 1.3.

28 Specify or Replace Texture Image from System Memory
glTexImage*(), glTexSubImage*() System Memory Texture Memory Frame Buffer Unpack Pack Fragment Operations Pixel Storage Modes Pixel Transfer Operations (Pixel Map) Rasterization (Pixel Zoom)

29 Specify or Replace Texture Image from Frame Buffer
glCopyTexImage*(), glCopyTexSubImage*() System Memory Texture Memory Frame Buffer Unpack Pack Fragment Operations Pixel Storage Modes Pixel Transfer Operations (Pixel Map) Rasterization (Pixel Zoom)

30 Two-Dimensional Texture
void glTexImage2D(GLenum target, GLint level, GLint internalFormat, GLsizei width, GLsizei height, GLint border, GLenum format, GLenum type, const GLvoid *texels) Specify a 2D texture image from system memory. The image is set for texture object currently bound to target. target: 2D texture image target. level: Mipmap level. Base level is 0. internalFormat: Internal format for texel data. Indicates how texture image is stored internally in texture memory. width: Width of the texture image. height: Height of the texture image. border: Width of the texture image border. Must be 0 or 1. format: Texel data format of source image. type: Texel data type of source image. texels: System memory that stores source image data.

31 width and height must be (2n + 2border), where n is an integer.
format and type have the same meaning as in glReadPixels() and glDrawPixels(), except that format can not be GL_STENCIL_INDEX. 2D Texture Image Target target Meaning GL_TEXTURE_2D 2D texture image GL_TEXTURE_CUBE_MAP_POSITIVE_X Cube map +x face GL_TEXTURE_CUBE_MAP_NEGATIVE_X Cube map x face GL_TEXTURE_CUBE_MAP_POSITIVE_Y Cube map +y face GL_TEXTURE_CUBE_MAP_NEGATIVE_Y Cube map y face GL_TEXTURE_CUBE_MAP_POSITIVE_Z Cube map +z face GL_TEXTURE_CUBE_MAP_NEGATIVE_Z Cube map z face

32 Internal Format Base Internal Format Texel data components GL_RGB (R, G, B, 1) GL_RGBA (R, G, B, A) GL_ALPHA (0, 0, 0, A) GL_LUMINANCE (L, L, L, 1) GL_LUMINANCE_ALPHA (L, L, L, A) GL_INTENSITY (I, I, I, I) GL_DEPTH_COMPONENT Depth Value R = Red, G = Green, B = Blue, A = Alpha L = Luminance, I = Intensity

33 Base Internal Format Internal Format GL_RGB
GL_RGB, GL_R3_G3_B2, GL_RGB4, GL_RGB5, GL_RGB8, GL_RGB10, GL_RGB12, GL_RGB16 GL_RGBA GL_RGBA, GL_RGBA2, GL_RGBA4, GL_RGB5_A1, GL_RGBA8, GL_RGB10_A2, GL_RGBA12, GL_RGBA16 GL_ALPHA GL_ALPHA, GL_ALPHA4, GL_ALPHA8, GL_ALPHA12, GL_ALPHA16 GL_LUMINANCE GL_LUMINANCE, GL_LUMINANCE4, GL_LUMINANCE8, GL_LUMINANCE12, GL_LUMINANCE16 GL_LUMINANCE_ALPHA GL_LUMINANCE_ALPHA, GL_LUMINANCE4_ALPHA4, GL_LUMINANCE6_ALPHA2, GL_LUMINANCE8_ALPHA8, GL_LUMINANCE12_ALPHA4, GL_LUMINANCE12_ALPHA12, GL_LUMINANCE16_ALPHA16 GL_INTENSITY GL_INTENSITY, GL_INTENSITY4, GL_INTENSITY8, GL_INTENSITY12, GL_INTENSITY16 GL_DEPTH_COMPONENT GL_DEPTH_COMPONENT, GL_DEPTH_COMPONENT16, GL_DEPTH_COMPONENT24, GL_DEPTH_COMPONENT32

34 Use glGetIntegerv(GL_MAX_TEXTURE_SIZE, …) to obtain maximum supported texture size without border.
Use glGetIntegerv(GL_MAX_CUBE_MAP_TEXTURE_SIZE, …) to obtain maximum supported cube map texture size without border. To use an image with width or height that is not power of two (without border) as texture image, it must be scaled first. int gluScaleImage(GLenum format, GLint widthin, GLint heightin, GLenum typein, const void *datain, GLint widthout, GLint heightout, GLenum typeout, const void *dataout) Scale an image. Return 0 if succeeds, otherwise return a GLU error code. format: Pixel data format widthin, heightin: Width and height of input image typein: Pixel data type of input image datain: Input image data widthout, heightout: Width and height of output image typeout: Pixel data type of output image dataout: Output image data

35 void glCopyTexImage2D(GLenum target, GLint level,
void glCopyTexImage2D(GLenum target, GLint level, GLint internalFormat, GLint x, GLint y, GLsizei width, GLsizei height, GLint border) Specify a 2D texture image from frame buffer. target: 2D texture image target. level: Mipmap level. Base level is 0. internalFormat: Internal format for texel data. x, y: Lower left corner of the source image in frame buffer. width: Width of the texture image. height: Height of the texture image. border: Width of the texture image border. Must be 0 or 1. target, level, internalFormat, width, height and border have the same meaning as in glTexImage2D().

36 void glTexSubImage2D(GLenum target, GLint level,
GLint xoff, GLint yoff, GLsizei width, GLsizei height, GLenum format, GLenum type, const GLvoid *texels) Replace a subimage of a 2D texture with image data from system memory. xoff, yoff: Texel offsets of the lower left corner of the subimage with respect to the lower left corner of the entire texture image. width: Width of the texture subimage. height: Height of the texture subimage. Other parameters have the same meaning as in glTexImage2D(). void glCopyTexSubImage2D(GLenum target, GLint level, GLint xoff, GLint yoff, GLint x, GLint y, GLsizei width, GLsizei height) Replace a subimage of a 2D texture with image data from frame buffer. x, y: Lower left corner of the source image rectangle in frame buffer. Other parameters have the same meaning as in glTexSubImage2D().

37 width Sub-Image height xoff yoff Texture Image

38 Automatic Mipmap Generation
int gluBuild2DMipmaps(GLenum target, GLint internalFormat, GLsizei width, GLsizei height, GLenum format, GLenum type, const GLvoid *texels) Construct a series of mipmaps down to resolution of 1  1, then call glTexImage2D() to load texture images on all mipmap levels. Also automatically scale the original image so that its width and height become power of two. All parameters have the same meaning as in glTexImage2D(). Return 0 if succeeds, otherwise return a GLU error code.

39 One-Dimensional Texture
glTexImage1D(), glTexSubImage1D() glCopyTexImage1D(), glCopyTexSubImage1D() gluBuild1DMipmaps() Three-Dimensional Texture glTexImage3D(), glTexSubImage3D() glCopyTexImage3D(), glCopyTexSubImage3D() gluBuild3DMipmaps()

40 Specify Texture Parameters
void glTexParameteri(GLenum target, GLenum pname, GLint param) void glTexParameterf(GLenum target, GLenum pname, GLfloat param) void glTexParameteriv(GLenum target, GLenum pname, GLint *param) void glTexParameterfv(GLenum target, GLenum pname, GLfloat *param) Specify texture parameters for texture object that is currently bound to target. target: GL_TEXTURE_1D GL_TEXTURE_2D GL_TEXTURE_3D GL_TEXTURE_CUBE_MAP pname: Texture parameter name param: Texture parameter value

41 Texture parameter name Values
GL_TEXTURE_BORDER_COLOR (R, G, B, A) GL_TEXTURE_MAG_FILTER GL_NEAREST GL_LINEAR GL_TEXTURE_MIN_FILTER GL_NEAREST_MIPMAP_NEAREST GL_NEAREST_MIPMAP_LINEAR GL_LINEAR_MIPMAP_NEAREST GL_LINEAR_MIPMAP_LINEAR GL_TEXTURE_WRAP_S GL_TEXTURE_WRAP_T GL_TEXTURE_WRAP_R GL_REPEAT GL_MIRRORED_REPEAT GL_CLAMP GL_CLAMP_TO_EDGE GL_CLAMP_TO_EDGE was added in OpenGL 1.2. GL_MIRRORED_REPEAT was added in OpenGL 1.4.

42 Constant Texture Border Color
pname = GL_TEXTURE_BORDER_COLOR Constant texture border color is used as border color when the texture image does not have borders. Texture Magnification Filter pname = GL_TEXTURE_MAG_FILTER param Meaning GL_NEAREST Nearest filtering GL_LINEAR Linear filtering

43 Texture Minification Filter
pname = GL_TEXTURE_MIN_FILTER param Meaning GL_NEAREST Nearest filtering (Mipmap is not used) GL_LINEAR Linear filtering (Mipmap is not used) GL_NEAREST_MIPMAP_NEAREST Nearest filtering within each mipmap Nearest filtering between mipmaps GL_NEAREST_MIPMAP_LINEAR Linear filtering between mipmaps GL_LINEAR_MIPMAP_NEAREST Linear filtering within each mipmap GL_LINEAR_MIPMAP_LINEAR

44 Texture Wrapping Modes
pname Meaning GL_TEXTURE_WRAP_S Texture wrapping mode for s coordinate GL_TEXTURE_WRAP_T Texture wrapping mode for t coordinate GL_TEXTURE_WRAP_R Texture wrapping mode for r coordinate param Meaning GL_REPEAT Integer part of texture coordinate is ignored. Border color is always ignored. For linear filtering, 22 texel array wraps to the opposite edge of the texture. GL_MIRRORED_REPEAT Fractional part is reversed at every integer boundary. The rest is the same as GL_REPEAT. GL_CLAMP Texture coordinate is clamped to [0, 1]. Edge color is used for nearest filtering. Border color is used in 22 texel array for linear filtering. GL_CLAMP_TO_EDGE Border color is always ignored. Texels at or near the edge are used.

45 Specify Texture Environment
void glTexEnvi(GLenum envtarget, GLenum pname, GLint param) void glTexEnvf(GLenum envtarget, GLenum pname, GLfloat param) void glTexEnviv(GLenum envtarget, GLenum pname, GLint *param) void glTexEnvfv(GLenum envtarget, GLenum pname, GLfloat *param) Specify texture environment. Texture environment determines how texture color is combined with incoming fragment color. envtarget: Must be GL_TEXTURE_ENV pname param GL_TEXTURE_ENV_MODE GL_REPLACE GL_MODULATE GL_DECAL GL_BLEND GL_TEXTURE_ENV_COLOR Constant environment color (Rc, Gc, Bc, Ac)

46 Base Internal Format Texture source color (Rs, Gs, Bs, As) GL_RGB (R, G, B, 1) GL_RGBA (R, G, B, A) GL_ALPHA (0, 0, 0, A) GL_LUMINANCE (L, L, L, 1) GL_LUMINANCE_ALPHA (L, L, L, A) GL_INTENSITY (I, I, I, I) Texture source color (Rs, Gs, Bs, As) Incoming fragment color (Rf, Gf, Bf, Af) Constant environment color (Rc, Gc, Bc, Ac) Final combined color (R, G, B, A)

47 Replace Mode The texture source color components replace corresponding fragment color components. Base Internal Format GL_REPLACE GL_ALPHA R = Rf, G = Gf , B = Bf A = As GL_RGB GL_LUMINANCE R = Rs, G = Gs , B = Bs A = Af GL_RGBA GL_LUMINANCE_ALPHA GL_INTENSITY

48 Modulation Mode (default mode)
The texture source color components multiply corresponding fragment color components. Base Internal Format GL_MODULATE GL_ALPHA R = Rf, G = Gf , B = Bf A = AfAs GL_RGB GL_LUMINANCE R = RfRs, G = GfGs , B = BfBs A = Af GL_RGBA GL_LUMINANCE_ALPHA GL_INTENSITY

49 Decal Mode For GL_RGB format, The texture source color replace fragment color. For GL_RGBA format, The texture source color is linearly blended with fragment color, using source alpha as blending weight. Base Internal Format GL_DECAL GL_RGB R = Rs, G = Gs , B = Bs A = Af GL_RGBA R = Rf(1  As) + RsAs G = Gf(1  As) + GsAs R = Bf(1  As) + BsAs GL_ALPHA GL_LUMINANCE GL_LUMINANCE_ALPHA GL_INTENSITY undefined

50 Blending Mode The fragment color is linearly blended with constant environment color, using texture source color as blending weight. Base Internal Format GL_BLEND GL_ALPHA R = Rf, G = Gf , B = Bf A = AfAs GL_RGB GL_LUMINANCE R = Rf(1  Rs) + RcRs G = Gf(1  Gs) + GcGs B = Bf(1  Bs) + BcBs A = Af GL_RGBA GL_LUMINANCE_ALPHA GL_INTENSITY

51 Specify Texture Coordinates
void glTexCoord1{sifd}(TYPE s) void glTexCoord2{sifd}(TYPE s, TYPE t) void glTexCoord3{sifd}(TYPE s, TYPE t , TYPE r) void glTexCoord4{sifd}(TYPE s, TYPE t , TYPE r , TYPE q) void glTexCoord{1234}{sifd}v(TYPE * texcoords) Specify current texture coordinates. Vertices defined using subsequent glVertex*() commands are assigned current texture coordinates. This command must be used between glBegin( ) and glEnd( ). Texture coordinates Meaning Applicable texture target (s) One-dimensional GL_TEXTURE_1D (s, t) Two-dimensional GL_TEXTURE_2D (s, t, r) Three-dimensional GL_TEXTURE_3D GL_TEXTURE_CUBE_MAP (s, t, r, q) Homogeneous

52 Texture Matrix Texture matrix is a 44 transformation matrix that is used to transform texture coordinates. Texture matrix stack has two entries. Use glMatrixMode(GL_TEXTURE) to set current matrix to texture matrix. All standard matrix manipulation commands can be applied to texture matrix. The default texture matrix is an identity matrix.

53 Homogeneous Texture Coordinates
Homogeneous coordinates (s) (s, 0, 0, 1) (s, t) (s, t, 0, 1) (s, t, r) (s, t, r, 1) (s, t, r, q) [MT] is the texture matrix. (s', t', r') are the transformed texture coordinates. Homogenous texture coordinates also undergo perspective division.

54 Automatic Texture Coordinate Generation
void glTexGen{ifd}(GLenum coord, GLenum pname, TYPE param) void glTexGen{ifd}v(GLenum coord, GLenum pname, TYPE *param) Specify functions for automatic texture coordinate generation. coord: Specify which texture coordinate to use automatic generation. Valid values are GL_S, GL_T, GL_R, GL_Q pname param GL_TEXTURE_GEN_MODE GL_OBJECT_LINEAR GL_EYE_LINEAR GL_SPHERE_MAP GL_REFLECTION_MAP GL_NORMAL_MAP GL_OBJECT_PLANE Plane equation coefficients (A, B, C, D) GL_EYE_PLANE

55 To enable automatic texture coordinate generation for s-coordinate,
use glEnable(GL_TEXTURE_GEN_S). To enable automatic texture coordinate generation for t-coordinate, use glEnable(GL_TEXTURE_GEN_T). To enable automatic texture coordinate generation for r-coordinate, use glEnable(GL_TEXTURE_GEN_R). To enable automatic texture coordinate generation for q-coordinate, use glEnable(GL_TEXTURE_GEN_Q).

56 Linear Generation Mode (GL_OBJECT_LINEAR, GL_EYE_LINEAR)
Texture coordinate = Ax + By + Cz + Dh (A, B, C, D) are plane equation coefficients specified using GL_OBJECT_PLANE parameter. (x, y, z, h) are vertex coordinates in object space. GL_EYE_LINEAR Texture coordinate = A'xe + B'ye + C'ze + D'he [A' B' C' D']=[A B C D][M]1 (A, B, C, D) are plane equation coefficients specified using GL_EYE_PLANE parameter. [M] is modeview matrix when (A, B, C, D) was specified. (xe, ye, ze, he) are vertex coordinates in eye space.

57 Sphere Map (GL_SPHERE_MAP)
Calculate reflection vector R in eye space. Use R to calculate texture coordinates (s, t) for sampling in a sphere environment map. Cube Map (GL_REFLECTION_MAP, GL_NORMAL_MAP) GL_REFLECTION_MAP Calculate reflection vector R in eye space. Use R directly as texture coordinates (s, t, r) for sampling in a cube environment map. GL_NORMAL_MAP Transform normal vector N from object space to eye space. Use transformed N as texture coordinates (s, t, r) for sampling in a cube environment map.


Download ppt "Texture Mapping + Texture Object = Texture Mapped Object."

Similar presentations


Ads by Google