Programming => LWJGL Documentation => Topic started by: atom on August 10, 2017, 17:47:26

Title: Need an little help with text rendering
Post by: atom on August 10, 2017, 17:47:26

I found that (http://forum.lwjgl.org/index.php?topic=6179.msg32956#msg32956) gitbook a few days ago, and I decided to try and understand more about lwjgl. I've read the book, which is quite amazing, followed the examples, and when i finished, i wanted more, so i told myself : "Mmmmm why not extending the example a bit and try something funny like a game?"

So my starting point was the last example from the book : https://github.com/lwjglgamedev/lwjglbook/tree/master/chapter27/c27-p2
I added a few things and deleted some and then I felt like printing some text on the screen. And all i could do is a black rectangle...

All my scene renders well, with all its elements, through its own shader program. My Hud has its own shader program too, which is much simpler, but doesn't render my text correctly.

What i do to make print some text :
- use awt to load a font and generate a texture from it (works correctly, i tried writing the texture into a png file and it's ok)
- compute quads for each character and choose what part of my texture it should be and put that into texture coordinates (works ok, i checked the values just before upload to gpu)
- draw the quads on the screen (doesn't work correctly)

I see a black rectangle where i should see text, so i used glPolygonMode(GL_FRONT_AND_BACK, GL_LINE) to see a bit more. And in fact all my quads are drawn where they should be, so my problem comes from the color or the texture. In my fragment shader, this computes the color :
Code: [Select]
fragColor = colour * texture(texture_sampler, outTexCoord);if i change it to
Code: [Select]
fragColor = colour;my rectangle isn't black anymore, it's the color i wanted my text to be (colour is a vec4 uniform set by my text's material's ambient color so this makes sense ^^), my problem is probably in my texture.

So i disabled all my rendering, except the HUD, and i still see a blue rectangle ( i wanted to print blue text =) ) which leads me to this part of my fragment shader :

Code: [Select]
texture(texture_sampler, outTexCoord);
If i understand correctly, texture() computes a color from a texture sampler and texture coodinates so either my texture sampler is wrong or my coordinates are.

texture_sampler is never set, but i read that the default is GL_TEXTURE0, and my text texture is bound to the same.

OutTexCoord is passed by my vertex shader :
Code: [Select]
    outTexCoord = texCoord;and before that
Code: [Select]
layout (location=1) in vec2 texCoord;which is my mesh's vao and the texture coordinate i put there are correct.

So i'm stuck, i don't know where to go from here, any help would be great.

Thank you for your time and sorry for the bad english ^^

(EDIT: i just realized that mybe i posted in the wrong section, maybe this would be bettre in the opengl section... apologies)
Title: Re: Need an little help with text rendering
Post by: Kai on August 10, 2017, 18:33:21
Did you set the minification filter of the texture?

The number 1 error people usually make when they see a black texture is not setting the texture minification filter. The minification filter specifies how the sampler should filter the texture when its texel footprint is smaller than a single pixel on the framebuffer. By default, this is set to GL_NEAREST_MIPMAP_LINEAR, which will return black when you do not create a mipmap chain for your texture.

Try this when you initialize your texture and have it bound to GL_TEXTURE_2D:
Code: [Select]
Title: Re: Need an little help with text rendering
Post by: Cornix on August 10, 2017, 20:48:17
I would recommend just getting basic text rendering working first and then gradually advancing it. Start without a custom shader using immediate mode (glBegin, glEnd) like in the examples. Then try to use VBO's, then add shaders. Step by step. Test your code at every step to see if it still works correctly.
Title: Re: Need an little help with text rendering
Post by: atom on August 11, 2017, 15:38:58
Thank you for your answers.

@Kai : I already set the minification filter in my texture class

@Cornix : You're right, i should have started without using shaders. But my shaders for scene rendering work so well that i got tempted :)

I tried something last night. To be sure my font texture was correctly generated, in addition to create a ByteBuffer which my texture is created from, i decided to write a png image. And the png was ok. But last night i tried to create my texture from this png file instead of the ByteBuffer, and my text renders correctly like that. So i probably have an error in the code i use to convert from the font image to the ByteBuffer, but i can't see it  ???
Code: [Select]
ByteArrayOutputStream out = new ByteArrayOutputStream();
//FileOutputStream out2 = new FileOutputStream("test.png");
ImageIO.write(img, "png", out);
//ImageIO.write(img, "png", out2);
ByteBuffer b = ByteBuffer.wrap(out.toByteArray());
texture = new Texture(b);
//texture = new Texture("test.png");

If i use the ByteBuffer b to generate the texture, my text doesn't render, if i use "test.png" to generate my texture, the text is ok.
Title: Re: Need an little help with text rendering
Post by: Cornix on August 11, 2017, 16:07:35
Are you sure you aint getting the formats mixed up? You need to have the image data in the right format for OpenGL or else you get garbage output.

By the way: Using AWT (ImageIO, BufferedImage, etc) together with LWJGL3 is not a good idea. Your code is going to crash on Mac OS. But even on Windows and Linux it will be less efficient because of all the overhead. LWJGL3 comes with bindings to STB to load images into textures and to generate textures for true type fonts.
Title: Re: Need an little help with text rendering
Post by: atom on August 11, 2017, 16:17:43
You're probably right, and i'll look into that direction.

The function i use to create a texture from a file uses STB and works well. So i'll try to get AWT away everywhere else as you advice.

Thanks a lot :)