Drawing Text using Slick Libary

Started by filoe, January 02, 2013, 19:03:34

Previous topic - Next topic

filoe

I am a newcomer in java and lwjgl. Now I tried to render a text and found the slick libary(if there is a "easy" way to solve my problem without slick please tell me).
Here is my code which I am using to render the text:
public class FontRenderer implements IComponent
{
	private UnicodeFont _font;
	private FloatBuffer _orthographicProjectionMatrix;
	private Color _color;
	private String _text;
	
	public FontRenderer()
	{
		this(new UnicodeFont(new Font("Times New Roman", Font.BOLD, 18)),
							 Color.white);
	}
	public FontRenderer(UnicodeFont font, Color color)
	{
		_font = font;
		_color = color;
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public void load(int time) {
		_font.getEffects().add(new ColorEffect(_color));
		_font.addAsciiGlyphs();
		try {
			_font.loadGlyphs();
		} catch (SlickException e) {
			System.out.println("Fehler beim Laden einer Schriftart.");
			e.printStackTrace();
		}
		
		_orthographicProjectionMatrix = BufferUtils.createFloatBuffer(16);
		_perspectiveProjectionMatrix = BufferUtils.createFloatBuffer(16);
		createMatrix(Display.getWidth(), Display.getHeight());
		
		//setup blending
		glEnable(GL_BLEND);
		glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
	}

	@Override
	public void draw(int time) {
		FloatBuffer perspectiveProjectionMatrix = BufferUtils.createFloatBuffer(16);
		glGetFloat(GL_MODELVIEW_MATRIX, perspectiveProjectionMatrix);
		
		glMatrixMode(GL_PROJECTION);
		glLoadMatrix(_orthographicProjectionMatrix);
		glMatrixMode(GL_MODELVIEW);
		glPushMatrix();
		glLoadIdentity();
		
		boolean enableLight = glGetBoolean(GL_LIGHTING);
		glDisable(GL_LIGHTING);
		
		_font.drawString(0, 0, _text);
		
		if(enableLight) glEnable(GL_LIGHTING);
		
		glLoadIdentity();
		glPopMatrix();
		glLoadMatrix(perspectiveProjectionMatrix);
		glMatrixMode(GL_PROJECTION);
		glLoadMatrix(_perspectiveProjectionMatrix);
		glMatrixMode(GL_MODELVIEW);
	}
	
	private void createMatrix(double width, double height)
	{	
		FloatBuffer perspectiveProjectionMatrix = BufferUtils.createFloatBuffer(16);
		glGetFloat(GL_MODELVIEW_MATRIX, perspectiveProjectionMatrix);
		glGetFloat(GL_PROJECTION_MATRIX, _perspectiveProjectionMatrix);
		glMatrixMode(GL_PROJECTION);
		glLoadIdentity();
		glOrtho(0, width, height, 0, 1, -1);
		glGetFloat(GL_PROJECTION_MATRIX, _orthographicProjectionMatrix);
		glLoadMatrix(_perspectiveProjectionMatrix);
		glMatrixMode(GL_MODELVIEW);
	}
}


My problem is that the text is rendered correctly but I can't see my rendered spheres, boxes,... anymore.
Sometimes if I move my camera exactly to one point(its always a different one) I can see half drawn objects in the 3d space.
You can see that in my attached file.

So I hope you can help me to solve that problem.
Ah and by the way I am rendering the text after rendering the 3d models.

Fool Running

It looks to me like you are over-complicating things. The best thing to do is to draw all your text/GUI last and not worry about having to mess with so many matrix manipulations:
This should be in your main loop:
public class MainLoopClass
{
	private FontRenderer _fontRenderer;

	public void Initialize()
	{
		// Do the other OpenGL stuff you need to set up in here
		glEnable(GL_BLEND);
		glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

		_fontRenderer = new FontRenderer(); // Or whatever you want to pass in to the constructor
		_fontRenderer.load(0);
	}
	public void MainLoop()
	{
		// Draw 3D stuff and other stuff you need to do

		glDisable(GL_LIGHTING);
		glMatrixMode(GL_PROJECTION);
		glLoadIdentity();
		glOrtho(0, width, height, 0, 1, -1);

		glMatrixMode(GL_MODELVIEW);
		glPushMatrix();
		glLoadIdentity();
		
		_fontRenderer.draw(time, "This is my string I want to draw");
		
		glPopMatrix();
	}
}

Your FontRenderer class should be much simpler, like this:
public class FontRenderer implements IComponent
{
	private UnicodeFont _font;
	private Color _color;
	
	public FontRenderer()
	{
		this(new UnicodeFont(new Font("Times New Roman", Font.BOLD, 18)),
							 Color.white);
	}
	public FontRenderer(UnicodeFont font, Color color)
	{
		_font = font;
		_color = color;
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public void load(int time) {
		_font.getEffects().add(new ColorEffect(_color));
		_font.addAsciiGlyphs();
		try {
			_font.loadGlyphs();
		} catch (SlickException e) {
			System.out.println("Fehler beim Laden einer Schriftart.");
			e.printStackTrace();
		}
	}

	@Override
	public void draw(int time, string text) {
		_font.drawString(0, 0, text);
	}
}
Programmers will, one day, rule the world... and the world won't notice until its too late.Just testing the marquee option ;D

filoe

Still the same problem. I tried it out. With my and your solution I everything works fine until i add the line:
_font.drawString(0, 0, _text);
.
This line is the only problem. I just need to comment that line and everything works fine.

EDIT: I just took a closer look to this article: http://www.lwjgl.org/wiki/index.php?title=Slick-Util_Library_-_Part_3_-_TrueType_Fonts_for_LWJGL. If I am using TrueTypeFont it works fine :) thx.
EDIT2: But there is still one problem left. I can't use a new line :(