Code example for ByteBuffer

Methods: capacity, clear, glTexImage2D, glTexSubImage2D

0
 
    /* round up */ 
    int requiredCapacity = ((width * height - 1) | 0x3fff) + 1;
 
    /* check if the existing buffer is already large enough */ 
    if (pixels == null || pixels.capacity() < requiredCapacity)
      pixels = ByteBuffer.allocate(requiredCapacity);
    else if (pixels != null)
      pixels.clear();
  } 
 
  public int[] getTextTextureGL(String text) {
    getTextBounds(text);
 
    // draw text into a bitmap 
    Bitmap bmp = Bitmap.createBitmap(extent[0], extent[1],
                                     Bitmap.Config.ALPHA_8);
    bmp.eraseColor(Color.TRANSPARENT);
    paint.setColor(Color.WHITE);
    Canvas canvas = new Canvas(bmp);
    canvas.drawText(text, 0, -paint.getFontMetricsInt().ascent, paint);
 
    // get bitmap pixels 
    makeBuffer(extent[0], extent[1]);
    bmp.copyPixelsToBuffer(pixels);
 
    // create OpenGL texture 
    int width2 = NativeView.validateTextureSize(extent[0]);
    int height2 = NativeView.validateTextureSize(extent[1]);
    glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
    glGenTextures(1, id, 0);
    glBindTexture(GL_TEXTURE_2D, id[0]);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
    if (extent[0] == width2 && extent[1] == height2) {
      glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE, width2, height2,
                   0, GL_LUMINANCE, GL_UNSIGNED_BYTE, pixels);
    } else { 
      glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE, width2, height2,
                   0, GL_LUMINANCE, GL_UNSIGNED_BYTE, null);
      glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, extent[0], extent[1],
                      GL_LUMINANCE, GL_UNSIGNED_BYTE, pixels);
    } 
 
    id[1] = extent[0];
    id[2] = extent[1];