Trouble with glBitmap

Started by CaseyB, November 09, 2006, 23:34:32

Previous topic - Next topic

CaseyB

I am having trouble with using glBitmap() to draw text.  I have:
void setText(String text)
{
	this.text = text;
	textBuffer = ByteBuffer.allocateDirect(text.length()*8).order(ByteOrder.nativeOrder());
	textBuffer.rewind();
	byte[] letter;

	// Load buffer
	for(int i = 0; i <text.length(); i++)
	{
		letter = font.get(text.substring(i, i+1).toUpperCase());
		if(letter == null) letter = font.get("*");
		textBuffer.put(letter);
	}
}
That loads the ByteBuffer and
void render(Vector3f position)
{
	GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
	GL11.glLoadIdentity();
	GL11.glTranslatef(0f, 0f, -75f);
	textBuffer.rewind();
	GL11.glDisable(GL11.GL_DEPTH_TEST);
	GL11.glRasterPos2f(0, 0);
	for(int h = 0; h < textBuffer.limit()/8; h++)
	{
		GL11.glBitmap(5, 8, 0, 0, 6, 0, textBuffer);
		for(int i = 0; i < 8; i++) textBuffer.get();
	}
	GL11.glEnable(GL11.GL_DEPTH_TEST);
}
That draws it, but I get garbage.  This is what it looks like:

Has anyone had this trouble before?

ndhb

It looks like your bytes are being interpreted/read wrong. It could be your byteorder or arguments to glBitmap (xmove/ymove/xorig/yorig). Try and allocate your bytebuffer with BufferUtils?

CaseyB

I tried allocating using BufferUtils and it didn't change, I also tried setting the byte order to Big and Litte Endian and it looked the same!  Could it be how I am defining my characters?  I can't imagine why that wouldn't work anymore.
private void buildFontTable()
{
	font.put("A",  new byte[]{(byte)0x00, (byte)0x88, (byte)0x88, (byte)0xF8, (byte)0x88, (byte)0x88, (byte)0x88,(byte)0x70});
	font.put("B",  new byte[]{(byte)0x00, (byte)0xF0, (byte)0x88, (byte)0x88, (byte)0xF0, (byte)0x88, (byte)0x88,(byte)0xF0});
	font.put("C",  new byte[]{(byte)0x00, (byte)0x70, (byte)0x88, (byte)0x80, (byte)0x80, (byte)0x80, (byte)0x88,(byte)0x70});
	font.put("D",  new byte[]{(byte)0x00, (byte)0xF0, (byte)0x90, (byte)0x88, (byte)0x88, (byte)0x88, (byte)0x90,(byte)0xF0});
	font.put("E",  new byte[]{(byte)0x00, (byte)0xF8, (byte)0x80, (byte)0x80, (byte)0xF0, (byte)0x80, (byte)0x80,(byte)0xF8});
	font.put("F",  new byte[]{(byte)0x00, (byte)0x80, (byte)0x80, (byte)0x80, (byte)0xF8, (byte)0x80, (byte)0x80,(byte)0xF8});
	font.put("G",  new byte[]{(byte)0x00, (byte)0x70, (byte)0x88, (byte)0x88, (byte)0xBB, (byte)0x80, (byte)0x88,(byte)0x70});
	font.put("H",  new byte[]{(byte)0x00, (byte)0x88, (byte)0x88, (byte)0x88, (byte)0xF8, (byte)0x88, (byte)0x88,(byte)0x88});
	font.put("I",  new byte[]{(byte)0x00, (byte)0xF8, (byte)0x20, (byte)0x20, (byte)0x20, (byte)0x20, (byte)0x20,(byte)0xF8});
	font.put("J",  new byte[]{(byte)0x00, (byte)0x60, (byte)0x90, (byte)0x10, (byte)0x10, (byte)0x10, (byte)0x10,(byte)0x38});
	font.put("K",  new byte[]{(byte)0x00, (byte)0x88, (byte)0x90, (byte)0xA0, (byte)0xC0, (byte)0xA0, (byte)0x90,(byte)0x88});
	font.put("L",  new byte[]{(byte)0x00, (byte)0xFE, (byte)0x80, (byte)0x80, (byte)0x80, (byte)0x80, (byte)0x80,(byte)0x80});
	font.put("M",  new byte[]{(byte)0x00, (byte)0x88, (byte)0x88, (byte)0x88, (byte)0xA8, (byte)0xA8, (byte)0xD8,(byte)0x88});
	font.put("N",  new byte[]{(byte)0x00, (byte)0x88, (byte)0x88, (byte)0x98, (byte)0xA8, (byte)0xC8, (byte)0x88,(byte)0x88});
	font.put("O",  new byte[]{(byte)0x00, (byte)0x70, (byte)0x88, (byte)0x88, (byte)0x88, (byte)0x88, (byte)0x88,(byte)0x70});
	font.put("P",  new byte[]{(byte)0x00, (byte)0x80, (byte)0x80, (byte)0x80, (byte)0xF0, (byte)0x88, (byte)0x88,(byte)0xF0});
	font.put("Q",  new byte[]{(byte)0x00, (byte)0x68, (byte)0x90, (byte)0xA8, (byte)0x88, (byte)0x88, (byte)0x88,(byte)0x70});
	font.put("R",  new byte[]{(byte)0x00, (byte)0x88, (byte)0x90, (byte)0xA0, (byte)0xF0, (byte)0x88, (byte)0x88,(byte)0xF0});
	font.put("S",  new byte[]{(byte)0x00, (byte)0x70, (byte)0x88, (byte)0x08, (byte)0x70, (byte)0x80, (byte)0x88,(byte)0x70});
	font.put("T",  new byte[]{(byte)0x00, (byte)0x20, (byte)0x20, (byte)0x20, (byte)0x20, (byte)0x20, (byte)0x20,(byte)0xF8});
	font.put("U",  new byte[]{(byte)0x00, (byte)0x70, (byte)0x88, (byte)0x88, (byte)0x88, (byte)0x88, (byte)0x88,(byte)0x88});
	font.put("V",  new byte[]{(byte)0x00, (byte)0x20, (byte)0x50, (byte)0x88, (byte)0x88, (byte)0x88, (byte)0x88,(byte)0x88});
	font.put("W",  new byte[]{(byte)0x00, (byte)0x50, (byte)0xA8, (byte)0xA8, (byte)0x88, (byte)0x88, (byte)0x88,(byte)0x88});
	font.put("X",  new byte[]{(byte)0x00, (byte)0x88, (byte)0x88, (byte)0x50, (byte)0x20, (byte)0x50, (byte)0x88,(byte)0x88});
	font.put("Y",  new byte[]{(byte)0x00, (byte)0x20, (byte)0x20, (byte)0x20, (byte)0x50, (byte)0x88, (byte)0x88,(byte)0x88});
	font.put("Z",  new byte[]{(byte)0x00, (byte)0xF8, (byte)0x80, (byte)0x40, (byte)0x20, (byte)0x10, (byte)0x08,(byte)0xF8});
	font.put(" ",  new byte[]{(byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00,(byte)0x00});
	font.put("0",  new byte[]{(byte)0x00, (byte)0x70, (byte)0x88, (byte)0xC8, (byte)0xA8, (byte)0x98, (byte)0x88,(byte)0x70});
	font.put("1",  new byte[]{(byte)0x00, (byte)0x70, (byte)0x20, (byte)0x20, (byte)0x20, (byte)0x20, (byte)0x60,(byte)0x20});
	font.put("2",  new byte[]{(byte)0x00, (byte)0xF8, (byte)0x40, (byte)0x20, (byte)0x10, (byte)0x08, (byte)0x88,(byte)0x70});
	font.put("3",  new byte[]{(byte)0x00, (byte)0x70, (byte)0x88, (byte)0x08, (byte)0x10, (byte)0x20, (byte)0x10,(byte)0xF8});
	font.put("4",  new byte[]{(byte)0x00, (byte)0x10, (byte)0x10, (byte)0xF8, (byte)0x90, (byte)0x50, (byte)0x30,(byte)0x10});
	font.put("5",  new byte[]{(byte)0x00, (byte)0x70, (byte)0x88, (byte)0x08, (byte)0x08, (byte)0xF0, (byte)0x80,(byte)0xF8});
	font.put("6",  new byte[]{(byte)0x00, (byte)0x70, (byte)0x88, (byte)0x88, (byte)0xF0, (byte)0x80, (byte)0x40,(byte)0x30});
	font.put("7",  new byte[]{(byte)0x00, (byte)0x40, (byte)0x40, (byte)0x40, (byte)0x20, (byte)0x10, (byte)0x08,(byte)0xF8});
	font.put("8",  new byte[]{(byte)0x00, (byte)0x70, (byte)0x88, (byte)0x88, (byte)0x70, (byte)0x88, (byte)0x88,(byte)0x70});
	font.put("9",  new byte[]{(byte)0x00, (byte)0x60, (byte)0x10, (byte)0x08, (byte)0x78, (byte)0x88, (byte)0x88,(byte)0x70});
	font.put("-",  new byte[]{(byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0xF8, (byte)0x00, (byte)0x00,(byte)0x00});
	font.put("+",  new byte[]{(byte)0x00, (byte)0x00, (byte)0x20, (byte)0x20, (byte)0xF8, (byte)0x20, (byte)0x20,(byte)0x00});
	font.put("*",  new byte[]{(byte)0x00, (byte)0x00, (byte)0x00, (byte)0x20, (byte)0xA8, (byte)0x70, (byte)0xA8,(byte)0x20});
	font.put("<",  new byte[]{(byte)0x00, (byte)0x08, (byte)0x10, (byte)0x20, (byte)0x40, (byte)0x20, (byte)0x10,(byte)0x08});
	font.put(">",  new byte[]{(byte)0x00, (byte)0x80, (byte)0x40, (byte)0x20, (byte)0x10, (byte)0x20, (byte)0x40,(byte)0x80});
	font.put("!",  new byte[]{(byte)0x00, (byte)0x20, (byte)0x00, (byte)0x20, (byte)0x20, (byte)0x20, (byte)0x20,(byte)0x20});
	font.put("\n", new byte[]{(byte)0xFF, (byte)0xFF, (byte)0xFF, (byte)0xFF, (byte)0xFF, (byte)0xFF, (byte)0xFF,(byte)0xFF});
}
I am just really at a loss!

CaseyB

D'OH!  I was missing
GL11.glPixelStorei(GL11.GL_UNPACK_ALIGNMENT, 1);
in my init() method!  :oops:

ndhb

Great! I started my font project with glBitmap and readPixels but eventually found them a bit lacking in performance. Then I changed to textured bitmap fonts because I also wanted to put 3D text labels in the world. I am using display lists to render the text and have made a text console for events in the engine. I'm currently quite satisfied with appearance and performance but I need to clean up and optimize the code a bit and then I need to write some more bitmap font importers :). I will put it somewhere on lwjgl.org forum when it's done if someone needs it.



CaseyB

Sweet!  I would love to see it!