Perfect (3D) texture mapping in opengl

前端 未结 1 413
小蘑菇
小蘑菇 2020-12-10 23:42

I am trying to create a simple 3D-array visualisation in opengl. The 3D-array contains color values. To achieve this, I could just draw lots of cubes, giving them texture-co

相关标签:
1条回答
  • 2020-12-10 23:48

    Can anyone tell me how to handle this?

    You're running into the infamous fenceposting problem. Simply spoken, texture coordinates 0 and 1 are not pixel centers, but pixel borders. Have a look at this simple sketch

      | 0 | 1 | 2 | 3 |
      ^               ^
     0.0             1.0
    
      0   1   2   3   4
     --- --- --- --- ---
      4   4   4   4   4
    

    So to address pixel i (say 0) of a texture N (say 4) pixels wide you must use the texture coordinate

    (i/N + (i+1)/N)/2 = i/2N + (i+1)/2N = (2i + 1) / 2N = (i + 0.5)/N
    

    So adding that 0.5 is not a hack. However in the shader you can just use texelFetch to address the texture in absolute pixels, instead of a sampling coordinate. However you must implement filtering yourself then.

    0 讨论(0)
提交回复
热议问题