How to normalize image coordinates for texture space in OpenGL?

后端 未结 2 771
余生分开走
余生分开走 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条回答
  •  灰色年华
    2021-01-14 18:33

    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
    

提交回复
热议问题