Sign in to follow this  

Color * Texture

This topic is 4390 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am working on a 2D game with LWJGL and I want to implement a method to change to bright a Sprite (Texture) with a particular color... It would be something like illuminate(float red, float green, float blue) where values are from 0.0 to 2.0 (from 1.0 to 2.0 for overbrighting)... How is, in general, the right way to realize that?

Share this post


Link to post
Share on other sites
Quote:
Original post by ufinii
I am working on a 2D game with LWJGL and I want to implement a method to change to bright a Sprite (Texture) with a particular color...

It would be something like illuminate(float red, float green, float blue) where values are from 0.0 to 2.0 (from 1.0 to 2.0 for overbrighting)...

How is, in general, the right way to realize that?


I don't know about LWJGL, but if you are using OpenGL with lighting & materials, GL_EMISSION with a high value in all the colors will make the material look brighter. If it's simple color (no lighting), then you could bind your textures with GL_MODULATE and make all vertices of color, let's say (0.8, 0.8, 0.8), and the one you want bright (1, 1, 1).

If that doesn't help, try more advanced techniques like real glow.

Share this post


Link to post
Share on other sites
Quote:
Original post by ufinii
hmmmm well thanks...

so i set a glColor3f(r,g,b) before the glBegin() right?

and what would you suggest for the overbright thing?


You could. But you usually set the color per vertex, e.g. inside glBegin-glEnd before each glVertex.

Assuming you don't use lighting, and that you put you r sprite on a quad.
Call glColor3d(0.8, 0.8, 0.8) befor glBegin. Enable texturing and bind your texture (don't forget GL_MODULATE). Render the quad (all verices will be bright gray modulated with the texture - the texture will look a bit darker than it should). Then when rendering the bright sprite, call glColor3d(1, 1, 1) and render it. The sprite will look like it should, but because other are darker this one will look brighter.

Share this post


Link to post
Share on other sites
Quote:
Original post by ufinii
hmm what i mean is this:

i have a texture and i want to overbright it with white (it becomes white)... which values i have to set in glColor3f() for example to do it?


Brighter, or completely white? Nevermind, looks like the basic stuff doesn't work.

Render all sprites as usual. Then create a mask of your sprite, bind it as a texture on a white quad, and render it with a certain alpha on top of the sprite you want bright.

By mask, I mean every opaque pixel of your sprite should be (1, 1, 1, 1) and every transparent pixel should be ([any values for RGB], 0).
I think there is a shorter way to do it (luminance texture, maybe even multitexturing) but creating mask should work.

The mask should be bound on quad of same size and position as your sprite. Draw it on the same position, the alpha being from 0 to 1 will make your sprite go from original to completely white. (Depth function less or equal)

Share this post


Link to post
Share on other sites
sorry I didn't explain good...


what i want is:

texture.bright(r,g,b)

if i do bright(0.0f, 0.0f, 0.0f) the texture becomes black
if i do bright(1.0f, 1.0f, 1.0f) the texture is equal
if i do bright(2.0f, 2.0f, 2.0f) the texture becomes white

other values are interpolations between this 3

Share this post


Link to post
Share on other sites
Quote:
Original post by ufinii
sorry I didn't explain good...


what i want is:

texture.bright(r,g,b)

if i do bright(0.0f, 0.0f, 0.0f) the texture becomes black
if i do bright(1.0f, 1.0f, 1.0f) the texture is equal
if i do bright(2.0f, 2.0f, 2.0f) the texture becomes white

other values are interpolations between this 3


Try out the masking like I said. I don't know how you handle the transparency of sprites, but most likely a second texture (either loaded from file or generated during runtime) will be necessary.

You want bright to be from 0 to 2, right?

K = 0 to 1:
The mask should be in black color, and alpha is 1-K
K = 1 to 2:
The mask should be in white color, and alpha is K-1

I think if you try out different blending functions, you might be able to do it with only one mask texture (maybe even with none).
There might also be an extension which extends GL_MODULATE, and then you won't have to render mask at all.

If you still want more detailed help, you might want to post some code, and also explain how are your sprites stored (RGBA, tranparent color key, mask sprite etc.).

Share this post


Link to post
Share on other sites
I've never done this myself, but off the top of my head I would think you can use a number of techniques;

One would be to continually manipulate the pixels of the image in main memory and keep reloading that data back to the texture object. ie. when you load your image from a file you can take a copy of the pixel data and multiply the pixel colors of the copy by some factor to lighten or darken the image, then load or reload that data back to the texuture unit. Costly in terms of memory usage and performance.

Multitexturing should also be able to perform something like this relatively easily. Generate a grey texture depending upon how much darkening or lightening you want and set your first texture unit as the base texture, the second as the grey texture, and combine.

I believe there are also some OpenGL extentions are available to perform the darkening/lightening of a texture using a combo of multitexturing and blending calls. I read something like this on gamedev I think. I will see if I can find the post, but it basically mentioned using glTexEnvi GL_COMBINE_EXT, GL_SOURCE1_RGB_EXT, GL_OPERAND_RGB_EXT and I think a blending function. Suffice to say, without finding the message I am guessing. Sorry. Perhaps a little searching will turn up results.

Of course, lastly, this is all probably very easy in a shader - which I have no experience with so we need someone more knowledgeable to comment.

Note: these are ideas completely off the top of my head. I do not know if these would realistically work! I will look for the post I read that mentioned the extentions.

hth
F451

Share this post


Link to post
Share on other sites
Standard blending is (1-t)*c1+t*c2 with 0<=t<=1. Here you are blending with two colors, i.e. black and white. So if you are between 0 and 1 t is your blend value, c1 is black and c2 is your texture color. If you are between 1 and 2 then t is your blend value minus 1, c1 is your texture color and c2 is white.

Logically you have three points, i.e. (0,0), (1,c) and (2,1). So above is simply drawing a line connecting those points. The "brightness" is X and the pixel color is Y. You could also fit a quadradic to those three points. Then you don't get that discontinuity at (1,c), but three points define a quadradic. There is only one quadradic going through those three points as long as they are distinct. There is an infinite number of cubics going through those three points though. That gives you a degree of freedom to play with the shape of the curve.

How do you fit a polynomial to a set of points? One way is a LaGrange Interpolating Polynomial. Using the above you get (x*(x-1))-(x*(x-2))*c for a quadradic. With a cubic you throw in a variable for the 4th point. In practice you might want that to actually be a function of c. As an example you might want to say that at 0.5 it is always a certain percentage of c so your fourth point is (0.5,p*c).

You can also patch two curves together using bezier curves or splines, but that is most likely a little more complex than you need for simply setting the brightness of a pixel. Overall the whole thing is pretty much a waste because setting your texture environment mode to GL_BLEND and modifying the environment color and vertex colors can fade the entire image to black, white or any other color you please. You can download Nate Robbins Tutorials here and play with the texture tutorial to see the impact of changing those parameters.

Share this post


Link to post
Share on other sites
Well... Since the game isn't really graphically complex i tried using additive blending

//drawSprite()

glDepthFunc(GL_LEQUAL);

glEnable(GL_BLEND);
glBlendFunc(GL_ONE, GL_ONE);

//drawSprite()

glDepthFunc(GL_LESS);
glDisable(GL_BLEND);


But it doesn't look smooth (I can notice the rectangle area around the sprites)... I'm doing something wrong?

Share this post


Link to post
Share on other sites
I tried with different blend functions but I'm still having that problem...


Looks like the alpha channel gets multiplied too but it's really strange since it doesn't change anything if I set for example:

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

or

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

before the blending...

Share this post


Link to post
Share on other sites
Quote:
Original post by ufinii
I tried with different blend functions but I'm still having that problem...


Looks like the alpha channel gets multiplied too but it's really strange since it doesn't change anything if I set for example:

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

or

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

before the blending...

To use the the alpha of the source color, set the blending equation params to GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA.

/edit: But if you use this method, it will still look like there is one oddly bright quad over the other regular ones (as you've already noticed and mentioned in your previous post). You may try using several small quads and set their alpha according to their distance from the bright object. The one directly above has an alpha of, say 0.5, and ones a little farther away have lower values, 0.1 or 0.01, say.

For really good results, use a bloom or glow effect. You don't need a shader, just render your sprite to a texture, blur the texture and render a textured quad over the original (like you were doing previously) with additive blending.

Share this post


Link to post
Share on other sites

This topic is 4390 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this