Displaying Text in OpenGL

Started by chrizo, February 06, 2008, 15:59:46

Previous topic - Next topic

chrizo

Greetings to all, I'm new here.  :)

A Question if you may.... Can anybody show me how to display text with LWJGL?

I hope my request won't bore you guys...  ;) Honestly, I'm a noob to OpenGL and Java but, i have basic knowledge of C.

I finally got Eclipse, Java & LWJGL working with the rotating quad and all... and I was like Holy crap WOW!!! I'm suddenly very interested about it.... ;D

I assure you guys, I can learn quickly... well, atleast thats what i think. :P



My apologies in advance if i don't reply quickly. Its already past my bedtime where I live.

Cheers :)

elias4444

In a nutshell: You paint the texture of the letter you want onto a quad.

And there's several ways to get the texture. You can either use premade textures (that you make using gimp or photoshop) with a single letter on each one, or one big texture with all the letters on it (and then just map with texture coordinates which one you want to display). Or you can dynamically create the texture(s) in your program using a truetype font in Java2D, and then translating the image into a texture.

Lot's of ways to do it.  ;D
=-=-=-=-=-======-=-=-=-=-=-
http://www.tommytwisters.com

chrizo

hmm, like placing decals on the quad? is that the way to do it?

I'm not sure if thats what I have in mind :)

I'm looking for something that resembles "floating text" on the console in some games where you can dynamically type some words directly on screen. I need it to feedback some information like frames per second etc.

I checked the Opengl website, Thats bitmaped font i guess. There is a link to the article... but its dead :(

http://www.opengl.org/resources/features/fontsurvey/

;D

chrizo


wolf_m

The way I do it:

I have a texture with all letters on it, made in Gimp.
I have a Blender project with one quad per letter and the UV-mapped letter from the texture mentioned above on it.
I export this project into the 3D format my engine can import, and then have quads with letters on them in my engine, in DisplayLists, to be precise. When I want to place a letter, I do glPushMatrix(), transform to where I want to place the letter, call the DisplayList of the letter to draw it and do glPopMatrix().

This is the way elias4444 described in his 3rd sentence above, and it works fine, especially for consoles and the like. You can even use letters of arbitrary width that way, or dynamically resize, skew, distort letters. You can even apply some shaders to them or switch the alphabet texture if the alphabet textures use the same layout.
Note that I disable GL_LIGHTING temporarily to draw a letter so that it isn't affected by lighting and that my alphabet texture is a PNG with alpha values for transparency. I hope I didn't forget anything.. Meh, you'll figure it out, it's not that hard.

This way of putting letters on the screen has advantages and disadvantages; but it gets the job done.

Also note that I didn't implement it that way in the Tetris clone mentioned in my signature; this is a very old project with some weird ways to do things in it. We all started somewhere.

chrizo

Hey, it works :)

i tried the tutorial from Nehe using ttf textures, quads and display lists.

But is there any other way? 2 Triangles per character...

It seems like an aweful waste of triangles... :D  ::)

"you can dynamically create the texture(s) in your program using a truetype font in Java2D, and then translating the image into a texture." - elias444

I suppose its possible to dynamically create a whole paragraph, turn it into a texture and stick it to one big quad? But, which one is faster computationally? The first one utilizes a displaylist but it eats up a lot of triangles, while the other keeps re-texturing itself??


elias4444

I wouldn't dynamically create the paragraph texture... any time you create a texture, it's computationally expensive. It's generally better to pre-cache them. I would recommend the bitmap method, or generating the texture(s) for each letter (and pre-caching them).

Also, I wouldn't worry too much about the number of triangles/quads you need for text. It's pretty low compared to most 3D objects (even if you draw out a paragraph). That's why we have 3D accelerated graphic chips these days.  ;)

=-=-=-=-=-======-=-=-=-=-=-
http://www.tommytwisters.com

chrizo

Ahh, many thanks guys.  :)

Ill try to play around with these and see what i can do.  ;D

biscon

I can mail you a class that builds a bitmap font with java2D and has a handy print method if you like.
My approach is quite simple. Render all characters to one texture, then render a quad for each letter you want to print, manipulating
texcoords to get the correct letter, it is fast too (immediate mode isn't so bad when you're only rendering one quad per letter, of course that depends on how much text you're likely to print at once).

numberR

for my case, i needed to display japanese characters, so it wasn't smart to have bunch of images with characters on them. i decided to use AWT for drawing characters dynamically to BufferedImage and created texture out of it. as mentioned, it is much slower solution, but now you can change font dynamically, draw any characters that AWT can handle, and draw in any size.

Se7enDays

Hi biscon, could you mail me your class? thx

xion.luhnis

Hi biscon - and everybody else since it's my first post on this forum. I'm new to lwjgl (and opengl) and hope this forum will help me to learn using opengl and lwjgl of course.
I would also be really interested in the class you've done. Is it normal there's no class like the TextRenderer of JOGL (java2d in fact) for rendering text via opengl ?
The fact is that it's important to have the most optimized class when we have to display some fast-variable text on the screen. The TextRenderer from sun is interesant due to the cache system it contains. Don't know if we can just use it with lwjgl... may be ?

Matzon


xion.luhnis

Thanks a lot. It seems to go beyond what I expected in fact...  ;) (thinking of the other features of this library)

Matzon

the purpose of the library is to complement lwjgl. it was extracted from the base slick library to help developers now that we have removed DevIL and FMOD.