How to normalize image coordinates for texture space in OpenGL?

后端 未结 2 758
余生分开走
余生分开走 2021-01-14 17:55

Say I have an image of size 320x240. Now, sampling from an sampler2D with integer image coordinates ux, uy I must normalize for te

2条回答
  •  旧时难觅i
    2021-01-14 18:23

    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)).

提交回复
热议问题