Jump to content
  • Advertisement
Sign in to follow this  
Glass_Knife

OpenGL Java, JOGL, OpenGL 3.3, and Text

This topic is 2052 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
Advertisement

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

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites
   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

Share this post


Link to post
Share on other sites

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).

Share this post


Link to post
Share on other sites


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.

Share this post


Link to post
Share on other sites

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.  

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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.  

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!