Sign in to follow this  
starvinmarvin

OpenGL Mipmapping and transparency

Recommended Posts

Hi there! I'm using mipmapping in opengl and some models use textures with an alpha layer. Now due to the mipmapping turned on, I get black borders around the alpha mask in distance, if you take a closer look it's rendered how it's supposed to be.
	if(!ResourceTexture::s_bGenerateMipMaps)
	{
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
	}
	else
	{
		glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR  );
		glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR_MIPMAP_LINEAR  );
		
		glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP, GL_TRUE);
		//glGenerateMipmapEXT(GL_TEXTURE_2D);
                //glGenerateMipmapEXT doesn't seem to work on my hardware
	}



Turning off mipmapping is a bad solution. What's the trick? Maybe there's a way to change the "mipmapping distance"/level? Thanks!

Share this post


Link to post
Share on other sites
The thing is that alpha testing requires a carefully crafted alpha mask. Well, for a base-level it's just a 0 och 1 for whether you want the pixel to be visible or not. But when mipmapping, you have to be careful, because if you just go the normal way and scale the image down with some averaging box-filter, you will average the alpha channel as well. The mask is no longer binary, and is no longer suitable for alpha test.

Also, the black border you have is likely because you have a black background on the transparent parts. Well, that black border will bleed into the green leaf when you do the averaging with the box-filter, this "polluting" the main image with the black background.

What you need to do is extend the image of the leaf over the black background, so the filtering does not bleed the back into the leaf. This is usually done by not having a uniform background color, but have the background correspond to the image as if it was extrapolated over the background. One method is to set the background to the color of the closest pixel of the leaf.

The alpha mask must also be treated specially, as you need to ensure the mask is binary. When downscaling the image, you can, for example, do average filtering on the color channel, but nearest filtering on the alpha channel.

And all of this means you cannot automatically generate the mipmaps. You must do that yourself.

Share this post


Link to post
Share on other sites
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR_MIPMAP_LINEAR );
is invalid.

but that is not related to your problem. The problem is that you have black pixels in your texture. If you make them green, it will alleviate your problem.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this