使用Open GL绘制的位图具有黑色方块和颜色叠加(Android)

时间:2017-04-05 12:53:51

标签: android bitmap opengl-es

以下是我的Sprite代码。使用此代码绘制的图像具有黑色背景(覆盖透明背景)并具有颜色叠加,使图像看起来扭曲。

如何在开放状态下改善透明图像绘制。

class Ship {
public int life = 5;

public FloatBuffer ShipVertexBuffer;
public FloatBuffer ShipTextureBuffer;
public PieceCluster cluster;

//! TEXTURES
private int[] textures = new int[1];
//! TEXTURES

public float ShipVerticles[] = {
        0, 0,       // лево низ
        0, 30,      // лево вверх
        30, 0,      // право низ
        30, 30      // право вверх
};

//! TEXTURES
public float ShipTextures[] = {
        0.0f, 1.0f,
        1.0f, 1.0f,
        0.0f, 0.0f,
        1.0f, 0.0f
};
//! TEXTURES

public Ship(PieceCluster c) {
    //! Буффер вертексов
    cluster = c;
    ByteBuffer bb = ByteBuffer.allocateDirect(36);
    bb.order(ByteOrder.nativeOrder());
    ShipVertexBuffer = bb.asFloatBuffer();
    ShipVertexBuffer.put(ShipVerticles);
    ShipVertexBuffer.position(0);

    //! TEXTURES
    bb = ByteBuffer.allocateDirect(ShipTextures.length * 4);
    bb.order(ByteOrder.nativeOrder());
    ShipTextureBuffer = bb.asFloatBuffer();
    ShipTextureBuffer.put(ShipTextures);
    ShipTextureBuffer.position(0);
    //! TEXTURES
}

public void loadGLTexture(GL10 gl) {
    // loading texture
    // generate one texture pointer

    Bitmap bitmap = cluster.Picture;
    gl.glGenTextures(1, textures, 0);
    // ...and bind it to our array
    gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[0]);

    // create nearest filtered texture
   // gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_NEAREST);
  // gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR);

    // Use Android GLUtils to specify a two-dimensional texture image from our bitmap

    gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_NEAREST);
    gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_NEAREST);
    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_CLAMP_TO_EDGE);

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

    // Clean up
    bitmap.recycle();
}

public void draw(GL10 gl, float x, float y) {
    //! TEXTURE
    gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[0]);
    gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
    gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
    //! TEXTURE

    gl.glColor4f(0.0f, 1.0f, 0.0f, 1.0f);
    gl.glTranslatef(x, y, 0.0f);
    gl.glVertexPointer(2, GL10.GL_FLOAT, 0, ShipVertexBuffer);

    //! TEXTURE
    gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, ShipTextureBuffer);
    //! TEXTURE

    gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, 4);

    //! TEXTURE
    gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
    gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
    //! TEXTURE
}

1 个答案:

答案 0 :(得分:0)

看起来你没有启用混音。

尝试在绘制调用之前添加glEnable(GL_BLEND)。使用glBlendFunc()glBlendEquation()更改混合样式以适合您想要的效果。