1

I am trying to display a bitmap using opengl but I don't want the black portion of the image to be displayed. I can do this in DirectX but not in opengl. In other words - I have images of plants with a black background and I want the plants to be drawn so they look realistic (without a black border).

genpfault
  • 51,148
  • 11
  • 85
  • 139
carpman
  • 75
  • 8

2 Answers2

2

You can do this using alpha-testing:

  1. Add an alpha channel to your image before uploading it to a texture, 0.0 on black pixels and 1.0 everywhere else.
  2. Enable alpha-testing with glEnable( GL_ALPHA_TEST )
  3. glAlphaFunc( GL_LESS, 0.1f )
  4. Render textured quad as usual. OpenGL will skip the zero-alpha texels thanks to the alpha-test.
genpfault
  • 51,148
  • 11
  • 85
  • 139
1

There are a couple of ways you can do this.

One is to use an image editing program like Photoshop or GIMP to add an alpha channel to your image and then set the black portions of the image to a max alpha value. The upside to this is it allows you to decide which portions of the image you want to be transparent, since a fully programmatic approach can sometimes hide things you want to be seen.

Another method is to loop through every pixel in your bitmap and set the alpha based on some defined threshold (i.e. if you want true black, check to see if each color channel is at 255). The downside to this is it will occasionally cause some of your lines to disappear.

Also, you will need to make sure that you have actually enabled the alpha channel and test, as stated in the answer above. Make sure to double check the order of your calls as well, as this can cause a lot of issues when you're trying to use transparency.

That's about as much as I can suggest since you haven't posted the code itself, but hopefully it should be enough to at least get you on the way to a solution.

jhyatt
  • 131
  • 3
  • Many thanks for the help - I will try both methods. I am finding opengl very much harder to use than directx. In directX this can be done easily without doing anything to the image. Thanks for taking the time to reply. – carpman Feb 17 '14 at 18:57