几年前,我正在为Android编写OpenGL 1.0 ES,并能够获得透明的纹理。
我正在2D图层上工作,该图层将具有正方形的精灵(由2个三角形组成),但我需要透明度。
[当我尝试添加我认为应该执行的代码时,我收到了OpenGL错误。
gl.glEnable(GL10.GL_ALPHA_TEST);
gl.glAlphaFunc(GL10.GL_GREATER,0.0f);
这里是完整的drawFrame(GL10 gl)函数...
public void drawFrame(GL10 gl)
{
super.drawFrame(gl);
gl.glEnable(GL10.GL_BLEND);
gl.glBlendFunc(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
gl.glEnable(GL10.GL_ALPHA_TEST);
gl.glAlphaFunc(GL10.GL_GREATER,0.0f);
gl.glEnable(GL10.GL_TEXTURE_2D);
gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
gl.glTexCoordPointer(2,GL10.GL_FLOAT,0, textureBuffer);
gl.glBindTexture(GL10.GL_TEXTURE_2D,textureID);
//Prep the Vertices
gl.glFrontFace(GL10.GL_CCW); //Set the CULL direction
gl.glEnable(GL10.GL_CULL_FACE); //Enable culling
gl.glCullFace(GL10.GL_BACK); //Set which side to CULL
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
gl.glColor4f(1f,1f,1f,0f);
gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer);
gl.glDrawArrays(GL10.GL_TRIANGLES,0,vertexBuffer.capacity()/3);
gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
gl.glDisable(GL10.GL_CULL_FACE);
gl.glDisable(GL10.GL_ALPHA_TEST);
gl.glDisable(GL10.GL_TEXTURE_2D);
gl.glDisable(GL10.GL_BLEND);
}
这是我用来加载图像的功能
public void LoadTexture(int resourceId, Context context, GL10 gl)
{
int[] textures = new int[1];
BitmapFactory.Options opts = new BitmapFactory.Options();
opts.inScaled = false;
Bitmap bm = BitmapFactory.decodeResource(context.getResources(),resourceId, opts);
gl.glGenTextures(1,textures,0);
gl.glBindTexture(GL10.GL_TEXTURE_2D,textures[0]);
textureID = textures[0];
gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_LINEAR);
gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR);
gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_S, GL10.GL_CLAMP_TO_EDGE);
gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_T, GL10.GL_REPEAT);
GLUtils.texImage2D(GL10.GL_TEXTURE_2D,0, bm, 0);
bm.recycle();
}
OpenGL ES 1.x区分Common和Common-Lite配置文件。参见OpenGL ES 1.0.02 Specification - Appendix A。虽然“通用”配置文件中支持Alpha-Test,但“通用-精简”配置文件中不支持。参见OpenGL ES 1.0.02 Specification - 4.1 Per-Fragment Operations
如果要绘制部分透明的纹理,则使用Blending就足够了。您根本不需要进行Alpha测试。