Applying a Shader to a BatchNode

Applying a Shader to a BatchNode
0.0 0


Hey all,

Been playing with Cocos2d-x for a while now and digging the framework. So much, in fact, that I’ve been getting in deep and messing with shaders.

I know how to hook up a shader with a sprite but when attempting to apply one to an entire batch node, it throws an error.

This is the code, the shader itself not needing any additional parameters at the moment:

CCGLProgram *glProgram = CCShaderCache::sharedShaderCache()->programForKey("BKG_LIGHTING");

if (!glProgram) {
    const GLchar *fragmentSource = (GLchar*) CCString::createWithContentsOfFile(CCFileUtils::sharedFileUtils()->fullPathForFilename("invert.fsh").c_str())->getCString();

    CCGLProgram *glProgram = new CCGLProgram();
    glProgram->initWithVertexShaderByteArray(ccPositionTextureA8Color_vert, fragmentSource);
    glProgram->addAttribute(kCCAttributeNamePosition, kCCVertexAttrib_Position);
    glProgram->addAttribute(kCCAttributeNameTexCoord, kCCVertexAttrib_TexCoords);

    CCShaderCache::sharedShaderCache()->addProgram(glProgram, "BKG_LIGHTING");

For my troubles, it returns:

Cocos2d: OpenGL error 0x0502 in /Users/colin/Documents/XCode/obt/proj.ios/../libs/cocos2dx/textures/CCTextureAtlas.cpp drawNumberOfQuads 681

And, of course, doesn’t work. My version is cocos2d-2.1rc0-x-2.1.2.

Is there some trick to applying shaders to batch nodes that I’m missing? Any ideas would be welcome!