• Create Account

## Java, JOGL, OpenGL 3.3, and Text

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

11 replies to this topic

### #1Glass_Knife  Moderators

Posted 27 February 2014 - 12:00 PM

My quest continues.  I have managed to get my Java/JOGL Opengl 3.3 core code running on Mac/Windows/Linux.

Now I just need to draw some text.  Should be simple enough...  and none of the examples in the JOGL demos use the shader pipeline.  The TextRenderer class, FPS code, etc, all use the GL2 interface which isn't available on the Mac.

Google suggested using the FreeType library, but I couldn't find that ported to Java.

Do you ever get the feeling that even though there seems to be a lot of information available, you seem to be the first person in history to actually try to run some code?

I'll post a solution if I ever figure it out.  In the mean time, if anyone can help, that would be legen (wait for it...) dary.

I think, therefore I am. I think? - "George Carlin"
My Website: Indie Game Programming

My Book: http://amzn.com/1305076532

### #2Jihodg  Members

Posted 27 February 2014 - 11:48 PM

There is no need to use freetype in java, since java already include functions to open fonts and render text or character to an image. After that you just need to decide how are you going to render your text in opengl... if it is mostly static strings (text that doesn't change often) you could just use java to generate an image of the complete string and copy that to an opengl texture and render it as a single quad, but if you have lots of changing string you would better do the common method of rendering all the characters needed to an image, moving it to a texture, and render a quad per character.

Basically it is all standard text rendering in opengl like in any other language, the only difference is the glyph (character) rendering and truetype font processing, in this case using java existing methods.

Edited by Jihodg, 27 February 2014 - 11:52 PM.

### #3Glass_Knife  Moderators

Posted 28 February 2014 - 08:08 AM

There is no need to use freetype in java, since java already include functions to open fonts and render text or character to an image. After that you just need to decide how are you going to render your text in opengl... if it is mostly static strings (text that doesn't change often) you could just use java to generate an image of the complete string and copy that to an opengl texture and render it as a single quad, but if you have lots of changing string you would better do the common method of rendering all the characters needed to an image, moving it to a texture, and render a quad per character.

Basically it is all standard text rendering in opengl like in any other language, the only difference is the glyph (character) rendering and truetype font processing, in this case using java existing methods.

For some reason, I imagined that there was a simple way that didn't have me writing my own bitmap font code.  I always like the GLU and GLUT libraries when I was first learning graphics.  Making a single call to render a teapot really helped you see what was possible.  I figured there was something else like that for the new shader stuff.

I will get some code rendering on a texture and billboarding that in the display and I will go from there.

I think, therefore I am. I think? - "George Carlin"
My Website: Indie Game Programming

My Book: http://amzn.com/1305076532

### #4Glass_Knife  Moderators

Posted 28 February 2014 - 06:07 PM

   glTexImage2D(GL_TEXTURE_2D, 0, ???, image.getWidth(), image.getHeight(), 0, ???, GL_UNSIGNED_BYTE, buffer);

So far I've gotten the textures to kind of work, but I have been unsuccessful creating my own texture.  I made a BufferedImage and
extracted the data, but when the buffer rendered, the pixel data was screwed up, which I assume means I didn't pick the correct
OpenGL pixel format.

More tests are needed...


Edited by Glass_Knife, 28 February 2014 - 06:08 PM.

I think, therefore I am. I think? - "George Carlin"
My Website: Indie Game Programming

My Book: http://amzn.com/1305076532

### #53TATUK2  Members

Posted 28 February 2014 - 06:37 PM

If you want to use some method of loading the font into that image texture, you'll have to supply the lib/function in order for anyone to have an idea of what might be wrong.

Anyway, this isn't the best approach for various reasons including performance and being proprietary.

http://en.wikibooks.org/wiki/OpenGL_Programming/Modern_OpenGL_Tutorial_Text_Rendering_01

### #6TheChubu  Members

Posted 28 February 2014 - 07:10 PM

If you want to do the text rendering yourself, no idea.

But NiftyGUI is the only OpenGL based GUI lib that I know of that supports an OpenGL core profile renderer. Has some standard widgets, text, etc. Haven't tried yet though since I'm still messing with my renderer's design. Its the standard widget lib of jMonkeyEngine.

The other OpenGL GUI lib for Java is TWL, which has tons of controls and a nice editor... but it runs on fixed function pipeline (AFAIK, 1.1 specifically). You might want to try to implement its backed with OGL 3 core but I haven't seen a clear path to do so. I mean, there is a clear path (it has separate backends so you can implement the renderer interface yourself) but I'm not sure its design lends itself to the programmable pipeline.

Lastly, I found that uploading compressed texture formats (ie, glCompressedTexImage) looks remarkably easier than loading uncompressed data. Just read the data in a buffer, slice the buffer for each mip map, and upload to the GPU. There are various DDS loaders for Java around the web (or you can implement your own having MSDN DDS reference at hand). And its easier than mess up with the various "automatic" mip map generation APIs there are for OpenGL (I think the OGL wiki mentioned like 3 different ways to generate mip maps).

"I AM ZE EMPRAH OPENGL 3.3 THE CORE, I DEMAND FROM THEE ZE SHADERZ AND MATRIXEZ"

My journals: dustArtemis ECS framework and Making a Terrain Generator

### #7Glass_Knife  Moderators

Posted 01 March 2014 - 08:56 AM

If you want to do the text rendering yourself, no idea.

I am not trying to make a user interface.  I have a display like matlab that needs a labeled graph, and I need to place text on all the tick marks.  It needs to translate and rotate, but always face the user.

I think, therefore I am. I think? - "George Carlin"
My Website: Indie Game Programming

My Book: http://amzn.com/1305076532

### #8Glass_Knife  Moderators

Posted 04 March 2014 - 01:26 PM

So after messing with this stuff, I have arrived at my first conclusion.  Trying to render a text string to a texture and then billboard that to the 3D display just doesn't work.  At least, what happens is that the text is so small I can't read it.

It was difficult to figure out what size font to use to maximize the area of a 256x256 texture, but after mipmaps, things are just fuzzy.

So now I am going to try to find the positions where the text would go, project those to screen space, and try and draw text on the window in the right location.  I'm not sure this will work either, but it seems like the next logical step.

I think, therefore I am. I think? - "George Carlin"
My Website: Indie Game Programming

My Book: http://amzn.com/1305076532

### #93TATUK2  Members

Posted 04 March 2014 - 07:22 PM

Obviously in order for the text to not be fuzzy, you have to use an appropriately sized texture. You have to use some custom calculations and automatic dynamic "regconfigurations" in order to "maximize the area". Basically, for a string in question which you want to render... Take a base font size and do a faux "render" of it. (in order to determine total horizontal pixel value -- of course this "faux render" can/should be it's own separate individual function which does no actual rendering and only the minimal required to get this total horizontal pixel value number). Save the total horizontal pixel value of said string. Then plug that number in with your desired texture size to get the font size you "actually" need. Use that font size for the real rendering.

### #10Glass_Knife  Moderators

Posted 05 March 2014 - 08:11 AM

Obviously in order for the text to not be fuzzy, you have to use an appropriately sized texture. You have to use some custom calculations and automatic dynamic "regconfigurations" in order to "maximize the area". Basically, for a string in question which you want to render... Take a base font size and do a faux "render" of it. (in order to determine total horizontal pixel value -- of course this "faux render" can/should be it's own separate individual function which does no actual rendering and only the minimal required to get this total horizontal pixel value number). Save the total horizontal pixel value of said string. Then plug that number in with your desired texture size to get the font size you "actually" need. Use that font size for the real rendering.

Yep, I did all that.  Created a 256x256 texture and procedurally kept increasing the font until it was as large as possible without going outside the bounds of the texture.  When rendered as a quad, it looked fantastic.  But the when the scene was zoomed out, the font got so small that you still couldn't read it.

This is why I am doing the text as an overlay.

If anyone is curious what it is I'm trying to accomplish, I am trying to copy a 3D Matlab graph view with scale labels.

I think, therefore I am. I think? - "George Carlin"
My Website: Indie Game Programming

My Book: http://amzn.com/1305076532

### #113TATUK2  Members

Posted 05 March 2014 - 10:16 AM

Try either using substantially larger than 256, or don't use mipmaps?

### #12Glass_Knife  Moderators

Posted 06 March 2014 - 08:28 AM

I did manage to get this working.  Text is rendered on a full-screen quad overlay like a HUD.  The positions of the text in the 3D world are projected into screen space, and those points are used to render the text to the 2D overlay texture.  When the world is zoomed out, the text moves but doesn't change size, so it is always legible.

There could be more work done to keep the text from overlapping when zoomed out too much, but that isn't even needed for this data view.

But man, did that take longer than I thought.

I think, therefore I am. I think? - "George Carlin"
My Website: Indie Game Programming