Skip to main content
GameDev.net gamedev.net
Using GameDev.net for your class this semester?
Learn more →
🔒 Locked

How to change color of a monocrome alpha texture in OpenGl?

Started by TooOld2rock-nRoll Mar 20, 2025 at 7:30 PM 1 replies 1.9k views
Original Post
TooOld2rock-nRoll
TooOld2rock-nRoll

I'm trying to render a font and the texture atlas is created as a single channel texture, that is treated as alpha (black is transparent, white is opaque).

I can correctly render the text as a sprite loading the texture with:

glTexImage2D (GL_TEXTURE_2D, 0, GL_ALPHA, width, height, 0, GL_ALPHA, GL_UNSIGNED_BYTE, data);

But changing the color as I do with any other sprint is not working, the text is always black (as expected for a colorless opaque pixel, but not desired hahaha)

I can even change the alpha field value of the sprite and it will blend, so the shader is doing its job.

I can find old OpenGl answers to the problem, it involves changing glTexEnvi() values that are no longer supported.

So, how do I do this???

"No, you're never too old to rock and roll
If you're too young to die"
TooOld2rock-nRoll
TooOld2rock-nRoll

JoeJ said:
Maybe this helps: https://learnopengl.com/Advanced-OpenGL/Blending

Like many many others, I also started the OpenGl path with leranopengl.com, so yes, alpha blending is already working correctly for all the sprites.

You can take a look if you like, my graphics section is pretty much like many others and should be simple to follow: https://notabug.org/TooOld2Rock-nRoll/ArcadeFighterDemo/src/master/lib/ArcadeFighter/inc/graphics

Other people seam to make it work, in the past at least…

Learnopengl.com uses the FreeType library and renders one texture per glyph, which is not very economical, and forces the single red channel to the alpha field at the shader. I really would like to avoid creating a special shader just for fonts, it would require duplication of many features making it a bug nesting ground.

Thanks for the suggestion though, I will play around with the blending options and see if any of them makes a difference.

"No, you're never too old to rock and roll
If you're too young to die"
JoeJ
JoeJ

TooOld2rock-nRoll said:
I will play around with the blending options and see if any of them makes a difference.

It should work after soem trial and error. Personally i did it for a mobile game - render a quad with alpha blending from alpha texture, the vertex color could be changed per glyph. Using OpenGL ES 1.0 i had no shaders at all.

TooOld2rock-nRoll
TooOld2rock-nRoll

Found it!

GL_ALPHA is deprecated, the modern way of doing this is with swizzle!

First swizzle the texture to use GL_RED in all RGBA fields with:

static GLint swizzleMask[] = {GL_RED, GL_RED, GL_RED, GL_RED};
glTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_RGBA, swizzleMask);

(you can compound the literals in the function call for the mask, but I'm using cpp and would like to avoid the warning at compilation)

And then load the monocrome texture as usual, all the other rendering behavior previously implemented will work with no need for special shader treatment.

glTexImage2D (GL_TEXTURE_2D, 0, GL_RED, width, height, 0, GL_RED, GL_UNSIGNED_BYTE, data);
"No, you're never too old to rock and roll
If you're too young to die"

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.