saarraz1
saarraz1

Reputation: 3029

OPEN GL Android - How to convert a Bitmap object to a drawable texture

I'm trying to implement a Grid of scrollable images in OPEN GL 2.0. I already have the view implemented using Canvas Drawing, but for performance reasons I decided to transition to OGL. In my implementation, at each frame I draw a list of Bitmap objects, each Bitmap is a cached row of image thumbnails. Now how do I go about converting those Bitmaps to textures I can use with OGL?

Upvotes: 4

Views: 5867

Answers (1)

James Coote
James Coote

Reputation: 1973

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

..where textureID is the unique ID for the texture (usually aquired from glGenTextures(), or otherwise from just keeping your own system of assigning each new texture a new ID number). bitmap is the Bitmap object.


An example of useage in my texture class:

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;
}


}

Upvotes: 5

Related Questions