Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 759795
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T15:38:24+00:00 2026-05-14T15:38:24+00:00

If I have an OpenGL texture, and I need to perform HSL modifications on

  • 0

If I have an OpenGL texture, and I need to perform HSL modifications on it before rendering the texture, from what I’ve heard I need a shader. Problem is, I know nothing about shaders. Does anyone know where I would need to look?

I want to write a function where I can pass in a texture and three values, a hue shift in degrees, and saturation and lightness multipliers between 0 and 2, and then have it call a shader that will apply these transformations to the texture before it renders. The interface would look something like this:

procedure HSLTransform(texture: GLuint; hShift: integer; sMult, lMult: GLfloat);

I have no idea what’s supposed to go inside the routine, though. I understand the basic math involved in HSL/RGB conversions, but I don’t know how to write a shader or how to apply it. Can someone point me in the right direction? Delphi examples preferred, but I can also read C if I have to.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T15:38:24+00:00Added an answer on May 14, 2026 at 3:38 pm

    This is quite involved, and if you’re new to shaders and FBO’s it will take some time to understand it. So here is some minimal, untested OpenGL code that implements Danvil’s answer. Error checking has been left out; it’s complicated enough as it is. If you call this function many times, you should hold on to any or all of the objects created in this code.

    If you don’t care about speed, VilleK’s answer that runs on the CPU is a lot easier…

    // Create the target texture object.
    GLuint target;
    glGenTextures(1, &target);
    
    // Bind the target texture.
    glBindTexture(GL_TEXTURE_2D, target);
    
    // Allocate texture memory. width and height must be the size of the original texture.
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB8, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
    
    // Create a framebuffer object.
    GLuint fbo;
    glGenFramebuffers(1, &fbo);
    
    // Bind the framebuffer object.
    glBindFramebuffer(GL_FRAMEBUFFER, fbo);
    
    // Attach the target texture as the render target.
    glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, target, 0);
    
    // Check framebuffer status.
    if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
        throw "Framebuffer incomplete.";
    
    // Create fragment shader to do the transformation.
    GLuint frag = glCreateShader(GL_FRAGMENT_SHADER);
    
    // Set the shader's source code.
    char const *source =
        "uniform sampler2D input;\n"
        "uniform float hShift, sMult, lMult;\n"
        "void main() {\n"
        "    vec4 color = texture2D(input, gl_FragCoord.xy);\n"
        "    /* Do your HSL transformations here... */\n"
        "    gl_FragColor = color;\n"
        "}\n";
    glShaderSource(frag, 1, &source, 0);
    
    // Compile the shader.
    glCompileShader(frag);
    
    // Check compilation result. Here, you probably want to use glGetShaderInfoLog() to get any compilation errors.
    GLint status;
    glGetShader(frag, GL_COMPILE_STATUS, &status);
    if (status == GL_FALSE)
        throw "Shader compilation failed";
    
    // Create the program.
    GLuint program = glCreateProgram();
    
    // Attach the shader to the program.
    glAttachShader(program, frag);
    
    // Link the program.
    glLinkProgram(program);
    
    // Check link result. Here, you probably want to use glGetProgramInfoLog() to get any link errors.
    glGetProgram(program, GL_LINK_STATUS, &status);
    if (status == GL_FALSE)
        throw "Program linking failed"
    
    // Use the program for subsequent rendering.
    glUseProgram(program);
    
    // Set the values of the uniform parameters.
    glUniform1i(glUniformLocation(program, "input"), 0);
    glUniform1f(glUniformLocation(program, "hShift"), hShift);
    glUniform1f(glUniformLocation(program, "sMult"), sMult);
    glUniform1f(glUniformLocation(program, "lMult"), lMult);
    
    // Bind the source texture to read from.
    glBindTexture(GL_TEXTURE_2D, texture);
    
    // Set up the viewport and matrices.
    glPushAttrib(GL_VIEWPORT_BIT | GL_TRANSFORM_BIT);
    glMatrixMode(GL_MODELVIEW);
    glPushMatrix();
    glLoadIdentity();
    glMatrixMode(GL_PROJECTION);
    glPushMatrix();
    glLoadIdentity();
    glOrtho(0, 1, 0, 1, 0, 1);
    glViewport(0, 0, width, height);
    
    // Render a quad.
    glBegin(GL_QUADS);
    glVertex2i(0, 0);
    glVertex2i(1, 0);
    glVertex2i(1, 1);
    glVertex2i(0, 1);
    glEnd();
    
    // Restore the matrices and viewport.
    glPopMatrix();
    glMatrixMode(GL_MODELVIEW);
    glPopMatrix();
    glPopAttrib();
    
    // Stop using the program.
    glUseProgram(0);
    
    // Stop using the framebuffer object.
    glBindFramebuffer(GL_FRAMEBUFFER, 0);
    
    // Delete created objects.
    glDeleteProgram(program);
    glDeleteShader(frag);
    glDeleteFramebuffers(1, &fbo);
    
    // Optionally, delete the old, untransformed texture.
    glDeleteTextures(1, &texture);
    
    // Return the new texture.
    return target;
    

    Hope that helps. For pre-2.0 OpenGL, you need to load the proper extensions first, and slap on some EXTs and/or ARBs here and there.

    If you’re willing to go down this road, and it doesn’t work out, don’t hesitate to post a comment describing the problems you encounter. I’ll be happy to help!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an OpenGL texture rendered to a first FBO and I need to
I have a square polygon with a texture (opengl es 1.1), and i need
I have an OpenGL texture with transparent and opaque pixels (eg, texture contains a
This is hopefully a simple question: I have an OpenGL texture and would like
I have a strange problem with opengl es(Android) .Some textures doesn't drawing correct ,its
I have an opengl scene rendering on an EAGLView layer and some other elements
I have a program in which I need to apply a 2-dimensional texture (simple
I'm writing data into a 3D texture from within a fragment shader, and I
I have a sprite loaded as a texture and I need to animate it,
I've read a texture example in OpenGL 2.1 . The fragment shader looks like

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.