OpenGL ES Texture Coordinates Slightly Off

前端 未结 2 1653
半阙折子戏
半阙折子戏 2020-12-24 09:02

I\'m trying to draw a subregion of a texture in OpenGL by specifying the coordinates I want. What\'s happening though is that, depending on the size of the image, it seems t

相关标签:
2条回答
  • 2020-12-24 09:45

    Propably this has to do with the wrap around. Try this when you create the source image:

    glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP )
    glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP )
    
    0 讨论(0)
  • 2020-12-24 09:46

    Keep in mind, that OpenGL samples textures at the texel centers. So when using linear filtering (like GL_LINEAR or GL_LINEAR_MIPMAP_LINEAR) the exact texel color is only returned if sampled at the texel center. Thus, when you only want to use a sub-region of a texture, you need to indent your texture coordinates by half a texel (or 0.5/width and 0.5/height). Otherwise the filtering will blend the border of the texture with neigbouting texels outside of your intended region. This causes your slightly pinkish border. If you use up the whole texture this effect is compensated by the GL_CLAMP_TO_EDGE wrapping mode, but when using a subregion, GL does not know where it's edge is and that filtering should not cross it.

    So when you got a subregion of the texture in the range [s1,s2]x[t1,t2] (0 <= s,t <= 1), the real valid texCoord interval should be [s1+x,s2-x]x[t1+y,t2-y] with x being 0.5/width and y being 0.5/height (the width and height of the whole texture corresponding to [0,1]x[0,1]).

    Therefore try

    data[0].t[0] = 0.0f * uFactor + 0.5/textureWidth;
    data[0].t[1] = 0.0f * vFactor + 0.5/textureHeight;
    data[1].t[0] = 1.0f * uFactor - 0.5/textureWidth;
    data[1].t[1] = 0.0f * vFactor + 0.5/textureHeight;
    data[2].t[0] = 0.0f * uFactor + 0.5/textureWidth;
    data[2].t[1] = 1.0f * vFactor - 0.5/textureHeight;
    data[3].t[0] = 1.0f * uFactor - 0.5/textureWidth;
    data[3].t[1] = 1.0f * vFactor - 0.5/textureHeight;
    
    0 讨论(0)
提交回复
热议问题