简体   繁体   中英

Technique which piles up textures with an alpha value on Android OpenGL ES

I'm developing 2D graphics program which used OpenGL for Android 2.2 and later.

Usually, when a translucent texture is piled up, it will become like the image left. I would like to pile up a texture first and to make it transparent like the image right. The way of piling up images by Canvas and making it a texture is inefficient. Is there any way with OpenGL API?

在此处输入图片说明

This is just a thought but might be worth trying: You might be able to draw the foreground elements without blending first and then use "glBlendFuncSeparate" to draw the background. The foreground part should draw your elements as described, then in blend func you could still control how much transparency you need to blend it with the background. The bad news is if background has more elements to be drawn.

In general you should be using a frame buffer object with binded texture to it to draw the foreground elements. Then use the binded texture on your scene:

(you will have to find the methods to replace this functions for Android)

    glGenFramebuffers(1, &framebuffer);
    glBindFramebuffer(GL_FRAMEBUFFER, framebuffer);
    glGenTextures(1, &texture);
    glBindTexture(GL_TEXTURE_2D, texture);
    glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, resolution, resolution, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
    glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texture, 0);
    glBindRenderbuffer(GL_RENDERBUFFER, 0);

This will kind of create a canvas you can draw anything to it as to your main scene and you can reuse the binded texture. Note that "resolution" will probably need to be of a power of 2. Also you need to set the matrices and viewport before drawing.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM