PDA

View Full Version : Border of Alpha-Blended Textures get black



thestr4ng3r
11-04-2011, 12:54 PM
I am rendering a scene to a texture using an fbo and then I render this texture to another one using a gaussian blur shader. But after that, the borders of all faces that are alpha blended get black. Here are some screenshots:
Before...:
http://i.stack.imgur.com/fgjxQ.jpg
..and after going through the blur (blur size is set to 0 in this case)
http://i.stack.imgur.com/tq94C.jpg

This is where the textures are created:


glGenTextures(1, &render.color_tex);
glBindTexture(GL_TEXTURE_2D, render.color_tex);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP, GL_TRUE);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, display_width * render.aa_level, display_height * render.aa_level, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0);
glBindTexture(GL_TEXTURE_2D, 0);

glGenTextures(1, &render.color_tex_temp);
glBindTexture(GL_TEXTURE_2D, render.color_tex_temp);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP, GL_TRUE);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, display_width * render.aa_level, display_height * render.aa_level, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0);
glBindTexture(GL_TEXTURE_2D, 0);

glGenTextures(1, &render.color_tex_blurred);
glBindTexture(GL_TEXTURE_2D, render.color_tex_blurred);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP, GL_TRUE);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, display_width * render.aa_level, display_height * render.aa_level, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0);
glBindTexture(GL_TEXTURE_2D, 0);

The function that blurs the rendered texture:


void CGame::BlurTexture(GLuint in, GLuint temp, GLuint out, int width, int height, float size)
{
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, blur.fbo);
glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D, temp, 0);

glDisable(GL_LIGHTING);
glDisable(GL_DEPTH_TEST);
glViewport(0, 0, width, height);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0, 0.0, 1.0, 1.0, -1.0, 1.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

blur_shader.BindShader();
blur_shader.SetBlur(size, BLUR_VERTICAL);
blur_shader.SetTexture(in, display_width, display_height);

glColor4f(1.0, 1.0, 1.0, 1.0);
glBegin(GL_QUADS);
blur_shader.SetTexCoord(Vec(0.0, 1.0));
glVertex2f(-1.0, 1.0);

blur_shader.SetTexCoord(Vec(0.0, 0.0));
glVertex2f(-1.0, -1.0);

blur_shader.SetTexCoord(Vec(1.0, 0.0));
glVertex2f(1.0, -1.0);

blur_shader.SetTexCoord(Vec(1.0, 1.0));
glVertex2f(1.0, 1.0);
glEnd();

UseNoShader();

glFlush();

glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D, out, 0);

glDisable(GL_LIGHTING);
glDisable(GL_DEPTH_TEST);
glViewport(0, 0, width, height);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0, 0.0, 1.0, 1.0, -1.0, 1.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

blur_shader.BindShader();
blur_shader.SetBlur(size, BLUR_HORIZONTAL);
blur_shader.SetTexture(temp, display_width, display_height);

glColor4f(1.0, 1.0, 1.0, 1.0);
glBegin(GL_QUADS);
blur_shader.SetTexCoord(Vec(0.0, 1.0));
glVertex2f(-1.0, 1.0);

blur_shader.SetTexCoord(Vec(0.0, 0.0));
glVertex2f(-1.0, -1.0);

blur_shader.SetTexCoord(Vec(1.0, 0.0));
glVertex2f(1.0, -1.0);

blur_shader.SetTexCoord(Vec(1.0, 1.0));
glVertex2f(1.0, 1.0);
glEnd();

UseNoShader();

glFlush();

glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D, 0, 0);
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);

glActiveTextureARB(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, out);
glGenerateMipmapEXT(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, 0);
}

And finally how the function is called:


BlurTexture(render.color_tex, render.color_tex_temp, render.color_tex_blurred,
display_width * render.aa_level, display_height * render.aa_level, 0.0
By the way, it is NOT the fault of the shader. I used the fixed functionality and the issue was still present

Dark Photon
11-04-2011, 01:53 PM
I am rendering a scene to a texture using an fbo and then I render this texture to another one using a gaussian blur shader. But after that, the borders of all faces that are alpha blended get black.
Likely candidate:

* Premultiplied alpha (Tom Forsyth) (http://home.comcast.net/~tom_forsyth/blog.wiki.html#%5B%5BPremultiplied%20alpha%5D%5D)

Search for "black", and highlight all instances in your browser.

thestr4ng3r
11-05-2011, 08:16 AM
Ah alright got it!
Thank you very much!