Sign in to follow this  
Devil_Inside

OpenGL Changing Hue of a texture

Recommended Posts

Everyone knows how do the color themes change in Winamp3+. I would like to implement something simillar in my game. The problem is that i dont know how to change the hue of a texture in OpenGL. I tried glColor3f to change the color of the poly that wears my texture, but that doesn't actualy change the hue, it changes the whole color (even the regions of the texture that shouldn't change at all (white and grey)). In Photoshop you can also change the hue of an image by simply adding a coloured layer over the image, and setting it's blending mode to "Hue". I also tried that one in OpenGl, but i couldn't find a blending mode that suits my needs. Is there any solution? Thanks in advance.

Share this post


Link to post
Share on other sites
Changing the hue would require you to convert the texture into a different colour space. Specifically, you need to convert it into HSV, change the hue, and then convert it back into RGB (or however your texture is represented in OpenGL).

Share this post


Link to post
Share on other sites
James is correct. Unfortunately in this case, the hue of a color is computed with all three of the individual color components. In OpenGL, I have found, there is no way to perform any operation which takes into account all three of the color components at the same time. Each color component is handled separately.

There is, however, one exception to this rule that I have found which may solve your problem: Pixel Shaders. A pixel shader can do a number of very complicated pixel operations on the hardware level. This means you don't have to perform the relatively slow conversions James discussed, but the drawback is that pixel shaders are relatively new technology (introduced in December of 2001, I believe) and if you are going for backwards-compatibility in your game, you may find it tough to do with pixel shaders. Furthermore, while NVidia provides a pixel shader package with tools to ease learning, it is still a new technology that you will need to take time to learn if you so choose.

If this is what you want, however, you may consider visiting http://www.nvidia.com/object/feature_pixelshader.html for more information.

Share this post


Link to post
Share on other sites
I'm a beginner in C++ and OpenGL, so i think it's too early for me to get into shader details.

I've got the functions that transform RGB->HSB and HSB->RGB. The process of loading a texture from a file(tga in my case) stores the image data to the memory, pixel by pixel. Maybe i can load all that data and change the hue of that texture, pixel by pixel, and then save it as another texture?

Share this post


Link to post
Share on other sites
That doesn't sound unreasonable. However, unless I've misunderstood, it sounds like you could optimize your texture loading routine. This is how I would do it:


1. Open the file, and read the tga headers to get the texture dimensions and bit depth.
2. Read in the actual image data all at once.
3. Loop through all the pixels
3.1. Convert the pixel to HSV
3.2. Change the hue
3.3. Convert the pixel back to RGB

Usually, when loading .tga files into OpenGL you would loop through all the pixels to convert from the .tga's BGR representation to RGB anyway. It's probably not too expensive to add the hue change, since you're doing it at loading time anyway.

Share this post


Link to post
Share on other sites
Ok. That's almost what i was talking about. But is it possible to get the image data of the texture after it was allready loaded. (For example i need "real_time hue changing"). And if it is possible, where do i get it from?
Thanks.

Share this post


Link to post
Share on other sites
Unfortunately, once you've created the texture, it's hands-off from that point on. You can't modify the context of the texture, except through calls to glTexSubImage*, or by actually creating a separate texture.

Share this post


Link to post
Share on other sites
Quote:
Original post by Devil_Inside
Ok. That's almost what i was talking about. But is it possible to get the image data of the texture after it was allready loaded. (For example i need "real_time hue changing"). And if it is possible, where do i get it from?
Thanks.
glGetTexImage will get the texture data from OpenGL for you. It's pretty slow though because it is reading the data from the video card (that's most likely where the texture is stored). It's better to just keep a copy of the data in system memory and edit that, then update the texture with glTexSubImage2D.

Share this post


Link to post
Share on other sites
I'm pretty sure you can use the material to modify the color of a texture, in how it shows up. If you have a texture which is black and white stripes, with no set material (or a white material), and then you change the material to one colors 255,0,0 and then render the same texture you will end up with red and black stripes.

Basicly what it does is multiply (in a 0.0 to 1.0 range) the color of the texture with the color of the material. White (1.0) multipled by any material ends up the material color. Black (0.0) multipled by any material ends up black. Things in between end up something in between.

The most obvious example fo this that I can think of is the armor in EverQuest used the Direct3D equiv of this. Base armor was a white/silver/black detail. Then different actual equipment had a material color associated with it. A user could switch between a red BP and blue BP, and while the color would change, the detailing would remain identicial.

This became most interesting with some of the robes, which were not a base white color. When you multiply a texture of pink and purble stripes and peach detail by 0,200,200 you end up with a very different look then when you multiple the same texture by 200,200,0 or 50,255,50.


If you want to use 'Hue' though you must still convert from HSV to RGB. But you don't need to do it every pixel. Convert once, set material to that color.


edit: In retrospect I think I completly missed the point of the question, huh. If you want white and grey areas to not be effected you have to do it entirly yourself, either by changing the texture by hand or pixel shaders.

Share this post


Link to post
Share on other sites
Ok, that's what i tried:
1. I duplicated my LoadTgaTexture() function and modified it so it doesn't create a texture after it loads the data from the file, but saves the data to a special structure;
2. I created a function that loops through the pixels of the image from the structure and changes their color.
3. I created a function that updates the old data of the texture with the new, modified data (using glTexSubImage2d).

Everything works fine except one thing. glTexSubImage2D aplies my new texture with its width and height smaller than the original texture. This way only a part of the original textures gets updated. And the strange thing is that the updated part contains the whole texture, but in a smaller scale. I don't understand how could this be, if the new texture has the old's texture width and height and the same number of pixels. It should fit just right, but it doesn't. Where could be the problem?

Share this post


Link to post
Share on other sites

void UpdateTexture(TGADATA *tgafile, int TexId)
{
glBindTexture(GL_TEXTURE_2D,TextureArray[TexId]);
int format;
if(tgafile->channel==4) format=GL_RGBA; else format=GL_RGB;
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, tgafile->width, tgafile->height, format, GL_UNSIGNED_BYTE, tgafile->data);
};

Here is the code. The other strange thing is that if i recreate the texture using:


gluBuild2DMipmaps(GL_TEXTURE_2D, tgafile->channel, tgafile->width, tgafile->height, TextureType, GL_UNSIGNED_BYTE, tgafile->data);

for example, it works fine, fullsize.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
I think the ARB_imaging extension can do this. IIRC, You can convolve the colour of a fragment with a matrix. It's not in the OpenGL core specification - at least in 1.2 it's optional, so ideally you'd need a fallback path.

Share this post


Link to post
Share on other sites
Quote:
Original post by Devil_Inside

void UpdateTexture(TGADATA *tgafile, int TexId)
{
glBindTexture(GL_TEXTURE_2D,TextureArray[TexId]);
int format;
if(tgafile->channel==4) format=GL_RGBA; else format=GL_RGB;
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, tgafile->width, tgafile->height, format, GL_UNSIGNED_BYTE, tgafile->data);
};

Well, I can't see anything wrong with the code.

Quote:


gluBuild2DMipmaps(GL_TEXTURE_2D, tgafile->channel, tgafile->width, tgafile->height, TextureType, GL_UNSIGNED_BYTE, tgafile->data);

for example, it works fine, fullsize.


Well, then apparently it's got something to do with the mipmapping. I can't tell you exactly what, it's a little strange. Maybe you have to upload the texture data for each of the mipmap leves individually with glTexSubImage2D...

Share this post


Link to post
Share on other sites
The other thing i noticed today is that i cant use glTexImage2D to create 32bit textures. The 32bit textures created using glTexImage2D are represented as white boxes (24bit textures are ok) while gluBuild2DMipmaps() works fine with both 32 and 24 bit.

That's what works:

glGenTextures(1,&TextureArray[TextureID]);
glBindTexture(GL_TEXTURE_2D,TextureArray[TextureID]);
int TextureType;
if(tgafile->channel==4) TextureType=GL_RGBA; else TextureType=GL_RGB;
gluBuild2DMipmaps(GL_TEXTURE_2D, tgafile->channel, tgafile->width, tgafile->height, TextureType, GL_UNSIGNED_BYTE, tgafile->data);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER, GL_LINEAR_MIPMAP_LINEAR);


And that's what doesn't work:

glGenTextures(1,&TextureArray[TextureID]);
glBindTexture(GL_TEXTURE_2D,TextureArray[TextureID]);
int TextureType;
if(tgafile->channel==4) TextureType=GL_RGBA; else TextureType=GL_RGB;
glTexImage2D(GL_TEXTURE_2D,0,tgafile->channel, tgafile->width, tgafile->height, 0, TextureType, GL_UNSIGNED_BYTE, tgafile->data);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);



And about the mipmap levels: how do i know how many levels there are?

Share this post


Link to post
Share on other sites
All right!!! I've got the solution to the problem with glTexSubImage2D(). My texture size wasn't "^2", i changed the size and now its updating fullsize.

If i get it right, i don't really need mipmaps if i'm using opengl for 2d games. I can only create level 0 texture and that's all, am i wrong? But the problem with glTexImage2D still exists. Could hardware be the cause of glTexImage2D 32-bit texture creation failure?

Share this post


Link to post
Share on other sites
Quote:
Original post by Devil_Inside
All right!!! I've got the solution to the problem with glTexSubImage2D(). My texture size wasn't "^2", i changed the size and now its updating fullsize.


Ah! Sure, that makes sense now.

Quote:

If i get it right, i don't really need mipmaps if i'm using opengl for 2d games. I can only create level 0 texture and that's all, am i wrong?


You're entirely right.

Quote:

But the problem with glTexImage2D still exists. Could hardware be the cause of glTexImage2D 32-bit texture creation failure?

You know, I had a similar problem, (might very well have been the exact same one), a little while ago. There was a case in which glTexImage2D should have worked perfectly fine, but for some reason the texture turned up white. I really had checked everything, (all the parameters were correct, glGetError returned GL_NO_ERROR, the texture had power of two dimensions, and everything...) It worked with gluBuild2DMipmaps, though. I didn't actually look into the problem, so I'm afraid I don't have a solution...

Anyway, congratulations on getting it working.

Share this post


Link to post
Share on other sites

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