Exemplo n.º 1
0
 private boolean bindTexture(BasicTexture texture) {
   if (!texture.onBind(this)) return false;
   int target = texture.getTarget();
   mGLState.setTextureTarget(target);
   mGL.glBindTexture(target, texture.getId());
   return true;
 }
Exemplo n.º 2
0
 // unloadTexture and deleteBuffer can be called from the finalizer thread,
 // so we synchronized on the mUnboundTextures object.
 public boolean unloadTexture(BasicTexture t) {
   synchronized (mUnboundTextures) {
     if (!t.isLoaded()) return false;
     mUnboundTextures.add(t.mId);
     return true;
   }
 }
Exemplo n.º 3
0
  public void drawMesh(
      BasicTexture tex, int x, int y, int xyBuffer, int uvBuffer, int indexBuffer, int indexCount) {
    float alpha = mAlpha;
    if (!bindTexture(tex)) return;

    mGLState.setBlendEnabled(mBlendEnabled && (!tex.isOpaque() || alpha < OPAQUE_ALPHA));
    mGLState.setTextureAlpha(alpha);

    // Reset the texture matrix. We will set our own texture coordinates
    // below.
    setTextureCoords(0, 0, 1, 1);

    saveTransform();
    translate(x, y);

    mGL.glLoadMatrixf(mMatrixValues, 0);

    mGL.glBindBuffer(GL11.GL_ARRAY_BUFFER, xyBuffer);
    mGL.glVertexPointer(2, GL11.GL_FLOAT, 0, 0);

    mGL.glBindBuffer(GL11.GL_ARRAY_BUFFER, uvBuffer);
    mGL.glTexCoordPointer(2, GL11.GL_FLOAT, 0, 0);

    mGL.glBindBuffer(GL11.GL_ELEMENT_ARRAY_BUFFER, indexBuffer);
    mGL.glDrawElements(GL11.GL_TRIANGLE_STRIP, indexCount, GL11.GL_UNSIGNED_BYTE, 0);

    mGL.glBindBuffer(GL11.GL_ARRAY_BUFFER, mBoxCoords);
    mGL.glVertexPointer(2, GL11.GL_FLOAT, 0, 0);
    mGL.glTexCoordPointer(2, GL11.GL_FLOAT, 0, 0);

    restoreTransform();
    mCountDrawMesh++;
  }
Exemplo n.º 4
0
 public void drawTexture(
     BasicTexture texture, float[] mTextureTransform, int x, int y, int w, int h) {
   mGLState.setBlendEnabled(mBlendEnabled && (!texture.isOpaque() || mAlpha < OPAQUE_ALPHA));
   if (!bindTexture(texture)) return;
   setTextureCoords(mTextureTransform);
   mGLState.setTextureAlpha(mAlpha);
   textureRect(x, y, w, h);
 }
Exemplo n.º 5
0
  private void drawTexture(BasicTexture texture, int x, int y, int width, int height, float alpha) {
    if (width <= 0 || height <= 0) return;

    mGLState.setBlendEnabled(mBlendEnabled && (!texture.isOpaque() || alpha < OPAQUE_ALPHA));
    if (!bindTexture(texture)) return;
    mGLState.setTextureAlpha(alpha);
    drawBoundTexture(texture, x, y, width, height);
  }
Exemplo n.º 6
0
  public void drawTexture(BasicTexture texture, int x, int y, int width, int height, float alpha) {

    if (!mTexture2DEnabled) {
      mGL.glEnable(GL11.GL_TEXTURE_2D);
      mTexture2DEnabled = true;
    }

    if (!texture.bind(this, mGL)) {
      throw new RuntimeException("cannot bind" + texture.toString());
    }
    if (width <= 0 || height <= 0) return;

    Matrix matrix = mTransformation.getMatrix();
    matrix.getValues(mMatrixValues);

    // Test whether it has been rotated or flipped, if so, glDrawTexiOES
    // won't work
    if (isMatrixRotatedOrFlipped(mMatrixValues)) {
      putRectangle(
          0,
          0,
          (texture.mWidth - 0.5f) / texture.mTextureWidth,
          (texture.mHeight - 0.5f) / texture.mTextureHeight,
          mUvBuffer,
          mUvPointer);
      setAlphaValue(alpha);
      drawRect(x, y, width, height, mMatrixValues);
    } else {
      // draw the rect from bottom-left to top-right
      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;
      if (width > 0 && height > 0) {
        setAlphaValue(alpha);
        ((GL11Ext) mGL).glDrawTexiOES(x, y, 0, width, height);
      }
    }
  }
Exemplo n.º 7
0
  // This function changes the source coordinate to the texture coordinates.
  // It also clips the source and target coordinates if it is beyond the
  // bound of the texture.
  private void convertCoordinate(RectF source, RectF target, BasicTexture texture) {

    int width = texture.getWidth();
    int height = texture.getHeight();
    int texWidth = texture.getTextureWidth();
    int texHeight = texture.getTextureHeight();
    // Convert to texture coordinates
    source.left /= texWidth;
    source.right /= texWidth;
    source.top /= texHeight;
    source.bottom /= texHeight;

    // Clip if the rendering range is beyond the bound of the texture.
    float xBound = (float) width / texWidth;
    if (source.right > xBound) {
      target.right = target.left + target.width() * (xBound - source.left) / source.width();
      source.right = xBound;
    }
    float yBound = (float) height / texHeight;
    if (source.bottom > yBound) {
      target.bottom = target.top + target.height() * (yBound - source.top) / source.height();
      source.bottom = yBound;
    }
  }
Exemplo n.º 8
0
  public void drawTexture(BasicTexture texture, RectF source, RectF target) {
    if (target.width() <= 0 || target.height() <= 0) return;

    // Copy the input to avoid changing it.
    mDrawTextureSourceRect.set(source);
    mDrawTextureTargetRect.set(target);
    source = mDrawTextureSourceRect;
    target = mDrawTextureTargetRect;

    mGLState.setBlendEnabled(mBlendEnabled && (!texture.isOpaque() || mAlpha < OPAQUE_ALPHA));
    if (!bindTexture(texture)) return;
    convertCoordinate(source, target, texture);
    setTextureCoords(source);
    mGLState.setTextureAlpha(mAlpha);
    textureRect(target.left, target.top, target.width(), target.height());
  }
Exemplo n.º 9
0
  private void drawMixed(
      BasicTexture from,
      int toColor,
      float ratio,
      int x,
      int y,
      int width,
      int height,
      float alpha) {
    // change from 0 to 0.01f to prevent getting divided by zero below
    if (ratio <= 0.01f) {
      drawTexture(from, x, y, width, height, alpha);
      return;
    } else if (ratio >= 1) {
      fillRect(x, y, width, height, toColor);
      return;
    }

    mGLState.setBlendEnabled(
        mBlendEnabled && (!from.isOpaque() || !Utils.isOpaque(toColor) || alpha < OPAQUE_ALPHA));

    final GL11 gl = mGL;
    if (!bindTexture(from)) return;

    //
    // The formula we want:
    //     alpha * ((1 - ratio) * from + ratio * to)
    //
    // The formula that GL supports is in the form of:
    //     combo * from + (1 - combo) * to * scale
    //
    // So, we have combo = alpha * (1 - ratio)
    //     and     scale = alpha * ratio / (1 - combo)
    //
    float combo = alpha * (1 - ratio);
    float scale = alpha * ratio / (1 - combo);

    // Interpolate the RGB and alpha values between both textures.
    mGLState.setTexEnvMode(GL11.GL_COMBINE);

    // Specify the interpolation factor via the alpha component of
    // GL_TEXTURE_ENV_COLORs.
    // RGB component are get from toColor and will used as SRC1
    float colorScale = scale * (toColor >>> 24) / (0xff * 0xff);
    setTextureColor(
        ((toColor >>> 16) & 0xff) * colorScale,
        ((toColor >>> 8) & 0xff) * colorScale,
        (toColor & 0xff) * colorScale,
        combo);
    gl.glTexEnvfv(GL11.GL_TEXTURE_ENV, GL11.GL_TEXTURE_ENV_COLOR, mTextureColor, 0);

    gl.glTexEnvf(GL11.GL_TEXTURE_ENV, GL11.GL_COMBINE_RGB, GL11.GL_INTERPOLATE);
    gl.glTexEnvf(GL11.GL_TEXTURE_ENV, GL11.GL_COMBINE_ALPHA, GL11.GL_INTERPOLATE);
    gl.glTexEnvf(GL11.GL_TEXTURE_ENV, GL11.GL_SRC1_RGB, GL11.GL_CONSTANT);
    gl.glTexEnvf(GL11.GL_TEXTURE_ENV, GL11.GL_OPERAND1_RGB, GL11.GL_SRC_COLOR);
    gl.glTexEnvf(GL11.GL_TEXTURE_ENV, GL11.GL_SRC1_ALPHA, GL11.GL_CONSTANT);
    gl.glTexEnvf(GL11.GL_TEXTURE_ENV, GL11.GL_OPERAND1_ALPHA, GL11.GL_SRC_ALPHA);

    // Wire up the interpolation factor for RGB.
    gl.glTexEnvf(GL11.GL_TEXTURE_ENV, GL11.GL_SRC2_RGB, GL11.GL_CONSTANT);
    gl.glTexEnvf(GL11.GL_TEXTURE_ENV, GL11.GL_OPERAND2_RGB, GL11.GL_SRC_ALPHA);

    // Wire up the interpolation factor for alpha.
    gl.glTexEnvf(GL11.GL_TEXTURE_ENV, GL11.GL_SRC2_ALPHA, GL11.GL_CONSTANT);
    gl.glTexEnvf(GL11.GL_TEXTURE_ENV, GL11.GL_OPERAND2_ALPHA, GL11.GL_SRC_ALPHA);

    drawBoundTexture(from, x, y, width, height);
    mGLState.setTexEnvMode(GL11.GL_REPLACE);
  }
Exemplo n.º 10
0
 private void drawBoundTexture(BasicTexture texture, int x, int y, int width, int height) {
   // Test whether it has been rotated or flipped, if so, glDrawTexiOES
   // won't work
   if (isMatrixRotatedOrFlipped(mMatrixValues)) {
     if (texture.hasBorder()) {
       setTextureCoords(
           1.0f / texture.getTextureWidth(),
           1.0f / texture.getTextureHeight(),
           (texture.getWidth() - 1.0f) / texture.getTextureWidth(),
           (texture.getHeight() - 1.0f) / texture.getTextureHeight());
     } else {
       setTextureCoords(
           0,
           0,
           (float) texture.getWidth() / texture.getTextureWidth(),
           (float) texture.getHeight() / texture.getTextureHeight());
     }
     textureRect(x, y, width, height);
   } else {
     // draw the rect from bottom-left to top-right
     float points[] = mapPoints(mMatrixValues, x, y + height, x + width, y);
     x = (int) (points[0] + 0.5f);
     y = (int) (points[1] + 0.5f);
     width = (int) (points[2] + 0.5f) - x;
     height = (int) (points[3] + 0.5f) - y;
     if (width > 0 && height > 0) {
       ((GL11Ext) mGL).glDrawTexiOES(x, y, 0, width, height);
       mCountTextureOES++;
     }
   }
 }