http://blog.csdn.net/hsluzilong/article/details/17447453
步骤1.
准备shaders文件-ccShad_Hsl.h
" \n\
#ifdef GL_ES \n\
precision mediump float; \n\
#endif \n\
\n\
varying vec2 v_texCoord; \n\
uniform sampler2D CC_Texture0; \n\
\n\
uniform float AddHue; \n\
uniform float AddSat; \n\
uniform float AddLig; \n\
uniform float AddRed; \n\
uniform float AddGreen; \n\
uniform float AddBlue; \n\
uniform float AddAlpha; \n\
\n\
float Hue_2_RGB(float v1,float v2,float vH ) \n\
{ \n\
float ret;\n\
if ( vH < 0.0 )\n\
vH += 1.0;\n\
if ( vH > 1.0 )\n\
vH -= 1.0;\n\
if ( ( 6.0 * vH ) < 1.0 )\n\
ret = ( v1 + ( v2 - v1 ) * 6.0 * vH );\n\
else if ( ( 2.0 * vH ) < 1.0 )\n\
ret = ( v2 );\n\
else if ( ( 3.0 * vH ) < 2.0 )\n\
ret = ( v1 + ( v2 - v1 ) * ( ( 2.0 / 3.0 ) - vH ) * 6.0 );\n\
else\n\
ret = v1;\n\
return ret;\n\
}\n\
\n\
void main(void)\n\
{\n\
float Cmax,Cmin;\n\
\n\
float D;\n\
\n\
float H,S,L;\n\
float R,G,B;\n\
\n\
vec4 color = texture2D(CC_Texture0,v_texCoord);\n\
\n\
R = color.r;\n\
G = color.g;\n\
B = color.b;\n\
Cmax = max (R,max (G,B));\n\
Cmin = min (R,min (G,B));\n\
L = (Cmax + Cmin) / 2.0;\n\
\n\
if (Cmax == Cmin)\n\
{\n\
H = 0.0;\n\
S = 0.0;\n\
}\n\
else\n\
{\n\
D = Cmax - Cmin;\n\
if (L < 0.5)\n\
{\n\
S = D / (Cmax + Cmin);\n\
}\n\
else\n\
{\n\
S = D / (2.0 - (Cmax + Cmin));\n\
}\n\
\n\
if (R == Cmax)\n\
{\n\
H = (G - B) / D;\n\
} else {\n\
if (G == Cmax)\n\
{\n\
H = 2.0 + (B - R) /D;\n\
}\n\
else\n\
{\n\
H = 4.0 + (R - G) / D;\n\
}\n\
}\n\
H = H / 6.0;\n\
}\n\
\n\
// modify H/S/L values\n\
H += AddHue;\n\
S += AddSat;\n\
L += AddLig;\n\
\n\
if (H < 0.0)\n\
{\n\
H = H + 1.0;\n\
}\n\
\n\
H = clamp(H,0.0,1.0);\n\
S = clamp(S,1.0);\n\
L = clamp(L,1.0);\n\
\n\
// convert back to RGB\n\
float var_2,var_1;\n\
\n\
if (S == 0.0)\n\
{\n\
R = L;\n\
G = L;\n\
B = L;\n\
}\n\
else\n\
{\n\
if ( L < 0.5 )\n\
{\n\
var_2 = L * ( 1.0 + S );\n\
}\n\
else\n\
{\n\
var_2 = ( L + S ) - ( S * L );\n\
}\n\
\n\
var_1 = 2.0 * L - var_2;\n\
\n\
R = Hue_2_RGB( var_1,var_2,H + ( 1.0 / 3.0 ) );\n\
G = Hue_2_RGB( var_1,H );\n\
B = Hue_2_RGB( var_1,H - ( 1.0 / 3.0 ) );\n\
}\n\
\n\
R = R * AddRed;\n\
G = G * AddGreen;\n\
B = B * AddBlue;\n\
\n\
gl_FragColor = vec4(R,B,color.a * AddAlpha);\n\
\n\
}\n\
";
步骤2.
在ccShaders.h 中加入代码
extern CC_DLL const GLchar * ccPositionColorHSL_frag;
extern CC_DLL const GLchar * ccPositionColorHSL_vert;
在ccShaders.cpp 中加入代码
const GLchar * ccPositionColorHSL_frag =
#include "ccShad_Hsl.h"
在CCGLProgram.h 中定义
#define KCCShader_Position_hsl "KCCShader_Position_hsl"
在CCshadercache.cpp 中 追加枚举kCCShaderType_Position_hsl,
在CCshadercache.cpp 的reloadDefaultShaders中加入代码
p = programForKey(KCCShader_Position_hsl);
p->reset();
loadDefaultShader(p,kCCShaderType_Position_hsl);
在CCshadercache.cpp 的loadDefaultShader中加入代码
case kCCShaderType_Position_hsl:
p->initWithVertexShaderByteArray(ccPositionTextureColor_vert,ccPositionColorHSL_frag);
p->addAttribute(kCCAttributeNamePosition,kCCVertexAttrib_Position);
p->addAttribute(kCCAttributeNameColor,kCCVertexAttrib_Color);
p->addAttribute(kCCAttributeNameTexCoord,kCCVertexAttrib_TexCoords);
break;
在CCshadercache.cpp 的loadDefaultShaders中加入代码
p = new CCGLProgram();
loadDefaultShader(p,kCCShaderType_Position_hsl);
m_pPrograms->setobject(p,KCCShader_Position_hsl);
p->release();
if(this->m_use_hsl)
{
m_use_hsl = false;
initHSL();
}
步骤3.
在CCSprite 类中 追加 HSL 接口
- boolm_use_hsl;
- GLfloatm_color_h;
- GLfloatm_color_s;
- GLfloatm_color_l;
GLfloat m_color_a;
gluint hLocation;
gluint sLocation;
gluint lLocation;
gluint rLocation;
gluint gLocation;
gluint bLocation;
gluint aLocation;
- voidinitHSL();
- voiddrawHSL();
- voidsetHSL(floath,floats,87); font-weight:bold; background-color:inherit">floatl);
- voidsetH(floath);
- voidsetS(floats);
- voidsetL(floatl);
- floatgetH(void){returnm_color_h;}
- floatgetS(returnm_color_s;}
- floatgetL(returnm_color_l;}