Android Open Source - Texample2 Texture Helper






From Project

Back to project page Texample2.

License

The source code is released under:

CC0 1.0 Universal http://creativecommons.org/publicdomain/zero/1.0/legalcode

If you think the Android project Texample2 listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.android.texample2;
//from  w ww .j a  v  a  2 s  .c om
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.opengl.GLES20;
import android.opengl.GLUtils;

public class TextureHelper {
  public static int loadTexture(final Context context, final int resourceId) {
    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inScaled = false; // No pre-scaling
    final Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), resourceId, options);
    
    return loadTexture(bitmap);
  }
  public static int loadTexture(Bitmap bitmap)
  {
      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;   // 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_LINEAR);
          GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_LINEAR);
          GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_S, GLES20.GL_CLAMP_TO_EDGE );  // Set U Wrapping
          GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_T, GLES20.GL_CLAMP_TO_EDGE );  // Set V Wrapping

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




Java Source Code List

com.android.texample2.AttribVariable.java
com.android.texample2.AttribVariable.java
com.android.texample2.GLText.java
com.android.texample2.GLText.java
com.android.texample2.SpriteBatch.java
com.android.texample2.SpriteBatch.java
com.android.texample2.Texample2Renderer.java
com.android.texample2.Texample2Renderer.java
com.android.texample2.Texample2.java
com.android.texample2.Texample2.java
com.android.texample2.TextureHelper.java
com.android.texample2.TextureHelper.java
com.android.texample2.TextureRegion.java
com.android.texample2.TextureRegion.java
com.android.texample2.Triangle.java
com.android.texample2.Triangle.java
com.android.texample2.Utilities.java
com.android.texample2.Utilities.java
com.android.texample2.Vertices.java
com.android.texample2.Vertices.java
com.android.texample2.programs.BatchTextProgram.java
com.android.texample2.programs.BatchTextProgram.java
com.android.texample2.programs.Program.java
com.android.texample2.programs.Program.java