Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (516)
Games in Android Showcase (122)
games submitted by our members
Games in WIP (577)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Texture brightness for a 2D sprite  (Read 5599 times)
0 Members and 1 Guest are viewing this topic.
Offline Rejechted

Senior Duke


Medals: 1
Projects: 1


Just a guy making some indie games :D


« Posted 2011-07-29 12:23:26 »

UPDATE: The code below is good.  The code I was actually using in my project had texenvi statements being called inside the glBegin which AFAIK isn't allowed.  I blame this on the feature not working, because now everything works fine.

Hello,

A buddy and I are working on a 2D side-scroller.  I'm currently experimenting with making the sprites for our enemy entities "Flash" white when hit with a bullet.  I've done extensive searching and encountered a lot of information on making a texture brighter using the COMBINE and ADD functionality in GL tex env.  So far all I have achieved is making the sprites darken.  The color range seems to "Cap" at 1.0f, although the posts and tutorials that suggested "ADD" seem to suggest that the range is higher.  Here's our initialization for the GL object, which occurs once at the start of the game:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
gl.glMatrixMode(GL.GL_PROJECTION);
gl.glLoadIdentity();
glu.gluOrtho2D(0.0, 1440, 0.0, 900);

//set up the viewports
gl.glViewport(0, 0, 1440,900 );

//set up some options
gl.glClearColor(1, 1, 1, 0);
gl.glDisable(gl.GL_DEPTH_TEST);
gl.glEnable(gl.GL_TEXTURE_2D);
gl.glBlendFunc(gl.GL_SRC_ALPHA,gl.GL_ONE_MINUS_SRC_ALPHA);
gl.glEnable(gl.GL_BLEND);


And here is the code that is responsible for texturing and drawing the entities:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
if (flashLength > 0)
{
     float brightness = 1.5f;
     gl.glTexEnvi(GL.GL_TEXTURE_ENV, GL.GL_TEXTURE_ENV_MODE,GL.GL_COMBINE);            //Need to be in this mode
     
     gl.glTexEnvi(GL.GL_TEXTURE_ENV, GL.GL_TEXTURE_ENV_COLOR, GL.GL_SRC2_RGB);       //Use 0-2 scale???
     gl.glTexEnvi(GL.GL_TEXTURE_ENV, GL.GL_RGB_SCALE, GL.GL_OPERAND2_RGB);              //Use 0-2 scale???
     
     gl.glTexEnvi(GL.GL_TEXTURE_ENV, GL.GL_COMBINE_RGB, GL.GL_ADD);                           //Add mode needed
     gl.glTexEnvi(GL.GL_TEXTURE_ENV, GL.GL_COMBINE_ALPHA,  GL.GL_REPLACE);               //Don't use add mode for alpha
     
     gl.glColor4f(brightness - 1.0f, brightness - 1.0f, brightness - 1.0f, 1.0f);             //Add brightness to RGB, keep alpha the same
     
     gl.glTexEnvi(GL.GL_TEXTURE_ENV, GL.GL_SRC0_RGB,         GL.GL_TEXTURE);                 //Set source 1
     gl.glTexEnvi(GL.GL_TEXTURE_ENV, GL.GL_SRC1_RGB,         GL.GL_PRIMARY_COLOR);     //Set source 2
     gl.glTexEnvi(GL.GL_TEXTURE_ENV, GL.GL_SRC0_ALPHA,       GL.GL_TEXTURE);               //Get texture's alpha
     
      flashLength--;                                                                      //Decrement Flash Length
}
else
{
     //If no flash, modulate and draw the default
     gl.glTexEnvi(GL.GL_TEXTURE_ENV, GL.GL_TEXTURE_ENV_MODE,GL.GL_MODULATE);
     gl.glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
}

//Draw the quad
gl.glBegin(GL.GL_QUADS);                            
  //set the texture coordinates for the next vertex
  gl.glTexCoord2d(0.0, 1.0);
  gl.glVertex2f(x - width/2, y - height/2); //bottom left
  gl.glTexCoord2d(1.0, 1.0);
  gl.glVertex2f(x + width/2,y - height/2);  //bottom right
  gl.glTexCoord2d(1.0, 0.0);
  gl.glVertex2f(x + width/2,y + height/2);  //top right
  gl.glTexCoord2d(0.0, 0.0);
  gl.glVertex2f(x - width/2,y + height/2);  //top left
gl.glEnd();


flashLength is assigned by a collision event in our physics engine and is the duration that the new color settings (white flash) will be applied.  Using modulate I can achieve a red tint, but I'm capped in brightness by the texture brightness.

There's a lot going on there, and I suspect that there's something wrong with the graphics object not having its settings reset between drawing.  We're completely new to this and have no idea about:

1. Whether the GL_TEXTURE_ENV adjustments need to be made inside the glBegin
2. How the ADD setting actually works and if this code is executing that functionality correctly
3. Any pre-defined environment variables we have forgotten to set in our initialization that make this functionality possible
4. A whole host of other things, but we're getting there.

Any help or at least a nudge in the right direction would be appreciated.  We're trying to adjust the brightness of a texture.

EDIT: Should probably clarify that we are using JOGL but were using LWJGL, not sure what was lost in the conversion.

Blog for our project (Codenamed Lead Crystal): http://silvergoblet.tumblr.com
Offline Cero
« Reply #1 - Posted 2011-07-29 13:10:23 »

Quote
Using modulate I can achieve a red tint, but I'm capped in brightness by the texture brightness.

I have actually the same problem, although using Slick. I'm not much of a low level OpenGl programmer, so not sure I will even fix it.

Offline Riven
« League of Dukes »

JGO Overlord


Medals: 823
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #2 - Posted 2011-07-29 13:21:32 »

Render the sprite on top of the previous one, with:
1  
glBlendFunc(GL_SRC_ALPHA, GL_ONE)


It will use additive blending, so obviously black remain black, but it will result in a pretty nice effect.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Rejechted

Senior Duke


Medals: 1
Projects: 1


Just a guy making some indie games :D


« Reply #3 - Posted 2011-07-29 13:25:54 »

Render the sprite on top of the previous one, with:
1  
glBlendFunc(GL_SRC_ALPHA, GL_ONE)


It will use additive blending, so obviously black remain black, but it will result in a pretty nice effect.

So I would literally use the glBegin , vertex code, glEnd twice for each sprite that needs to be flashed?  Does this mean scrapping the ADD mode logic all together?  Is this the only parameter that would differ?

 ie. set the blend func to:
glBlendFunc(GL_SRC_ALPHA, GL_ONE); when I want to render the second "brightness" image
gl.glBlendFunc(gl.GL_SRC_ALPHA,gl.GL_ONE_MINUS_SRC_ALPHA); for everything else

Blog for our project (Codenamed Lead Crystal): http://silvergoblet.tumblr.com
Offline Cero
« Reply #4 - Posted 2011-07-29 14:21:31 »

well it does work, but its kinda subtle
doing it many more times helps (drawing it like 100 times more per frame), but obviously thats not how you would do it

any way to intensify the result ?

If I want to really red-out the sprite... now is only a little more redish

like I said each to you draw it it gets better and brighter, but I already use max red

Offline Rejechted

Senior Duke


Medals: 1
Projects: 1


Just a guy making some indie games :D


« Reply #5 - Posted 2011-07-29 14:23:12 »

well it does work, but its kinda subtle
doing it many more times helps (drawing it like 100 times more per frame), but obviously thats not how you would do it

any way to intensify the result ?

If I want to really red-out the sprite... now is only a little more redish

like I said each to you draw it it gets better and brighter, but I already use max red

What are you actually executing?  Does it have to do with what Riven posted?

I'm trying to draw attention to my original post here, I don't want anyone helping to get confused.  If I get a solution to my problem it will probably work for yours too.

Blog for our project (Codenamed Lead Crystal): http://silvergoblet.tumblr.com
Offline Cero
« Reply #6 - Posted 2011-07-29 14:29:55 »

well it does work, but its kinda subtle
doing it many more times helps (drawing it like 100 times more per frame), but obviously thats not how you would do it

any way to intensify the result ?

If I want to really red-out the sprite... now is only a little more redish

like I said each to you draw it it gets better and brighter, but I already use max red

What are you actually executing?  Does it have to do with what Riven posted?

1  
2  
3  
draw Normal sprite
glBlendFunc(GL_SRC_ALPHA, GL_ONE);
draw sprite with red filter


using white, like Rejechted wants, works much better
but I want to completey red-out a sprite when being hit, and then is fades (well maybe not completely, but more than this)

Offline Rejechted

Senior Duke


Medals: 1
Projects: 1


Just a guy making some indie games :D


« Reply #7 - Posted 2011-07-29 14:32:40 »

When rendering the image a second time, why not try using glColor4f(1.0f, 0.0f, 0.0f, 1.0f)?  Won't this "Red it out"?

Blog for our project (Codenamed Lead Crystal): http://silvergoblet.tumblr.com
Offline Cero
« Reply #8 - Posted 2011-07-29 14:50:13 »

well it adds red
and each consecutive time you draw this again it adds more (actually, if you do it 10 times, thats it, not getting more red)

but you know not enough

I'm sure this has to do with the fact that you cannot lighten an image really...

Actually when I used Java2D I got this working. (Although Java2D is slow of course)

1  
2  
3  
4  
5  
6  
7  
8  
scratchG2d = flashImg.createGraphics();
scratchG2d.setComposite(AlphaComposite.Src);
scratchG2d.setColor(new Color(255,0,0,flashedFrames));
scratchG2d.fillRect(0,0,getAtmVisual().getImageFrame().getWidth(),getAtmVisual().getImageFrame().getHeight());
scratchG2d.setComposite(AlphaComposite.DstAtop);
scratchG2d.drawImage(getAtmVisual().getImageFrame(),0,0,null);
scratchG2d.dispose();
scratchG2d = null;


then you render the original and then "flashImg" over it and there you have it
since flashimg is a red silhouette which fades into transparency

EDIT: "getAtmVisual().getImageFrame()" is obsiously just the original sprite

Offline Rejechted

Senior Duke


Medals: 1
Projects: 1


Just a guy making some indie games :D


« Reply #9 - Posted 2011-07-29 14:59:25 »

well it adds red
and each consecutive time you draw this again it adds more (actually, if you do it 10 times, thats it, not getting more red)

but you know not enough

So if I render my flashimg with the new blend equation, using:

glColor4f(1.0f, 1.0f, 1.0f, 1.0f);

Yes, it's white, with an alpha of 1.0f, but won't that also "not be enough" if yours isn't either?  The only difference in that color call is the presence of green and blue. 

Blog for our project (Codenamed Lead Crystal): http://silvergoblet.tumblr.com
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Cero
« Reply #10 - Posted 2011-07-29 15:07:01 »

well obviously I have not enough understanding of how it actually works but, doing it with white 3 times, pretty much makes it all white, not so with red

Offline Rejechted

Senior Duke


Medals: 1
Projects: 1


Just a guy making some indie games :D


« Reply #11 - Posted 2011-07-29 15:38:50 »

well obviously I have not enough understanding of how it actually works but, doing it with white 3 times, pretty much makes it all white, not so with red

What glColor function are using and what parameters.  Copy paste

Blog for our project (Codenamed Lead Crystal): http://silvergoblet.tumblr.com
Offline avm1979
« Reply #12 - Posted 2011-07-29 15:41:04 »

How much red you get entirely depends on how much red you had in your original sprite, so it's not a good way to go if you want to do a redout. It does make a nice flashing effect, though, just not if you want it tinted to a particular color.

You could create a second sprite, which has the same shape, but is all white, and then set the color to red and render it on top of the original - that would give you consistent results. Rendering it once would be enough, since all of its color channels are maxed out. This is simple to do, but requires loading more images, or generating them with your texture loading code.


As an alternative, you could use the destination alpha channel to keep track of the shape of the sprite, and then blend a white quad on top of it while discarding the pixels that are transparent in the sprite.

The rough steps to do that are:
- Make sure to request 8 bits of destination alpha when creating the Display

When rendering:
- set glColorMask(false,false,false,true) - this makes rendering only affect the alpha
- set glColor 0,0,0,0, glBlendFunc to GL_ONE, GL_ZERO and render a quad large enough to cover the area your sprite will be in - this will set destination alpha in the area to 0
- set glColorMask(true,true,true,true)
- set glColor to 1,1,1,1
- Render the original sprite normally - this will render the sprite on the screen, and also render its alpha value
- set glColor to 1,0,0,1
- set glBlendFunc to GL_DST_ALPHA, GL_ONE - this will render only in areas where your original sprite wasn't transparent
- render a <whatever color you want> quad, same size as you used to clear the destination alpha - it should add on top of the sprite


You could also use the stencil buffer for this (make sure to request 8 bits of stencil when creating the Display). It's a similar idea - you draw the shape to the stencil buffer, then use it to discard pixels in subsequent drawing calls. I think that's a bit tougher to wrap your head around, though, if it's your first time looking at it.



Offline Rejechted

Senior Duke


Medals: 1
Projects: 1


Just a guy making some indie games :D


« Reply #13 - Posted 2011-07-29 15:56:15 »

No insights as to the original stuff that I tried with the brightness variable and end mode?  I'm still a bit wonky as to why there are so many ways to do something which seems so simple.

Blog for our project (Codenamed Lead Crystal): http://silvergoblet.tumblr.com
Offline Cero
« Reply #14 - Posted 2011-07-29 17:13:34 »

I actually got what I wanted, but not sure it would help as I am using primarily Slick and only rarely direct opengl calls, so... just nevermind me I got it now

Offline Rejechted

Senior Duke


Medals: 1
Projects: 1


Just a guy making some indie games :D


« Reply #15 - Posted 2011-07-29 17:24:13 »

Can anyone explain how my code and the code in the brightness function here:

1  
http://developer.apple.com/library/ios/#samplecode/GLImageProcessing/Listings/Imaging_c.html#//apple_ref/doc/uid/DTS40009053-Imaging_c-DontLinkElementID_9


are different, and what's preventing mine from working?

Blog for our project (Codenamed Lead Crystal): http://silvergoblet.tumblr.com
Offline Eli Delventhal

JGO Kernel


Medals: 42
Projects: 11
Exp: 10 years


Game Engineer


« Reply #16 - Posted 2011-07-29 17:31:41 »

It's not the solution to your question, but why not just draw the same image with/without like 50% alpha? It looks fine.

See my work:
OTC Software
Offline Rejechted

Senior Duke


Medals: 1
Projects: 1


Just a guy making some indie games :D


« Reply #17 - Posted 2011-07-29 18:03:38 »

It's not the solution to your question, but why not just draw the same image with/without like 50% alpha? It looks fine.

Draw the same image on top of the existing one?  I'm afraid to hog resources.  I'm aware that this works, though.  If you were to draw the same image again with 100% alpha, would you have full white?

Blog for our project (Codenamed Lead Crystal): http://silvergoblet.tumblr.com
Offline avm1979
« Reply #18 - Posted 2011-07-29 22:15:28 »

No, you wouldn't.

Think of it in terms of individual pixels.

Say a given pixel is 0,100,200,255 in the image. Drawing it twice (with glBlendFunc GL_SRC_ALPHA, GL_ONE) will double each value - so it'll be 0,200,255,255.

Now, most colors tend to have non-zero values in each color channel, so it'll look overall brighter, and will turn white in some places - i.e., where doubling the rgb values gives 255 across the board.

Offline Rejechted

Senior Duke


Medals: 1
Projects: 1


Just a guy making some indie games :D


« Reply #19 - Posted 2011-07-29 22:36:23 »

is that the equivalent of increasing saturation?

Blog for our project (Codenamed Lead Crystal): http://silvergoblet.tumblr.com
Offline Eli Delventhal

JGO Kernel


Medals: 42
Projects: 11
Exp: 10 years


Game Engineer


« Reply #20 - Posted 2011-08-01 17:04:17 »

It's not the solution to your question, but why not just draw the same image with/without like 50% alpha? It looks fine.

Draw the same image on top of the existing one?  I'm afraid to hog resources.  I'm aware that this works, though.  If you were to draw the same image again with 100% alpha, would you have full white?
No I was saying just draw the same one and alternate between trans and not trans.

See my work:
OTC Software
Offline Rejechted

Senior Duke


Medals: 1
Projects: 1


Just a guy making some indie games :D


« Reply #21 - Posted 2011-08-01 17:07:25 »

Wouldn't that cause flickering o_O

1 frame is actually a pretty long time

Blog for our project (Codenamed Lead Crystal): http://silvergoblet.tumblr.com
Offline theagentd
« Reply #22 - Posted 2011-08-02 09:59:47 »

Why not just keep a white sprite texture separately and drawing it with glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA) and an alpha value at like, 0.5f? Would make any sprite brighter.
You could also do this with shaders, just do a texture lookup and discard the RGB components (only keeping alpha) and you just generated your white mask.
I think you're really overcomplicating stuff. I would've just kept 2 different textures, one normal and one brighter one and then just switched between them, instead of trying to combine textures etc.

Myomyomyo.
Offline kevglass

JGO Kernel


Medals: 191
Projects: 24
Exp: 18 years


Coder, Trainee Pixel Artist, Game Reviewer


« Reply #23 - Posted 2011-08-02 10:13:14 »

The secondary colour extension can help you with this, Slick uses it for Image.drawFlash()

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
blic void drawFlash(float x,float y,float width,float height, Color col) {
                init();
               
                col.bind();
                texture.bind();

                if (GL.canSecondaryColor()) {
                        GL.glEnable(SGL.GL_COLOR_SUM_EXT);
                        GL.glSecondaryColor3ubEXT((byte)(col.r * 255),
                                                                                                         (byte)(col.g * 255),
                                                                                                         (byte)(col.b * 255));
                }
               
                GL.glTexEnvi(SGL.GL_TEXTURE_ENV, SGL.GL_TEXTURE_ENV_MODE, SGL.GL_MODULATE);


Kev

Offline Rejechted

Senior Duke


Medals: 1
Projects: 1


Just a guy making some indie games :D


« Reply #24 - Posted 2011-08-02 12:38:03 »

Wow... if I knew it was this simple. 

In our game, we are going to design "RenderEffects" that are somewhat similar to combat effects (poison, stun, etc.) but relate to visual modifications to the entity only.  This seems like it'd be a great function to have in our render effect API, among other things. (grow, shrink, alpha mod, fade, show, spin, etc)

Blog for our project (Codenamed Lead Crystal): http://silvergoblet.tumblr.com
Offline Eli Delventhal

JGO Kernel


Medals: 42
Projects: 11
Exp: 10 years


Game Engineer


« Reply #25 - Posted 2011-08-02 17:16:42 »

Wouldn't that cause flickering o_O

1 frame is actually a pretty long time
Isn't that the point? Go to 0:57 in this video, it was just the first classic brawler I thought of. Or this one, at 0.15, look for the guy on the right of the screen. Temporary flickering or fading is an incredibly common way of representing invincibility. That's what I thought you were asking about.

But anyway, you've got your solution already. Smiley

See my work:
OTC Software
Offline Rejechted

Senior Duke


Medals: 1
Projects: 1


Just a guy making some indie games :D


« Reply #26 - Posted 2011-08-02 17:18:41 »

Ah, my problem wasn't related to alpha levels, it was related to how to increase the brightness or color intensity of the pixels so that they're closer to white.  (Brighter image).  Thanks for the tip though Smiley

Blog for our project (Codenamed Lead Crystal): http://silvergoblet.tumblr.com
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

TehJavaDev (31 views)
2014-10-27 03:28:38

TehJavaDev (26 views)
2014-10-27 03:27:51

DarkCart (40 views)
2014-10-26 19:37:11

Luminem (21 views)
2014-10-26 10:17:50

Luminem (26 views)
2014-10-26 10:14:04

theagentd (32 views)
2014-10-25 15:46:29

Longarmx (61 views)
2014-10-17 03:59:02

Norakomi (57 views)
2014-10-16 15:22:06

Norakomi (46 views)
2014-10-16 15:20:20

lcass (43 views)
2014-10-15 16:18:58
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!