简体   繁体   English

OpenGL中复杂的纹理映射?

[英]Complex texture mapping in OpenGL?

Using a texture in OpenGL with the following parameters 在OpenGL中使用具有以下参数的纹理

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. 我知道glTexCoord中大于1的值将如何使纹理在x轴上重复,而0和1纹素将在y轴上重复。 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. 请记住,环绕模式适用于纹理坐标 (s和t),而不适用于x轴和y轴。 Your attributes specify that the s-coordinates wrap, and the t-coordinates clamp. 您的属性指定s坐标换行和t坐标钳位。

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. 将其应用于您的坐标(基于纸面草图),您的s坐标沿着从P3到P4的边缘,从P2到P1的边缘从0.0到3.0。 These edges are in the direction of the y-axis. 这些边缘在y轴方向上。 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. 由于您为s坐标指定了REPEAT ,并且s坐标对应于纹理中的水平方向,因此纹理的原始左右方向将与渲染中的y轴对齐。朝这个方向重复3次。

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. 同样,您的t坐标沿着从P3到P2的边缘,从P4到P1的边缘从0.0到2.0。 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. 由于您为t坐标指定了CLAMP ,并且t坐标对应于纹理内的垂直方向,因此渲染四边形的右侧将在顶部到底部(或从底部到顶部)显示纹理,具体取决于纹理的方式方向),则四边形的左侧将重复纹理的顶部/底部像素行。

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. 由于S和T坐标只不过是其中的纹理,你从你获取更多的纹理像素。 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). 让我们考虑一个漂亮的三角形(P1,P2,P3)...沿着边P1,P2, T坐标是恒定的, S坐标的范围是3.00.0 (纹理在S方向上重复3次,沿此边缘)。

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 ) (来源: docstoccdn.com

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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