How to render GL_ALPHA texture?

I am trying to work with the FTGL font library, create a font with a texture and display it on the screen. The library creates a texture in GL_ALPHA format for storing glyphs of fonts, and then when you tell it to render the line, it goes through glBegin(GL_QUADS) and displays each glyph as you expected.

The only problem is that I am not getting any text. In one scenario, I get solid black boxes instead of text, and in the other I get nothing. I ran the code under gDEBugger to make sure the texture loads and configures correctly and everything looks right, but I still don't see the way out.

I tried the obvious things. Calling glColor in advance to try to set the color of the text does nothing. Nor is it trying to make sure that alpha blending will work correctly, for example:

 glEnable(GL_ALPHA_TEST); glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); 

Does anyone know what I am missing to make rendering from the GL_ALPHA texture on the screen correctly displayed?

+4
source share
1 answer

After learning the FTGL code, itโ€™s clear that the API is for text processing itself. It is expected that you will call its rendering functions, which will make OpenGL calls internal.

In other words, FTGL is not designed to load a font into a texture, and then gives you this texture object and you draw the glyphs yourself. This does not mean that you cannot, so the API will not help you do this.

The reason you blacken is because the GL_ALPHA textures return zeros for the RGB component on access. Since you use GL_MODULATE in your texture environment, this will multiply zero by a portion of the RGB color.

To do what you intend, you need to set GL_TEXTURE_ENV_MODE to GL_COMBINE instead of GL_MODULATE . This means that you need to use the environment merge mode , which is not so simple. Having lost a fixed function for shaders almost as soon as they existed, I did not touch combinators for a long time. So take what follows with salt. Here guess what the environment code looks like:

 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE); glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB, GL_REPLACE); glTexEnvi(GL_TEXTURE_ENV, GL_SRC0_RGB, GL_PRIMARY_COLOR); glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_RGB, GL_SRC_COLOR); glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_ALPHA, GL_REPLACE); glTexEnvi(GL_TEXTURE_ENV, GL_SRC0_ALPHA, GL_TEXTURE); glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_ALPHA, GL_SRC_ALPHA); 

What you want to do is generate a color in which the RGB part comes from glColor and the Alpha part comes from texture. This is how you do it.

In English, the first line says that the texture mode is "combined", which activates a more complex mode of operation. The next three lines indicate how part of the RGB color is generated. The GL_COMBINE_RGB value set to GL_REPLACE means that the original RGB value 0 will be what is the output RGB. Setting GL_SRC0_RGB to GL_PRIMARY_COLOR means that the source value of RGB 0 is the base color, which is the color generated by vertex processing (i.e. if you are not using lighting, this is exactly what you passed with glColor ). Setting GL_OPERAND0_RGB to GL_SRC_COLOR means to take the color as is (there are special operations that you can do, for example, take 1 - the color or something else).

The next 3 lines set where the alpha part of the output color comes from. The GL_COMBINE_ALPHA value set to GL_REPLACE means that the original alpha value of 0 (other than the original RGB value of 0) will be what the output alpha is. Setting GL_SRC0_ALPHA to GL_TEXTURE means that the original alpha value of 0 is obtained from the texture of the current texture block. Setting GL_OPERAND0_ALPHA to GL_SRC_ALPHA means taking alpha as it is (as before, you can apply some conversion to alpha here).

Alternatively, if your OpenGL implementation has ARB_texture_swizzle , you can configure the swizzle mask to efficiently convert the GL_ALPHA format to GL_INTENSITY . Then you can use GL_MODULATE as your GL_TEXTURE_ENV_MODE . However, you must ensure that your blend mode is glBlendMode(GL_ONE, GL_ONE_MINUS_SRC_ALPHA) . This is necessary because GL_MODULATE will multiply color by alpha. If you used GL_SRC_ALPHA instead of GL_ONE , you will do multiplication by alpha twice (once at the texture stage, once at the mixing stage).

+3
source

Source: https://habr.com/ru/post/1386613/


All Articles