/**
  * Sets the OpenGL integer texture parameter for the texture's target. This gives control over
  * parameters such as GL_TEXTURE_WRAP_S and GL_TEXTURE_WRAP_T, which by default are set to
  * GL_CLAMP_TO_EDGE if OpenGL 1.2 is supported on the current platform and GL_CLAMP if not. 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 setTexParameteri(int parameterName, int value) {
   bind();
   GL gl = GLU.getCurrentGL();
   gl.glTexParameteri(target, parameterName, value);
 }
  /**
   * Updates the content area of the specified target of this texture using the data in the given
   * image. In general this is intended for construction of cube maps.
   *
   * @throws GLException if no OpenGL context was current or if any OpenGL-related errors occurred
   */
  public void updateImage(TextureData data, int target) throws GLException {
    GL gl = GLU.getCurrentGL();

    imgWidth = data.getWidth();
    imgHeight = data.getHeight();
    aspectRatio = (float) imgWidth / (float) imgHeight;
    mustFlipVertically = data.getMustFlipVertically();

    int texTarget = 0;
    int texParamTarget = this.target;

    // See whether we have automatic mipmap generation support
    boolean haveAutoMipmapGeneration =
        (gl.isExtensionAvailable("GL_VERSION_1_4")
            || gl.isExtensionAvailable("GL_SGIS_generate_mipmap"));

    // Indicate to the TextureData what functionality is available
    data.setHaveEXTABGR(gl.isExtensionAvailable("GL_EXT_abgr"));
    data.setHaveGL12(gl.isExtensionAvailable("GL_VERSION_1_2"));

    // Note that automatic mipmap generation doesn't work for
    // GL_ARB_texture_rectangle
    if ((!isPowerOfTwo(imgWidth) || !isPowerOfTwo(imgHeight)) && !haveNPOT(gl)) {
      haveAutoMipmapGeneration = false;
    }

    boolean expandingCompressedTexture = false;
    if (data.getMipmap() && !haveAutoMipmapGeneration) {
      // GLU always scales the texture's dimensions to be powers of
      // two. It also doesn't really matter exactly what the texture
      // width and height are because the texture coords are always
      // between 0.0 and 1.0.
      imgWidth = nextPowerOfTwo(imgWidth);
      imgHeight = nextPowerOfTwo(imgHeight);
      texWidth = imgWidth;
      texHeight = imgHeight;
      texTarget = GL.GL_TEXTURE_2D;
    } else if ((isPowerOfTwo(imgWidth) && isPowerOfTwo(imgHeight)) || haveNPOT(gl)) {
      if (DEBUG) {
        if (isPowerOfTwo(imgWidth) && isPowerOfTwo(imgHeight)) {
          System.err.println("Power-of-two texture");
        } else {
          System.err.println("Using GL_ARB_texture_non_power_of_two");
        }
      }

      texWidth = imgWidth;
      texHeight = imgHeight;
      texTarget = GL.GL_TEXTURE_2D;
    } else if (haveTexRect(gl) && !data.isDataCompressed()) {
      // GL_ARB_texture_rectangle does not work for compressed textures
      if (DEBUG) {
        System.err.println("Using GL_ARB_texture_rectangle");
      }

      texWidth = imgWidth;
      texHeight = imgHeight;
      texTarget = GL.GL_TEXTURE_RECTANGLE_ARB;
    } else {
      // If we receive non-power-of-two compressed texture data and
      // don't have true hardware support for compressed textures, we
      // can fake this support by producing an empty "compressed"
      // texture image, using glCompressedTexImage2D with that to
      // allocate the texture, and glCompressedTexSubImage2D with the
      // incoming data.
      if (data.isDataCompressed()) {
        if (data.getMipmapData() != null) {

          // We don't currently support expanding of compressed,
          // mipmapped non-power-of-two textures to the nearest power
          // of two; the obvious port of the non-mipmapped code didn't
          // work
          throw new GLException(
              "Mipmapped non-power-of-two compressed textures only supported on OpenGL 2.0 hardware (GL_ARB_texture_non_power_of_two)");
        }

        expandingCompressedTexture = true;
      }

      if (DEBUG) {
        System.err.println("Expanding texture to power-of-two dimensions");
      }

      if (data.getBorder() != 0) {
        throw new RuntimeException(
            "Scaling up a non-power-of-two texture which has a border won't work");
      }
      texWidth = nextPowerOfTwo(imgWidth);
      texHeight = nextPowerOfTwo(imgHeight);
      texTarget = GL.GL_TEXTURE_2D;
    }

    texParamTarget = texTarget;
    setImageSize(imgWidth, imgHeight, texTarget);

    if (target != 0) {
      // Allow user to override auto detection and skip bind step (for
      // cubemap construction)
      texTarget = target;
      if (this.target == 0) {
        throw new GLException("Override of target failed; no target specified yet");
      }
      texParamTarget = this.target;
      gl.glBindTexture(texParamTarget, texID);
    } else {
      gl.glBindTexture(texTarget, texID);
    }

    if (data.getMipmap() && !haveAutoMipmapGeneration) {
      int[] align = new int[1];
      gl.glGetIntegerv(GL.GL_UNPACK_ALIGNMENT, align, 0); // save alignment
      gl.glPixelStorei(GL.GL_UNPACK_ALIGNMENT, data.getAlignment());

      if (data.isDataCompressed()) {
        throw new GLException("May not request mipmap generation for compressed textures");
      }

      try {
        GLU glu = new GLU();
        glu.gluBuild2DMipmaps(
            texTarget,
            data.getInternalFormat(),
            data.getWidth(),
            data.getHeight(),
            data.getPixelFormat(),
            data.getPixelType(),
            data.getBuffer());
      } finally {
        gl.glPixelStorei(GL.GL_UNPACK_ALIGNMENT, align[0]); // restore alignment
      }
    } else {
      checkCompressedTextureExtensions(data);
      Buffer[] mipmapData = data.getMipmapData();
      if (mipmapData != null) {
        int width = texWidth;
        int height = texHeight;
        for (int i = 0; i < mipmapData.length; i++) {
          if (data.isDataCompressed()) {
            // Need to use glCompressedTexImage2D directly to allocate and fill this image
            // Avoid spurious memory allocation when possible
            gl.glCompressedTexImage2D(
                texTarget,
                i,
                data.getInternalFormat(),
                width,
                height,
                data.getBorder(),
                mipmapData[i].remaining(),
                mipmapData[i]);
          } else {
            // Allocate texture image at this level
            gl.glTexImage2D(
                texTarget,
                i,
                data.getInternalFormat(),
                width,
                height,
                data.getBorder(),
                data.getPixelFormat(),
                data.getPixelType(),
                null);
            updateSubImageImpl(data, texTarget, i, 0, 0, 0, 0, data.getWidth(), data.getHeight());
          }

          width = Math.max(width / 2, 1);
          height = Math.max(height / 2, 1);
        }
      } else {
        if (data.isDataCompressed()) {
          if (!expandingCompressedTexture) {
            // Need to use glCompressedTexImage2D directly to allocate and fill this image
            // Avoid spurious memory allocation when possible
            gl.glCompressedTexImage2D(
                texTarget,
                0,
                data.getInternalFormat(),
                texWidth,
                texHeight,
                data.getBorder(),
                data.getBuffer().capacity(),
                data.getBuffer());
          } else {
            ByteBuffer buf =
                DDSImage.allocateBlankBuffer(texWidth, texHeight, data.getInternalFormat());
            gl.glCompressedTexImage2D(
                texTarget,
                0,
                data.getInternalFormat(),
                texWidth,
                texHeight,
                data.getBorder(),
                buf.capacity(),
                buf);
            updateSubImageImpl(data, texTarget, 0, 0, 0, 0, 0, data.getWidth(), data.getHeight());
          }
        } else {
          if (data.getMipmap() && haveAutoMipmapGeneration) {
            // For now, only use hardware mipmapping for uncompressed 2D
            // textures where the user hasn't explicitly specified
            // mipmap data; don't know about interactions between
            // GL_GENERATE_MIPMAP and glCompressedTexImage2D
            gl.glTexParameteri(texParamTarget, GL.GL_GENERATE_MIPMAP, GL.GL_TRUE);
            usingAutoMipmapGeneration = true;
          }

          gl.glTexImage2D(
              texTarget,
              0,
              data.getInternalFormat(),
              texWidth,
              texHeight,
              data.getBorder(),
              data.getPixelFormat(),
              data.getPixelType(),
              null);
          updateSubImageImpl(data, texTarget, 0, 0, 0, 0, 0, data.getWidth(), data.getHeight());
        }
      }
    }

    int minFilter = (data.getMipmap() ? GL.GL_LINEAR_MIPMAP_LINEAR : GL.GL_LINEAR);
    int magFilter = GL.GL_LINEAR;
    int wrapMode = (gl.isExtensionAvailable("GL_VERSION_1_2") ? GL.GL_CLAMP_TO_EDGE : GL.GL_CLAMP);

    // REMIND: figure out what to do for GL_TEXTURE_RECTANGLE_ARB
    if (texTarget != GL.GL_TEXTURE_RECTANGLE_ARB) {
      gl.glTexParameteri(texParamTarget, GL.GL_TEXTURE_MIN_FILTER, minFilter);
      gl.glTexParameteri(texParamTarget, GL.GL_TEXTURE_MAG_FILTER, magFilter);
      gl.glTexParameteri(texParamTarget, GL.GL_TEXTURE_WRAP_S, wrapMode);
      gl.glTexParameteri(texParamTarget, GL.GL_TEXTURE_WRAP_T, wrapMode);
      if (this.target == GL.GL_TEXTURE_CUBE_MAP) {
        gl.glTexParameteri(texParamTarget, GL.GL_TEXTURE_WRAP_R, wrapMode);
      }
    }

    // Don't overwrite target if we're loading e.g. faces of a cube
    // map
    if ((this.target == 0)
        || (this.target == GL.GL_TEXTURE_2D)
        || (this.target == GL.GL_TEXTURE_RECTANGLE_ARB)) {
      this.target = texTarget;
    }

    // This estimate will be wrong for cube maps
    estimatedMemorySize = data.getEstimatedMemorySize();
  }