problem with custom shader with cocos2d-x 2.1

hi!
with cocos2d-x 2.0.3 this code works perfectly:

CCGLProgram *pBWShaderProgram = new CCGLProgram();
    pBWShaderProgram->autorelease();
    pBWShaderProgram->initWithVertexShaderFilename("BlackAndWhite.vsh", "BlackAndWhite.fsh");
    pBWShaderProgram->addAttribute(kCCAttributeNamePosition, kCCVertexAttrib_Position);
    pBWShaderProgram->addAttribute(kCCAttributeNameTexCoord, kCCVertexAttrib_TexCoords);
    pBWShaderProgram->link();
    pBWShaderProgram->updateUniforms();
    CCShaderCache::sharedShaderCache()->addProgram(pBWShaderProgram, "kBWShaderProgram");

    CCSprite *mySprite = CCSprite::create("meat.png");
    mySprite->setPosition(ccp(getContentSize().width/2.0f, getContentSize().height/2.0f));
    addChild(mySprite);

    mySprite->setShaderProgram(CCShaderCache::sharedShaderCache()->programForKey("kBWShaderProgram"));
    mySprite->getShaderProgram()->use();

but it doesn’t with cocos2d-x v2.1beta3-x-2.1.0

and mySprite instead to be in black and white colors just invisible.

why?

here are my vertex and fragment shaders accordingly:

BlackAndWhite.vsh

attribute vec4 a_position;
attribute vec2 a_texCoord;

uniform mat4 u_MVPMatrix;

#ifdef GL_ES
varying mediump vec2 v_texCoord;
#else
varying vec2 v_texCoord;
#endif

void main()
{
    gl_Position = u_MVPMatrix * a_position;
    v_texCoord = a_texCoord;
}

BlackAndWhite.fsh

#ifdef GL_ES
precision mediump float;
#endif

varying vec2 v_texCoord;
uniform sampler2D u_texture;

void main()
{
    vec4 normalColor = texture2D(u_texture, v_texCoord);

    float gray = 0.299*normalColor.r + 0.587*normalColor.g + 0.114*normalColor.b;
    gl_FragColor = vec4(gray, gray, gray, normalColor.a);
}

how to make works this code with cocos2d-x v2.1beta3-x-2.1.0 ?
i would appreciate any suggestions

1 Like

Now it adds the codes before every shader

const GLchar *sources[] = {
#if (CC_TARGET_PLATFORM != CC_PLATFORM_WIN32 && CC_TARGET_PLATFORM != CC_PLATFORM_LINUX && CC_TARGET_PLATFORM != CC_PLATFORM_MAC)
        (type == GL_VERTEX_SHADER ? "precision highp float;\n" : "precision mediump float;\n"),
#endif
        "uniform mat4 CC_PMatrix;\n"
        "uniform mat4 CC_MVMatrix;\n"
        "uniform mat4 CC_MVPMatrix;\n"
        "uniform vec4 CC_Time;\n"
        "uniform vec4 CC_SinTime;\n"
        "uniform vec4 CC_CosTime;\n"
        "uniform vec4 CC_Random01;\n"
        "//CC INCLUDES END\n\n",

So your shader should use CC_MVPMatrix instead of u_MVPMatrix.

Minggo Zhang wrote:

Now it adds the codes before every shader
>
[…]
>
So your shader should use CC_MVPMatrix instead of u_MVPMatrix.

Thank you, Minggo Zhang Now all works like a charm :smiley:

Hi,

How to Make GrayScale Effect in cocos2dx…

I Have googled but not got the correct information…

can anyone tell me

Minggo Zhang wrote:

Now it adds the codes before every shader
>
[…]
>
So your shader should use CC_MVPMatrix instead of u_MVPMatrix.

04-26 20:41:16.045: D/cocos2d-x debug info(7110): cocos2d: ERROR: Failed to compile shader:

I got this error, after change the u_MVPMatrix to CC_MVPMatrix…Please help, thx!

:slight_smile: fixed it! remove this line, and change u_MVPMatrix to CC_MVPMatrix:
uniform mat4 u_MVPMatrix;