Mipmapping and transparency

Started by
1 comment, last by V-man 14 years, 11 months ago
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!
Advertisement
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.
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.
Sig: http://glhlib.sourceforge.net
an open source GLU replacement library. Much more modern than GLU.
float matrix[16], inverse_matrix[16];
glhLoadIdentityf2(matrix);
glhTranslatef2(matrix, 0.0, 0.0, 5.0);
glhRotateAboutXf2(matrix, angleInRadians);
glhScalef2(matrix, 1.0, 1.0, -1.0);
glhQuickInvertMatrixf2(matrix, inverse_matrix);
glUniformMatrix4fv(uniformLocation1, 1, FALSE, matrix);
glUniformMatrix4fv(uniformLocation2, 1, FALSE, inverse_matrix);

This topic is closed to new replies.

Advertisement