-1
$\begingroup$

I'm rendering an image onto a mesh. And I need to render fragments with uv coordinates that fall outside (0,1) range with full transparency. In the code below I'm specifying a border color with zero alpha. However I'm getting fully opaque border. That is when texture itself does not have alpha channel. Is it possible to use a texture without alpha channel but still render fragments outside of the range as transparent?

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER);
float border[] = {1, 1, 1, 0};
glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_BORDER_COLOR, border);

Here is the fragment shader code:

out vec4 color;
in vec2 uv;
uniform sampler2D tex;

void main() {
    color = texture(tex, uv);
}

I enabled alpha blending like this:

glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); 
$\endgroup$
4
  • $\begingroup$ Did you try clamp to edge in the sampler setup? $\endgroup$
    – pmw1234
    Dec 15, 2020 at 12:14
  • $\begingroup$ @pmw1234 I don't think so. How does one do that? $\endgroup$ Dec 15, 2020 at 16:59
  • $\begingroup$ change gl_clamp_to_border to gl_clamp_to_edge in the texparameteri calls $\endgroup$
    – pmw1234
    Dec 15, 2020 at 19:41
  • $\begingroup$ @pmw1234 Oh that's what you meant. But how would that possibly fix the issue? gl_clamp_to_edge just takes the texture colors at the border of the image and uses them outside of the (0,1) range. Unless the the corners of the image have full transparency I can't imagine how this would give me the desired results. $\endgroup$ Dec 15, 2020 at 19:57

1 Answer 1

1
$\begingroup$

Assuming that alpha works properly in the rest of the code it can be solved on shader level:

out vec4 color;
in vec2 uv;
uniform sampler2D tex;

void main() {
    color = texture(tex, uv);
    color.a = 0.0;
    if (all(greaterThan(uv, vec2(0.0))) && all(lessThan(uv, vec2(1.0)))) {
        color.a = 1.0;
    }
}
$\endgroup$
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.