My text renderer is coloring my screen black

Started by Daimoth, December 05, 2013, 02:48:12

Previous topic - Next topic

Daimoth

Hi everyone, yet another openGL newcomer. I am making my way through the red book, but I figured asking here might get me an answer quicker.

My text renders, but it colors the entire screen black to do so. And it remains black even after I've stopped rendering the text. The text is just the word "pause", which obviously renders when the game is paused.

Anyways, the src:

Relevant methods from TextRenderer.ttf
public void init() {
		// load a default java font
		Font awtFont = new Font("Times New Roman", Font.BOLD, 24);
		font = new TrueTypeFont(awtFont, antiAlias);
		
		// load font from file
		try {
			InputStream inputStream	= ResourceLoader.getResourceAsStream("res/font1.ttf");
			
			Font awtFont2 = Font.createFont(Font.TRUETYPE_FONT, inputStream);
			awtFont2 = awtFont2.deriveFont(12f); // set font size... 56 is apparently max without bugs?
			font2 = new TrueTypeFont(awtFont2, antiAlias);
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void render(int xPos, int yPos, String string, Color color) {
		Color.white.bind();
 
		//font.drawString(100, 50, "THE LIGHTWEIGHT JAVA GAMES LIBRARY", Color.yellow);
		font2.drawString(xPos, yPos, string, color);
	}


Relevant methods from my canvass class
public static void main(String[] args) {

		try {
			Display.setDisplayMode(new DisplayMode(width, height));
			Display.setTitle("W O R M");
			Display.setVSyncEnabled(true);
			Display.create();
			setupAudio();
			textrenderer.init();
		} catch (LWJGLException e) {
			e.printStackTrace();
			terminate(1);
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		logo = loadTexture("logo1");
		
		glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        // (0, 0) is center, (1, 1) is the upper-right, (-1, -1) is the bottom-left
        //glOrtho(1, 1, 1, 1, 1, -1);
        glOrtho(0, 640, 480, 0, 1, -1);
        glMatrixMode(GL_MODELVIEW);
        // Enable trancluency
        glEnable(GL_BLEND);
        glEnable(GL_TEXTURE_2D);
        glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
        
        // GAME LOOP \\
		while(!Display.isCloseRequested()) {
			if(!paused){
				tick();
			}
			checkInput();
			render();
			Display.update();
			Display.sync(60);
		}
		// GAME LOOP \\
		
        logo.release();
		terminate(0);
	}

private static void render() {
        glClear(GL_COLOR_BUFFER_BIT);
        switch (state) {       
            case GAME:
				 glColor3f(0.065f, 0.065f, 0.065f);
				 glRectf(0, 0, 640, 480);
				 glBegin(GL_QUADS);                  
             //RENDER SPRITE
             if(dir == Dir.RIGHT){
	             glColor3b((byte) 0, (byte) 0, (byte) 0);				//BLACK
	             glVertex2f(initPosX, initPosY);                                 // Upper-left
	             glColor3b((byte) 50, (byte) 50, (byte) 100);			//BLUE
	             glVertex2f(initPosX + spriteWidth, initPosY);                   // Upper-right
	             glColor3b((byte) 50, (byte) 50, (byte) 100);			//BLUE
	             glVertex2f(initPosX + spriteWidth, initPosY + spriteHeight);    // Bottom-right
	             glColor3b((byte) 0, (byte) 0, (byte) 0);				//BLACK
	             glVertex2f(initPosX, initPosY + spriteHeight);                  // Bottom-left
             } else if (dir == Dir.LEFT){
	             glColor3b((byte) 50, (byte) 50, (byte) 100);			//BLUE
	             glVertex2f(initPosX, initPosY);                                 // Upper-left
	             glColor3b((byte) 0, (byte) 0, (byte) 0);				//Black
	             glVertex2f(initPosX + spriteWidth, initPosY);                   // Upper-right
	             glColor3b((byte) 0, (byte) 0, (byte) 0);				//black
	             glVertex2f(initPosX + spriteWidth, initPosY + spriteHeight);    // Bottom-right
	             glColor3b((byte) 50, (byte) 50, (byte) 100);			//BLUE
	             glVertex2f(initPosX, initPosY + spriteHeight);                  // Bottom-left
             }
             glEnd();
             
             //RENDER PAUSE MENU
             if(paused){
	             glColor4b((byte) 50, (byte) 50, (byte) 100, (byte)60);
	             glRectf(width - 175, height - 150, 175, 150);
	             textrenderer.render(width - 180, height - 155, "PAUSE", Color.white);	             
             }
             
             if(fade >= 0){
                 glColor4f(0F, 0F, 0F, (float) Math.sin(Math.toRadians(fade)));
                 glRectf(0, 0, 640, 480); 
                 System.out.println(fade);
             	fade -= 2F;
             }             
                break;            
        }
    }

Cornix

What kind of class is "Font" and where did you get it from?
As far as I know there is no text-rendering library build into LWJGL.

Fool Running

Best guess is that the TrueTypeFont.drawString method is changing the texture coordinates. You are not specifying any texture coordinates for your other stuff (i.e. the stuff that is not text) and yet you are enabling texturing (glEnable(GL_TEXTURE_2D)). You should either disable texturing (glDisable(GL_TEXTURE_2D)) until you draw your text, or you should specify your texture coordinates for your other stuff.
Programmers will, one day, rule the world... and the world won't notice until its too late.Just testing the marquee option ;D