Displaying Text Onscreen

Started by curlyfries0511, October 19, 2012, 16:32:51

Previous topic - Next topic

curlyfries0511

Hello. I am new to LWJGL so don't be hating. I want to display the coordinates (X, Y, Z) on screen but I can't work out how to do it.

This is my code:

package org.curlyfries.LWJGL;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.lwjgl.LWJGLException;
import org.lwjgl.input.Keyboard;
import org.lwjgl.input.Mouse;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import org.newdawn.slick.opengl.Texture;
import org.newdawn.slick.opengl.TextureLoader;
import static org.lwjgl.opengl.GL11.*;

public class LWJGLApplication {

	static boolean mousevis = false;
	static int count = 0;

	public static void main(String[] args) {
		initDisplay();
		gameLoop();
		cleanUp();
	}

	public static Texture loadTexture(String key) {
		try {
			return TextureLoader.getTexture("png", new FileInputStream(new File("res/" + key + ".png")));
		} catch (IOException e) {
			Logger.getLogger(LWJGLApplication.class.getName()).log(Level.SEVERE, null, e);
		}
		return null;
	}

	public static void gameLoop() {

		Texture wood = loadTexture("wood");

		Camera cam = new Camera(70, (float) Display.getWidth() / (float) Display.getHeight(), 0.3f, 1000);
		float x = 0;
		cam.setY(2);

		while (!Display.isCloseRequested()) {
			count++;

			if (!mousevis) {
				cam.rotateY(Mouse.getDX());
			}

			boolean forward = Keyboard.isKeyDown(Keyboard.KEY_W);
			boolean backward = Keyboard.isKeyDown(Keyboard.KEY_S);
			boolean left = Keyboard.isKeyDown(Keyboard.KEY_A);
			boolean right = Keyboard.isKeyDown(Keyboard.KEY_D);

			if (forward)
				cam.move(0.008f, 1);
			if (backward)
				cam.move(-0.008f, 1);
			if (left)
				cam.move(0.008f, 0);
			if (right)
				cam.move(-0.008f, 0);

			if (count == 100) {
				keycheck();
				count = 0;
			}

			if (Keyboard.isKeyDown(Keyboard.KEY_LSHIFT)) {
				cam.setY(cam.getY() - 0.005f);
			}

			if (Keyboard.isKeyDown(Keyboard.KEY_SPACE)) {
				cam.setY(cam.getY() + 0.005f);
			}

			Mouse.setGrabbed(!mousevis);

			glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
			glLoadIdentity();
			cam.useView();

			glPushMatrix();
			{
				glColor3f(1.0f, 0.5f, 0f);
				glTranslatef(0, 2, 0);
				glRotatef(x, 1, 1.5f, 0);

				wood.bind();

				glBegin(GL_QUADS);
				{
					// FrontFace
					glColor3f(1f, 0f, 0f);
					glTexCoord2f(0, 0);
					glVertex3f(-1, -1, 1);
					glTexCoord2f(0, 1);
					glVertex3f(-1, 1, 1);
					glTexCoord2f(1, 1);
					glVertex3f(1, 1, 1);
					glTexCoord2f(1, 0);
					glVertex3f(1, -1, 1);

					// BackFace
					glColor3f(0f, 1f, 0f);
					glTexCoord2f(0, 0);
					glVertex3f(-1, -1, -1);
					glTexCoord2f(0, 1);
					glVertex3f(-1, 1, -1);
					glTexCoord2f(1, 1);
					glVertex3f(1, 1, -1);
					glTexCoord2f(1, 0);
					glVertex3f(1, -1, -1);

					// BottomFace
					glColor3f(0f, 0f, 1f);
					glTexCoord2f(0, 0);
					glVertex3f(-1, -1, -1);
					glTexCoord2f(0, 1);
					glVertex3f(-1, -1, 1);
					glTexCoord2f(1, 1);
					glVertex3f(-1, 1, 1);
					glTexCoord2f(1, 0);
					glVertex3f(-1, 1, -1);

					// TopFace
					glColor3f(1f, 1f, 0f);
					glTexCoord2f(0, 0);
					glVertex3f(1, -1, -1);
					glTexCoord2f(0, 1);
					glVertex3f(1, -1, 1);
					glTexCoord2f(1, 1);
					glVertex3f(1, 1, 1);
					glTexCoord2f(1, 0);
					glVertex3f(1, 1, -1);

					// LeftFace
					glColor3f(0f, 1f, 1f);
					glTexCoord2f(0, 0);
					glVertex3f(-1, -1, -1);
					glTexCoord2f(0, 0);
					glVertex3f(1, -1, -1);
					glTexCoord2f(0, 0);
					glVertex3f(1, -1, 1);
					glTexCoord2f(0, 0);
					glVertex3f(-1, -1, 1);

					// Right Face
					glColor3f(1f, 0f, 1f);
					glTexCoord2f(0, 0);
					glVertex3f(-1, 1, -1);
					glTexCoord2f(0, 0);
					glVertex3f(1, 1, -1);
					glTexCoord2f(0, 0);
					glVertex3f(1, 1, 1);
					glTexCoord2f(0, 0);
					glVertex3f(-1, 1, 1);
				}
				glEnd();
			}
			glPopMatrix();
			x += 0.1f;
			Display.update();
		}
	}

	public static void cleanUp() {
		Display.destroy();
	}

	public static void keycheck() {
		if (Keyboard.isKeyDown(Keyboard.KEY_ESCAPE)) {
			mousevis = !mousevis;

			if (mousevis) {
				Mouse.setCursorPosition(Display.getWidth() / 2, Display.getHeight() / 2);
			}
		}
	}

	public static void initDisplay() {

		try {
			// Display.setFullscreen(true);
			Display.setDisplayMode(new DisplayMode(800, 600));
			Display.create();
		} catch (LWJGLException ex) {
			Logger.getLogger(LWJGLApplication.class.getName()).log(Level.SEVERE, null, ex);
		}
	}
}


package org.curlyfries.LWJGL;

import static org.lwjgl.opengl.GL11.*;
import static org.lwjgl.util.glu.GLU.*;

public class Camera {
	private float x;
	private float y;
	private float z;
	private float rx;
	private float ry;
	private float rz;

	private float fov;
	private float aspect;
	private float near;
	private float far;

	public Camera(float fov, float aspect, float near, float far) {
		x = 0;
		y = 0;
		z = 0;
		rx = 0;
		ry = 0;
		rz = 0;

		this.fov = fov;
		this.aspect = aspect;
		this.near = near;
		this.far = far;
		initProjection();
	}

	private void initProjection() {
		glMatrixMode(GL_PROJECTION);
		glLoadIdentity();
		gluPerspective(fov, aspect, near, far);
		glMatrixMode(GL_MODELVIEW);
		
		glEnable(GL_2D);
		glEnable(GL_DEPTH_TEST);
	}

	public void useView() {
		glRotatef(rx, 1, 0, 0);
		glRotatef(ry, 0, 1, 0);
		glRotatef(rz, 0, 0, 1);
		glTranslatef(x, y, z);
	}

	public float getX() {
		return x;
	}

	public float getY() {
		return -y;
	}

	public float getZ() {
		return z;
	}

	public void setX(float x) {
		this.x = x;
	}

	public void setY(float y) {
		this.y = -y;
	}

	public void setZ(float z) {
		this.z = z;
	}

	public float getRX() {
		return rx;
	}

	public float getRY() {
		return ry;
	}

	public float getRZ() {
		return rz;
	}

	public void setRX(float rx) {
		this.rx = rx;
	}

	public void setRY(float ry) {
		this.ry = ry;
	}

	public void setRZ(float rz) {
		this.rz = rz;
	}

	float speed = 0.01f;

	public void move(float amt, float dir) {
		z += amt * Math.sin(Math.toRadians(ry + 90 * dir));
		x += amt * Math.cos(Math.toRadians(ry + 90 * dir));
	}

	public void rotateY(float amt) {
		ry += amt;
	}
}


If anybody knows why I can't get it to work I will be very grateful.
Thanks in advance. :D
-Curly

Rekenaar


curlyfries0511

Quote from: Rekenaar on October 19, 2012, 21:16:29
You're using Slick so take a look here :)
http://www.lwjgl.org/wiki/index.php?title=Slick-Util_Library_-_Part_3_-_TrueType_Fonts_for_LWJGL
Last time I tried that it didn't work. I just tried it again and I get no errors but I can't see the text.

EDIT: This is my code. Have I missed anything?
package org.curlyfries.LWJGL;

import java.awt.Font;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.lwjgl.LWJGLException;
import org.lwjgl.input.Keyboard;
import org.lwjgl.input.Mouse;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import org.newdawn.slick.Color;
import org.newdawn.slick.TrueTypeFont;
import org.newdawn.slick.opengl.Texture;
import org.newdawn.slick.opengl.TextureLoader;
import static org.lwjgl.opengl.GL11.*;

public class LWJGLApplication {

	static boolean mousevis = false;
	static int count = 0;
	static TrueTypeFont font;

	public static void main(String[] args) {
		initDisplay();
		startup();
		gameLoop();
		cleanUp();
	}

	public static Texture loadTexture(String key) {
		try {
			return TextureLoader.getTexture("png", new FileInputStream(new File("res/" + key + ".png")));
		} catch (IOException e) {
			Logger.getLogger(LWJGLApplication.class.getName()).log(Level.SEVERE, null, e);
		}
		return null;
	}

	public static void gameLoop() {

		Texture wood = loadTexture("wood");

		Camera cam = new Camera(70, (float) Display.getWidth() / (float) Display.getHeight(), 0.3f, 1000);
		float x = 0;
		cam.setY(2);

		while (!Display.isCloseRequested()) {
			
			font.drawString(100, 50, "THE LIGHTWEIGHT JAVA GAMES LIBRARY", Color.yellow);
			
			count++;

			if (!mousevis) {
				cam.rotateY(Mouse.getDX());
			}

			boolean forward = Keyboard.isKeyDown(Keyboard.KEY_W);
			boolean backward = Keyboard.isKeyDown(Keyboard.KEY_S);
			boolean left = Keyboard.isKeyDown(Keyboard.KEY_A);
			boolean right = Keyboard.isKeyDown(Keyboard.KEY_D);

			if (forward)
				cam.move(0.008f, 1);
			if (backward)
				cam.move(-0.008f, 1);
			if (left)
				cam.move(0.008f, 0);
			if (right)
				cam.move(-0.008f, 0);

			if (count == 100) {
				keycheck();
				count = 0;
			}

			if (Keyboard.isKeyDown(Keyboard.KEY_LSHIFT)) {
				cam.setY(cam.getY() - 0.005f);
			}

			if (Keyboard.isKeyDown(Keyboard.KEY_SPACE)) {
				cam.setY(cam.getY() + 0.005f);
			}

			Mouse.setGrabbed(!mousevis);

			glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
			glLoadIdentity();
			cam.useView();

			glPushMatrix();
			{
				glColor3f(1.0f, 0.5f, 0f);
				glTranslatef(0, 2, 0);
				glRotatef(x, 1, 1.5f, 0);

				wood.bind();

				glBegin(GL_QUADS);
				{
					// FrontFace
					glColor3f(1f, 0f, 0f);
					glTexCoord2f(0, 0);
					glVertex3f(-1, -1, 1);
					glTexCoord2f(0, 1);
					glVertex3f(-1, 1, 1);
					glTexCoord2f(1, 1);
					glVertex3f(1, 1, 1);
					glTexCoord2f(1, 0);
					glVertex3f(1, -1, 1);

					// BackFace
					glColor3f(0f, 1f, 0f);
					glTexCoord2f(0, 0);
					glVertex3f(-1, -1, -1);
					glTexCoord2f(0, 1);
					glVertex3f(-1, 1, -1);
					glTexCoord2f(1, 1);
					glVertex3f(1, 1, -1);
					glTexCoord2f(1, 0);
					glVertex3f(1, -1, -1);

					// BottomFace
					glColor3f(0f, 0f, 1f);
					glTexCoord2f(0, 0);
					glVertex3f(-1, -1, -1);
					glTexCoord2f(0, 1);
					glVertex3f(-1, -1, 1);
					glTexCoord2f(1, 1);
					glVertex3f(-1, 1, 1);
					glTexCoord2f(1, 0);
					glVertex3f(-1, 1, -1);

					// TopFace
					glColor3f(1f, 1f, 0f);
					glTexCoord2f(0, 0);
					glVertex3f(1, -1, -1);
					glTexCoord2f(0, 1);
					glVertex3f(1, -1, 1);
					glTexCoord2f(1, 1);
					glVertex3f(1, 1, 1);
					glTexCoord2f(1, 0);
					glVertex3f(1, 1, -1);

					// LeftFace
					glColor3f(0f, 1f, 1f);
					glTexCoord2f(0, 0);
					glVertex3f(-1, -1, -1);
					glTexCoord2f(0, 0);
					glVertex3f(1, -1, -1);
					glTexCoord2f(0, 0);
					glVertex3f(1, -1, 1);
					glTexCoord2f(0, 0);
					glVertex3f(-1, -1, 1);

					// Right Face
					glColor3f(1f, 0f, 1f);
					glTexCoord2f(0, 0);
					glVertex3f(-1, 1, -1);
					glTexCoord2f(0, 0);
					glVertex3f(1, 1, -1);
					glTexCoord2f(0, 0);
					glVertex3f(1, 1, 1);
					glTexCoord2f(0, 0);
					glVertex3f(-1, 1, 1);
				}
				glEnd();
			}
			glPopMatrix();
			x += 0.1f;
			Display.update();
		}
	}

	public static void cleanUp() {
		Display.destroy();
	}

	public static void keycheck() {
		if (Keyboard.isKeyDown(Keyboard.KEY_ESCAPE)) {
			mousevis = !mousevis;

			if (mousevis) {
				Mouse.setCursorPosition(Display.getWidth() / 2, Display.getHeight() / 2);
			}
		}
	}
	
	public static void startup() {
		Font awtFont = new Font("Times New Roman", Font.BOLD, 24);
		font = new TrueTypeFont(awtFont, false);
	}

	public static void initDisplay() {

		try {
			// Display.setFullscreen(true);
			Display.setDisplayMode(new DisplayMode(800, 600));
			Display.create();
		} catch (LWJGLException ex) {
			Logger.getLogger(LWJGLApplication.class.getName()).log(Level.SEVERE, null, ex);
		}
	}
}

broumbroum

Quote from: curlyfries0511 on October 20, 2012, 08:03:24
Last time I tried that it didn't work. I just tried it again and I get no errors but I can't see the text.
Hello, you should always give a try to
Util.checkGLerror()
to read openGL error, if there are any, it will throw an OpenGLException.

curlyfries0511

Quote from: broumbroum on October 20, 2012, 11:39:46
Quote from: curlyfries0511 on October 20, 2012, 08:03:24
Last time I tried that it didn't work. I just tried it again and I get no errors but I can't see the text.
Hello, you should always give a try to
Util.checkGLerror()
to read openGL error, if there are any, it will throw an OpenGLException.
What does this do? And is this what I do with it?:
try {
				Util.checkGLError();
			} catch (OpenGLException e) {
				System.out.println(e);
			}

curlyfries0511

I've updated my code:
https://dl.dropbox.com/u/49046656/code.txt
Now I can see a huge yellow rectangle:
https://dl.dropbox.com/u/49046656/Capture.JPG

Does anybody know why it wont display any text?

Obsyd

Did you enable blending?

glEnable(GL_BLEND);

curlyfries0511

Quote from: Obsyd on October 21, 2012, 11:58:19
Did you enable blending?

glEnable(GL_BLEND);
I just added it and still got the same rectangle.

curlyfries0511

Change of plan. I don't think it is necessary to show the coords on screen. Instead I will do System.out.println(...);
Thanks for your help though

th3barri3

Ive got the same issue, its happened before but i cant remember what causes it. If i find out ill let you know.

My init code is...
glEnable(GL_TEXTURE_2D);
        glShadeModel(GL_SMOOTH);
        glDisable(GL_LIGHTING);
        glDisable(GL_DEPTH_TEST);
        
        glEnable(GL_BLEND);
        glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
        
        glClearColor(0, 0, 0, 0);
        glClearDepth(-1);
        
        glViewport(0, 0, cfg.width, cfg.height);
        glMatrixMode(GL_MODELVIEW);
        
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        
        glOrtho(0, cfg.width, cfg.height, 0, -1, 1);
        glMatrixMode(GL_MODELVIEW);


My font load code is...
UnicodeFont fnt = new UnicodeFont(new Font(name, style, size));
            
            fnt.getEffects().add(new ColorEffect(Color.WHITE));
            fnt.loadGlyphs();

I am using UnicodeFont out of the slick libraries.

My render code is...
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
        
        glDisable(GL_TEXTURE_2D);
        
        glColor3f(1, 0, 0);
        glBegin(GL_QUADS);
            glVertex2f(0, 0);
            glVertex2f(50, 0);
            glVertex2f(50, 50);
            glVertex2f(0, 50);
        glEnd();
        
        glEnable(GL_TEXTURE_2D);
        
        org.newdawn.slick.Color.green.bind();
        loaderFont.drawString(50, 50, message);
        
        Display.update();


Any help would be awesome!!!

broumbroum

Gl_MODULATE would enable translucent textures as font bitmaps are... I dont rember if its in the texenv param or just a switch to enable.. Please check it up. :)

Dresden

I've experimented a little bit with the UnicodeFont class from Slick. It requires slightly different initialization than TrueType, this link explains it well: http://slick.cokeandcode.com/wiki/doku.php?id=unicode_font_support

However, UnicodeFont seems to have an odd way of rendering strings. It messes up the OpenGL primitives and I scoured the source code a little but couldn't find any easy solution. This poster just used the deprecated TrueTypeFont class and it seemed to work: http://lwjgl.org/forum/index.php?topic=4868.0

My solution was to implement my own font texture and class because I will learn more from the experience. If you'd like some advice on how to go about this, or at least how I'm currently going about it I'd be happy to help.