Example #1
0
 public void setNative(int[] pixels, int x, int y, int w, int h) {
   updatePixelBuffer(pixels);
   setNative(pixelBuffer, x, y, w, h);
   releasePixelBuffer();
 }
Example #2
0
  public void set(int[] pixels, int x, int y, int w, int h, int format) {
    if (pixels == null) {
      PGraphics.showWarning("The pixels array is null.");
      return;
    }
    if (pixels.length < w * h) {
      PGraphics.showWarning(
          "The pixel array has a length of "
              + pixels.length
              + ", but it should be at least "
              + w * h);
      return;
    }

    if (pixels.length == 0) {
      // Nothing to do (means that w == h == 0) but not an erroneous situation
      return;
    }

    boolean enabledTex = false;
    if (!pgl.texturingIsEnabled(glTarget)) {
      pgl.enableTexturing(glTarget);
      enabledTex = true;
    }
    pgl.bindTexture(glTarget, glName);

    if (usingMipmaps) {
      if (PGraphicsOpenGL.autoMipmapGenSupported) {
        // Automatic mipmap generation.
        loadPixels(w * h);
        convertToRGBA(pixels, format, w, h);
        updatePixelBuffer(rgbaPixels);
        pgl.texSubImage2D(glTarget, 0, x, y, w, h, PGL.RGBA, PGL.UNSIGNED_BYTE, pixelBuffer);
        pgl.generateMipmap(glTarget);
      } else {
        // TODO: finish manual mipmap generation, replacing Bitmap with AWT's BufferedImage,
        // making it work in npot textures (embed npot tex into larger pot tex?), subregions,
        // and moving GLUtils.texImage2D (originally from Android SDK) into PGL.
        // Actually, this whole code should go into PGL, so the Android implementation can
        // use Bitmap, and desktop use BufferedImage.

        /*
          if (w != width || h != height) {
            System.err.println("Sorry but I don't know how to generate mipmaps for a subregion.");
            return;
          }

          // Code by Mike Miller obtained from here:
          // http://insanitydesign.com/wp/2009/08/01/android-opengl-es-mipmaps/
          int w0 = glWidth;
          int h0 = glHeight;
          int[] argbPixels = new int[w0 * h0];
          convertToARGB(pixels, argbPixels, format);
          int level = 0;
          int denom = 1;

          // We create a Bitmap because then we use its built-in filtered downsampling
          // functionality.
          Bitmap bitmap = Bitmap.createBitmap(w0, h0, Config.ARGB_8888);
          bitmap.setPixels(argbPixels, 0, w0, 0, 0, w0, h0);

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

            // We are done.
            if (w0 == 1 && h0 == 1) {
              break;
            }

            // Increase the mipmap level
            level++;
            denom *= 2;

            // Downsampling bitmap. We must eventually arrive to the 1x1 level,
            // and if the width and height are different, there will be a few 1D
            // texture levels just before.
            // This update formula also allows for NPOT resolutions.
            w0 = PApplet.max(1, PApplet.floor((float)glWidth / denom));
            h0 = PApplet.max(1, PApplet.floor((float)glHeight / denom));
            // (see getScaledInstance in AWT Image)
            Bitmap bitmap2 = Bitmap.createScaledBitmap(bitmap, w0, h0, true);

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

        loadPixels(w * h);
        convertToRGBA(pixels, format, w, h);
        updatePixelBuffer(rgbaPixels);
        pgl.texSubImage2D(glTarget, 0, x, y, w, h, PGL.RGBA, PGL.UNSIGNED_BYTE, pixelBuffer);
      }
    } else {
      loadPixels(w * h);
      convertToRGBA(pixels, format, w, h);
      updatePixelBuffer(rgbaPixels);
      pgl.texSubImage2D(glTarget, 0, x, y, w, h, PGL.RGBA, PGL.UNSIGNED_BYTE, pixelBuffer);
    }

    pgl.bindTexture(glTarget, 0);
    if (enabledTex) {
      pgl.disableTexturing(glTarget);
    }

    releasePixelBuffer();
    releaseRGBAPixels();

    updateTexels(x, y, w, h);
  }