• Advertisement
Sign in to follow this  

OpenGL Changing Hue of a texture

This topic is 4597 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

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
Advertisement
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
Sign in to follow this  

  • Advertisement
  • Advertisement
  • Popular Tags

  • Advertisement
  • Popular Now

  • Similar Content

    • By LifeArtist
      Good Evening,
      I want to make a 2D game which involves displaying some debug information. Especially for collision, enemy sights and so on ...
      First of I was thinking about all those shapes which I need will need for debugging purposes: circles, rectangles, lines, polygons.
      I am really stucked right now because of the fundamental question:
      Where do I store my vertices positions for each line (object)? Currently I am not using a model matrix because I am using orthographic projection and set the final position within the VBO. That means that if I add a new line I would have to expand the "points" array and re-upload (recall glBufferData) it every time. The other method would be to use a model matrix and a fixed vbo for a line but it would be also messy to exactly create a line from (0,0) to (100,20) calculating the rotation and scale to make it fit.
      If I proceed with option 1 "updating the array each frame" I was thinking of having 4 draw calls every frame for the lines vao, polygons vao and so on. 
      In addition to that I am planning to use some sort of ECS based architecture. So the other question would be:
      Should I treat those debug objects as entities/components?
      For me it would make sense to treat them as entities but that's creates a new issue with the previous array approach because it would have for example a transform and render component. A special render component for debug objects (no texture etc) ... For me the transform component is also just a matrix but how would I then define a line?
      Treating them as components would'nt be a good idea in my eyes because then I would always need an entity. Well entity is just an id !? So maybe its a component?
      Regards,
      LifeArtist
    • By QQemka
      Hello. I am coding a small thingy in my spare time. All i want to achieve is to load a heightmap (as the lowest possible walking terrain), some static meshes (elements of the environment) and a dynamic character (meaning i can move, collide with heightmap/static meshes and hold a varying item in a hand ). Got a bunch of questions, or rather problems i can't find solution to myself. Nearly all are deal with graphics/gpu, not the coding part. My c++ is on high enough level.
      Let's go:
      Heightmap - i obviously want it to be textured, size is hardcoded to 256x256 squares. I can't have one huge texture stretched over entire terrain cause every pixel would be enormous. Thats why i decided to use 2 specified textures. First will be a tileset consisting of 16 square tiles (u v range from 0 to 0.25 for first tile and so on) and second a 256x256 buffer with 0-15 value representing index of the tile from tileset for every heigtmap square. Problem is, how do i blend the edges nicely and make some computationally cheap changes so its not obvious there are only 16 tiles? Is it possible to generate such terrain with some existing program?
      Collisions - i want to use bounding sphere and aabb. But should i store them for a model or entity instance? Meaning i have 20 same trees spawned using the same tree model, but every entity got its own transformation (position, scale etc). Storing collision component per instance grats faster access + is precalculated and transformed (takes additional memory, but who cares?), so i stick with this, right? What should i do if object is dynamically rotated? The aabb is no longer aligned and calculating per vertex min/max everytime object rotates/scales is pretty expensive, right?
      Drawing aabb - problem similar to above (storing aabb data per instance or model). This time in my opinion per model is enough since every instance also does not have own vertex buffer but uses the shared one (so 20 trees share reference to one tree model). So rendering aabb is about taking the model's aabb, transforming with instance matrix and voila. What about aabb vertex buffer (this is more of a cosmetic question, just curious, bumped onto it in time of writing this). Is it better to make it as 8 points and index buffer (12 lines), or only 2 vertices with min/max x/y/z and having the shaders dynamically generate 6 other vertices and draw the box? Or maybe there should be just ONE 1x1x1 cube box template moved/scaled per entity?
      What if one model got a diffuse texture and a normal map, and other has only diffuse? Should i pass some bool flag to shader with that info, or just assume that my game supports only diffuse maps without fancy stuff?
      There were several more but i forgot/solved them at time of writing
      Thanks in advance
    • By RenanRR
      Hi All,
      I'm reading the tutorials from learnOpengl site (nice site) and I'm having a question on the camera (https://learnopengl.com/Getting-started/Camera).
      I always saw the camera being manipulated with the lookat, but in tutorial I saw the camera being changed through the MVP arrays, which do not seem to be camera, but rather the scene that changes:
      Vertex Shader:
      #version 330 core layout (location = 0) in vec3 aPos; layout (location = 1) in vec2 aTexCoord; out vec2 TexCoord; uniform mat4 model; uniform mat4 view; uniform mat4 projection; void main() { gl_Position = projection * view * model * vec4(aPos, 1.0f); TexCoord = vec2(aTexCoord.x, aTexCoord.y); } then, the matrix manipulated:
      ..... glm::mat4 projection = glm::perspective(glm::radians(fov), (float)SCR_WIDTH / (float)SCR_HEIGHT, 0.1f, 100.0f); ourShader.setMat4("projection", projection); .... glm::mat4 view = glm::lookAt(cameraPos, cameraPos + cameraFront, cameraUp); ourShader.setMat4("view", view); .... model = glm::rotate(model, glm::radians(angle), glm::vec3(1.0f, 0.3f, 0.5f)); ourShader.setMat4("model", model);  
      So, some doubts:
      - Why use it like that?
      - Is it okay to manipulate the camera that way?
      -in this way, are not the vertex's positions that changes instead of the camera?
      - I need to pass MVP to all shaders of object in my scenes ?
       
      What it seems, is that the camera stands still and the scenery that changes...
      it's right?
       
       
      Thank you
       
    • By dpadam450
      Sampling a floating point texture where the alpha channel holds 4-bytes of packed data into the float. I don't know how to cast the raw memory to treat it as an integer so I can perform bit-shifting operations.

      int rgbValue = int(textureSample.w);//4 bytes of data packed as color
      // algorithm might not be correct and endianness might need switching.
      vec3 extractedData = vec3(  rgbValue & 0xFF000000,  (rgbValue << 8) & 0xFF000000, (rgbValue << 16) & 0xFF000000);
      extractedData /= 255.0f;
    • By Devashish Khandelwal
      While writing a simple renderer using OpenGL, I faced an issue with the glGetUniformLocation function. For some reason, the location is coming to be -1.
      Anyone has any idea .. what should I do?
  • Advertisement