11package com .yalantis .starwars .utils .gl ;
22
3- import android .content .Context ;
43import android .graphics .Bitmap ;
5- import android .graphics . BitmapFactory ;
4+ import android .opengl . GLES20 ;
65import android .opengl .GLUtils ;
76
7+ import timber .log .Timber ;
8+
89import static android .opengl .GLES20 .GL_CLAMP_TO_EDGE ;
910import static android .opengl .GLES20 .GL_NEAREST ;
1011import static android .opengl .GLES20 .GL_TEXTURE_2D ;
1718import static android .opengl .GLES20 .glTexParameteri ;
1819
1920public class TextureHelper {
20- public static int loadTexture (final Context context , final int resourceId ) {
21- final BitmapFactory .Options options = new BitmapFactory .Options ();
22- options .inScaled = false ; // No pre-scaling
23- final Bitmap bitmap = BitmapFactory .decodeResource (context .getResources (), resourceId , options );
24- return loadTexture (bitmap );
25- }
2621
2722 public static int loadTexture (final Bitmap bitmap ) {
2823 final int [] textureHandle = new int [1 ];
@@ -42,49 +37,17 @@ public static int loadTexture(final Bitmap bitmap) {
4237 // Load the bitmap into the bound texture.
4338 GLUtils .texImage2D (GL_TEXTURE_2D , 0 , bitmap , 0 );
4439
45- // Buffer byteBuffer = ByteBuffer.allocate(bitmap.getByteCount());
46- // bitmap.copyPixelsToBuffer(byteBuffer);
47- // glTexImage2D(GL_TEXTURE_2D, 0, GLES20.GL_RGB, bitmap.getWidth(), bitmap.getHeight(),
48- // 0, GLES20.GL_RGB, GLES20.GL_UNSIGNED_BYTE, byteBuffer);
49-
5040 // Recycle the bitmap, since its data has been loaded into OpenGL.
5141 bitmap .recycle ();
5242 } else {
53- throw new RuntimeException ("Error loading texture." );
43+ int errorCode = GLES20 .glGetError ();
44+ String errorString = GLUtils .getEGLErrorString (errorCode );
45+ RuntimeException e = new RuntimeException (errorCode + " " + errorString );
46+ Timber .e (e , "" );
47+ throw e ;
5448 }
5549
5650 return textureHandle [0 ];
5751 }
5852
59- public static int loadTexture2 (final Bitmap bitmap ) {
60- final int [] textureHandle = new int [1 ];
61-
62- glGenTextures (1 , textureHandle , 0 );
63-
64- if (textureHandle [0 ] != 0 ) {
65- // Bind to the texture in OpenGL
66- glBindTexture (GL_TEXTURE_2D , textureHandle [0 ]);
67-
68- // Set filtering
69- glTexParameteri (GL_TEXTURE_2D , GL_TEXTURE_MIN_FILTER , GL_NEAREST );
70- glTexParameteri (GL_TEXTURE_2D , GL_TEXTURE_MAG_FILTER , GL_NEAREST );
71- glTexParameteri (GL_TEXTURE_2D , GL_TEXTURE_WRAP_S , GL_CLAMP_TO_EDGE );
72- glTexParameteri (GL_TEXTURE_2D , GL_TEXTURE_WRAP_T , GL_CLAMP_TO_EDGE );
73-
74- // Load the bitmap into the bound texture.
75- GLUtils .texSubImage2D (GL_TEXTURE_2D , 0 , 0 , 0 , bitmap );
76-
77- // Buffer byteBuffer = ByteBuffer.allocate(bitmap.getByteCount());
78- // bitmap.copyPixelsToBuffer(byteBuffer);
79- // glTexImage2D(GL_TEXTURE_2D, 0, GLES20.GL_RGB, bitmap.getWidth(), bitmap.getHeight(),
80- // 0, GLES20.GL_RGB, GLES20.GL_UNSIGNED_BYTE, byteBuffer);
81-
82- // Recycle the bitmap, since its data has been loaded into OpenGL.
83- bitmap .recycle ();
84- } else {
85- throw new RuntimeException ("Error loading texture." );
86- }
87-
88- return textureHandle [0 ];
89- }
9053}
0 commit comments