0

Using the answer from my previous question I have managed to render the entire font atlas to the screen. However, when it comes to rendering individual characters from the atlas my coordinates appear to be somewhat wrong:

alt text http://img.condorcraft110.net/font%20renderer%20fail.png

From that (the text is supposed to read 'Test font renderer text') I can assume that the texture coordinates are wrong. But how are they wrong?

Complete working example:

import java.awt.image.*;
import java.io.*;
import java.nio.*;
import javax.imageio.*;
import org.lwjgl.*;
import org.lwjgl.opengl.*;

public class OpenGLFontRendererTest
{
    private static int textureID;

    public static void main(String[] args) throws Exception
    {
        Display.setTitle("OpenGL Font Renderer Test");
        Display.setDisplayMode(new DisplayMode(640, 480));
        Display.create();

        GL11.glMatrixMode(GL11.GL_PROJECTION);
        GL11.glLoadIdentity();
        GL11.glOrtho(0, 640, 0, 480, 1, -1);
        GL11.glMatrixMode(GL11.GL_MODELVIEW);

        textureID = bindTextureFile("textures/font.png");

        while(!Display.isCloseRequested())
        {
            Display.sync(60);

            GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
            GL11.glClearColor(0, 0, 0, 1);

            GL11.glColor4f(1, 1, 1, 1);
            drawText(20, 20, "Test font renderer text");

            Display.update();
        }

        Display.destroy();
    }

    private static void drawText(int x, int y, String text)
    {
        GL11.glPushMatrix();
            GL11.glTranslatef(x, y, 0);
            GL11.glDisable(GL11.GL_DEPTH_TEST);
            GL11.glEnable(GL11.GL_TEXTURE_2D);
            GL11.glEnable(GL11.GL_BLEND);
                GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
                GL11.glBindTexture(GL11.GL_TEXTURE_2D, textureID);
                GL11.glBegin(GL11.GL_QUADS);
                    int xOff = 0;
                    int yOff = 0;
                    for(char c : text.toCharArray())
                    {
                        if(c == '\n')
                        {
                            xOff = 0;
                            yOff += 17;
                            continue;
                        }
                        else if(c == '\r')
                        {
                            xOff = 0;
                            continue;
                        }

                        float pos = (float)c;

                        float textureX = (pos % 16F) / 16F;
                        float textureY = (pos / 16F) / 16F;

                        GL11.glTexCoord2f(textureX, textureY + 0.0625F);
                        GL11.glVertex2i(xOff, yOff);

                        GL11.glTexCoord2f(textureX, textureY);
                        GL11.glVertex2i(xOff, yOff + 16);

                        GL11.glTexCoord2f(textureX + 0.0625F, textureY);
                        GL11.glVertex2i(xOff + 16, yOff + 16);

                        GL11.glTexCoord2f(textureX + 0.0625F, textureY + 0.0625F);
                        GL11.glVertex2i(xOff + 16, yOff);

                        xOff += 17;
                    }
                GL11.glEnd();
            GL11.glDisable(GL11.GL_BLEND);
            GL11.glDisable(GL11.GL_TEXTURE_2D);
            GL11.glEnable(GL11.GL_DEPTH_TEST);
        GL11.glPopMatrix();
    }

    private static int bindTextureFile(String file)
    {
        try
        {
            BufferedImage image = ImageIO.read(new FileInputStream(file));

            int[] pixels = new int[image.getWidth() * image.getHeight()];

            image.getRGB(0, 0, image.getWidth(), image.getHeight(), pixels, 0, image.getWidth());

            ByteBuffer buffer = BufferUtils.createByteBuffer(image.getWidth() * image.getHeight() * 4);

            for(int y = 0; y < image.getWidth(); y++)
            {
                for(int x = 0; x < image.getHeight(); x++)
                {
                    int pixel = pixels[y * image.getWidth() + x];

                    buffer.put((byte)((pixel >> 16) & 0xFF));
                    buffer.put((byte)((pixel >> 8) & 0xFF));
                    buffer.put((byte)(pixel & 0xFF));
                    buffer.put((byte)((pixel >> 24) & 0xFF));
                }
            }

            buffer.flip();

            int textureID = GL11.glGenTextures();

            GL11.glBindTexture(GL11.GL_TEXTURE_2D, textureID);

            GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_S, GL12.GL_CLAMP_TO_EDGE);
            GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_T, GL12.GL_CLAMP_TO_EDGE);

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

            GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0, GL11.GL_RGBA, image.getWidth(), image.getHeight(), 0, GL11.GL_RGBA, GL11.GL_UNSIGNED_BYTE, buffer);

            return textureID;
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }

        return -1;
    }
}

font.png is again the default Minecraft font file (/fonts/default.png) for testing purposes.

Community
  • 1
  • 1
condorcraft110 II
  • 261
  • 1
  • 2
  • 15
  • Can you also attach the actual font.png that you are using? There seem to be several. FWIW: I usually separate drawing a single character from drawing an entire string. That way, you can test one independent of the other. – Jongware Feb 21 '15 at 14:24
  • @Jongware it's `default.png` - I don't think I can post it due to copyright. – condorcraft110 II Feb 21 '15 at 16:00
  • Two educated guesses: (1) you take source pixels from (0,16) (divided by 16), where you should use (0,15); (2) [`%` may show unexpected behavior](http://mindprod.com/jgloss/modulus.html) (see also http://stackoverflow.com/a/2947064/2564301). Defer the cast to `float` for your `c` until right before the division so the divide and modulus are on an integer instead. That may fix the off-by-a-few-pixels. – Jongware Feb 21 '15 at 16:24
  • JOGL might be better for text rendering... as it has a TextRenderer class that makes it really easy to manipulate text inside OpenGL – Ubica Feb 22 '15 at 00:38

1 Answers1

0

OK, I managed to solve this. I was dividing as a float, so I ended up with decimal places which messed up the coordinates.

Revised code:

import java.awt.image.*;
import java.io.*;
import java.nio.*;
import javax.imageio.*;
import org.lwjgl.*;
import org.lwjgl.opengl.*;

public class OpenGLFontRendererTest
{
    private static int textureID;

    public static void main(String[] args) throws Exception
    {
        Display.setTitle("OpenGL Font Renderer Test");
        Display.setDisplayMode(new DisplayMode(640, 480));
        Display.create();

        GL11.glMatrixMode(GL11.GL_PROJECTION);
        GL11.glLoadIdentity();
        GL11.glOrtho(0, 640, 0, 480, 1, -1);
        GL11.glMatrixMode(GL11.GL_MODELVIEW);

        textureID = bindTextureFile("textures/font.png");

        while(!Display.isCloseRequested())
        {
            Display.sync(60);

            GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
            GL11.glClearColor(0, 0, 0, 1);

            GL11.glColor4f(1, 1, 1, 1);
            drawText(20, 20, "Test font renderer text");

            Display.update();
        }

        Display.destroy();
    }

    private static void drawText(int x, int y, String text)
    {
        GL11.glPushMatrix();
            GL11.glTranslatef(x, y, 0);
            GL11.glDisable(GL11.GL_DEPTH_TEST);
            GL11.glEnable(GL11.GL_TEXTURE_2D);
            GL11.glEnable(GL11.GL_BLEND);
                GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
                GL11.glBindTexture(GL11.GL_TEXTURE_2D, textureID);
                GL11.glBegin(GL11.GL_QUADS);
                    int xOff = 0;
                    int yOff = 0;
                    for(char c : text.toCharArray())
                    {
                        if(c == '\n')
                        {
                            xOff = 0;
                            yOff -= 17;
                            continue;
                        }
                        else if(c == '\r')
                        {
                            xOff = 0;
                            continue;
                        }

                        int pos = (int)c; // THESE

                        float textureX = (float)(pos % 16F) / 16F; // THREE
                        float textureY = (float)(pos / 16) / 16F; // LINES

                        GL11.glTexCoord2f(textureX, textureY + 0.0625F);
                        GL11.glVertex2i(xOff, yOff);

                        GL11.glTexCoord2f(textureX, textureY);
                        GL11.glVertex2i(xOff, yOff + 16);

                        GL11.glTexCoord2f(textureX + 0.0625F, textureY);
                        GL11.glVertex2i(xOff + 16, yOff + 16);

                        GL11.glTexCoord2f(textureX + 0.0625F, textureY + 0.0625F);
                        GL11.glVertex2i(xOff + 16, yOff);

                        xOff += 17;
                    }
                GL11.glEnd();
            GL11.glDisable(GL11.GL_BLEND);
            GL11.glDisable(GL11.GL_TEXTURE_2D);
            GL11.glEnable(GL11.GL_DEPTH_TEST);
        GL11.glPopMatrix();
    }

    private static int bindTextureFile(String file)
    {
        try
        {
            BufferedImage image = ImageIO.read(new FileInputStream(file));

            int[] pixels = new int[image.getWidth() * image.getHeight()];

            image.getRGB(0, 0, image.getWidth(), image.getHeight(), pixels, 0, image.getWidth());

            ByteBuffer buffer = BufferUtils.createByteBuffer(image.getWidth() * image.getHeight() * 4);

            for(int y = 0; y < image.getWidth(); y++)
            {
                for(int x = 0; x < image.getHeight(); x++)
                {
                    int pixel = pixels[y * image.getWidth() + x];

                    buffer.put((byte)((pixel >> 16) & 0xFF));
                    buffer.put((byte)((pixel >> 8) & 0xFF));
                    buffer.put((byte)(pixel & 0xFF));
                    buffer.put((byte)((pixel >> 24) & 0xFF));
                }
            }

            buffer.flip();

            int textureID = GL11.glGenTextures();

            GL11.glBindTexture(GL11.GL_TEXTURE_2D, textureID);

            GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_S, GL12.GL_CLAMP_TO_EDGE);
            GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_T, GL12.GL_CLAMP_TO_EDGE);

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

            GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0, GL11.GL_RGBA, image.getWidth(), image.getHeight(), 0, GL11.GL_RGBA, GL11.GL_UNSIGNED_BYTE, buffer);

            return textureID;
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }

        return -1;
    }
}
condorcraft110 II
  • 261
  • 1
  • 2
  • 15