<p>Assuming that alpha works properly in the rest of the code it can be solved on shader level:</p>
<pre><code>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))) &amp;&amp; all(lessThan(uv, vec2(1.0)))) {
        color.a = 1.0;
    }
}
</code></pre>

CC BY-SA 4.0
2021-01-06T20:15:52.023Z
2021-01-07T09:00:42.217Z
2021-01-07T09:00:42.217Z
Sergio
<p>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?</p>
<pre><code>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);
</code></pre>
<p>Here is the fragment shader code:</p>
<pre><code>out vec4 color;
in vec2 uv;
uniform sampler2D tex;

void main() {
    color = texture(tex, uv);
}
</code></pre>
<p>I enabled alpha blending like this:</p>
<pre><code>glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); 
</code></pre>

CC BY-SA 4.0
2020-12-15T12:14:33.410Z
Did you try clamp to edge in the sampler setup?
pmw1234
CC BY-SA 4.0
2020-12-15T16:59:14.183Z
@pmw1234 I don't think so. How does one do that?
Lenny White
CC BY-SA 4.0
2020-12-15T19:41:57.427Z
change gl_clamp_to_border to gl_clamp_to_edge in the texparameteri calls
pmw1234
CC BY-SA 4.0
2020-12-15T19:57:19.970Z
@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.
Lenny White
<opengl><c++>
Render fragments with uvs outside (0,1) range as transparent
Lenny White
# Render fragments with uvs outside (0,1) range as transparent

(Asked by: username_0 on 2020-12-15)

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); 

```

**Tags:** opengl, c++

> **Comments:**
> **username_1**: Did you try clamp to edge in the sampler setup?  
> **username_0**: @username_1 I don't think so. How does one do that?  
> **username_1**: change gl_clamp_to_border to gl_clamp_to_edge in the texparameteri calls  
> **username_0**: @username_1 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.  

---

## 1 answers

### Answer by username_2 on 2021-01-06. Score: 1 | Accepted answer

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;
    }
}

```


---

