HSV shift

This commit is contained in:
Jonas Kulla 2016-10-04 15:40:12 +02:00
parent 89bb3e85fd
commit 58b304ae2e
5 changed files with 33 additions and 31 deletions

View File

@ -1,12 +1,36 @@
uniform sampler2D texture; uniform sampler2D texture;
uniform lowp mat3 rotationMat; uniform mediump float hueAdjust;
varying vec2 v_texCoord; varying vec2 v_texCoord;
/* Source: gamedev.stackexchange.com/a/59808/24839 */
vec3 rgb2hsv(vec3 c)
{
const vec4 K = vec4(0.0, -1.0 / 3.0, 2.0 / 3.0, -1.0);
vec4 p = mix(vec4(c.bg, K.wz), vec4(c.gb, K.xy), step(c.b, c.g));
vec4 q = mix(vec4(p.xyw, c.r), vec4(c.r, p.yzx), step(p.x, c.r));
float d = q.x - min(q.w, q.y);
/* Avoid divide-by-zero situations by adding a very tiny delta */
const float eps = 1.0e-10;
return vec3(abs(q.z + (q.w - q.y) / (6.0 * d + eps)), d / (q.x + eps), q.x);
}
vec3 hsv2rgb(vec3 c)
{
const vec4 K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0);
vec3 p = abs(fract(c.xxx + K.xyz) * 6.0 - K.www);
return c.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y);
}
void main () void main ()
{ {
vec4 color = texture2D (texture, v_texCoord.xy); vec4 color = texture2D (texture, v_texCoord.xy);
vec3 hsv = rgb2hsv(color.rgb);
gl_FragColor = vec4(rotationMat * color.rgb, color.a); hsv.x += hueAdjust;
color.rgb = hsv2rgb(hsv);
gl_FragColor = color;
} }

View File

@ -29,8 +29,6 @@
#include <pixman.h> #include <pixman.h>
#include <math.h>
#include "gl-util.h" #include "gl-util.h"
#include "gl-meta.h" #include "gl-meta.h"
#include "quad.h" #include "quad.h"
@ -856,28 +854,10 @@ void Bitmap::hueChange(int hue)
quad.setTexPosRect(texRect, texRect); quad.setTexPosRect(texRect, texRect);
quad.setColor(Vec4(1, 1, 1, 1)); quad.setColor(Vec4(1, 1, 1, 1));
/* Calculate hue rotation matrix */
hue = wrapRange(hue, 0, 359);
float hueRad = (M_PI / 180.0f) * hue;
float x = cos(hueRad);
float y = sin(hueRad);
float a = (1.0f - x) / 3.0f;
float b = sqrt(1.0f / 3.0f) * y;
float mat[9];
mat[0] = x + a;
mat[1] = a + b;
mat[2] = a - b;
mat[3] = mat[2];
mat[4] = mat[0];
mat[5] = mat[1];
mat[6] = mat[1];
mat[7] = mat[2];
mat[8] = mat[0];
HueShader &shader = shState->shaders().hue; HueShader &shader = shState->shaders().hue;
shader.bind(); shader.bind();
shader.setRotationMat(mat); /* Shader expects normalized value */
shader.setHueAdjust(wrapRange(hue, 0, 359) / 360.0f);
FBO::bind(newTex.fbo); FBO::bind(newTex.fbo);
p->pushSetViewport(shader); p->pushSetViewport(shader);

View File

@ -90,7 +90,6 @@ typedef void (APIENTRYP _PFNGLUNIFORM1FPROC) (GLint location, GLfloat v0);
typedef void (APIENTRYP _PFNGLUNIFORM2FPROC) (GLint location, GLfloat v0, GLfloat v1); typedef void (APIENTRYP _PFNGLUNIFORM2FPROC) (GLint location, GLfloat v0, GLfloat v1);
typedef void (APIENTRYP _PFNGLUNIFORM4FPROC) (GLint location, GLfloat v0, GLfloat v1, GLfloat v2, GLfloat v3); typedef void (APIENTRYP _PFNGLUNIFORM4FPROC) (GLint location, GLfloat v0, GLfloat v1, GLfloat v2, GLfloat v3);
typedef void (APIENTRYP _PFNGLUNIFORM1IPROC) (GLint location, GLint v0); typedef void (APIENTRYP _PFNGLUNIFORM1IPROC) (GLint location, GLint v0);
typedef void (APIENTRYP _PFNGLUNIFORMMATRIX3FVPROC) (GLint location, GLsizei count, GLboolean transpose, const GLfloat* value);
typedef void (APIENTRYP _PFNGLUNIFORMMATRIX4FVPROC) (GLint location, GLsizei count, GLboolean transpose, const GLfloat* value); typedef void (APIENTRYP _PFNGLUNIFORMMATRIX4FVPROC) (GLint location, GLsizei count, GLboolean transpose, const GLfloat* value);
/* Vertex attribute */ /* Vertex attribute */
@ -175,7 +174,6 @@ typedef void (APIENTRYP _PFNGLRELEASESHADERCOMPILERPROC) (void);
GL_FUN(Uniform2f, _PFNGLUNIFORM2FPROC) \ GL_FUN(Uniform2f, _PFNGLUNIFORM2FPROC) \
GL_FUN(Uniform4f, _PFNGLUNIFORM4FPROC) \ GL_FUN(Uniform4f, _PFNGLUNIFORM4FPROC) \
GL_FUN(Uniform1i, _PFNGLUNIFORM1IPROC) \ GL_FUN(Uniform1i, _PFNGLUNIFORM1IPROC) \
GL_FUN(UniformMatrix3fv, _PFNGLUNIFORMMATRIX3FVPROC) \
GL_FUN(UniformMatrix4fv, _PFNGLUNIFORMMATRIX4FVPROC) \ GL_FUN(UniformMatrix4fv, _PFNGLUNIFORMMATRIX4FVPROC) \
/* Vertex attribute */ \ /* Vertex attribute */ \
GL_FUN(BindAttribLocation, _PFNGLBINDATTRIBLOCATIONPROC) \ GL_FUN(BindAttribLocation, _PFNGLBINDATTRIBLOCATIONPROC) \

View File

@ -550,12 +550,12 @@ HueShader::HueShader()
ShaderBase::init(); ShaderBase::init();
GET_U(rotationMat); GET_U(hueAdjust);
} }
void HueShader::setRotationMat(const float value[9]) void HueShader::setHueAdjust(float value)
{ {
gl.UniformMatrix3fv(u_rotationMat, 1, GL_FALSE, value); gl.Uniform1f(u_hueAdjust, value);
} }

View File

@ -240,10 +240,10 @@ class HueShader : public ShaderBase
public: public:
HueShader(); HueShader();
void setRotationMat(const float value[9]); void setHueAdjust(float value);
private: private:
GLint u_rotationMat; GLint u_hueAdjust;
}; };
class SimpleMatrixShader : public ShaderBase class SimpleMatrixShader : public ShaderBase