Esempio n. 1
0
  public static int loadTexture(GLSurfaceView mActivityContext2, 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(mActivityContext2.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];
  }
Esempio n. 2
0
  public static int LoadTexture(GLSurfaceView view, int imgResID) {
    Log.d("Utils", "Loadtexture");
    Bitmap img = null;
    int textures[] = new int[1];
    try {
      img = BitmapFactory.decodeResource(view.getResources(), imgResID);
      GLES20.glGenTextures(1, textures, 0);

      GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textures[0]);
      GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_LINEAR);
      GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_LINEAR);

      GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, img, 0);
      Log.d("LoadTexture", "Loaded texture" + ":H:" + img.getHeight() + ":W:" + img.getWidth());
    } catch (Exception e) {
      Log.d("LoadTexture", e.toString() + ":" + e.getMessage() + ":" + e.getLocalizedMessage());
    }
    img.recycle();
    return textures[0];
  }