Exemplo n.º 1
0
  private void uploadToGL(GL11 gl) throws GLOutOfMemoryException {
    Bitmap bitmap = getBitmap();
    int glError = GL11.GL_NO_ERROR;
    if (bitmap != null) {
      int[] textureId = new int[1];
      try {
        // Define a vertically flipped crop rectangle for
        // OES_draw_texture.
        int width = bitmap.getWidth();
        int height = bitmap.getHeight();
        int[] cropRect = {0, height, width, -height};

        // Upload the bitmap to a new texture.
        gl.glGenTextures(1, textureId, 0);
        gl.glBindTexture(GL11.GL_TEXTURE_2D, textureId[0]);
        gl.glTexParameteriv(GL11.GL_TEXTURE_2D, GL11Ext.GL_TEXTURE_CROP_RECT_OES, cropRect, 0);
        gl.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_S, GL11.GL_CLAMP_TO_EDGE);
        gl.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_T, GL11.GL_CLAMP_TO_EDGE);
        gl.glTexParameterf(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_LINEAR);
        gl.glTexParameterf(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_LINEAR);

        int widthExt = Util.nextPowerOf2(width);
        int heightExt = Util.nextPowerOf2(height);
        int format = GLUtils.getInternalFormat(bitmap);
        int type = GLUtils.getType(bitmap);

        mTextureWidth = widthExt;
        mTextureHeight = heightExt;
        gl.glTexImage2D(GL11.GL_TEXTURE_2D, 0, format, widthExt, heightExt, 0, format, type, null);
        GLUtils.texSubImage2D(GL11.GL_TEXTURE_2D, 0, 0, 0, bitmap, format, type);
      } finally {
        freeBitmap(bitmap);
      }
      if (glError == GL11.GL_OUT_OF_MEMORY) {
        throw new GLOutOfMemoryException();
      }
      if (glError != GL11.GL_NO_ERROR) {
        mId = 0;
        mState = STATE_UNLOADED;
        throw new RuntimeException("Texture upload fail, glError " + glError);
      } else {
        // Update texture state.
        mGL = gl;
        mId = textureId[0];
        mState = BitmapTexture.STATE_LOADED;
      }
    } else {
      mState = STATE_ERROR;
      throw new RuntimeException("Texture load fail, no bitmap");
    }
  }
Exemplo n.º 2
0
  @Override
  public void draw(GL10 gl) {
    if (mTileBank.mTexture != null) {
      if (mTileBank.mTexture.mHWTextureID > -1) {
        if (mTileBank != null) {
          int TW = (int) (mTileBank.mTileWidth * mScale);
          int TH = (int) (mTileBank.mTileHeight * mScale);
          int ttdX = (mWidth / TW) + 1;
          int ttdY = (mHeight / TH) + 1;

          gl.glBindTexture(GL10.GL_TEXTURE_2D, mTileBank.mTexture.mHWTextureID);
          gl.glColor4f(mRed, mGreen, mBlue, mAlpha);

          for (int y = 0; y < ttdY; y++) {
            int py = y * TH - (((int) (mTop * mScale)) % TH);

            mTextureIV[3] = -mTileBank.mTileHeight; // (int) (-H/mScale); // Hcr
            for (int x = 0; x < ttdX; x++) {
              int tile = getTile(x + (int) (mLeft * mScale / TW), y + (int) (mTop * mScale / TH));
              int px = x * TW - (((int) (mLeft * mScale)) % TW);

              if ((tile >= 0) && (tile < mTileBank.mTilesCount)) {
                mTextureIV[0] =
                    (tile % mTileBank.mTilesColumns)
                        * (mTileBank.mTileWidth); // +(dx/mScale));// Ucr
                mTextureIV[1] =
                    (tile / mTileBank.mTilesColumns) * (mTileBank.mTileHeight)
                        + mTileBank.mTileHeight; // -(dy/mScale));// Vcr
                mTextureIV[2] = mTileBank.mTileWidth; // (int) (W/mScale); // Wcr
                ((GL11) gl)
                    .glTexParameteriv(
                        GL10.GL_TEXTURE_2D, GL11Ext.GL_TEXTURE_CROP_RECT_OES, mTextureIV, 0);
                ((GL11Ext) gl)
                    .glDrawTexfOES(
                        mPosition.mX + px,
                        AngleSurfaceView.roHeight - (mPosition.mY + py + TH),
                        mZ,
                        TW,
                        TH);
              }
            }
          }
        }
      } else mTileBank.mTexture.linkToGL(gl);
    }
    super.draw(gl);
  }
Exemplo n.º 3
0
  public void copyTexture2D(RawTexture texture, int x, int y, int width, int height)
      throws GLOutOfMemoryException {
    Matrix matrix = mTransformation.getMatrix();
    matrix.getValues(mMatrixValues);

    if (isMatrixRotatedOrFlipped(mMatrixValues)) {
      throw new IllegalArgumentException("cannot support rotated matrix");
    }
    float points[] = mapPoints(matrix, x, y + height, x + width, y);
    x = (int) points[0];
    y = (int) points[1];
    width = (int) points[2] - x;
    height = (int) points[3] - y;

    GL11 gl = mGL;
    int newWidth = Util.nextPowerOf2(width);
    int newHeight = Util.nextPowerOf2(height);
    int glError = GL11.GL_NO_ERROR;

    gl.glBindTexture(GL11.GL_TEXTURE_2D, texture.getId());

    int[] cropRect = {0, 0, width, height};
    gl.glTexParameteriv(GL11.GL_TEXTURE_2D, GL11Ext.GL_TEXTURE_CROP_RECT_OES, cropRect, 0);
    gl.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_S, GL11.GL_CLAMP_TO_EDGE);
    gl.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_T, GL11.GL_CLAMP_TO_EDGE);
    gl.glTexParameterf(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_LINEAR);
    gl.glTexParameterf(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_LINEAR);
    gl.glCopyTexImage2D(GL11.GL_TEXTURE_2D, 0, GL11.GL_RGBA, x, y, newWidth, newHeight, 0);
    glError = gl.glGetError();

    if (glError == GL11.GL_OUT_OF_MEMORY) {
      throw new GLOutOfMemoryException();
    }

    if (glError != GL11.GL_NO_ERROR) {
      throw new RuntimeException("Texture copy fail, glError " + glError);
    }

    texture.setSize(width, height);
    texture.setTextureSize(newWidth, newHeight);
  }
Exemplo n.º 4
0
 @Override
 public void glTexParameteriv(int target, int pname, int[] params, int offset) {
   gl.glTexParameteriv(target, pname, params, offset);
 }
Exemplo n.º 5
0
 @Override
 public void glTexParameteriv(int target, int pname, IntBuffer params) {
   gl.glTexParameteriv(target, pname, params);
 }