简体   繁体   中英

Complex texture mapping in OpenGL?

Using a texture in OpenGL with the following parameters

glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP );

and mapping it like this :

glBegin( GL_QUADS );
glTexCoord2f( 3.0, 2.0 ); glVertex2f( -2.0, 2.0 ); // P1
glTexCoord2f( 0.0, 2.0 ); glVertex2f( -2.0, -1.0 ); // P2
glTexCoord2f( 0.0, 0.0 ); glVertex2f( 2.0, -2.0 ); // P3
glTexCoord2f( 3.0, 0.0 ); glVertex2f( 2.0, 1.0 ); // P4
glEnd( );

I'm not sure how the end result would look like. I understand how values greater than 1 in the glTexCoord will make the texture repeat on the x-axis and the 0 and 1 texels will be repeated on the y axis. I'm confused as to how, in this case, would the texture be mapped. Would it be backwards? I can't seem to find any similar example on the Internet...

Keep in mind that your wrap modes are for the texture coordinates (s and t), not for the x- and y-axis. Your attributes specify that the s-coordinates wrap, and the t-coordinates clamp.

Applying this to your coordinates (based on a paper sketch), your s-coordinates go from 0.0 to 3.0 along the edge from P3 to P4, and the edge from P2 to P1. These edges are in the direction of the y-axis. Since you specify REPEAT for the s-coordinates, and the s-coordinate corresponds to the horizontal direction within your texture, the original left-to-right direction of your texture will align with the y-axis in your rendering, and the image will be repeated in this direction 3 times.

Similarly, your t-coordinates go from 0.0 to 2.0 along the edge from P3 to P2, and the edge from P4 to P1. These edges go from right to left, and slightly upwards, in your rendering coordinate system. Since you specify CLAMP for the t-coordinates, and the t-coordinate corresponds to the vertical direction within your texture, the right side of your rendered quad will show the texture in top to bottom (or bottom to top, depending on how your texture is stored) direction, and the left side of your quad will repeat the top/bottom row of pixels of your texture.

This sounds kind of convoluted when explained in text, but it's really not that complicated. It would be much easier to explain with a few sketches on a whiteboard.

First you have to picture how the vertices themselves are connected in this example. Because the S and T coordinates are nothing more than where in a texture you fetch your texel from. They have nothing to do with any general spatial axis (though they can be used to create special axes of their own), they're really just coordinates that get interpolated along the surface of your polygon.

Now, since you are dealing with a quad here, discussion of the interpolation of these texture coordinates is a little more complicated than it needs to be. Let us instead, consider a nice triangle (P1,P2,P3)... Along the edge P1,P2, the T coordinate is constant and the S coordinate ranges from 3.0 to 0.0 (the texture repeats three times in the S direction along this edge).

Repeat this for all edges to get a general picture of the behavior of the texture along the edges, then lookup Barycentric coordinates to understand how the coordinates are interpolated on the interior parts of the triangle.

Here's a crude diagram that illustrates the math to implement Barycentric interpolation:

重
(source: docstoccdn.com )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM