How to normalize image coordinates for texture space in OpenGL?

爷,独闯天下 提交于 2019-12-01 10:36:24

No, the first one is actually correct:

texture(image, vec2(ux/320.0, uy/240.0))

Your premise that "ux = 0 ... 319 and uy = 0 ... 239" is incorrect. If you render a 320x240 quad, say, then it is actually ux = 0 ... 320 and uy = 0 ... 240.

This is because pixels and texels are squares sampled at half-integer coordinates. So, for example, let's assume that you render your 320x240 texture on a 320x240 quad. Then the bottom-left pixel (0,0) will actually be sampled at screen-coordinates (.5,.5). You normalize it by dividing by (320,240), but then OpenGL will multiply the normalized coordinates back by (320,240) to get the actual texel coordinates, so it will sample (.5,.5) from the texture, which corresponds to the center of the (0,0) pixel, which returns its exact color.

It is important to think of pixels in OpenGL as squares, so that coordinates (0,0) correspond the bottom-left corner of the bottom-left pixel and the non normalized (w,h) corresponds to the top-right corner of the top-right pixel (for texture of size (w,h)).

Texture coordinates (and pixel coordinates) go from 0 to 1 on the edges of the pixels no matter how many pixels.

A 4 pixel wide texture

   0          0.5          1    <- texture coordinate
   |           |           |
   V           V           v 
   +-----+-----+-----+-----+
   |     |     |     |     |
   |     |     |     |     |   <- texels
   +-----+-----+-----+-----+

A 5 pixel wide texture

   0             0.5             1    <- texture coordinate
   |              |              |
   V              V              v 
   +-----+-----+-----+-----+-----+
   |     |     |     |     |     |
   |     |     |     |     |     |   <- texels
   +-----+-----+-----+-----+-----+

A 6 pixel wide texture

   0                0.5                1    <- texture coordinate
   |                 |                 |
   V                 V                 V 
   +-----+-----+-----+-----+-----+-----+
   |     |     |     |     |     |     |
   |     |     |     |     |     |     |   <- texels
   +-----+-----+-----+-----+-----+-----+

A 1 pixel wide texture

   0 0.5 1   <- texture coordinate 
   |  |  |
   V  V  V 
   +-----+
   |     |
   |     |   <- texels
   +-----+

If you use u = integerTextureCoordinate / width for each texture coordinate you'd get these coordinates

   0    0.25  0.5   0.75       <- u = intU / width;
   |     |     |     |     
   V     V     V     V     
   +-----+-----+-----+-----+
   |     |     |     |     |
   |     |     |     |     |   <- texels
   +-----+-----+-----+-----+

Those coordinates point directly between texels.

But, the texture coords you want if you want to address specific texels are like this

     0.125 0.375 0.625 0.875   
      |     |     |     |   
      V     V     V     V  
   +-----+-----+-----+-----+
   |     |     |     |     |
   |     |     |     |     |   <- texels
   +-----+-----+-----+-----+

Which you get from

   u = (integerTextureCoord + .5) / width
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!