这是相当复杂的,如果您不熟悉着色器和 FBO,则需要一些时间来理解它。所以这里有一些最小的、未经测试的 OpenGL 代码,它们实现了 Danvil 的答案。错误检查已被忽略;它已经足够复杂了。如果您多次调用此函数,您应该保留在此代码中创建的任何或所有对象。
如果您不关心速度,那么在 CPU 上运行的VilleK 答案要容易得多……
// 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;
希望有帮助。对于 2.0 之前的 OpenGL,您需要先加载适当的扩展,然后在此处和那里添加一些EXT
s 和/或s。ARB
如果您愿意走这条路,但没有成功,请不要犹豫,发表评论,描述您遇到的问题。我很乐意提供帮助!