我被一个变灰的问题搞崩溃了,具体是这样的,使用自定义着色器可以将图片变灰。不太懂gl,因此找的现成的代码。
问题来了!!!!
一种方式是将下面的着色器代码以文件方式放到资源目录下,gray.vsh 和gray.fsh, 然后
auto glprogram = GLProgram::createWithFilenames(“gray.vsh”, “gray.fsh”);
这样使用
gray.vsh
#ifdef GL_ES
precision mediump float;
#endif
uniform sampler2D u_texture;
varying vec2 v_texCoord;
varying vec4 v_fragmentColor;
void main(void)
{
vec4 col = texture2D(u_texture, v_texCoord);
float grey = dot(col.rgb, vec3(0.299, 0.587, 0.114));
gl_FragColor = vec4(grey,grey,grey,col.a);
}
gray.fsh
#ifdef GL_ES
precision mediump float;
#endif
uniform sampler2D u_texture;
varying vec2 v_texCoord;
varying vec4 v_fragmentColor;
void main(void)
{
vec4 col = texture2D(u_texture, v_texCoord);
float grey = dot(col.rgb, vec3(0.299, 0.587, 0.114));
gl_FragColor = vec4(grey,grey,grey,col.a);
}
另一种方式是直接内存加载
const GLchar* pszFragSource =
“#ifdef GL_ES \n
precision mediump float; \n
#endif \n
uniform sampler2D u_texture; \n
varying vec2 v_texCoord; \n
varying vec4 v_fragmentColor; \n
void main(void) \n
{ \n
// Convert to greyscale using NTSC weightings \n
vec4 col = texture2D(u_texture, v_texCoord); \n
float grey = dot(col.rgb, vec3(0.299, 0.587, 0.114)); \n
gl_FragColor = vec4(grey, grey, grey, col.a); \n
}”;
GLProgram* pProgram = new GLProgram();
pProgram->initWithByteArrays(ccPositionTextureColor_noMVP_vert, pszFragSource);
但是,一样的代码,第二种方式成功变灰!!!!!
第一种方式却显示个黑块!!!!!!!!!!!!!!
我知道,这应该是透明信息丢失,但问题是一样的着色器代码阿!!!!!仅仅是一个读文件,一个内存加载,我已经被搞疯了,完全没搞懂啊!求救!