Color overlay on Texture (sprite)

Started by royger, April 28, 2007, 20:59:05

Previous topic - Next topic

royger

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.

ndhb

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

royger

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.

Fool Running

you might need to enable texture modulation:
GL11.glTexEnvi(GL11.GL_TEXTURE_ENV, GL11.GL_TEXTURE_ENV_MODE, GL11.GL_MODULATE);
Programmers will, one day, rule the world... and the world won't notice until its too late.Just testing the marquee option ;D

ndhb

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