Exemplo n.º 1
0
  public static int loadTiledTexture(int file) {
    GL.glGenTextures(1, singleID, 0);

    GL.glBindTexture(GL.GL_TEXTURE_2D, singleID[0]);

    GL.glTexParameterf(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MIN_FILTER, GL.GL_LINEAR);
    GL.glTexParameterf(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MAG_FILTER, GL.GL_LINEAR);
    GL.glTexParameterf(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_WRAP_S, GL.GL_REPEAT);
    GL.glTexParameterf(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_WRAP_T, GL.GL_REPEAT);

    InputStream is = Resource.getResInputStream(file);
    Bitmap bitmap = null;
    try {
      bitmap = BitmapFactory.decodeStream(is);
    } catch (Exception e) {
      Log.e(TAG, "Could not read texture file " + file, e);
      return 0;
    } finally {
      try {
        is.close();
      } catch (Exception e) {
        Log.e(TAG, "Error closing stream", e);
      }
    }

    if (bitmap != null) {
      GLUtils.texImage2D(GL.GL_TEXTURE_2D, 0, bitmap, 0);
      bitmap.recycle();
    } else {
      Log.e(TAG, "Could not read bitmap " + file);
      return 0;
    }

    return singleID[0];
  }
Exemplo n.º 2
0
  public static int loadMipMappedTexture(Bitmap bitmap) {

    GL.glGenTextures(1, singleID, 0);

    int textureID = singleID[0];
    GL.glBindTexture(GL.GL_TEXTURE_2D, textureID);

    GL.glTexParameterf(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MIN_FILTER, GL.GL_LINEAR_MIPMAP_LINEAR);
    GL.glTexParameterf(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MAG_FILTER, GL.GL_LINEAR);
    GL.glTexParameterf(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_WRAP_S, GL.GL_REPEAT);
    GL.glTexParameterf(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_WRAP_T, GL.GL_REPEAT);

    if (autoMipMap) {
      // To use automatic mipmap generation
      GL.glTexParameterf(GL.GL_TEXTURE_2D, GL.GL_GENERATE_MIPMAP, GL.GL_TRUE);
      GLUtils.texImage2D(GL.GL_TEXTURE_2D, 0, bitmap, 0);
      return textureID;
    }

    // Original code is buggy on certain devices ("HTC Desire S", "Motorola Milestone (Droid) 3
    // XT860 Android 2.3.5 Gingerbread")
    // Need conversion as a workaround
    Bitmap oldBitmap = bitmap;
    bitmap = oldBitmap.copy(Bitmap.Config.ARGB_8888, true);
    if (bitmap == null) return 0;
    oldBitmap.recycle();

    int level = 0;
    int height = bitmap.getHeight();
    int width = bitmap.getWidth();

    while (height >= 1 || width >= 1) {
      // First of all, generate the texture from our bitmap and set it to the according level
      GLUtils.texImage2D(GL.GL_TEXTURE_2D, level, bitmap, 0);

      if (height == 1 && width == 1) break;

      // Increase the mipmap level
      level++;

      height /= 2;
      if (height == 0) height = 1;
      width /= 2;
      if (width == 0) width = 1;
      Bitmap bitmap2 = Bitmap.createScaledBitmap(bitmap, width, height, true);

      // Clean up
      bitmap.recycle();
      bitmap = bitmap2;
    }

    return textureID;
  }
  /**
   * Sets a specified texture's OpenGL <code>Texture</code> parameters.
   *
   * @param dc the current draw context.
   * @param texture the texture whose parameters to set.
   */
  protected void setTextureParameters(DrawContext dc, Texture texture) {
    // Enable the appropriate mip-mapping texture filters if the caller has specified that
    // mip-mapping should be
    // enabled, and the texture itself supports mip-mapping.
    boolean useMipMapFilter =
        this.useMipMaps
            && (this.getTextureData().getMipmapData() != null
                || texture.isUsingAutoMipmapGeneration());

    GL gl = dc.getGL();
    gl.glTexParameteri(
        GL.GL_TEXTURE_2D,
        GL.GL_TEXTURE_MIN_FILTER,
        useMipMapFilter ? GL.GL_LINEAR_MIPMAP_LINEAR : GL.GL_LINEAR);
    gl.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MAG_FILTER, GL.GL_LINEAR);
    gl.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_WRAP_S, GL.GL_CLAMP_TO_EDGE);
    gl.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_WRAP_T, GL.GL_CLAMP_TO_EDGE);

    if (this.isUseAnisotropy() && useMipMapFilter) {
      double maxAnisotropy = dc.getGLRuntimeCapabilities().getMaxTextureAnisotropy();
      if (dc.getGLRuntimeCapabilities().isUseAnisotropicTextureFilter() && maxAnisotropy >= 2.0) {
        gl.glTexParameterf(
            GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MAX_ANISOTROPY_EXT, (float) maxAnisotropy);
      }
    }
  }
Exemplo n.º 4
0
  public static boolean loadTexture(int glId, Bitmap bitmap) {
    GL.glBindTexture(GL.GL_TEXTURE_2D, glId);

    GL.glTexParameterf(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MIN_FILTER, GL.GL_LINEAR);
    GL.glTexParameterf(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MAG_FILTER, GL.GL_LINEAR);
    GL.glTexParameterf(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_WRAP_S, GL.GL_REPEAT);
    GL.glTexParameterf(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_WRAP_T, GL.GL_REPEAT);

    if (bitmap != null) {
      GLUtils.texImage2D(GL.GL_TEXTURE_2D, 0, bitmap, 0);
      bitmap.recycle();
    } else {
      Log.e(TAG, "Bitmap null!");
      return false;
    }

    return true;
  }
 /**
  * Sets the OpenGL floating-point texture parameter for the texture's target. This gives control
  * over parameters such as GL_TEXTURE_MAX_ANISOTROPY_EXT. Causes this texture to be bound to the
  * current texture state.
  *
  * @throws GLException if no OpenGL context was current or if any OpenGL-related errors occurred
  */
 public void setTexParameterf(int parameterName, float value) {
   bind();
   GL gl = GLU.getCurrentGL();
   gl.glTexParameterf(target, parameterName, value);
 }