Now I know this topic has been covered quite a bit, and I’ve done several searches on this forum and others, but I still can’t get this quite right. I’ve managed it with PNG files without an alpha.
Here is my image that I want to use (it’s been borrowed from Quake till I design my own).
http://users.bigpond.net.au/ap_kelly/projects/screenshots/bigchars.tga
Here is the code I use to init GL
gl = new GL(this.getClass().getName(), 50, 50, SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_COLOUR_DEPTH, 1, 0, 0);
Here is my loading code.
private static int _load32bitImage(Image image) {
BufferedImage tex = new BufferedImage(image.getWidth(null), image.getHeight(null), BufferedImage.TYPE_4BYTE_ABGR);
Graphics2D g = (Graphics2D)tex.getGraphics();
g.drawImage(image, null, null);
g.dispose();
// Flip Image
AffineTransform tx = AffineTransform.getScaleInstance(1, -1);
tx.translate(0, -image.getHeight(null));
AffineTransformOp op = new AffineTransformOp(tx, AffineTransformOp.TYPE_NEAREST_NEIGHBOR);
tex = op.filter(tex, null);
// Put Image In Memory
ByteBuffer scratch = ByteBuffer.allocateDirect(4 * tex.getWidth() * tex.getHeight());
int scratchAddress = Sys.getDirectBufferAddress(scratch);
byte data[] = (byte[])tex.getRaster().getDataElements(0, 0, tex.getWidth(), tex.getHeight(), null);
scratch.clear();
scratch.put(data);
// Create A IntBuffer For Image Address In Memory
IntBuffer buf = ByteBuffer.allocateDirect(4).order(ByteOrder.nativeOrder()).asIntBuffer();
int bufPtr = Sys.getDirectBufferAddress(buf); // Get Image Address
gl.genTextures(1, bufPtr); // Create Texture In OpenGL
gl.bindTexture(GL.TEXTURE_2D, buf.get(0)); // Typical Texture Generation Using Data From The Image
// Linear Filtering
gl.texParameteri(GL.TEXTURE_2D, GL.TEXTURE_MAG_FILTER, GL.LINEAR);
gl.texParameteri(GL.TEXTURE_2D, GL.TEXTURE_MIN_FILTER, GL.LINEAR);
// Generate The Texture
gl.texImage2D(GL.TEXTURE_2D, 0, GL.RGBA, tex.getWidth(), tex.getHeight(), 0, GL.RGBA, GL.UNSIGNED_BYTE, scratchAddress);
return buf.get(0); // Return Image Address In Memory
}
Here is my rendering code.
public static void print(int pX, int pY, String pMsg, int pFontset) {
if (!initialised) {
System.out.println("FontUtils.init(GL pGL, GLU pGLU) must be called first.");
} else {
gl.enable(GL.TEXTURE_2D);
gl.enable(GL.BLEND);
gl.enable(GL.ALPHA_TEST);
gl.blendFunc(GL.SRC_ALPHA, GL.ONE_MINUS_SRC_ALPHA);
// gl.blendFunc(GL.SRC_ALPHA, GL.ONE);
// gl.blendFunc(GL.ONE, GL.ONE);
gl.bindTexture(GL.TEXTURE_2D, fontTextures[pFontset]);
gl.translated(pX, pY, 0);
gl.listBase(fontSets[pFontset]);
ByteBuffer scratch = ByteBuffer.allocateDirect(pMsg.getBytes().length);
scratch.put(pMsg.getBytes());
gl.callLists(pMsg.length(), GL.BYTE, Sys.getDirectBufferAddress(scratch));
// gl.translated(-(pX + (pMsg.length() * FONT_WIDTH)), -pY, 0);
gl.disable(GL.ALPHA_TEST);
gl.disable(GL.BLEND);
gl.disable(GL.TEXTURE_2D);
}
}
All I get is white boxes on the screen rather than the text with transparent/alpha blended backgrounds.
Any advice would be appreciated.
Andy.
