为什么1.0的纹理坐标超出了纹理的边缘? [英] Why is a texture coordinate of 1.0 getting beyond the edge of the texture?

查看:193
本文介绍了为什么1.0的纹理坐标超出了纹理的边缘?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用纹理进行颜色查找以将效果应用于图片。我的查找是使用第一个纹理片段的亮度的渐变映射,然后在第二个纹理上查找。第二个纹理是256x256,水平渐变,几个不同的渐变从上到下。所以32个水平条纹,每个8像素高。我对x的查找是亮度,y是渐变,我的目标是条纹的中心以避免交叉。

I'm doing a color lookup using a texture to apply an effect to a picture. My lookup is a gradient map using the luminance of the fragment of the first texture, then looking that up on a second texture. The 2nd texture is 256x256 with gradients going horizontally and several different gradients top to bottom. So 32 horizontal stripes each 8 pixels tall. My lookup on the x is the luminance, on the y it's a gradient and I target the center of the stripe to avoid crossover.

我的片段着色器如下所示:

My fragment shader looks like this:

 lowp vec4 source = texture2D(u_textureSampler, v_fragmentTexCoord0);
 float luminance = 1.0 - dot(source.rgb, W);
 lowp vec2 texPos;
 texPos.x = clamp(luminance, 0.0, 1.0);
 // the y value selects which gradient to use by supplying a T value
 // this would be more efficient in the vertex shader
 texPos.y = clamp(u_value4, 0.0, 1.0);

 lowp vec4 newColor1 = texture2D(u_textureSampler2, texPos);

它运作良好,但我在白色的最白的部分和最黑暗的部分变形黑人。基本上它看起来像是从texture2上一个完全不同的地方抓住了newColor,或者可能只是为这些碎片找不到任何东西。我在着色器中添加了夹子,以防止它超出查找纹理的边缘,但这没有帮助。我没有正确使用钳位?

It works good but I was getting distortion in the whitest parts of the whites and the blackest part of the blacks. Basically it looked like it grabbed that newColor from a completely different place on texture2, or possibly was just getting nothing for those fragments. I added the clamps in the shader to try to keep it from getting outside the edge of the lookup texture but that didn't help. Am I not using clamp correctly?

最后我认为它可能与我的源纹理或它的加载方式有关。我最后通过添加以下内容来修复它:

Finally I considered that it might have something to do with my source texture or the way it's loaded. I ended up fixing it by adding:

    glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
    glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);

那么......为什么?

So.. WHY?

这是有点烦恼必须夹住纹理,因为这意味着当我加载查找表时我必须在我的代码中编写一个异常..

It's a little annoying to have to clamp the textures because it means I have to write an exception in my code when I'm loading lookup tables..

如果我的textPos.x和.y被夹紧到0-1 ..如何将样品拉到边缘以外?

If my textPos.x and .y are clamped to 0-1.. how is it pulling a sample beyond the edge?

另外......在创建时我是否必须使用上述钳位调用纹理还是可以在我即将使用纹理时调用它?

Also.. do I have to use the above clamp call when creating the texture or can I call it when I'm about to use the texture?

推荐答案

这是纹理采样器的正确行为。

This is correct behavior of texture sampler.

让我解释一下。使用 GL_LINEAR 采样的纹理时,GPU将采用与附近像素混合的像素的平均颜色(这就是为什么你没有像 GL_NEAREST那样看到像素化的原因模式 - 像素模糊不清)。
GL_REPEAT 模式纹理坐标将从0换算为1,反之亦然,与附近的像素混合(即在极坐标中,它将与纹理的另一面混合)。 GL_CLAMP_TO_EDGE 可防止此包装行为,并且像素不会与纹理对面的像素混合。

Let me explain this. When you use textures with GL_LINEAR sampling GPU will take an average color of pixel blended with nearby pixels (that's why you don't see pixelation as with GL_NEAREST mode - pixels are blurred instead). And with GL_REPEAT mode texture coordinates will wrap from 0 to 1 and vice versa, blending with nearby pixels (i.e. in extreme coordinates it will blend with opposite side of texture). GL_CLAMP_TO_EDGE prevents this wrapping behavior, and pixels won't blend with pixels from opposite side of texture.

希望我的解释清楚。

这篇关于为什么1.0的纹理坐标超出了纹理的边缘?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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