示例#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");
    }
  }
 public static RawTexture newInstance(GLCanvas canvas) {
   int[] textureId = new int[1];
   GL11 gl = canvas.getGLInstance();
   gl.glGenTextures(1, textureId, 0);
   return new RawTexture(canvas, textureId[0]);
 }