Drawing text with transparent background
I use code like below to update text (i开发者_运维百科n a bitmap) dynamically into a texture:
public void UpdateTexture(GL10 gl, int x, int y, int textureId, Bitmap bitmap)
{
gl.glBindTexture(GL10.GL_TEXTURE_2D, textureId);
GLUtils.texSubImage2D(GL10.GL_TEXTURE_2D, 0, x, y, bitmap);
}
The problem is that texSubImage2D function does not completely replace the existing pixels in the texture but it keeps the existing information and draws the new bitmap over it. And because the new bitmap has transparent pixels the old bitmap is displayed underneath it when the texture is rendered. Is there a way to completely erase the old information from the target area from the texture before texSubImage2D call?
The answer is quite simple. Your drawing a new texture over the old one which is the desired behavior (since you would want to create a texture out of combinitions).
You should delete the old texture first (quote from this doc).
Cleaning Up Texture Objects
As you bind and unbind texture objects, their data still sits around somewhere among your texture resources. If texture resources are limited, deleting textures may be one way to free up resources.
void glDeleteTextures(GLsizei n, const GLuint *textureNames)
:
Deletes n texture objects, named by elements in the array textureNames. The freed texture names may now be reused (for example, by glGenTextures()).
If a texture that is currently bound is deleted, the binding reverts to the default texture, as if glBindTexture() were called with zero for the value of textureName. Attempts to delete nonexistent texture names or the texture name of zero are ignored without generating an error.
There was actually a bug elsewhere in the code.
texSubImage2D
actually replaces all the texture data in the given region. The drawing of the new texture over the old one was occurring on the bitmap/canvas side.
canvas.drawColor()
call was used by mistake to clear the bitmap when bitmap.eraseColor()
should have been used.
精彩评论