1 Can we do alpha test at Global?
glEnable(GL_ALPHA_TEST);
glAlphaFunc(GL_EQUAL, 0.7);
Use these two functions We can draw a texture and display the image where alpha >= 0.7. My question is if I draw 2 texture some part of them overlap, their alpha sum to 0.7, how to display this part?
2 How to draw a bitmap more efficient
About question 1, I try some functions in OpenGL, and did not achieve. so I try another way. I set a map[512][512]. I first draw all texture on this map and accumulate the alpha myself. If a pixel alpha >= 0.7, I set it color , if not I set white color. At end, I draw the whole map as a texture to the window. Using this method , I can achieve do alpha test at global. but it very very slow.
(This method comes from Bartek Banachewicz at stackoverflow)
Here is my function :
-
init
map[512][512]all set to zero -
draw every thing on this map, and accumulate the alpha
-
loop every pixel at map, set them color according there alpha
-
using
glTexImage2D()to generate texture (I think this may be slow) -
using
glTexCoord2fandglVertex3fto draw this on the window.
Using this method, I draw about 100 things, It become really slow (about 20 FPS)
Can I do something to optimize this Method or have another achievement?
First, some terminology:
You cannot draw textures at all. You can only draw triangles, which may involve mapping one or more texture to them.
If you are mapping both textures to the triangle, then what you do with their alpha depends on your shader (or if you’re not using shaders, then your
glTexEnvsettings). If you’re drawing them separately, then you’re not drawing 2 textures. You’re drawing one texture, followed by another texture.Unless you use a blend mode, what the first texture drew cannot affect the second. Or the third.
In general, you don’t. There are ways to make transfering image data more efficient (centering around pixel buffer objects for async transfer and buffer streaming techniques), but if all you’re doing is blitting images, then the correct way to do this is not to create textures every frame.
What you want to do is give OpenGL all the images you ever plan to use, and then use texture mapped triangles (quads) to draw them directly to the screen. This is how it is done in most applications that do 2D blitting.