Sign in to follow this  

OpenGL Java, JOGL, OpenGL 3.3, and Text

This topic is 1413 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

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

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

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.

Share this post


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

  • Similar Content

    • By xhcao
      Does sync be needed to read texture content after access texture image in compute shader?
      My simple code is as below,
      glUseProgram(program.get());
      glBindImageTexture(0, texture[0], 0, GL_FALSE, 3, GL_READ_ONLY, GL_R32UI);
      glBindImageTexture(1, texture[1], 0, GL_FALSE, 4, GL_WRITE_ONLY, GL_R32UI);
      glDispatchCompute(1, 1, 1);
      // Does sync be needed here?
      glUseProgram(0);
      glBindFramebuffer(GL_READ_FRAMEBUFFER, framebuffer);
      glFramebufferTexture2D(GL_READ_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
                                     GL_TEXTURE_CUBE_MAP_POSITIVE_X + face, texture[1], 0);
      glReadPixels(0, 0, kWidth, kHeight, GL_RED_INTEGER, GL_UNSIGNED_INT, outputValues);
       
      Compute shader is very simple, imageLoad content from texture[0], and imageStore content to texture[1]. Does need to sync after dispatchCompute?
    • By Jonathan2006
      My question: is it possible to transform multiple angular velocities so that they can be reinserted as one? My research is below:
      // This works quat quaternion1 = GEQuaternionFromAngleRadians(angleRadiansVector1); quat quaternion2 = GEMultiplyQuaternions(quaternion1, GEQuaternionFromAngleRadians(angleRadiansVector2)); quat quaternion3 = GEMultiplyQuaternions(quaternion2, GEQuaternionFromAngleRadians(angleRadiansVector3)); glMultMatrixf(GEMat4FromQuaternion(quaternion3).array); // The first two work fine but not the third. Why? quat quaternion1 = GEQuaternionFromAngleRadians(angleRadiansVector1); vec3 vector1 = GETransformQuaternionAndVector(quaternion1, angularVelocity1); quat quaternion2 = GEQuaternionFromAngleRadians(angleRadiansVector2); vec3 vector2 = GETransformQuaternionAndVector(quaternion2, angularVelocity2); // This doesn't work //quat quaternion3 = GEQuaternionFromAngleRadians(angleRadiansVector3); //vec3 vector3 = GETransformQuaternionAndVector(quaternion3, angularVelocity3); vec3 angleVelocity = GEAddVectors(vector1, vector2); // Does not work: vec3 angleVelocity = GEAddVectors(vector1, GEAddVectors(vector2, vector3)); static vec3 angleRadiansVector; vec3 angularAcceleration = GESetVector(0.0, 0.0, 0.0); // Sending it through one angular velocity later in my motion engine angleVelocity = GEAddVectors(angleVelocity, GEMultiplyVectorAndScalar(angularAcceleration, timeStep)); angleRadiansVector = GEAddVectors(angleRadiansVector, GEMultiplyVectorAndScalar(angleVelocity, timeStep)); glMultMatrixf(GEMat4FromEulerAngle(angleRadiansVector).array); Also how do I combine multiple angularAcceleration variables? Is there an easier way to transform the angular values?
    • By dpadam450
      I have this code below in both my vertex and fragment shader, however when I request glGetUniformLocation("Lights[0].diffuse") or "Lights[0].attenuation", it returns -1. It will only give me a valid uniform location if I actually use the diffuse/attenuation variables in the VERTEX shader. Because I use position in the vertex shader, it always returns a valid uniform location. I've read that I can share uniforms across both vertex and fragment, but I'm confused what this is even compiling to if this is the case.
       
      #define NUM_LIGHTS 2
      struct Light
      {
          vec3 position;
          vec3 diffuse;
          float attenuation;
      };
      uniform Light Lights[NUM_LIGHTS];
       
       
    • By pr033r
      Hello,
      I have a Bachelor project on topic "Implenet 3D Boid's algorithm in OpenGL". All OpenGL issues works fine for me, all rendering etc. But when I started implement the boid's algorithm it was getting worse and worse. I read article (http://natureofcode.com/book/chapter-6-autonomous-agents/) inspirate from another code (here: https://github.com/jyanar/Boids/tree/master/src) but it still doesn't work like in tutorials and videos. For example the main problem: when I apply Cohesion (one of three main laws of boids) it makes some "cycling knot". Second, when some flock touch to another it scary change the coordination or respawn in origin (x: 0, y:0. z:0). Just some streng things. 
      I followed many tutorials, change a try everything but it isn't so smooth, without lags like in another videos. I really need your help. 
      My code (optimalizing branch): https://github.com/pr033r/BachelorProject/tree/Optimalizing
      Exe file (if you want to look) and models folder (for those who will download the sources):
      http://leteckaposta.cz/367190436
      Thanks for any help...

    • By Andrija
      I am currently trying to implement shadow mapping into my project , but although i can render my depth map to the screen and it looks okay , when i sample it with shadowCoords there is no shadow.
      Here is my light space matrix calculation
      mat4x4 lightViewMatrix; vec3 sun_pos = {SUN_OFFSET * the_sun->direction[0], SUN_OFFSET * the_sun->direction[1], SUN_OFFSET * the_sun->direction[2]}; mat4x4_look_at(lightViewMatrix,sun_pos,player->pos,up); mat4x4_mul(lightSpaceMatrix,lightProjMatrix,lightViewMatrix); I will tweak the values for the size and frustum of the shadow map, but for now i just want to draw shadows around the player position
      the_sun->direction is a normalized vector so i multiply it by a constant to get the position.
      player->pos is the camera position in world space
      the light projection matrix is calculated like this:
      mat4x4_ortho(lightProjMatrix,-SHADOW_FAR,SHADOW_FAR,-SHADOW_FAR,SHADOW_FAR,NEAR,SHADOW_FAR); Shadow vertex shader:
      uniform mat4 light_space_matrix; void main() { gl_Position = light_space_matrix * transfMatrix * vec4(position, 1.0f); } Shadow fragment shader:
      out float fragDepth; void main() { fragDepth = gl_FragCoord.z; } I am using deferred rendering so i have all my world positions in the g_positions buffer
      My shadow calculation in the deferred fragment shader:
      float get_shadow_fac(vec4 light_space_pos) { vec3 shadow_coords = light_space_pos.xyz / light_space_pos.w; shadow_coords = shadow_coords * 0.5 + 0.5; float closest_depth = texture(shadow_map, shadow_coords.xy).r; float current_depth = shadow_coords.z; float shadow_fac = 1.0; if(closest_depth < current_depth) shadow_fac = 0.5; return shadow_fac; } I call the function like this:
      get_shadow_fac(light_space_matrix * vec4(position,1.0)); Where position is the value i got from sampling the g_position buffer
      Here is my depth texture (i know it will produce low quality shadows but i just want to get it working for now):
      sorry because of the compression , the black smudges are trees ... https://i.stack.imgur.com/T43aK.jpg
      EDIT: Depth texture attachment:
      glTexImage2D(GL_TEXTURE_2D, 0,GL_DEPTH_COMPONENT24,fbo->width,fbo->height,0,GL_DEPTH_COMPONENT,GL_FLOAT,NULL); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, fbo->depthTexture, 0);
  • Popular Now