LWJGL Forum

Programming => OpenGL => Topic started by: royger on April 28, 2007, 20:59:05

Title: Color overlay on Texture (sprite)
Post by: royger on April 28, 2007, 20:59:05
Hello,

I'm trying to overlay a color over a sprite, so that the sprite turns red for a few seconds when hit, I add a sample as attachment.

I've loaded my texture with GL11.GL_TEXTURE_2D and GL11.GL_RGBA, any help would be apreciated.

Thanks and regards, Roger.
Title: Re: Color overlay on Texture (sprite)
Post by: ndhb on April 28, 2007, 22:26:42
Hi Roger.

One way of doing it is to change the blending function (glBlendFunc) when you character is hit (maybe you have a state that says he's been hit or damaged). Here's some pseudocode for blending red into what is drawn:

if (character.isDamaged()) {
  GL14.glBlendColor(0.8f, 0, 0, 0);
  GL11.glBlendFunc(GL11.GL_SRC_COLOR, GL11.GL_CONSTANT_COLOR);
  GL11.glEnable(GL11.GL_BLEND); // you might already have it enabled
  character.draw();
  GL11.glDisable(GL11.GL_BLEND); // you might want to leave it enabled - you might want to change your BlendFunc back or something third.
} else {
  character.draw();
}

(As usual, there's many ways to skin a cat.)

Regards
Nicolai de Haan Brøgger
Title: Re: Color overlay on Texture (sprite)
Post by: royger on May 08, 2007, 17:14:10
Hello,

Thanks for the reply, your code was really useful. I've had to change it a little, because I also use transparent sprites, so that's how it looks now:

GL14.glBlendColor(1f, 0, 0, 0);
GL11.glBlendFunc(GL11.GL_SRC_COLOR, GL11.GL_ONE_MINUS_SRC_ALPHA);
GL11.glEnable(GL11.GL_BLEND);

The problem is that it isn't really red, it looks like if the sprite has been dakened, but not red, there's a sample attached. Do you know a way to solve this? I've tryed changing the glBlendColor indexes, but with no luck so far.
Title: Re: Color overlay on Texture (sprite)
Post by: Fool Running on May 08, 2007, 18:36:35
you might need to enable texture modulation:
GL11.glTexEnvi(GL11.GL_TEXTURE_ENV, GL11.GL_TEXTURE_ENV_MODE, GL11.GL_MODULATE);
Title: Re: Color overlay on Texture (sprite)
Post by: ndhb on May 09, 2007, 20:43:34
The texture mode GL_REPLACE should be okay.

On my example the colours darken too when I use GL_ONE_MINUS_SRC_ALPHA but it turns red with GL_CONSTANT_COLOR

Have you tried playing around with other functions if GL_CONSTANT_COLOR doesn't work for you?

http://pyopengl.sourceforge.net/documentation/manual/glBlendFunc.3G.html

Also you can try paiting your quad in red and switch texturing mode from GL_REPLACE to GL_MODULATE or something...

- Nicolai de Haan Brøgger