Пример #1
0
  /**
   * Convert the buffered image to a texture
   *
   * @param bufferedImage The image to convert to a texture
   * @param texture The texture to store the data into
   * @return A buffer containing the data
   */
  private static ByteBuffer convertImageData(BufferedImage bufferedImage, Texture texture) {
    ByteBuffer imageBuffer;
    WritableRaster raster;
    BufferedImage texImage;

    int texWidth = 2;
    int texHeight = 2;

    // find the closest power of 2 for the width and height
    // of the produced texture
    while (texWidth < bufferedImage.getWidth()) {
      texWidth *= 2;
    }
    while (texHeight < bufferedImage.getHeight()) {
      texHeight *= 2;
    }

    texture.setTextureHeight(texHeight);
    texture.setTextureWidth(texWidth);

    // create a raster that can be used by OpenGL as a source
    // for a texture
    if (bufferedImage.getColorModel().hasAlpha()) {
      raster = Raster.createInterleavedRaster(DataBuffer.TYPE_BYTE, texWidth, texHeight, 4, null);
      texImage =
          new BufferedImage(glAlphaColorModel, raster, false, new Hashtable<Object, Object>());
    } else {
      raster = Raster.createInterleavedRaster(DataBuffer.TYPE_BYTE, texWidth, texHeight, 3, null);
      texImage = new BufferedImage(glColorModel, raster, false, new Hashtable<Object, Object>());
    }

    // copy the source image into the produced image
    Graphics g = texImage.getGraphics();
    g.setColor(new Color(0f, 0f, 0f, 0f));
    g.fillRect(0, 0, texWidth, texHeight);
    g.drawImage(bufferedImage, 0, 0, null);

    // build a byte buffer from the temporary image
    // that be used by OpenGL to produce a texture.
    byte[] data = ((DataBufferByte) texImage.getRaster().getDataBuffer()).getData();

    imageBuffer = ByteBuffer.allocateDirect(data.length);
    imageBuffer.order(ByteOrder.nativeOrder());
    imageBuffer.put(data, 0, data.length);
    imageBuffer.flip();

    return imageBuffer;
  }
Пример #2
0
  public static UIFont createGuiFont(String name, int font_size) {
    Font font = load_ttf(StringUtils.concat(name, ".ttf"), font_size);
    UIFont tmp = new UIFont();

    tmp.font_texture = new Texture();

    Graphics2D graphics = new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB).createGraphics();
    graphics.setFont(font);

    tmp.fontMetrics = graphics.getFontMetrics();

    WritableRaster raster;
    BufferedImage bufferedImage;

    raster =
        Raster.createInterleavedRaster(
            DataBuffer.TYPE_BYTE,
            (int) tmp.getFontImageWidth(),
            (int) tmp.getFontImageHeight(),
            4,
            null);
    bufferedImage = new BufferedImage(glAlphaColorModel, raster, false, null);

    // Draw the characters on our image
    Graphics2D imageGraphics = bufferedImage.createGraphics();
    imageGraphics.setRenderingHint(
        RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF);
    imageGraphics.setRenderingHint(
        RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
    imageGraphics.setFont(font);
    imageGraphics.setColor(Color.white.getAWTColor());

    // draw every CHAR by line...
    for (int i : key_table.keySet())
      imageGraphics.drawString(
          key_table.get(i), 0, (int) (tmp.fontMetrics.getMaxAscent() + (tmp.getHeight() * i)));

    // Generate texture data
    byte[] data = ((DataBufferByte) bufferedImage.getRaster().getDataBuffer()).getData();

    ByteBuffer imageData = BufferUtils.create_byte_buffer(data.length);
    imageData.order(ByteOrder.nativeOrder());
    imageData.put(data, 0, data.length);
    imageData.flip();

    glBindTexture(GL_TEXTURE_2D, tmp.font_texture.id);

    // Setup wrap mode
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);

    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);

    glTexImage2D(
        GL_TEXTURE_2D,
        0,
        GL_RGBA8,
        (int) tmp.getFontImageWidth(),
        (int) tmp.getFontImageHeight(),
        0,
        GL_RGBA,
        GL_UNSIGNED_BYTE,
        imageData);

    return tmp;
  }