Android Open Source - GLaunchy3 Texture Helper






From Project

Back to project page GLaunchy3.

License

The source code is released under:

Apache License

If you think the Android project GLaunchy3 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.frak.glaunchy.common;
/*from  ww  w.  j  a  va2 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 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_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];
  }

  public static int loadTextureFromBitmap(final Context context, Bitmap bmp)
  {
    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

      // 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, bmp, 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.frak.glaunchy.ApplicationInfo.java
com.frak.glaunchy.LGLActivity.java
com.frak.glaunchy.MyGLRenderer.java
com.frak.glaunchy.common.ShapeBuilder.java
com.frak.glaunchy.common.TextureHelper.java
com.frak.glaunchy.opengl.GENogl.java
com.frak.glaunchy.opengl.MenuActivity.java