neoaikon

Android Texture2D - A simple OpenGL ES 2.0 texture class

Mar 24th, 2013
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.47 KB | None | 0 0
  1. package com.example.android;
  2.  
  3. /* Android Texture2D Class - A dead simple OpenGL ES 2.0 texture class
  4.  * http://msdn.microsoft.com/en-us/library/bb195312(v=xnagamestudio.31).aspx
  5.  *
  6.  * Copyright (C) 2013  Mykel Aikon
  7.  * This program is free software: you can redistribute it and/or modify
  8.  * it under the terms of the GNU General Public License as published by
  9.  * the Free Software Foundation, either version 3 of the License, or
  10.  * (at your option) any later version.
  11.  *
  12.  * This program is distributed in the hope that it will be useful,
  13.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15.  * GNU General Public License for more details.
  16.  *
  17.  * You should have received a copy of the GNU General Public License
  18.  * along with this program.  If not, see <http://www.gnu.org/licenses/>
  19. */
  20.  
  21. import android.graphics.Bitmap;
  22. import android.graphics.BitmapFactory;
  23. import android.opengl.GLES20;
  24. import android.opengl.GLUtils;
  25.  
  26. import java.io.InputStream;
  27. import java.io.IOException;
  28.  
  29. public class Texture2D {
  30.  
  31.     String fileName;
  32.     int textureHandle, width, height;
  33.    
  34.     public Texture2D() {
  35.         fileName = "";
  36.         textureHandle = -1;
  37.         width = -1;
  38.         height = -1;
  39.     }
  40.    
  41.     public static Texture2D fromFile(String fileName) throws IOException {
  42.         Texture2D tmp = new Texture2D();
  43.        
  44.         // Open the file and decode the stream
  45.         InputStream bitmapStream = Assets.manager.open(fileName);
  46.         Bitmap bitmap = BitmapFactory.decodeStream(bitmapStream);
  47.        
  48.         //Get a new texture handle
  49.         int[] tmpHandles = new int[1];
  50.         GLES20.glGenTextures(1, tmpHandles, 0);
  51.        
  52.         // Bind the handle to a GL_TEXTURE_2D
  53.         GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, tmpHandles[0]);
  54.        
  55.         // Setup the parameters for the texture
  56.         GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_LINEAR);
  57.         GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_LINEAR);
  58.         GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_S, GLES20.GL_CLAMP_TO_EDGE);
  59.         GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_T, GLES20.GL_CLAMP_TO_EDGE);
  60.        
  61.         // Load the file into memory
  62.         GLUtils.texImage2D(GLES20.GL_TEXTURE_2D, 0, bitmap, 0);
  63.        
  64.         // Setup the Texture2D fields
  65.         tmp.fileName = fileName;
  66.         tmp.textureHandle = tmpHandles[0];
  67.         tmp.width = bitmap.getWidth();
  68.         tmp.height = bitmap.getHeight();
  69.        
  70.         // Return the Texture2D
  71.         return tmp;
  72.     }
  73. }
Advertisement
Add Comment
Please, Sign In to add comment