简体   繁体   English

OpenGL中的纹理坐标

[英]Texture coordinates in OpenGL

Sorry for a really noob-level question... 对不起,这是一个非常级别的问题...

I want to apply a specific piece of the texture (not the entire texture) to a quad. 我想将一个特定的纹理(不是整个纹理)应用到四边形。 The texture is a 256x64 image and I'd like to be able to specify the relevant piece by stating the pixel coordinates of its upper-left and bottom-right corners ( [0,0] being the upper left corner of the whole image and [256,64] being the bottom right). 纹理是256x64图像,我希望能够通过说明其左上角和右下角的像素坐标来指定相关的部分([0,0]是整个图像的左上角和[256,64]是右下角)。

Any ideas on how to do that? 有关如何做到这一点的任何想法?

Thanks. 谢谢。

The fractional answer is correct, but if you want to use integer texture coordinates (for example in a VBO) you can use the GL_TEXTURE matrix to change your texture coordinate system: 小数答案是正确的,但如果要使用整数纹理坐标(例如在VBO中),可以使用GL_TEXTURE矩阵来更改纹理坐标系:

        glMatrixMode(GL_TEXTURE)
        glLoadIdentity()
        glScalef(1f/256f, 1f/64f, 1f)

After that your texture coordinate units would be pixels. 之后,您的纹理坐标单位将是像素。 Another scaling strategy would be to scale so each tile is 1x1 in the final units. 另一种缩放策略是缩放,因此每个图块在最终单位中为1x1。

imagine you want to use the 20x20 texel starting at 10,10, you'd use the following coordinates: 想象你想使用从10,10开始的20x20纹素,你可以使用以下坐标:

[10.f/256.f,10.f/64.f]
[30.f/256.f,10.f/64.f]
[30.f/256.f,30.f/64.f]
[10.f/256.f,30.f/64.f]

You use float-based texture coordinates, where they range from (0.0f, 0.0f) to (1.0f, 1.0f). 您使用基于浮动的纹理坐标,其范围从(0.0f,0.0f)到(1.0f,1.0f)。 In your case, you take your pixel values and divide them by either 256-1 or 64-1, depending on which dimension you are dealing with. 在您的情况下,您将获取像素值并将其除以256-1或64-1,具体取决于您要处理的维度。 (This assumes you really meant to say that your image goes to [255,63] and not [256,64], meaning that your coordinates are 0-based and not 1-based.) (这假设你真的想说你的图像是[255,63]而不是[256,64],这意味着你的坐标是基于0而不是基于1。)

Let's look at the following, simplified example of 1D texture of 8 pixels width (the pixel centers are at the digits): 让我们看一下以下8个像素宽度的1D纹理的简化示例(像素中心位于数字处):

 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 |

The corresponding texture coordinates are: 相应的纹理坐标是:

 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 |
0/8 1/8 2/8 3/8 4/8 5/8 6/8 7/8 8/8

Now if you want the subtexture from texels 3 to 7 the texture coordinates to use are 2/8 to 7/8. 现在,如果你想要纹素3到7的子纹理,那么使用的纹理坐标是2/8到7/8。

You should also take note about something else: Integer texture coordinates don't address texel centers, but the exact mid between them; 您还应注意其他内容:整数纹理坐标不会解决纹素中心,而是它们之间的确切中间位置; this may cause a blurred appearance in certain situations. 在某些情况下,这可能会导致外观模糊。

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

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