Java-Gaming.org Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (775)
Games in Android Showcase (230)
games submitted by our members
Games in WIP (856)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  [Solved][JOGL] Rendering text  (Read 709 times)
0 Members and 1 Guest are viewing this topic.
Offline Chennorris

Senior Newbie


Exp: 10 years



« Posted 2018-02-20 10:49:08 »

Hello everyone,

As many JOGL developers, I'm facing troubles to easily render text over a 3D scene (the TextRenderer class didn't provide me suitable results as it always ended with a "not a GL2 context" error). Then I decided to create my own class that could do the job and after a few days, I managed to render text with the following steps :
  • creation of a 2D graphics structure,
  • rendering my text inside a buffered image using this 2D graphics structure,
  • generating some texture data from the buffered image,
  • rendering a 2D square in the foreground where the texture data is applyied.

The code that implements those steps is available below Wink

I'm starting to get very interesting results but the fact is that my 2D square always have a black background. I can solve my problem inside the vertex shader, re-computing the alpha value with 100% black => 0% alpha, but it's not a very nice solution to me. Anyway, here's the vertex shader code if anybody's interested :
1  
2  
3  
4  
5  
void main() {
    vec4 textureColor = texture(defaultTexture, UvCoordinates);
    outColor.a = 1. - (textureColor.r + textureColor.g + textureColor.b) / 3.;
    outColor.rgb = vec3(1, 1, 1) - textureColor.rgb;
}


Considering the following code that implements text rendering into texture data, does someone see why my texture data always have a black background, please ?

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
public void drawFont(GL3 gl, float x, float y, String text, Color color, int size) {
    // Generating the font
    BufferedImage bufferedImage = new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB);
    Graphics2D graphics = bufferedImage.createGraphics();
    java.awt.Font drawingFont = font.deriveFont(java.awt.Font.PLAIN, size);
    graphics.setFont(drawingFont);

    // Computing the dimensions of the text depending on the font and on the text to draw
    FontMetrics fontMetrics = graphics.getFontMetrics();
    int width = fontMetrics.stringWidth(text);
    int height = fontMetrics.getHeight();
    float screenRelativeWidth = 2f * width / (float) DisplayManager.getDrawingSurfaceWidth();
    float screenRelativeHeight = 2f * height / (float) DisplayManager.getDrawingSurfaceHeight();
    graphics.dispose();

    // Buffering the rendered font into a buffered image
    bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
    graphics = bufferedImage.createGraphics();
    graphics.setRenderingHint(RenderingHints.KEY_ALPHA_INTERPOLATION,
            RenderingHints.VALUE_ALPHA_INTERPOLATION_QUALITY);
    graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    graphics.setRenderingHint(RenderingHints.KEY_COLOR_RENDERING, RenderingHints.VALUE_COLOR_RENDER_QUALITY);

    graphics.setFont(drawingFont);
    fontMetrics = graphics.getFontMetrics();
    graphics.setColor(color);
    graphics.drawString(text, 0, fontMetrics.getAscent());
    graphics.dispose();

    // The following lines generates a file where the text is properly rendered, with right alpha
    try {
        File outputfile = new File("Font rendering.png");
        ImageIO.write(bufferedImage, "png", outputfile);
    }
    catch (IOException e) {
        logger.error("Could not save the generated buffered image to draw the font :", e);
    }

    // Converting the buffered image into a texture
    AWTTextureData textureData = new AWTTextureData(gl.getGLProfile(), GL.GL_RGBA, 0, false, bufferedImage);
    Texture texture = new Texture(gl, textureData);

    // Activating the texture containing the text
    FxManager.useShader(gl, fontMaterial.shading);
    if (FxManager.defaultTextureHandler != -1 && texture != null) {
        gl.glBindTexture(GL.GL_TEXTURE_2D, setTextureHandler(gl));
        gl.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MIN_FILTER, GL.GL_LINEAR_MIPMAP_LINEAR);
        gl.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MAG_FILTER, GL.GL_LINEAR);
        gl.glTexImage2D(GL.GL_TEXTURE_2D, 0, texture.getInternalFormat(), texture.getWidth(), texture.getHeight(),
                0, texture.getPixelFormat(), texture.getPixelType(), texture.getBuffer());
        gl.glGenerateMipmap(GL.GL_TEXTURE_2D);
        gl.glBindTexture(GL.GL_TEXTURE_2D, 0);
        gl.glActiveTexture(GL.GL_TEXTURE0);
        gl.glBindTexture(GL.GL_TEXTURE_2D, textureHandler);
    }

    // Rendering the square on which the font texture is applyied
    bufferRenderingSquareData(gl, x, y, screenRelativeWidth, screenRelativeHeight);
    gl.glDrawElements(GL3.GL_TRIANGLES, 6, GL3.GL_UNSIGNED_INT, 0);
    gl.glDisableVertexAttribArray(0);
    gl.glBindVertexArray(0);

    // Once the text is rendered, disabling the material
    MaterialManager.disableMaterial(gl);


The problem seems to occur when I convert the buffered image into a texture (line 40 above). I tried to have a look inside the AWTTextureData constructor but nothing seems to be wrong in the way I declare it. Of course, if anymore information is needed, feel free to ask.

Thanks for your help.
Offline Chennorris

Senior Newbie


Exp: 10 years



« Reply #1 - Posted 2018-02-20 14:12:39 »

OK, I think I spotted the problem. In fact, everything's OK in my algorithm but transparency is computed in a very specific way :
  • if no 3D scene is loaded, it takes the glClearColor as the background color (in my case : black) : black text over black background = black rectangle,
  • when a 3D scene is loaded, the text's transparency is based on the scene : the text is properly displayed.

To understand the full problem, I'm trying to display :
  • a 3D scene (or a black background if nothing's loaded) => OK
  • 2D white rectangles for each menu item => OK when no scene's loaded, but nothing once a 3D scene is loaded and displayed
  • text over the 2D white rectangles => black rectangle when no scene's loaded, OK when a 3D scene is loaded and displayed

    So in fact, the real problems I need to solve are :
  • displaying 2D text over 2D rectangles,
  • displaying 2D rectangles over a 3D scene.

Anyway, I'll note this post as solved because the original trouble's not one anymore.
Thanks for your reading Wink
Offline gouessej
« Reply #2 - Posted 2018-02-20 14:16:18 »

Hey

Rather ask JOGL specific questions on our official forum:
http://forum.jogamp.org/

By the way, there is already a build-in method in JOGL to make a capture of the rendering and to copy it into a buffered image, maybe it could be useful for you:
http://forum.jogamp.org/JOGL-Drawing-on-Image-e-g-JButton-tp4038652p4038653.html

Julien Gouesse | Personal blog | Website | Jogamp
Pages: [1]
  ignore  |  Print  
 
 

 
hadezbladez (40 views)
2018-11-16 13:46:03

hadezbladez (43 views)
2018-11-16 13:41:33

hadezbladez (24 views)
2018-11-16 13:35:35

hadezbladez (18 views)
2018-11-16 13:32:03

EgonOlsen (1894 views)
2018-06-10 19:43:48

EgonOlsen (1930 views)
2018-06-10 19:43:44

EgonOlsen (1285 views)
2018-06-10 19:43:20

DesertCoockie (1716 views)
2018-05-13 18:23:11

nelsongames (1404 views)
2018-04-24 18:15:36

nelsongames (2036 views)
2018-04-24 18:14:32
Deployment and Packaging
by mudlee
2018-08-22 18:09:50

Java Gaming Resources
by gouessej
2018-08-22 08:19:41

Deployment and Packaging
by gouessej
2018-08-22 08:04:08

Deployment and Packaging
by gouessej
2018-08-22 08:03:45

Deployment and Packaging
by philfrei
2018-08-20 02:33:38

Deployment and Packaging
by philfrei
2018-08-20 02:29:55

Deployment and Packaging
by philfrei
2018-08-19 23:56:20

Deployment and Packaging
by philfrei
2018-08-19 23:54:46
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
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!