OpenGL automatically calculates texture coordinates (by Dong shchong jerrydong@tom.com)

Source: Internet
Author: User
OpenGL can specify texture coordinates for vertex in two ways:
1. Assign coordinates to each vertex manually. You can use the gltexcord * () function.
2. OpenGL automatically allocates coordinates for each vertex. This task is completed by the function gltexgen.
At first, I didn't quite understand the gltexgen * () function. I don't know how OpenGL automatically generates texture coordinates. After reading the relevant materials, I began to understand that to generate automatic texture coordinates, I must first specify the pattern (and algorithm) used to generate texture coordinates.
In opengl1.1, three texture coordinate generation modes can be specified: gl_object_linear, gl_eye_linear, and gl_sphere_map. In opengl1.3, two generation modes are added: gl_reflection_map and gl_normal_map. the understanding of these modes is the key to using gltexgen * (), and also determines the effect of texture pasters. The following describes the meanings of these modes:
1. gl_object_linear
In this mode, the texture generation function is a linear combination of vertex coordinates (x0, y0, z0, w0:
Generated coordinates = P0 * x0 + p1 * y0 + p2 * z0 + P3 * W0;
Suppose we construct a plane with the parameters (P0, P1, P2, P3): P0 * x + p1 * Y + p2 * z + P3 = 0;
If (P0, P1, P2) has been normalized, the distance from any point to this plane is:
(P0 * x + p1 * Y + p2 * z + P3)/SQRT (P0 * P0 + p1 * P1 + p2 * P2) = P0 * x + p1 * Y + p2 * z + P3;
We can see that the coordinates generated in this mode are equivalent to the distance from the vertex coordinates to a specific plane.
2. gl_eye_linear
In this mode, the texture generation function is also a linear combination of the human eye coordinates (xe, ye, Ze, We) of the vertex:
Generated coordinates = P0 '* XE + p1' * Ye + p2' * ze + p3' * We;
Where: (P0 ', p1', p1', p3') = (P0, P1, P2, P3) * inverse (m)
It can be seen that the gl_eye_linear and gl_object_linear modes have similar texture generation functions. The only difference is that gl_object_linear is within the object space and is subject to changes, the texture coordinates will not change with the changes of the view, while gl_eye_linear is in the view space. When the view changes, the texture coordinates will change with the changes of the view, the effect after changing the texture.
3. gl_sphere_map
In this mode, the texture coordinate generation algorithm is as follows:
A unit vector that uses u to identify the origin to the vertex in the visual coordinate system;
Use 'n' to identify the normal vector of vertices in the visual coordinate system;
Using r = trans (RX, Ry, Rz) to identify the reflection vector of the vertex, t can be calculated by the following formula:
R = U-2 * trans (n') (n' U );
In addition, m = 2 * SQRT (RX * RX + Ry * ry + (RZ + 1) * (RZ + 1), the generated coordinates are as follows:
(S, t) = (Rx/m + 0.5, Ry/m + 0.5); // In this case, the specified R and Q coordinates are invalid.
It can be seen that this mode simulates the reflection effect of the sphere on the plane.
3. gl_reflection_map
In this mode, like gl_sphere_map, the reflection vector of the vertex is calculated first, and the vector value is used as the corresponding texture coordinate:
(S, T, R) = (RX, Ry, Rz); // In this case, the specified Q coordinate is invalid.
4. gl_normal_map
First, you need to understand the background of normal transformation.
Before performing the illumination operation, you need to convert normal to the visual coordinate system, and then convert it to the unit vector through the rescaling and normalization operations:
Set the Model-View matrix to m and convert the vertex normal to the visual coordinate system using the following formula:
(Nx ', ny', nz') = (NX, NY, NZ) inverse (MU );
The Mu is a 3 × 3 matrix in the upper left corner of M.
Then, after scaling and normalization, a unit vector is obtained for illumination calculation.
In this texture generation mode, the obtained unit vector value is used as the corresponding texture coordinate:
(S, T, R) = (nfx, nfy, NFZ); // In this case, the specified Q coordinate is invalid.

The following is a few simple examples:
1. Information with parameters, including gl_object_linear and gl_eye_linear;
1. Enable the switch to automatically generate texture coordinates:
Glenable (texture_gen_s); // texture_gen_s, textue_gen_r, texture_gen_q
2. Set the mode:
Gltexgeni (gl_s, gl_texture_env_mode, gl_object_linear );
3. Set parameters:
Glfloat Params [] = {1.0, 0.0, 0.0, 0.0 };
Gltexgenv (gl_s, gl_object_plane, Params );
4. Disable the switch.
Gldisable (texture_gen_s );
2. Without parameters, including gl_sphere_map, gl_reflection_map, and gl_normal_map
1. Open the switch
Glenable (texture_gen_s); // texture_gen_s, textue_gen_r, texture_gen_q
2. Set the mode:
Gltexgeni (gl_s, gl_texture_env_mode, gl_noraml_map );
3. Disable the switch.
Gldisable (texture_gen_s );

References:
1. <OpenGL programming guide>
2. OpenGL specification 1.3
 

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.