Why is alpha-blending so slow?

Started by
2 comments, last by freemain 17 years, 4 months ago
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 ?
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.

If I was helpful, feel free to rate me up ;)If I wasn't and you feel to rate me down, please let me know why!
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
Emil Jonssonvild
Thanks cannonicus & Lord_Evil, I'll try soon :-)

This topic is closed to new replies.

Advertisement