3D Views: Source Code
Page 8
Texture Helper
This Java code is modified from Learn OpenGL ES code posted on GitHub. The WebGL tutorials can be found at http://www.learnopengles.com.
Introduction
The TextureHelper
Java class loads a texture
from a resource ID.
I modified the code to allocate just one texture buffer
and delete that buffer when required.
Java Code
package com.seventhundersoftware.cubeview.common; import android.content.Context; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.opengl.GLES20; import android.opengl.GLUtils; import android.util.Log; /*** * Helps prepare images * as textures for OpenGL ES. */ public class TextureHelper { private static final String TAG = "TextureHelper"; final static int[] textureHandle = new int[1]; /*** * Load a bitmap as a texture. * @param context: Context of this app. * @param resourceId: Drawable number of the current * image to use as a texture map. * @return integer: the texture handle. */ public static int loadTexture(final Context context, final int resourceId) { // Just allocate memory for one texture once. if(textureHandle[0] == 0){ GLES20.glGenTextures(1, textureHandle, 0); } if (textureHandle[0] == 0) { throw new RuntimeException("Error generating texture."); } final BitmapFactory.Options options = new BitmapFactory.Options(); options.inScaled = false; // No pre-scaling // Read in the resource final Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), resourceId, options); // Bind to the texture in OpenGL GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textureHandle[0]); // Set filtering 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(); return textureHandle[0]; } /*** * Free the texture buffer. * The buffer's reused for each * texture map and view. * But be sure to free the resources * when the program's destroyed. */ public static void FreeTexture(){ if(textureHandle[0] != 0) { GLES20.glDeleteTextures(1, textureHandle, 0); Log.d(TAG,"FreeTexture()"); textureHandle[0] = 0; } } }
Summary
The TextureHelper
Java class loads a texture
from a resource ID.
I modified the code to allocate just one texture buffer
and delete that buffer when required.
Java & OpenGL ES
This set of pages include Android Java, combined with OpenGL ES, to display a set of simple three dimensional views. The views apply a unique, simple concept to render 3D backgrounds, without Skyboxes or time consuming shader switching. Each view's contained in one graphic, with room for other sprites and meshes.
Java and OpenGL ES source code was ported from WebGL with only minor changes.
The book, 3D Scenes: Learn WebGL Book 3
, explains implementation
and graphics preparation for the WebGL app, in detail.