Slick2D/LWJGL angel code font problem

Like in topic. I have problem with angel code fonts (and any other kind of font, too). I have enabled GL_TEXTURE_2D, glEnable(GL_BLEND);, glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); Below you can see how “Hello world!” is displayed, above placeholder ship:

http://img15.imageshack.us/img15/2848/screenshot017q.png

Here is part of my code:

public static AngelCodeFont font;

public static void main(String args[]) {
         start();
         makeFonts();
         gameLoop();
    }

static public void makeFonts() {
         Font awtFont = new Font("Times New Roman", Font.BOLD, 72);
        try {
            glBindTexture(GL_TEXTURE_2D,2);
            font = new AngelCodeFont("Jadro\\TimesNewRoman32.fnt", new Image("Jadro\\TimesNewRoman32.png"));
            glBindTexture(GL_TEXTURE_2D,1);
        } catch (SlickException ex) {
            Logger.getLogger(Jadro.class.getName()).log(Level.SEVERE, null, ex);
        }
     }
Core.font.drawString(0, 0, "Hello World!", Color.white);

Just in case: OpenGL creation:

public void utworz() {
         try {
            Display.setDisplayMode(new DisplayMode(SCREEN_X,SCREEN_Y));
            Display.setTitle("Silent Space 0.0.4 --- team work prototype");
            Display.create(new PixelFormat(8,8,8));
            Display.setVSyncEnabled(true);
        } catch (LWJGLException e) {
            System.exit(0);
        }
        glClearColor(0.0f,0.0f,0.0f,0.0f);
        glDisable(GL_DEPTH_TEST);
        glDisable(GL_LIGHTING);
        
        glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
        glEnable(GL_TEXTURE_2D);
        glEnable(GL_BLEND);
        glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
        
        glLoadIdentity();
        glShadeModel(GL_SMOOTH);
        glClearDepth(1.0f);
        glDepthFunc(GL_LEQUAL);
        glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
        
        glEnable(GL_ALPHA_TEST);
        glAlphaFunc(GL_GEQUAL, 0.1f);
        glEnable(GL_SCISSOR_TEST);
        glEnable(GL_STENCIL_TEST);
        
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        glOrtho(0, SCREEN_X, 0, SCREEN_Y, 1, -1);
        glMatrixMode(GL_MODELVIEW);
    }

Well, as you can see, it is using the regions registered for the font, on the texture loaded for the ship. Without me having any knowledge of OpenGL, I’m going to venture a guess that your try/catch block is the problem. Are you sure you need both bindTexture-calls?

I have deleted them, but I still have the same problem.

Don’t use glBindTexture with Slick. Instead, use this:

texture.bind();

If you want to disable texturing, use this:

TextureImpl.bindNone();

I am using Slick only to draw strings, everything other is displayed in LWJGL.

Edit: When I am rendering only text, this is what can I see:

http://img826.imageshack.us/img826/8510/screenshot019bt.png

Is there any way to “separate” displaying of text and graphics?

The problem is because Slick caches the last bound texture to minimize redundant calls to glBindTexture. When you bind your own texture, Slick still thinks the font texture is bound, and doesn’t bother calling glBindTexture again. This is why you see your own texture showing up where the font glyphs should be.

The solution is to use Slick’s texture binding (which you may as well since you’re already using slick to decode and load font textures), or to call TextureImpl.unbind() before rendering your Slick objects. This will clear the last cached texture, forcing Slick to call glBindTexture upon rendering.

Unfortunately TextureImpl.unbind() does not work, I still see last texture instead of text. Any other ideas? :wink:

Next parts of my code, I hope them will help solve this case:

  1. Current font rendering code:
        glPushMatrix();
            glTranslated(0, 0, 0);
            glRotated(180, 180, 0, 0);
            Core.font.drawString(0, 0, "Hello World!", Color.white);
        glPopMatrix();
  1. Current texture binding and displaying code:
        glPushMatrix();
            glTranslated(x, y, 0);
            glRotated(angleRad/(Math.PI/180), 0, 0, 1);
            glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, szerokosc, wysokosc, 0, GL_RGBA, GL_UNSIGNED_BYTE, texture);
            glBindTexture(GL_ADD, GL_LOAD);
            glBindTexture(GL_TEXTURE_2D,1);
            glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
            glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
            glBegin(GL_QUADS);
                glColor4f(1,1,1,1);
                glTexCoord2f(0,1); glVertex2f(-height/2, -width/2);
                glTexCoord2f(0,0); glVertex2f(-height/2, width/2);
                glTexCoord2f(1,0); glVertex2f(height/2, width/2);
                glTexCoord2f(1,1); glVertex2f(height/2, -width/2);
            glEnd();
        glPopMatrix();

Why are you calling glTexImage2D every frame?
Why are you calling it before binding your texture?
What is the call to GL_ADD, GL_LOAD for?

You really need to learn a little more about OpenGL…

Your text rendering should look like this:

        glPushMatrix();
            glTranslated(0, 0, 0);
            glRotated(180, 180, 0, 0);
            TextureImpl.unbind();
            Core.font.drawString(0, 0, "Hello World!", Color.white);
        glPopMatrix();

Your font loader should look like this:

        try {
            TextureImpl.unbind();
            font = new AngelCodeFont("Jadro\\TimesNewRoman32.fnt", new Image("Jadro\\TimesNewRoman32.png"));
        } catch (SlickException ex) {
            Logger.getLogger(Jadro.class.getName()).log(Level.SEVERE, null, ex);
        }

I found the solution - I had to load my font png as texture before using it. I know that this is a workaround, but I think there is no easier way to do it.