Search code examples
javaopengllwjglslick2d

UnicodeFont rendering seems to block everything else from rendering?


After over two hours (Yes, a flippin' long time!) of attempting to debug this code, I appear to have a line rendering text which seems to be blotting out everything else behind it (or something).

The text I am attempting to draw on that method works fine, however for whatever reason, there seems to be nothing else rendering.

I did two experiments with if (true) return; lines.

The first experiment looked like this:

 public static void render() {
    //glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glLoadIdentity();

    glDisableClientState(GL_VERTEX_ARRAY);
    glDisableClientState(GL_NORMAL_ARRAY);
    glBindBuffer(GL_ARRAY_BUFFER, 0);
    glMatrixMode(GL_PROJECTION);
    glLoadMatrix(orthographicProjectionMatrix);
    glMatrixMode(GL_MODELVIEW);
    glPushMatrix();
    glLoadIdentity();
    glDisable(GL_LIGHTING);

    width = Display.getWidth()/2;
    height = Display.getHeight()/2;
    int ch = Display.getHeight()/10;
   // int locationY = 100;
    //if (true) return;
    //if (true) return;



        String msg = loadMessage;

        if (msg.contains("RED")){
            msg = msg.replace("RED", "");
            int offset = font.getWidth(msg)/2;
            font.drawString(Display.getWidth()/2 - offset, Display.getHeight()/2, msg, Color.red);
            if (true) return;
        }else if (msg.contains("YELLOW")){
            msg = msg.replace("YELLOW", "");
            int offset = font.getWidth(msg)/2;
            font.drawString(Display.getWidth()/2 - offset, Display.getHeight()/2, msg, Color.yellow);
            if (true) return;
        }else{
            int offset = font.getWidth(msg)/2;
         font.drawString(Display.getWidth()/2 - offset, Display.getHeight()/2, msg, Color.magenta);
         if (true) return;
        }


    glEnableClientState(GL_VERTEX_ARRAY);
    glEnableClientState(GL_NORMAL_ARRAY);

    glEnable(GL_LIGHTING);
    glPopMatrix();
    glMatrixMode(GL_PROJECTION);
    glLoadMatrix(perspectiveProjectionMatrix);
    glMatrixMode(GL_MODELVIEW);

}

And the second one was like this:

    public static void render() {
    //glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glLoadIdentity();

    glDisableClientState(GL_VERTEX_ARRAY);
    glDisableClientState(GL_NORMAL_ARRAY);
    glBindBuffer(GL_ARRAY_BUFFER, 0);
    glMatrixMode(GL_PROJECTION);
    glLoadMatrix(orthographicProjectionMatrix);
    glMatrixMode(GL_MODELVIEW);
    glPushMatrix();
    glLoadIdentity();
    glDisable(GL_LIGHTING);

    width = Display.getWidth()/2;
    height = Display.getHeight()/2;
    int ch = Display.getHeight()/10;
   // int locationY = 100;
    //if (true) return;
    //if (true) return;



        String msg = loadMessage;

        if (msg.contains("RED")){
            msg = msg.replace("RED", "");
            int offset = font.getWidth(msg)/2;
            if (true) return;
            font.drawString(Display.getWidth()/2 - offset, Display.getHeight()/2, msg, Color.red);
        }else if (msg.contains("YELLOW")){
            msg = msg.replace("YELLOW", "");
            int offset = font.getWidth(msg)/2;
            if (true) return;
            font.drawString(Display.getWidth()/2 - offset, Display.getHeight()/2, msg, Color.yellow);

        }else{
            int offset = font.getWidth(msg)/2;
          if (true) return;
         font.drawString(Display.getWidth()/2 - offset, Display.getHeight()/2, msg, Color.magenta);

        }


    glEnableClientState(GL_VERTEX_ARRAY);
    glEnableClientState(GL_NORMAL_ARRAY);

    glEnable(GL_LIGHTING);
    glPopMatrix();
    glMatrixMode(GL_PROJECTION);
    glLoadMatrix(perspectiveProjectionMatrix);
    glMatrixMode(GL_MODELVIEW);

}

With the first one, nothing rendered (Edit: Text now renders, however still just blue behind it).

With the second one, everything except the text that this particular method is rendering worked perfectly.

So, what is the difference? I just can't fathom it. And I just can't fix it.

Result of test 1: enter image description here Result of test 2: enter image description here

Other relevant code:

static void setUpFonts() {
    java.awt.Font awtFont = new java.awt.Font("Times New Roman", java.awt.Font.BOLD, 18);
    font = new UnicodeFont(awtFont);
    font.getEffects().add(new ColorEffect(java.awt.Color.white));
    font.addAsciiGlyphs();
    try {
        font.loadGlyphs();
    } catch (SlickException e) {
        e.printStackTrace();
        cleanUp();
    }
}

Calling the method:

ready2D();
    TextDemo.setUpCamera();
    TextDemo.render();
    camSetup();
    ready3D();

Calling the method variation (Suggested in comments) which had no difference whatsoever:

    ready2D();
    glPushMatrix();
    TextDemo.setUpCamera();
    TextDemo.render();
    glPopMatrix();
    ready3D();

Readying 2D:

static void ready2D()
{
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();

    GLU.gluOrtho2D(0.0f, 1, 1, 0.0f);

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    glTranslatef(0.375f, 0.375f, 0.0f);

    glDisable(GL_DEPTH_TEST);
}

Ready 3D:

static void ready3D()
{
    glViewport(0, 0, Display.getWidth(),Display.getHeight());
    glMatrixMode(GL_PROJECTION);

    glLoadIdentity();
    GLU.gluPerspective(45, (float) Display.getWidth()/Display.getHeight(), 0.1f, 5000.0f);

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();

    glDepthFunc(GL_LEQUAL);
    glEnable(GL_DEPTH_TEST);
}

Ready2D and Ready3D are known to work, as I use them further down in the code to implement the health bar in one of the above screenshots with no issues.

Please tell me if any more information is required. I hope I have put enough.


Solution

  • We have

    if (TerrainDemo.reg){
      if (true) return;
    

    and

    static boolean reg = false;
    

    Since TerrainDemo.reg is false, the statement if (true) return isn't executed (unless you change the value of TerrainDemo.reg, obviously).

    Since your code works if the method render() returns almost immediately, then there must be an error in the OpenGL calls of said method that prevents the rest of the scene from rendering correctly, not a logical error. Use glGetError() to identify the error.