Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.android;
- /* Android Texture2D Class - A dead simple OpenGL ES 2.0 texture class
- * http://msdn.microsoft.com/en-us/library/bb195312(v=xnagamestudio.31).aspx
- *
- * Copyright (C) 2013 Mykel Aikon
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program. If not, see <http://www.gnu.org/licenses/>
- */
- import android.graphics.Bitmap;
- import android.graphics.BitmapFactory;
- import android.opengl.GLES20;
- import android.opengl.GLUtils;
- import java.io.InputStream;
- import java.io.IOException;
- public class Texture2D {
- String fileName;
- int textureHandle, width, height;
- public Texture2D() {
- fileName = "";
- textureHandle = -1;
- width = -1;
- height = -1;
- }
- public static Texture2D fromFile(String fileName) throws IOException {
- Texture2D tmp = new Texture2D();
- // Open the file and decode the stream
- InputStream bitmapStream = Assets.manager.open(fileName);
- Bitmap bitmap = BitmapFactory.decodeStream(bitmapStream);
- //Get a new texture handle
- int[] tmpHandles = new int[1];
- GLES20.glGenTextures(1, tmpHandles, 0);
- // Bind the handle to a GL_TEXTURE_2D
- GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, tmpHandles[0]);
- // Setup the parameters for the texture
- 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);
- GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_T, GLES20.GL_CLAMP_TO_EDGE);
- // Load the file into memory
- GLUtils.texImage2D(GLES20.GL_TEXTURE_2D, 0, bitmap, 0);
- // Setup the Texture2D fields
- tmp.fileName = fileName;
- tmp.textureHandle = tmpHandles[0];
- tmp.width = bitmap.getWidth();
- tmp.height = bitmap.getHeight();
- // Return the Texture2D
- return tmp;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment