3
我正在開發一個使用Android的Opengl-es視頻遊戲。我使用的圖像背景有問題,因爲它會繪製背景的顏色。使用opengles具有透明背景
這裏我的渲染器:
@Override
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
//Initialize GL:
gl.glDisable(GL10.GL_DITHER);
gl.glEnable(GL10.GL_TEXTURE_2D); //Enable Texture Mapping (NEW)
gl.glShadeModel(GL10.GL_SMOOTH); //Enable Smooth Shading
gl.glClearColor(0.0f, 0.0f, 0.0f, 0.5f); //Black Background
gl.glClearDepthf(1.0f); //Depth Buffer Setup
gl.glEnable(GL10.GL_DEPTH_TEST); //Enables Depth Testing
gl.glEnable(GL10.GL_CULL_FACE);
gl.glDepthFunc(GL10.GL_LEQUAL); //The Type Of Depth Testing To Do
gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_NICEST); //Really Nice Perspective Calculations //<---- SI LAG, probar GL_FASTEST
LoadTextures(gl);
}
這裏抽獎方法:
@Override
public void onDrawFrame(GL10 gl) {
gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
gl.glEnable(GL10.GL_BLEND);
gl.glBlendFunc(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
gl.glLoadIdentity();
for(Entity e : entities)
{
if(e.IsActive())
{
gl.glPushMatrix();
e.Draw(gl);
gl.glPopMatrix();
}
}
gl.glDisable(GL10.GL_BLEND);
isDrawing = false;
}
而且每個實體的繪製方法:
gl.glColor4f(red, green, blue, alpha);
// bind the previously generated texture
gl.glBindTexture(GL10.GL_TEXTURE_2D, textureId);
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
// Point to our vertex buffer
gl.glVertexPointer(2, GL10.GL_FLOAT, 0, screenBuffer); //2 dimensiones
gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, textureBuffer);
// Draw the vertices as triangle strip
gl.glDrawArrays(GL10.GL_TRIANGLE_FAN, 0, 4); //12 /3 -> 12 es el tamano del vector de vertices del cuadrado
//Disable the client state before leaving
gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
我也有這個在我的GLSurfaceView:
setEGLConfigChooser(8,8,8,8,16,0);
renderer = new GRenderer(c);
setRenderer(renderer);
setRenderMode(RENDERMODE_WHEN_DIRTY);
getHolder().setFormat(PixelFormat.TRANSLUCENT);
setZOrderOnTop(true);
但它不起作用。我應該使用哪種顏色來實現半透明背景?代碼中的任何錯誤?謝謝