In OpenGL ES, how do I load a texture that has transparent pixels?

谁说胖子不能爱 提交于 2019-12-18 10:35:07

问题


And then have display correctly? An example would be having a round ball in a rectangle while being able to see another texture in the background.

edit: At the moment, when I load the texture the transparent pixels from the source image are displayed as black.


回答1:


For iPhone and N95 this works:

If you are loading texture from raw data, set internal and source format to GL_RGBA.

glTexImage2D(GL_TEXTURE_2D, 0, 
    GL_RGBA,
    textureWidth,
    textureHeight, 
    0, 
    GL_RGBA,
    GL_UNSIGNED_BYTE,
    pointerToPixels);

And when rendering, enable alpha blend:

glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_BLEND);



回答2:


The answer provided by @Virne is correct, and I was able to use it for Android devices with a few small modifications. The myImage object I used is a standard .png image with transparency.

I created the texture using this:

GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, myImage, 0);

And (like Virne), I enabled alpha blend when rendering:

gl.glBlendFunc(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
gl.glEnable(GL10.GL_BLEND);


来源:https://stackoverflow.com/questions/611219/in-opengl-es-how-do-i-load-a-texture-that-has-transparent-pixels

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!