Jump to content
  • Advertisement
Sign in to follow this  
freemain

OpenGL Why is alpha-blending so slow?

This topic is 4241 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am trying to develop a 2D game using OpenGL.(WindowsXP, Dev C++, glut) In the image of background, the alpha value of each pixel is assigned 0. In the image of role, the alpha values of those pixels that belong to the role's body are assigned 255, and those that do not are assinged 0. Then glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA ) ensures that the body of the role overlays the background, and the rest of the role's image won't overlay the background. This approach works, but in an unacceptable speed, I mean, roles walks rather slow when enable alpha-blending. Disable it results in much higher speed. Finally, I wrote a filter to keep those pixels with 0 alpha value away from the screen_buffer before calling glDrawPixels( screen_buffer ). It works fine and can keep in a reasonalbe speed. My question: Is alpha-blending too expensive for my purpose, or it's just my fault? Why ?

Share this post


Link to post
Share on other sites
Advertisement
glDrawPixels itself is slow.

Try rendering textured quads using a orthogonal projection matrix.

If your screen size is 640 x 480 for example you build a orthogonal projection matrix with left = 0, right = 640, top = 0, bottom = 480 (you can swap top and bottom, depending on the desired location of 0/0). Then your quads are sized to match the desired size in pixels and are assigned texture coordinates between 0 and 1. So, if your object should be 64 x 64 pixels, you have your quad have vertex coordinates of 0 or 64 for x and y and 0 for z (or something other, depending on whether you use depth testing or nor). Translate your quad to the correct position and it will be rendered as if you called glDrawPixels.

Since OpenGL is primarily a 3D graphics library, 2D graphics are rather slow in most implementations, so 'faking' 2D graphics with 3D means will usually be much faster.

Share this post


Link to post
Share on other sites
Yes alphablending can be expensive. This is because instead of just writing pixels to the backbuffer and be done with it the hardware has to read from the backbuffer (to get dest-alpha), compare and then write to the backbuffer. Depending on your current hardware and the size of the picture being alphablended it could slow your program. Be sure you disable alphablending when your done with it.

It seems what your trying to do is to make one color seethru. In that case you could go with alpha testing. This technique only requires the pipeline to check if the pixel being written has its alpha under a specified threshold, if it is, its not written to the backbuffer. Much cheaper than alphablending.

//Emil

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!