load opengl Texture - Android android.opengl

Android examples for android.opengl:OpenGL Texture

Description

load opengl Texture

Demo Code


//package com.java2s;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory;
import android.opengl.GLES20;
import android.opengl.GLUtils;

public class Main {
    public static int loadTexture(final Context context,
            final int resourceID) {

        final int textureHandle[] = new int[1];

        GLES20.glGenTextures(1, textureHandle, 0);

        if (textureHandle[0] != 0) {
            final BitmapFactory.Options options = new BitmapFactory.Options();
            options.inScaled = false;//ww  w. jav  a 2  s  .  c  o  m

            final Bitmap bitmap = BitmapFactory.decodeResource(
                    context.getResources(), resourceID, options);

            bitmap.getConfig();
            int bitmapFormat = bitmap.getConfig() == Config.RGB_565 ? GLES20.GL_RGBA
                    : GLES20.GL_RGB;

            GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textureHandle[0]);
            GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D,
                    GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_NEAREST);
            GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D,
                    GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_NEAREST);

            // Load the bitmap into the bound texture.
            GLUtils.texImage2D(GLES20.GL_TEXTURE_2D, 0, bitmap, 0);

            // Recycle the bitmap, since its data has been loaded into OpenGL.
            bitmap.recycle();
        }

        if (textureHandle[0] == 0) {
            throw new RuntimeException("Error loading texture.");
        }

        return textureHandle[0];
    }
}

Related Tutorials