OPEN GL Android - 如何将Bitmap对象转换为可绘制的纹理

4

我正在尝试在OPEN GL 2.0中实现一个可滚动图像的网格。我已经使用Canvas Drawing实现了视图,但出于性能原因,我决定转换到OGL。

在我的实现中,在每个帧上,我绘制一系列位图对象,每个位图都是缓存的图像缩略图行。

现在,我该如何将这些位图转换为可以在OGL中使用的纹理?

1个回答

5
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textureID);
GLUtils.texImage2D(GLES20.GL_TEXTURE_2D, 0, bitmap, 0);

在我的纹理类中使用的示例:

..其中textureID是纹理的唯一标识符(通常从glGenTextures()获取,或者通过保持自己的分配每个新纹理一个新的ID号码的系统获得)。bitmap是位图对象。

public class Texture {

protected String name;
protected int textureID = -1;
protected String filename;

public Texture(String filename){
    this.filename = filename;
}

public void loadTexture(GL10 gl, Context context){

    String[] filenamesplit = filename.split("\\.");

    name = filenamesplit[filenamesplit.length-2];

    int[] textures = new int[1];
    //Generate one texture pointer...
    //GLES20.glGenTextures(1, textures, 0);

    // texturecount is just a public int in MyActivity extends Activity
    // I use this because I have issues with glGenTextures() not working                
    textures[0] = ((MyActivity)context).texturecount;
    ((MyActivity)context).texturecount++;

    GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textures[0]);

    //Create Nearest Filtered Texture
    GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_NEAREST);
    GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_LINEAR);

    //Different possible texture parameters, e.g. GLES20.GL_CLAMP_TO_EDGE
    GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_S, GLES20.GL_REPEAT);
    GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_T, GLES20.GL_REPEAT);

    Bitmap bitmap = FileUtil.openBitmap(name, context);

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

    bitmap.recycle();   

    textureID = textures[0];

}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public int getTextureID() {
    return textureID;
}

public void setTextureID(int textureID) {
    this.textureID = textureID;
}


}

你在每帧处理之前都会增加纹理吗? - user924

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接