package com.yalantis.starwars.utils.gl;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.opengl.GLUtils;

import static android.opengl.GLES20.GL_CLAMP_TO_EDGE;
import static android.opengl.GLES20.GL_NEAREST;
import static android.opengl.GLES20.GL_TEXTURE_2D;
import static android.opengl.GLES20.GL_TEXTURE_MAG_FILTER;
import static android.opengl.GLES20.GL_TEXTURE_MIN_FILTER;
import static android.opengl.GLES20.GL_TEXTURE_WRAP_S;
import static android.opengl.GLES20.GL_TEXTURE_WRAP_T;
import static android.opengl.GLES20.glBindTexture;
import static android.opengl.GLES20.glGenTextures;
import static android.opengl.GLES20.glTexParameteri;

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(final Bitmap bitmap) {
		final int[] textureHandle = new int[1];

		glGenTextures(1, textureHandle, 0);

		if (textureHandle[0] != 0) {
			// Bind to the texture in OpenGL
			glBindTexture(GL_TEXTURE_2D, textureHandle[0]);

			// Set filtering
			glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
			glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
			glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
			glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);

			// Load the bitmap into the bound texture.
			GLUtils.texImage2D(GL_TEXTURE_2D, 0, bitmap, 0);

//			Buffer byteBuffer = ByteBuffer.allocate(bitmap.getByteCount());
//			bitmap.copyPixelsToBuffer(byteBuffer);
//			glTexImage2D(GL_TEXTURE_2D, 0, GLES20.GL_RGB, bitmap.getWidth(), bitmap.getHeight(),
//					0, GLES20.GL_RGB, GLES20.GL_UNSIGNED_BYTE, byteBuffer);

			// Recycle the bitmap, since its data has been loaded into OpenGL.
			bitmap.recycle();
		} else {
			throw new RuntimeException("Error loading texture.");
		}

		return textureHandle[0];
	}

	public static int loadTexture2(final Bitmap bitmap) {
		final int[] textureHandle = new int[1];

		glGenTextures(1, textureHandle, 0);

		if (textureHandle[0] != 0) {
			// Bind to the texture in OpenGL
			glBindTexture(GL_TEXTURE_2D, textureHandle[0]);

			// Set filtering
			glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
			glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
			glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
			glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);

			// Load the bitmap into the bound texture.
			GLUtils.texSubImage2D(GL_TEXTURE_2D, 0, 0, 0, bitmap);

//			Buffer byteBuffer = ByteBuffer.allocate(bitmap.getByteCount());
//			bitmap.copyPixelsToBuffer(byteBuffer);
//			glTexImage2D(GL_TEXTURE_2D, 0, GLES20.GL_RGB, bitmap.getWidth(), bitmap.getHeight(),
//					0, GLES20.GL_RGB, GLES20.GL_UNSIGNED_BYTE, byteBuffer);

			// Recycle the bitmap, since its data has been loaded into OpenGL.
			bitmap.recycle();
		} else {
			throw new RuntimeException("Error loading texture.");
		}

		return textureHandle[0];
	}
}

@@ -38,40 +38,8 @@
 l;%0A%0A
-import android.content.Context;%0A
 impo
@@ -85,30 +85,21 @@
 oid.
-graphics.BitmapFactory
+opengl.GLES20
 ;%0Aim
@@ -128,16 +128,43 @@
 Utils;%0A%0A
+import timber.log.Timber;%0A%0A
 import s
@@ -724,333 +724,8 @@
 er %7B
-%0A%09public static int loadTexture(final Context context, final int resourceId) %7B%0A%09%09final BitmapFactory.Options options = new BitmapFactory.Options();%0A%09%09options.inScaled = false;%09// No pre-scaling%0A%09%09final Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), resourceId, options);%0A%09%09return loadTexture(bitmap);%0A%09%7D
 %0A%0A%09p
@@ -1385,1513 +1385,253 @@
 );%0A%0A
-//%09%09%09Buffer byteBuffer = ByteBuffer.allocate(bitmap.getByteCount());%0A//%09%09%09bitmap.copyPixelsToBuffer(byteBuffer);%0A//%09%09%09glTexImage2D(GL_TEXTURE_2D, 0, GLES20.GL_RGB, bitmap.getWidth(), bitmap.getHeight(),%0A//%09%09%09%09%090, GLES20.GL_RGB, GLES20.GL_UNSIGNED_BYTE, byteBuffer);%0A%0A%09%09%09// Recycle the bitmap, since its data has been loaded into OpenGL.%0A%09%09%09bitmap.recycle();%0A%09%09%7D else %7B%0A%09%09%09throw new RuntimeException(%22Error loading texture.%22);%0A%09%09%7D%0A%0A%09%09return textureHandle%5B0%5D;%0A%09%7D%0A%0A%09public static int loadTexture2(final Bitmap bitmap) %7B%0A%09%09final int%5B%5D textureHandle = new int%5B1%5D;%0A%0A%09%09glGenTextures(1, textureHandle, 0);%0A%0A%09%09if (textureHandle%5B0%5D != 0) %7B%0A%09%09%09// Bind to the texture in OpenGL%0A%09%09%09glBindTexture(GL_TEXTURE_2D, textureHandle%5B0%5D);%0A%0A%09%09%09// Set filtering%0A%09%09%09glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);%0A%09%09%09glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);%0A%09%09%09glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);%0A%09%09%09glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);%0A%0A%09%09%09// Load the bitmap into the bound texture.%0A%09%09%09GLUtils.texSubImage2D(GL_TEXTURE_2D, 0, 0, 0, bitmap);%0A%0A//%09%09%09Buffer byteBuffer = ByteBuffer.allocate(bitmap.getByteCount());%0A//%09%09%09bitmap.copyPixelsToBuffer(byteBuffer);%0A//%09%09%09glTexImage2D(GL_TEXTURE_2D, 0, GLES20.GL_RGB, bitmap.getWidth(), bitmap.getHeight(),%0A//%09%09%09%09%090, GLES20.GL_RGB, GLES20.GL_UNSIGNED_BYTE, byteBuffer);%0A%0A%09%09%09// Recycle the bitmap, since its data has been loaded into OpenGL.%0A%09%09%09bitmap.recycle();%0A%09%09%7D else %7B%0A%09%09%09throw
+%09%09%09// Recycle the bitmap, since its data has been loaded into OpenGL.%0A%09%09%09bitmap.recycle();%0A%09%09%7D else %7B%0A%09%09%09int errorCode = GLES20.glGetError();%0A            String errorString = GLUtils.getEGLErrorString(errorCode);%0A            RuntimeException e =
  new
@@ -1652,33 +1652,70 @@
 ion(
-%22E
+e
 rror
- loading texture.%22)
+Code + %22 %22 + errorString);%0A%09%09%09Timber.e(e, %22%22);%0A%09%09%09throw e
 ;%0A%09%09
@@ -1747,10 +1747,11 @@
 %5B0%5D;%0A%09%7D%0A
+%0A
 %7D%0A

Enhance texture error reporting & clean up