Creates an OpenGl texture, filling it with the given bitmap's pixels. - Android android.opengl

Android examples for android.opengl:OpenGL Texture

Description

Creates an OpenGl texture, filling it with the given bitmap's pixels.

Demo Code

/*//from  ww w.j a va 2  s .com
 * Created by Zahari Pastarmadjiev.
 * Copyright (c) 2013 Wacom. All rights reserved.
 */
import java.io.IOException;
import java.io.InputStream;
import javax.microedition.khronos.egl.EGL10;
import javax.microedition.khronos.egl.EGLContext;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.opengl.GLES20;
import android.opengl.GLUtils;

public class Main{
    public static Logger logger = new Logger(OpenGLUtils.class);
    /**
     * Constant: no OpenGl texture
     */
    public static int NO_TEXTURE_ID = 0;
    /**
     * Creates an OpenGl texture, filling it with the given bitmap's pixels.
     * @param bitmap a bitmap object
     * @param sampleMode a value to be used as {@link GLES20#GL_TEXTURE_MIN_FILTER} and {@link GLES20#GL_TEXTURE_MAG_FILTER} texture parameter
     * @param wrapMode a value to be used as {@link GLES20#GL_TEXTURE_WRAP_S} and {@link GLES20#GL_TEXTURE_WRAP_T} texture parameter
     * @return id of the allocated OpenGl texture
     */
    public static int bitmapToOpenGL(Bitmap bitmap, int sampleMode,
            int wrapMode) {
        return bitmapToOpenGL(bitmap, NO_TEXTURE_ID, sampleMode, wrapMode);
    }
    /**
     * Creates or overwrites an OpenGl texture, filling it with the given bitmap's pixels.
     * @param bitmap a bitmap object
     * @param textureId OpenGl texture id. If this value equals {@link #NO_TEXTURE_ID}, a new OpenGl texture will be created.
     * @param sampleMode a value to be used as {@link GLES20#GL_TEXTURE_MIN_FILTER} and {@link GLES20#GL_TEXTURE_MAG_FILTER} texture parameter
     * @param wrapMode a value to be used as {@link GLES20#GL_TEXTURE_WRAP_S} and {@link GLES20#GL_TEXTURE_WRAP_T} texture parameter
     * @return OpengGl texture id
     */
    public static int bitmapToOpenGL(Bitmap bitmap, int textureId,
            int sampleMode, int wrapMode) {
        int textures[] = new int[1];

        if (textureId == NO_TEXTURE_ID) {
            GLES20.glGenTextures(1, textures, 0);
            textureId = textures[0];
        }
        GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textureId);

        GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D,
                GLES20.GL_TEXTURE_WRAP_S, wrapMode);
        GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D,
                GLES20.GL_TEXTURE_WRAP_T, wrapMode);
        GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D,
                GLES20.GL_TEXTURE_MAG_FILTER, sampleMode);
        GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D,
                GLES20.GL_TEXTURE_MIN_FILTER, sampleMode);

        if (Logger.LOG_ENABLED)
            logger.i("bitmapToOpenGL: " + bitmap.getWidth() + ","
                    + bitmap.getHeight() + " | " + wrapMode + ","
                    + sampleMode);

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

        if (GLES20.glGetError() > 0) {
            Utils.alertAndAssert("bitmapToOpenGL failed!");
        }

        return textureId;
    }
}

Related Tutorials