开发者

OpenGL - Texture Atlas "smudging"

开发者 https://www.devze.com 2023-03-26 11:44 出处:网络
I am using a texture atlas which I have a made by stitching 2 identically sized images together side-by-side.

I am using a texture atlas which I have a made by stitching 2 identically sized images together side-by-side. When I modify the texture co-ordinates I do the following in this order:

  1. if(texCoordx>1) texCoordx = 开发者_运维技巧texCoordx % 1 - So one texture doesn't leak into another.
  2. if(texCoordx<0) texCoordx = 1 + texCoordx - Again, so one texture doesn't leak into another.
  3. Scale the texture co-ordinates by 0.5.
  4. Add 0.5 if wanting to draw the texture on the right.

This works out mostly fine, however, I seem to get 'smudging' at the edges where the texture s co-ordinates approach 1 and 0. It looks like it is being smudged along the t axis. I have attached a screenshot.

OpenGL - Texture Atlas "smudging"

I know that there was one answer found here but it only slightly narrows the smudging. I'm using my own shader, but it's not doing any modification of texture co-ordinates. I am also enabling GL_REPEAT. Does anybody know any possible causes/solutions?


If I interpret your picture correctly, this texture is one of the two textures (suppose the left) from the atlas and your original texCoords were (from left to right) something like

0.2, 0.4, 0.6, 0.8, 1, 1.2, 1.4, 1.6, 1.8, 2

after your own wrapping in step 1 you now got something like

0.2, 0.4, 0.6, 0.8, 1, 0.2, 0.4, 0.6, 0.8, 1

, which later became

0.1, 0.2, 0.3, 0.4, 0.5, 0.1, 0.2, 0.3, 0.4, 0.5

Here we see the problem at 0.5, 0.1, where the monotonicity changes. OpenGL doesn't know that you want to go from 0.5 to 0.1 in the left-right direction. It just takes the difference, which is -0.4 and thus interpolates from right to left back to the beginning of the texture (and therefore squezes the whole texture inside this small interval backwards, so along the t-axis it's actually correct). This is due to the fact, that OpenGL doesn't know that your texture wraps at the middle of the texture, as it conceptually only sees one large texture and not an atlas of small textures and the GL_REPEAT wrapping mode only works at the edge of the large texture.

You have to do the wrapping (the first two steps) in the fragment shader before accessing the texture. So your texture coordinates are all monotone and get interpolated correctly. Then at the fragment you can safely wrap the texture coordinates. Actually you can do all the steps in the fragment shader. Just give it a uniform for the offset inside the atlas (selecting the actual sub-texture). Then you do the wrapping, scaling and add the offset just before accessing the texture.

0

精彩评论

暂无评论...
验证码 换一张
取 消