Chapter 9 of OpenGL programming guide: texture ing

Source: Internet
Author: User

Texture is a sequence of rectangular data. The stored data includes the color, brightness, and Alpha values. Each unit of texture data is called Texel. Texture data can be mapped to any geometric surface.

1. Texture ing Basics

Texture is a relatively complex operation. The following steps are generally required:
1. Create a texture object and specify the data:
Texture data can be two-dimensional images or one-dimensional or three-dimensional images;

2. specify the method in which texture will be mapped to pixels:
There are four functions that can be used to calculate the fragment color and texture color. One is to replace the texture color directly, and the other is to modulo the fragment color with the texture color, the third is the scale operation, and the fourth is the combination of a color constant and fragment based on the texture value;

3. Activate the pixel ing function:
Call the glable () function for activation. The parameters can be gl_texture_1d, gl_texture_2d, gl_texture_3d, and gl_texture_cube_map to activate one-dimensional, two-dimensional, three-dimensional, and three-dimensional textures respectively, if multiple active ones take effect for a large number of dimensions;

4. Draw a scenario and specify the geometric and texture coordinates:
Just like specifying a color for a vertex, you can specify the texture coordinates for the corresponding vertex. For a two-dimensional texture, the coordinates are 0 ~ 1. Just like the flat shade color, texture coordinates between geometric vertices are automatically interpolated. In this way, the texture coordinates outside the vertex are greater than [0, 1], and the calculation method needs to be specified: repeated, cut .... 2 \

2. Texture data

Glteximage2d (glenum target, glint level, glint internalformat, glsizei width, glsizei height, glint border, glenum format, glenum type, const glvoid * texels) is used to specify data for textures );
Taget: constants such as gl_texture_2d
Level: when you are preparing to provide versions with multiple resolutions for the same texture object, the level value is useful. Otherwise, set it to 0.
Internalformat: Specifies the data members of texture elements, such as gl_rbga and gl_alpha.
Width, height, border: Specifies the texture size and edge width. Before opengl2.0, the width and height must be 2 exponent.
Format, type: the format and type of incoming data, consistent with gldrawpixels

You can directly read framebuffer data as the glcopyteximage2d texture (glenum target, glint level, glint internalformat, glint X, glint y, glsizei width, glsizei height, glint border );
Glint internalformat, glint X, glint y, glsizei width, glsizei height, glint border );
X, Y: to read the coordinates of framebuffer. Other parameters are the same as those of glteximage2d.

The cost of modifying the data volume of an existing texture object is low when a new texture object is created. gltexsubimage2d (glenum target, glint level, glint xoffset, glint yoffset, glsizei width, glsizei height,
Glenum format, glenum type, const glvoid * texels );
Xoffset, yoffset: coordinates of the target texture

The glcopytexsubimage2d and gltexsubimage2d functions are similar, and the data comes from framebuffer.

Textures can be stored in the compressed format or loaded directly from the Compressed Mode.
Determine whether the texture is compressed: glgettexlevelparameteriv (gl_texture_2d, gl_texture_compressed, bool & compressed );
The texture compression mode OpenGL is not specified. The specific implementation will define, load the compressed texture glcompressedteximage * D ()

3. The mipmaps texture object should also be displayed in the scenario. When the physical object is far away from the viewpoint, the texture object will be scaled and filtered. In this continuous process, flashes may occur. To avoid this, a set of image data with different resolutions can be provided for the texture object, which is called mipmap. You need to provide all 2-level images from the maximum size to the 1x1; glteximage2d () API starts from level = 0, and 0 indicates the maximum size. You can use the API to generate mipmaps based on the maximum size image for the texture, glgeneratemipmap (glenum target); sometimes you can limit the level of mipmaps, for example, you do not want to have a small size of mipmap, or you do not want the texture to be switched between very large and very small levels. You can use the gltexparameter * () API. The parameters are as follows:
Gl_texture_base_level: maximum size level limit;
Gl_texture_max_level: the minimum size of the mipmap level limit. With these two values, you can reduce the required values of mipmapgl_texture_min_levels and gl_texture_max_levels: the selection of mimap level during openggl rendering is affected. 4. There is no one-to-one correspondence between the texture Texel and pixels. One Texel may need to correspond to one pixel, it is also possible that a piece of Texel will eventually correspond to one pixel. These operations are called filtering. OpenGL allows you to set some options to control filtering. The API is also gltexparameteri. Gltexparameteri (gl_texture_2d, gl_texture_mag_filter, gl_nearest): sets the texture zoom filter to gl_nearest;
Gltexparameteri (gl_texture_2d, gl_texture_min_filter, gl_nearest): Set the filter to gl_nearest. gl_nearest indicates selecting a pixel closest to the texture coordinate; other values include 2x2 pixels near the gl_linear-coordinate Center for weighting. 5. Manage a texture object. Each texture object has an unsigned integer as its name. To avoid duplication, Use API glgentextures (glsizei N, gluint * texturenames) to generate it. glistexture () you can determine whether a name is a texture Object Name in use. Before using a texture object, you need to bind glbindtexture () first, and then glteximage * () and gltexsubimage * () operations will be performed on the bound texture. Gldeletetextures can delete texture that is no longer in use. Some OpenGL implementations support a set of efficient texture called resident. Usually there are dedicated hardware to store and operate these texture, so you should try to put texture into this group. If the number of texture in the program exceeds the limit of this group, some texture cannot enter this group. You can use glgettexparameter (gl_texture_resident) or glaretexturesresident to determine whether texture is in resident. You can use glprioritizetextures () to assign a priority to texture to increase the chance of entering resident. 6. texture function the texture data is generally used as the color to replace the color of the object surface, but other texture functions can also be used. Gltexenv {if} V (glenum target, glenum pname, const type * PARAM, gl_replace, gl_modulate, gl_blend, gl_add, gl_combine, specify the function used to calculate the combination of fragement and texture color. If pname is gl_texture_env_color, specify a color constant for texture operations. Of course, texture's internalformat will also affect the calculation here. 7. The texture coordinate specifies the texture coordinate for the vertex, just like the coordinate specified for the vertex. Generally, each component is represented by (S, T, R, Q, y, Z, W) to indicate that the vertex coordinates are the same. The API for setting texture coordinates is gltexcoord {1234} {sifd} V (const type * coords). The texture is a rectangular area, and the surface of a geometric object may not be mapped to a rectangle, such as a surface. Except for regular surfaces such as cylindrical and cone, other surfaces are distorted when mapped to the rectangle texture. For example, the closer the sphere is to the two poles, the more severe the distortion. A surface is composed of a large number of small polygon. It may be complicated to specify texture coordinates for these polygon vertices. If a texture coordinate value other than [0, 1] is specified, the clamp or repeat occurs. The former adjusts the coordinates out of the range to 0 or 1, and the latter repeats the texture. The clamp and repeat modes can be set through gltexparameteri. OpenGL provides APIs to automatically generate texture coordinates for geometric vertices:
Void gltexgen {IFD} (glenum coord, glenum pname, type PARAM );
Void gltexgen {IFD} V (glenum coord, glenum pname, const type * PARAM );
Void gltexgen {IFD} V (glenum coord, glenum pname, const type * PARAM); coord: gl_s, gl_t, gl_r, or gl_q specifies the coordinates of the generated dimension
Pname: coordinate, gl_object_plane, or coordinate, which specifies the method for generating coordinates. The values can be gl_object_linear, struct, gl_sphere_map, gl_reflection_map, or gl_normal_map; gl_object_plane and struct. If gl_texture_gen_mode is specified as gl_object_linear and gl_object_plane is added as (P0, P1, P2, P3), the formula for calculating the texture coordinate value of the vertex is as follows: p0 * x + p1 * Y + p2 * z + P3 * w, that is, the distance from the vertex to the plane. If gl_texture_gen_mode is gl_eye_linear, the vertex coordinates are converted to the eye Coordinate System for calculation. The actual effect is that the texture of the former is fixed relative to the object, and the texture of the latter is fixed relative to the scene. Gl_sphere_map:This method of generating texture coordinates is mainly used to reflect the environment. For details, refer to the reference books. Cube map:This uses six texture objects to form a cube centered at the origin. The texture coordinate is a unit vector pointing to a point on the cube. In this case, use automatic coordinate generation to set the generation mode to gl_reflection_map or gl_normal_map. There is an example in the original book code. 8. multitexturingopengl can use multiple textures simultaneously. These textures are bound to a sequence of texture units. texture units is calculated and applied to vertices in sequence. Each Texture unit has its own image, filter parameter, coordinate transformation matrix, automatic coordinate generation method, and vertex-array. Void glactivetexture (glenum texunit) activates the corresponding Texture unit, and all subsequent texture operations are for this Texture unit. The gl_texturei parameter has a value ranging from 0 to K. The maximum value is determined by the specific implementation. In this case, gltexcoord is equivalent to specifying the coordinates of gl_texture0, and glmultitexcoord must be used to specify coordinates for other units. 9. Texture combination Function

In section 6, when one or more texture units are used, the texture combination function can flexibly control the combination of fragment and texture color. You can use the combination function to calculate the color or alpha value from 3 Sources to generate an output value. When multiple texture units are used, the combination function forms a pipeline.

Void gltexenv {if} (glenum target, glenum pname, type PARAM) controls composite functions: target is gl_texture_env, pname is callback, Param is gl_decal, gl_replace, gl_modulate, gl_blend, gl_add, or gl_combine; When Param is gl_combine, you can continue to configure the composite function. You can use pname = gl_combine_rgb to configure the combined functions of RGB: gl_replace, gl_modulate, gl_add, gl_add_signed, gl_interpolate, gl_subtract, merge, or merge. These functions use one or three parameters for calculation, the values are arg0, arg1, and arg2 respectively. Therefore, the gl_replace expression arg0 indicates that gl_add is arg0 + arg1, and gl_modulate is arg0 * arg1. The source of arg0 and arg1 can be set through pname = gl_src0_rgb, gl_src1_rgb, gl_src2_rgb. Param can be gl_texture: texture color value, gl_texturen: color value of Texture unit N, gl_constant: color constant, gl_primary_color: fragement applies the color before the texture. gl_previous: the output value of the previous combination function. For the Texture unit 0, it is equivalent to gl_primary_color. In general, we specify gl_src2_rgb (arg2) as a constant. The actual OpenGL is like this by default. For the selected color source, you can also specify which part of it is used for calculation. pname = gl_operandi_rgb, Param can be gl_src_color, gl_one_minus_src_color, gl_src_alpha, or male, the RGB values involved in the calculation are (a, a, ). Pname = gl_operandi_alpha, Param can be gl_src_alpha or gl_one_minus_src_alpha. you can also set a scale factor, gltexenvf (gl_texture_env, gl_rgb_scale, 1.0); gltexenvf (gl_texture_env, gl_alpha_scale, 1.0 ); 10. The primary color mentioned in the previous section of secondary color is the fragment color after the illumination effect is calculated. After applying the texture, before the fog effect, openGL can add a secondary color to fragment to achieve more realistic highlights. When no light is available, glsecondarycolor * () sets the color, and gl_color_sum (gl_color_sum) is enabled, secondary color is added to the fragment after the texture. In the case of illumination, the combination of the fragment color and the texture color after illumination is reduced. In this case, we can calculate two colors for the illumination effect. One is primary color, which does not contain the specular component, and the other is secondary color. The value is the specular component. The latter will be added to fragment After texture. 11. Point spritesglpointsize () can be the size of the control point, but the solid color points obviously cannot meet the requirements. Point sprite solves this problem. You can apply textures to fragment in point elements. You need to call gltexenv * (gl_point_sprite, gl_coord_replace, gl_true) to activate this function. The texture coordinate s component of Sprite is 0 at the leftmost fragement of Sprite, 1 at the rightmost, linear increase, and the T component increase can be changed by setting, glpointparameter (gl_point_sprite_coord_origin, gl_lower_left or gl_upper_left ). 12. Texture matrix is like a vertex that can be transformed using a model view. texture coordinates can also be transformed using a 4x4 matrix. By default, this matrix is the unit matrix. After glmatrixmode (gl_texture) is called, you can operate the texture matrix just like the model matrix. 13. In the depth texture lighting system, no shadow is generated for the object. You can use the depth buffer as the texture and add multiple rounds of rendering to generate a shadow. First, move the viewpoint to the light source, draw the scenario, and read the depth buffer and save it as texture. The second plot generates texture coordinates for the elements. (S, T) is used to reference the Coordinate Position of texture, and the third dimension r is the distance from the vertex to the light source. When texture is applied, compare the texture coordinate R and texture (s, t) values of fragment to determine whether to apply the shadow texture to the fragment.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.