OpenGL Cube Texture

Source: Internet
Author: User
Tags windows support
OpenGL cubic Map CopyrightNVIDIA Corporation, 1999. Specified cial publication in written, electronic, or other forms without expressed written permission is prohibited. Electronic redistribution for educational or private use is permitted. SummaryThis tutorial explains the purpose of hardware cube textures. Cube maps can be used for Environment Maps, stable specular highlights, and bump maps for Pixel-by-pixel illumination ). Programmers can download the instance code and learn about OpenGL. Ext_texture_cube_map extension.Introduction nowadays, hardware-accelerated texture pasters have become very popular. Once only in those high-end workstations and flight simulation, it has become a standard feature of ordinary PCs and home video games. The principle is very simple. Some vertices on a 2D image (texture) are attached to the vertex of the 3D model. When rendering the 3D model, the texture image % 26 ldquo; wallpaper % 26 rdquo; similarly, it is attached to the 3D geometric model. When the 3D model is animated or changed, the texture map % 26 ldquo; sticks % 26 rdquo; contains the model. The following is an example: the texture that says % 26 ldquo; geforce % 26 rdquo; is % 26 ldquo; wrapped in % 26 rdquo; one tire (or % 26 ldquo; fried bread % 26 rdquo ;). We can also use different textures to make the tires look like % 26 ldquo; wood % 26 rdquo. We can apply any texture to any geometric shape. Generally, texture ing can be applied to almost all 3D games, but it is not suitable for all situations. Consider an object like % 26 ldquo; silver candle holder % 26 rdquo, the shiny surface reflects ambient light. Because the surface is reflective, no texture can be attached to such a surface. If the candle holder or the point of View moves, the reflection on the candle holder also changes the position. Therefore, instead of matching the fixed 2D positions on the vertex and texture map of the candle holder, the reflective texture should be mapped to the object surface in the appropriate direction. Unfortunately, the usual hardware texture ing method is not suitable for this situation. Therefore, instead of using 2D textures to map the surface position, the surface position is truly mapped in the 360-degree direction. (The level is limited and it is very difficult !) Observe your surroundings, just rotate your head and eyes, and you can see all directions. The environment can be understood as a comprehensive, head-centered graph. This is fundamentally different from the position (x, y) on your 2D image. If your environment can be encoded as a texture image, it can be accessed through a 3D direction instead of a 2D position. Use cubic textures to encode the environmentA cubic texture is a form of texture. It uses a 3D vector (that is, the direction) to correspond to a texture. Six square 2D textures form six sides of the cube. Now, please consider your surroundings again. You can stand in a single position and capture % 26 ldquo; % 26 rdquo; 360 degrees of view (6 photos taken every 90 degrees ), for 6 captured outdoor scenes:

Back bottom
Front left
Right top

Describes how to map the cube texture to the face of the legislation. Example of a cubic MapUsing hardware-accelerated cubic maps makes rendering an object in a dynamic reflection environment possible. The coolest thing is that it can implement % 26 ldquo; real-time % 26 rdquo ;. Below is a % 26 ldquo; bubble % 26 rdquo; program running on a PC with NVIDIA geforce 256 graphics processor. The program uses a physical model to distort % 26 ldquo in real time; bubble % 26 rdquo; shape, turbulent % 26 ldquo; bubble % 26 rdquo; surface causes dynamic environment ing. Different Environment ing methodsComputer Graphics researchers call the rendering technology % 26 ldquo; Environment ing % 26 rdquo ;. This technology is not a new technology. BlinN and Newell published this idea for the first time in 1976, such as the film % 26 ldquo; abyss % 26 rdquo; and % 26 ldquo; Terminator 2% 26 rdquo; this technology has been popularized (but these effects are rendered offline, rather than real-time ). Cube pasters are only one of the methods to implement environment ing. Other methods such as % 26 ldquo; sphere ing % 26 rdquo; and % 26 ldquo; dual parabolic ing % 26 rdquo; ( Heidrich % 26amp; SeidelDevelopment) can produce similar effects (using normal environment ing), but they all have serious disadvantages and limit their usefulness. Sphere ing is very simple and directly supported by OpenGL, but it is related to viewpoint. Different viewpoints require different sphere ing. In addition, sphere ing will generate % 26 ldquo; % 26 rdquo; on the boundary ;. As shown in: Dual-parabolic ing can overcome these problems, but it requires two texture units or two-time rendering, so the cost is relatively high. The texture coordinates must be calculated at the same time for dual-parabolic ing (unless supported by OpenGL extension ). Creating a texture map for Sphere ing or parabolic ing is not intuitive. It requires professional graphic deformation operations. As shown in:

Sphere ing

Dual-parabolic ing diagram (forward) Dual-parabolic ing diagram (backward)

 

Cube map eliminates % 26 ldquo; % 26 rdquo; of Sphere ing ;. Unlike the dual-parabolic ing, it requires only one Texture unit and one rendering. Because the texture map of a cubic map is only six square surfaces in the environment, you can easily obtain and dynamically render the texture by taking a photo. Cube maps fully utilize each pixel of a texture image, while sphere or parabolic ing may only use 78% of texture pixels. At the same time, it does not have the deformation operation of the sphere or dual parabolic. Therefore, the cubic texture is more intuitive, but the cubic texture needs to be able to access six texture maps at a time, which requires updated texture hardware. In addition, cubic textures are more complex than common texture textures. The rapid development of semiconductor technology has enabled the hardware cube Paster Technology on PC. NVIDIA's geforce 256 is the first GPU that supports cubic textures. The hardware can exert its power only when it has software interfaces. Fortunately, both Microsoft's direct3d API and OpenGL on Windows support cube pasters. OpenGL usageExt_texture_cube_map extension supports cubic textures.Other applications of cube texturesMirror highlight:When a 3D object is displayed, the CAD program adds a reflection illumination effect to convey the surface curvature. A shining point on the sphere surface is an example of a mirror highlight. Because people are very familiar with the mirror highlights on real objects, it provides very important visual prompts about surface curvature. A troublesome problem is that it is very difficult to sample these mirror highlights, because CAD programs usually use solid mesh to render 3D objects, you can only perform expensive mirror highlight calculations on mesh vertices. Then, the color after illumination is interpolated on the surface through the mesh vertex. Unfortunately, if the mesh is not detailed enough, the sampling of the mirror highlight will be insufficient. When an object is rotated, % 26 ldquo; % 26 rdquo; the intensity of the highlights is dependent on the closeness of vertices in the mesh. Important visual prompts of highlight are greatly reduced. However, a finer grid is impractical, resulting in a slower speed. Ideally, the mirror highlight should be bright and stable enough not to be affected by the mesh density of the object's vertices. Cube pasters provide an intuitive way to render stable mirrored highlights. Multiple mirror highlights can be encoded into a cubic texture, which can be obtained through the reflection vector of the surface. You do not have to calculate the color of each vertex and perform Interpolation on the representation. The hardware only calculates the reflection vector (much faster than the actual reflected light contribution) to calculate the texture coordinates. A stable highlight generated by using a cube texture. The left is a highlighted area of white and orange. Another advantage of using a cube texture to produce a stable mirror highlight using a common vertices-by-vertices on the right is the number of light sources reflected on the mirror (encoded into a single cubic texture) it has nothing to do with the rendering performance, and does not affect the Interaction performance (it has been coded before the exchange ). The disadvantage is that it is only suitable for situations where the light source is located far or infinitely far. Fortunately, CAD software usually uses infinite light sources.Skylight LightingAnother application of cubic textures is precise modeling of outdoor lighting. Computer Graphics programmers often regard sunlight as an infinite light source, which is a rough approximation that will lead to unrealistic outdoor lighting. Most of the outdoor light is directly from the solar and atmospheric scattering. Skylight lighting is often mentioned: Changes in days, clouds, and pollution will affect the lighting of skylights. Cube textures capture scattering components in Skylight lighting. By using the surface method, the cubic texture can be accessed to quickly obtain the scattered light illumination of the skylight. It is complicated to calculate the cubic texture for a specific skylight structure, but some graphic researchers have made efforts and achieved objective results. For more information, see Siggraph 99's paper: "A Practical analytic model for daylight" and Klassen's "modeling the effect of the atmosphere on light" (ACM transactions on graphics, July 1987) and "modeling of skylight and rendering of outdoor scenes" (eurographics '93) such as tadamura ).Dynamic cubic map reflectionIn the example of % 26 ldquo; bubble % 26 rdquo; above, it can be moved and distorted, and the observer can change the viewpoint position in real time. The reflected environment itself will not change. for the dynamic reflection environment, every change requires programmers to re-produce the cubic map. Dynamic cubic environment textures are used to handle this situation. The dynamic cubic texture is generated when the scene is rendered for the first time. It can be used in OpenGL.The glcopysubteximage2d Command copies each face to the cube texture. Then, use a cubic map to draw an object. It can be predicted that this process is more time-consuming than static cubic textures because more painting is required. The following is an example: When % 26 ldquo; bubble % 26 rdquo; is moved, the Environment ing on it will change dynamically.Shows the dynamic rendering of faces in a cubic texture: if there are multiple reflection objects in the scene, this technology will become more time-consuming, and each reflection object requires a dynamic environment ing, if the object can also reflect the light reflected by other objects, it will become more complicated. Generally, you may need to consider the ray tracing algorithm to solve the problem. The two reflected sphere are displayed. After careful observation, we can find that the following sphere contains the reflection of the above sphere. The reflection of the following sphere can be generated by recursion in the above sphere, but 18 times of rendering is used, so it is not suitable for real-time interaction.Strange pixel-by-pixel illuminationCube textures provide support for Pixel-by-pixel illumination. Pixel-by-pixel illumination includes calculating the scattering, reflection, environment light, and other components of each pixel (corresponding to vertex-by-vertex illumination and interpolation ). The Application of Environment ing, stable mirror highlight, skylight lighting, and other cubic textures discussed above is only a special case of General pixel-by-pixel illumination technology. Because light computing relies on 3D direction vectors, cubic textures accessed by direction vectors are naturally used to express pixel-by-pixel illumination operations. Art-level pixel-by-pixel illumination requires hardware to support pixel operations, such as vector dot multiplication and texture synthesis. In the following example, all images are generated through OpenGL (on the geforce 256 GPU ),New cubic texture targetsCurrently, OpenGL contains 1d, 2d, 3D, and cubic textures. Each texture target has a corresponding enumerated value, which is passedGlbindtexture,GltexparameterAndGlteximage and other texture functions.2D texture isGl_texture_2d. 1D and 3D textures areGl_texture_1dAndGl_texture_3d. For cubic texturesGl_texture_cube_map_ext. HoweverUnlike gl_texture_1d, gl_texture_2d, and gl_texture_3d, gl_texture_3d cannot be passed to glteximage2d and glcopysubteximage2d functions. Because there are six mipmap sets in the cubic texture (one for each face), the target of the six textures is:Gl_texture_cube_map_positive_x_ext
Gl_texture_cube_map_negative_x_ext
Gl_texture_cube_map_positive_y_ext
Gl_texture_cube_map_negative_y_ext
Gl_texture_cube_map_positive_z_ext
Gl_texture_cube_map_negative_z_extThe convenience is that these targets have the following continuity:Gl_texture_cube_map_positive_y_ext=Gl_texture_cube_map_positive_x_ext+ 2,
Gl_texture_cube_map_negative_z_ext=Gl_texture_cube_map_positive_x_ext+ 5 these targets will be usedGlteximage2d,Glcopysubteximage2d, glgetteximage, glcopyteximage2d,Gltexsubimage2dAndGlgettexlevelparameterAnd other functions.Set a cubic texture imageThe following example shows how to load the six faces of a cube texture.Glubyte face [6] [64] [64] [3]; For (I = 0; I <6; I ++ ){
Glteximage2d (gl_texture_cube_map_positive_x_ext + I,
0, // level
Gl_rgb8, // internal format
64, // width
64, // height
0, // border
Gl_rgb, // format
Gl_unsigned_byte, // type
% 26amp; face [I] [0] [0] [0]); // pixel data
}Each plane is64x64 RGB image. Similar to 2D textures, you can useGlubuild2dmipmapsFunction to create the mipmap. For example:Glubuild2dmipmaps (gl_texture_cube_map_negative_x_ext,
Gl_rgb8, 64, 64, gl_rgb, gl_unsigned_byte, % 26amp; face [1] [0] [0] [0]);Enable and disable cube texture function call method:Glenable (gl_texture_cube_map_ext );
Gldisable (gl_texture_cube_map_ext );Map texture coordinates to the surfaceTarget SC TC Ma
----------------------------------------------------
+ RX gl_texture_cube_map_positive_x_ext-RZ-ry RX
-Rx gl_texture_cube_map_negative_x_ext + rz-ry RX
+ Ry gl_texture_cube_map_positive_y_ext + RX + rz ry
-Ry gl_texture_cube_map_negative_y_ext + RX-RZ ry
+ Rz gl_texture_cube_map_positive_z_ext + RX-ry Rz
-RZ gl_texture_cube_map_negative_z_ext-RX-ry RzS = (SC/| Ma | + 1)/2
T = (TC/| Ma | + 1)/2Gltexcoord3f (S, T, R); // user-supplied direction vector for cube map texturing
Glvertex3f (x, y, z );Reflection map example:Gltexgenfv (gl_s, gl_texture_gen_mode, gl_reflection_map_ext );
Gltexgenfv (gl_t, gl_texture_gen_mode, gl_reflection_map_ext );
Gltexgenfv (gl_r, gl_texture_gen_mode, gl_reflection_map_ext );
Glable (gl_texture_gen_s );
Glable (gl_texture_gen_t );
Glable (gl_texture_gen_r );Normal map example:Gltexgenfv (gl_s, gl_texture_gen_mode, gl_normal_map_ext );
Gltexgenfv (gl_t, gl_texture_gen_mode, gl_normal_map_ext );
Gltexgenfv (gl_r, gl_texture_gen_mode, gl_normal_map_ext );
Glable (gl_texture_gen_s );
Glable (gl_texture_gen_t );
Glable (gl_texture_gen_r );For these two modes to operate correctly, correct per-vertex normals must be supplied. used before useGlgetstring (gl_extensions)To ensure that the extension is available. If the OpenGL 'header file does not includeExt_texture_cube_mapThe latest enumeration value in the extension, which can be defined in your own header file:# Ifndef gl_ext_texture_cube_map
/* Ext_texture_cube_map */
# Define gl_ext_texture_cube_map 1
# Define gl_normal_map_ext 0x8511
# Define gl_reflection_map_ext 0x8512
# Define gl_texture_cube_map_ext 0x8513
# Define gl_texture_binding_cube_map_ext0 x 8514
# Define gl_texture_cube_map_positive_x_ext 0x8515
# Define gl_texture_cube_map_negative_x_ext 0x8516
# Define gl_texture_cube_map_positive_y_ext 0x8517
# Define gl_texture_cube_map_negative_y_ext 0x8518
# Define gl_texture_cube_map_positive_z_ext 0 x8519
# Define gl_texture_cube_map_negative_z_ext 0x851a
# Define gl_proxy_texture_cube_map_ext 0x851b
# Define gl_max_cube_map_texture_size_ext 0x851c
# Endif

 

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.