OpenGL 纹理图集流血 [英] OpenGL texture atlas bleeding

查看:29
本文介绍了OpenGL 纹理图集流血的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从纹理图集中绘制由较小瓷砖组成的基本 2d 地面网格(注意 1 像素透明边框):

I'm trying to draw a basic 2d ground mesh made up of smaller tiles from a texture atlas (note the 1 pixel transparent border):

我使用以下代码将图块渲染为纹理四边形:

I render the tiles as texture quads using the following code:

glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, m_texture);

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);

glVertexPointer(2, GL_SHORT, 0, &m_coords[0]);
glTexCoordPointer(2, GL_FLOAT, 0, &m_uvs[0]);

glDrawArrays(GL_TRIANGLES, 0, m_coords.size() / 2);

glBindTexture(GL_TEXTURE_2D, 0);
glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
glDisable(GL_TEXTURE_2D);

位置显然是整数坐标.角的 UV 坐标计算如下:

The positions are obviously integer coordinates. The UV coordinates for the corners are calculated like this:

u0 = float(x) / float(texWidth);
v0 = float(y) / float(texHeight);
u1 = float(x+w) / float(texWidth);
v1 = float(y+h) / float(texHeight);

其中 w 和 h 是没有填充的图块的大小.

Where w and h is the size of the tile without the padding.

当模型视图变换被捕捉到整数位置时看起来很棒(右),但是当它开始移动时,我在瓷砖之间看到黑色的东西(左):

It looks great when the modelview transform is snapped to an integer position (right), but when it starts to move I get black thingies between the tiles (left):

据我所知,我应该用半纹素偏移 UV 坐标以使其工作,但如果我将 UV 计算更改为:

From what I understand I should offset the UV coordinates with a half texel to make it work, but if I change the UV calculations to:

u0 = float(x+0.5f) / float(texWidth);
v0 = float(y+0.5f) / float(texHeight);
u1 = float(x+w-0.5f) / float(texWidth);
v1 = float(y+h-0.5f) / float(texHeight);

还是不行.这是正确的方法吗?我需要混合才能工作吗?如果我偏移瓷砖以确保它们与像素网格对齐,则它可以工作,但是在缓慢移动时会使其对齐.人们通常如何解决这个问题?

It still doesn't work. Is this the correct way to do it? Do I need blending for this to work? If I offset the tiles to make sure they're snapped to the pixel grid it works, but that makes it snap when moving slowly. How do people usually solve this?

编辑

我应该说它在 iphone 上.

I should ofc have said that it's on the iphone.

推荐答案

你的边框不应该是透明的,而应该是每个子纹理相对侧的像素.例如,每个子纹理右侧的边框应该是最左侧像素行的副本,即它会环绕到的像素.

Your border shouldn't be transparent, but rather the pixels from the opposing side of each subtexture. For example the border on the right hand side of each sub-texture should be a copy of the left-most line of pixels, i.e. the pixels that it would wrap around to.

这就是你在边框上欺骗"纹理采样器的包装方式.

That is how you "cheat" wrapping for the texture sampler on the borders.

这篇关于OpenGL 纹理图集流血的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆