Hi,I'm learning OpenGL using LWJGL, but i have a litte question, and i've found no answer on google (but i might not search very well..).How can i write a text in my Display screen ?I want to add the number of the frame (not the FPS) and as i'm in fullscreen mode, i can't take it in the title of the Window..no way i want to know how to write a text in opengl.

PS : Is the Window class of LWJGL removed in the lasts versions ? Because i see many docs where they used the org.lwjgl.opengl.Window class, and i don't have it And maybe, an other question : what's the origin point in opengl (and so lwjgl) ? i believed that was the left bottom corner , but when i do :

1 2 3 4

glVertex2f(2,-2);glVertex2f(-2,-2);glVertex2f(-2,2);glVertex2f(2,2 );

The quad is drown at the top right corner..

Thanks for answering and sorry for my bad english (i'm a french man...).

PS : Is the Window class of LWJGL removed in the lasts versions ? Because i see many docs where they used the org.lwjgl.opengl.Window class, and i don't have it And maybe, an other question : what's the origin point in opengl (and so lwjgl) ? i believed that was the left bottom corner , but when i do :

The functionality of the Window class was mearged into the Display class a couple of versions back, and some docs havn't been updated yet. All the same stuff is still there though.

The origin depends on how you set up your projection and modelview matrices. By default LWJGL sets up the screen with the origin at the bottom left, with one pixel per unit. You can easily change this via glOrtho or glPerspective though.

For simple text, one of the NeHe tutorials does text with display lists and glDrawPixels, it should be ported to LWJGL somewhere.

For the text, i've found the nehe tutorial, but it looks a bit difficult, no portable as he said, and he seems use a special API for Windows (wgl) and Java and the Jogl ports used AWT and Canvas.But maybe, with de AWTGLCanvas, i can do something ? But it make me use AWT, and i want to be independant of it (taht's why i use lwjgl instead of jogl).

shame on me, i didn't see the LWJGL port. But it looks very very complexe for a simple text...Will be an easier method implemented in the nexts LWJGL versions ?

Text is fundamentally complicated, especially if you want nice crisp fonts and variable-width characters. In fact text in OpenGL is always going to be tricky, as font-rendering is almost always tied to a platform-specific API. I doubt you'll see anything 'simple' appear in LWJGL as it would be somewhat outside the aims.

If you want another method of drawing text you could try Cas' SPGL lib (on sourceforge), but be warned that it'll likely not be any simpler and doesn't exactly come with much documentation (although you might be able to find more details in the forums here).

Thansk, but in all little games in java, they used text in their opengl display, didn't they ?So, it's possible, and it's difficult to make a correct game without using text in it...I'm trying to make a little class, based on the NeHe tutorial, but for the moment, that just doesn't work...

I preprocess normal Java fonts and write them into .jgfont files, which contain a square luminance-alpha bitmap of all the glyphs in the font packed tightly, and a whole load of kerning information. The class in charge of the conversion is in the spgl-tools project, and it's com.shavenpuppy.jglib.tools.FontConverter. It creates com.shavenpuppy.jglib.Font objects. Then I load these into com.shavenpuppy.jglib.opengl.GLFont objects, which creates a GLTexture out of the bitmap; and then I use GLTextAreas and GLStrings in the same package to do rendering.

Method 11) Use AWT to draw the desired text onto a BufferedImage.2) Get the bytes from the BufferedImage and use them as an OpenGL Texture3) Draw a Textured Quad using OpenGL

Method 21) Use a paint program to draw all the letters into a .png graphics file. (A fixed width font like Courier is easiest)2) Read the graphics file into a BufferedImage3) Get the bytes from the BufferedImage and use them as an OpenGL Texture4) Draw a series of Quads, with the texture coordinates set so each Quad displays the correct letter

Method 3 (I've used this one)1) Use a paint program to draw all the letters into a .png graphics file. (A fixed width font like Courier is easiest)2) Write a java program to convert it into a series of NxN two colour bitmaps with the width a multiple of 83) Use glBitmap() (easy) or glDrawPixels() (harder) to paint each letter on the screen as required.

No, it doesn't. Its actually about as fast as it can get... that is as long as you dont use intermediate mode. One texture bind and then a call list per line of text.

If you do something similar in Java2D its also way faster than drawString.

Drawing a fart of polys with one texture isn't much work for your graphics card.

*selfquote*

"The speed seems to be ok. I get around 100 fps if I fill the whole screen with 4368 characters (that's 48lines with 91 characters each... all cramped together like the red/right stuff [line height 10]). Oh and my machine has only 500mhz and a gf2mx... so, yea... it should be fast enough "

4368 characters is a lot of text. (Don't forget that there are games which are <=4096 bytes.)

edit: Oh and I used vertex arrays instead of display lists because of rather odd circumstances. Well, with display lists its most likely faster.

Thanks, the last seems to be the best solution (for me, because i don't want to use AWT) ...

If you don't want to use AWT, that means you can't use BufferedImage. So... You need to make a separate program (which does use AWT) to read in the .png with the characterset. You then get the underlying bytes and save them to a file.

You AWTless program, then directly reads the bytes from the file and puts them into a texture or a bitmap. The textured Quad solution looks tempting, however I do have some code for glDrawPixels. It will slow your game down if you write lots of letters.

Here is part of my code to convert a .png file to a set of font characters. Note that this code misses out characters that aren't used in the game to save space

Now we need to open the data file (not shown) & stuff the data into a series of ByteBuffers. Of course you could just stick it in one texture & use texture coordinates to select the letter for each Quad, which is probably faster (but I have no code for it)

BTW. You need a fair bit of setup (not shown) to use glDrawPixels, so as to make the background transparent. glBitmap() is easier as the background is automatically transparent, but you can't scale the letters, nor can you do multi-colour (or anti-aliased) letters.

java-gaming.org is not responsible for the content posted by its members, including references to external websites,
and other references that may or may not have a relation with our primarily
gaming and game production oriented community.
inquiries and complaints can be sent via email to the info‑account of the
company managing the website of java‑gaming.org