Download presentation
Presentation is loading. Please wait.
1
And Some Extra Information From www.opengl.org
Texture Mapping From RedBook Chapter 9 And Some Extra Information From Tong-Yee Lee
2
The Quest for Visual Realism
3
Standard Quake 2 Model Rendering
Texture key-frame with decal skin + Result Key-frame model geometry Decal skin
4
OpenGL Texture Mapping
CPU DL Poly. Per Vertex Raster Frag FB Pixel Texture Apply a 1D, 2D, or 3D image to geometric primitives Uses of Texturing simulating materials reducing geometric complexity image warping reflections In this section, we’ll discuss texture ( sometimes also called image ) mapping. Texture mapping augments the colors specified for a geometric primitive with the colors stored in an image. An image can be a 1D, 2D, or 3D set of colors called texels. 2D textures will be used throughout the section for demonstrations, however, the processes described are identical for 1D and 3D textures. Some of the many uses of texture mapping include: simulating materials like wood, bricks or granite reducing the complexity ( number of polygons ) of a geometric object image processing techniques like image warping and rectification, rotation and scaling simulating reflective surfaces like mirrors or polished floors
5
Environment Maps If, instead of using the ray from the surface point to the projected texture's center, we used the direction of the reflected ray to index a texture map. We can simulate reflections. This approach is not completely accurate. It assumes that all reflected rays begin from the same point, and that all objects in the scene are the same distance from that point.
6
Reflection Sphere map:
7
Mapping a Texture Based on parametric texture coordinates
CPU DL Poly. Per Vertex Raster Frag FB Pixel Texture Based on parametric texture coordinates glTexCoord*() specified at each vertex Texture Space Object Space t 1, 1 (s, t) = (0.2, 0.8) 0, 1 A a When you want to map a texture onto a geometric primitive, you need to provide texture coordinates. The glTexCoord*() call sets the current texture coordinates. Valid texture coordinates are between 0 and 1, for each texture dimension, and the default texture coordinate is ( 0, 0, 0, 1 ). If you pass fewer texture coordinates than the currently active texture mode ( for example, using glTexCoord1d() while GL_TEXTURE_2D is enabled ), the additionally required texture coordinates take on default values. c (0.4, 0.2) b B C (0.8, 0.4) 0, 0 1, 0 s
8
Texture Mapping y z x screen geometry t image s
Textures are images that can be thought of as continuous and be one, two, three, or four dimensional. By convention, the coordinates of the image are s, t, r and q. Thus for the two dimensional image above, a point in the image is given by its (s, t) values with (0, 0) in the lower-left corner and (1, 1) in the top-right corner. A texture map for a two-dimensional geometric object in (x, y, z) world coordinates maps a point in (s, t) space to a corresponding point on the screen. image
9
Texture Mapping and the OpenGL Pipeline
Images and geometry flow through separate pipelines that join at the rasterizer “complex” textures do not affect geometric complexity The advantage of texture mapping is that visual detail is in the image, not in the geometry. Thus, the complexity of an image does not affect the geometric pipeline (transformations, clipping) in OpenGL. Texture is added during rasterization where the geometric and pixel pipelines meet. geometry pipeline vertices pixel pipeline image rasterizer
10
Steps in Texture Mapping
Specify the texture Indicate how the texture is to be applied to each pixel. Enable texture mapping Draw the scene, supplying both texture and geometric coordinates Keep in mind that texture mapping works only in RGB mode; the results in color-index mode are undefined.
11
Texture Texture Image metrics must be power of 2
glEnable( GL_TEXTURE_2D );
12
Step 1: Specifying Texture
A texture is usually thought of as being two-dimensional, like most images, but it can also be one-dimensional. The data describing a texture can consist of one, two, three, or four elements per texel, representing anything from a modulation constant to an (R, G, B, A) quadruple. glTexImage2D() defines a two-dimensional texture. glTexImage1D(), is described in "One-Dimensional Textures.
13
glTexImage2D() glTexImage2D(GLenum target, GLint level, GLint internal_components, GLsizei width, GLsizei height, GLint border, GLenum format, GLenum type, const Glvoid *pixels); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 32, 32, 0, GL_RGBA, GL_UNSIGNED_BYTE, texImage32); The target parameter must be set to the constant GL_TEXTURE_2D You use the level parameter if you're supplying multiple resolutions of the texture map; with only one resolution, level should be 0. The next parameter, internal_components, is an integer from 1 to 4 indicating which of the R, G, B, and A components are selected for use in modulating or blending. The width and height components must be a power of 2 border indicates the width of the border, which is usually zero. The format and type parameters describe the format and data type of the texture image data. The format parameter can be GL_COLOR_INDEX, GL_RGB, GL_RGBA,GL_RED, GL_GREEN, GL_BLUE, GL_ALPHA, GL_LUMINANCE, or GL_LUMINANCE_ALPHA the type parameter can be GL_BYTE, GL_UNSIGNED_BYTE, GL_SHORT, GL_UNSIGNED_SHORT, GL_INT, GL_UNSIGNED_INT, GL_FLOAT, or GL_BITMAP. pixels contains the texture-image data. This data describes the texture image itself as well as its border.
14
glPixelStorei(GL_UNPACK_ALIGNMENT, 1)
It describes how the bitmap data is stored in computer memory Ex: glPixelStorei(GL_UNPACK_ALIGNMENT, 1); glTexImage2D(GL_TEXTURE_2D, 0, 3, 32, 32, 0, GL_RGB, GL_UNSIGNED_BYTE, &mipmapImage32[0][0][0]); GL_UNPACK: from image to memory Alignment: better fit memory boundary
16
Texture Mapping y z x screen geometry t image s
Textures are images that can be thought of as continuous and be one, two, three, or four dimensional. By convention, the coordinates of the image are s, t, r and q. Thus for the two dimensional image above, a point in the image is given by its (s, t) values with (0, 0) in the lower-left corner and (1, 1) in the top-right corner. A texture map for a two-dimensional geometric object in (x, y, z) world coordinates maps a point in (s, t) space to a corresponding point on the screen. image
18
Anti-aliasing Techniques
19
Mip-Mapping Concept
20
W/O MIP W
21
Multiple Levels of Detail (Mip-Mapping )
In a dynamic scene, for example, as a textured object moves farther from the viewpoint, the texture map must decrease in size along with the size of the projected image. Specified in the “level” parameter in glTexImage2D*
22
Mip-Mapping Example: glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 32, 32, 0, GL_RGBA, GL_UNSIGNED_BYTE, mipmapImage32); glTexImage2D(GL_TEXTURE_2D, 1, GL_RGBA, 16, 16, 0, GL_RGBA, GL_UNSIGNED_BYTE, mipmapImage16); glTexImage2D(GL_TEXTURE_2D, 2, GL_RGBA, 8, 8, 0, GL_RGBA, GL_UNSIGNED_BYTE, mipmapImage8); glTexImage2D(GL_TEXTURE_2D, 3, GL_RGBA, 4, 4, 0, GL_RGBA, GL_UNSIGNED_BYTE, mipmapImage4); glTexImage2D(GL_TEXTURE_2D, 4, GL_RGBA, 2, 2, 0, GL_RGBA, GL_UNSIGNED_BYTE, mipmapImage2); glTexImage2D(GL_TEXTURE_2D, 5, GL_RGBA, 1, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE, mipmapImage1); Note: you need to create multiple resolutions by yourself
23
Mip-Mapping Using GLU helper
int gluBuild2DMipmaps(GLenum target, GLint components, GLint width, GLint height, GLenum format, GLenum type, void *data); gluBuild2DMipmaps( GL_TEXTURE_2D, 3, 32, 32, GL_RGB, GL_UNSIGNED_BYTE, texImage32 );
24
Scale Texture Image int gluScaleImage(GLenum format, GLint widthin, GLint heightin, GLenum typein, const void *datain, GLint widthout, GLint heightout, GLenum typeout, void *dataout); Assuming that you have constructed the level 0, or highest-resolution map, the routines gluBuild1DMipmaps() and gluBuild2DMipmaps() construct and define the pyramid of mipmaps down to a resolution of 1 × 1 (or 1, for one-dimensional texture maps). Both these routines require that the original image already be suitable for a texture map, namely that its dimensions must be powers of 2. Most scanned images don't satisfy this property, so you have to scale the incoming image to some appropriate size
25
Control Filtering Texture maps are square or rectangular, but after being mapped to a polygon or surface and transformed into screen coordinates, the individual texels of a texture rarely correspond to individual pixels of the final screen image. Depending on the transformations used and the texture mapping applied, a single pixel on the screen can correspond to anything from a tiny portion of a texel (magnification) to a large collection of texels (minification), as shown in Figure 9-4 . In either case, it's unclear exactly which texel values should be used and how they should be averaged or interpolated. Consequently, OpenGL allows you to specify any of several filtering options to determine these calculations. The options provide different trade-offs between speed and image quality. Also, you can specify the filtering methods for magnification and minification independently.
28
The following lines are examples of how to use glTexParameter
The following lines are examples of how to use glTexParameter*() to specify the magnification and minification filtering methods: glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
31
Step 2:Indicate How the Texture Is to Be Applied to Each Pixel
You can choose any of three possible functions for computing the final RGBA value from the fragment color and the texture-image data. (decal or modulate or blend) One possibility is to simply use the texture color as the final color; this is the decal mode, in which the texture is painted on top of the fragment, just as a decal would be applied. Another method is to use the texture to modulate, or scale, the fragment's color; this technique is useful for combining the effects of lighting with texturing. Finally, a constant color can be blended with that of the fragment, based on the texture value.
32
glTexEnv() void glTexEnv{if}[v](GLenum target, GLenum pname, TYPE param); Sets the current texturing function. target must be GL_TEXTURE_ENV. If pname is GL_TEXTURE_ENV_MODE, param can be GL_DECAL, GL_MODULATE, or GL_BLEND, to specify how texture values are to be combined with the color values of the fragment being processed. In decal mode and with a three-component texture, the texture's colors replace the fragment's colors. With either of the other modes or with a four-component texture, the final color is a combination of the texture's and the fragment's values. If pname is GL_TEXTURE_ENV_COLOR, param is an array of four floating-point values representing R, G, B, and A components. These values are used only if the GL_BLEND texture function has been specified as well.
36
Why? There are two cases: Ct= white or Ct=black Ct=white C=Cc*Ct=Green
C=Cf=white
37
GL_DECAL GL_MODULATE GL_BLEND Non-textured Shaded Teapot See texgen.c
38
More Examples and Experiments about glTexEnv()
Texture mapping Demo: is Available on our web Soon!!
39
Step 3: Enable Texture Operate texture as an object
Steps to use texture object Generate texture names Bind texture objects to texture data & parameters You can set priority for each texture object Rebind the texture objects for applying
40
Texture Object Naming a texture object
void glGenTextures(GLsizei n, GLuint *textureNames); glIsTexture() determines if a texture name is actually in use Bind a texture name to a texture object void glBindTexture(GLenum target, GLuint textureName); Cleaning up texture objects void glDeleteTextures(GLsizei n, const GLuint *textureNames);
41
Texture Object static GLuint texName[1]; void init(void) { …..
glPixelStorei(GL_UNPACK_ALIGNMENT, 1); glGenTextures(1, texName); glBindTexture(GL_TEXTURE_2D, texName[0]); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 32, 32, 0, GL_RGBA, GL_UNSIGNED_BYTE, texImage32 ); glEnable(GL_TEXTURE_2D); }
42
Step 4: Assigning Texture Coordinates
As you draw your texture-mapped scene, you must provide both object coordinates and texture coordinates for each vertex. Texture coordinates can comprise one, two, three, or four coordinates. They're usually referred to as the s, t, r, and q coordinates to distinguish them from object coordinates (x, y, z, and w) void glTexCoord{1234}{sifd}{v}(TYPEcoords)
43
Mapping a Texture Based on parametric texture coordinates
CPU DL Poly. Per Vertex Raster Frag FB Pixel Texture Based on parametric texture coordinates glTexCoord*() specified at each vertex Texture Space Object Space t 1, 1 (s, t) = (0.2, 0.8) 0, 1 A a When you want to map a texture onto a geometric primitive, you need to provide texture coordinates. The glTexCoord*() call sets the current texture coordinates. Valid texture coordinates are between 0 and 1, for each texture dimension, and the default texture coordinate is ( 0, 0, 0, 1 ). If you pass fewer texture coordinates than the currently active texture mode ( for example, using glTexCoord1d() while GL_TEXTURE_2D is enabled ), the additionally required texture coordinates take on default values. c (0.4, 0.2) b B C (0.8, 0.4) 0, 0 1, 0 s
44
Example glBegin(GL_QUADS); glTexCoord2f(0.0, 0.0);
glVertex3f(-2.0, -1.0, 0.0); glTexCoord2f(0.0, 8.0); glVertex3f(-2.0, 1.0, 0.0); glTexCoord2f(8.0, 8.0); glVertex3f(2000.0, 1.0, ); glTexCoord2f(8.0, 0.0); glVertex3f(2000.0, -1.0, ); glEnd();
45
Transform Texture Texture Matrix Stack glMatrixMode( GL_TEXTURE );
Translate, scale, rotation Similar to VRML to transform that “cutting frame”.
51
Repeating and Clamping Texture
Using glTexParameter*(…); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP); Repeating a texture
52
Repeating and Clamping Texture
Clamping a texture Repeating and Clamping a texture
53
Texture Parameters
57
Automatic Texture-Coordinate Generation
You can use texture mapping to make contours on your models or to simulate the reflections from an arbitrary environment on a shiny model. To achieve these effects, let OpenGL automatically generate the texture coordinates for you, rather than explicitly assigning them with glTexCoord*(). Actually, the mechanism for automatic texture-coordinate generation is useful for many different applications such as showing contours and environment mapping. To generate texture coordinates automatically, use the command glTexGen(). void glTexGen{ifd}{v}(GLenum coord, GLenum pname, TYPEparam);
59
Automatic Texture-Coordinate Generation
void glTexGen{ifd}(GLenum coord, GLenum pname, TYPEparam); void glTexGen{ifd}v(GLenum coord, GLenum pname, TYPE *param); coord can be GL_S, GL_T, GL_R, GL_Q If pname is GL_TEXTURE_GEN_MODE, param is either GL_OBJECT_LINEAR, GL_EYE_LINEAR, GL_SPHERE_MAP If pname is GL_OBJECT_PLANE or GL_EYE_PLANE, param is the plane coefficients array Ex: plane equation is 1x + 2y + 3z = 0 GLfloat plane[4] = { 1, 2, 3, 0 };
62
Automatic Texture-Coordinate Generation
(a) The texture contour stripes are parallel to the plane x = 0, (GL_OBJECT_LINEAR). As the object moves, the texture appears to be attached to it. (b) A different planar equation (x + y + z = 0) is used, so the stripes have a different orientation. (c) The texture coordinates are calculated relative to eye coordinates and hence aren't fixed to the object (GL_EYE_LINEAR). As the object moves, it appears to "swim" through the texture REDBOOK Texgen2.c
63
Example GLfloat sgenparams[] = {1.0, 0.0, 0.0, 0.0};
loadStripeImage(); glPixelStorei(GL_UNPACK_ALIGNMENT, 1); glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); glTexParameterf(GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, GL_REPEAT); glTexParameterf(GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameterf(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexImage1D(GL_TEXTURE_1D, 0, 3, stripeImageWidth, 0, GL_RGB, GL_UNSIGNED_BYTE, stripeImage); glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR); glTexGenfv(GL_S, GL_OBJECT_PLANE, sgenparams);
67
Automatic Texture-Coordinate Generation
Sphere map:
69
Environment Mapping The goal of environment mapping is to render an object as if it were perfectly reflective, so that the colors on its surface are those reflected to the eye from its surroundings Example: if you look at a perfectly polished, perfectly reflective silver object in a room, you see the walls, floor, and other objects in the room reflected off the object. The objects whose reflections you see depend on the position of your eye and on the position and surface angles of the silver object. Environment mapping is an approximation based on the assumption that the items in the environment are far away compared to the surfaces of the shiny object - that is, it's a small object in a large room. With this assumption, to find the color of a point on the surface, take the ray from the eye to the surface, and reflect the ray off the surface. The direction of the reflected ray completely determines the color to be painted there. Encoding a color for each direction on a flat texture map is equivalent to putting a polished perfect sphere in the middle of the environment and taking a picture of it with a camera that has a lens with a very long focal length placed far away. Mathematically, the lens has an infinite focal length and the camera is infinitely far away. The encoding therefore covers a circular region of the texture map, tangent to the top, bottom, left, and right edges of the map. The texture values outside the circle make no difference, as they are never accessed in environment mapping.
70
Sphere-mapping Sphere-mapping provides an interesting way of mapping a single-image texture onto the surface of an object that allows for view-dependent effects such as reflections, refractions, and even outlining and other non-photorealistic effects. More specifically, sphere-mapping maps all "reflected" vector directions intoa single image (actually into the largest circle that will fit in the texture image). The problem then becomes determining the relationship between the (s,t) texture coordinates, the texture-map, the reflection vector, the eye, and a particular vertex for which we want to find tex-coords for.
74
Automatic Texture-Coordinate Generation
Environment Mapping To automatically generate the texture coordinates to support environment mapping, use this code in your program: glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP); glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP); glEnable(GL_TEXTURE_GEN_S); glEnable(GL_TEXTURE_GEN_T); The GL_SPHERE_MAP constant creates the proper texture coordinates for the environment mapping.
75
Original Texture Three shots of scene Our final scene will contain two different 'objects'. The first one will be a 'p' shaped object that will use dynamic coordinates to simulate a 'waterfall' effect. The second will be an environmentally mapped torus.
80
NewQuadric GLUquadricObj *quadratic; // Storage For Our //Quadratic Objects ( NEW ) quadobject = gluNewQuadric (); gluQuadricDrawStyle (quadobject, GLU_FILL); gluQuadricNormals (quadobject, GLU_SMOOTH); gluQuadricTexture(quadobject,GL_TRUE);//Generates Texture coordinates glBindTexture(GL_TEXTURE_2D, texName[0]); gluSphere(quadobject,r,25,20); As you can see the QuadricTexture is called and then the BindTexture is called to bind the texture to the Sphere.
81
NewQuadric gluCylinder(quadratic,base_R,top_R,height,32,32);
gluDisk(quadratic,inner_R,outer_R,32,32); // Draw A Disc (CD Shape) With An Inner Radius Of 0.5, gluSphere(quadratic,Radius,32,32);
83
gluSphere(quadratic,Radius,32,32);
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.