Apply Shader Cocos2DX

... // create mesh Sprite3D* mesh = Sprite3D::create("mymesh.c3b"); // create the shader GLProgram* shader = GLProgram::createWithFilenames("shaders/lightmap.vert","shaders/lightmap.frag"); // apply shader to mesh GLProgramState* state = GLProgramState::create(shader); mesh->setGLProgramState(state); long offset = 0; auto attributeCount = mesh->getMesh()->getMeshVertexAttribCount(); for (auto i = 0; i < attributeCount; i++) { auto meshattribute = mesh->getMesh()->getMeshVertexAttribute(i); state->setVertexAttribPointer(s_attributeNames[meshattribute.vertexAttrib], meshattribute.size, meshattribute.type, GL_FALSE, mesh->getMesh()->getVertexSizeInBytes(), (GLvoid*)offset); offset += meshattribute.attribSizeBytes; } // set shader variables Texture2D* lightmap = Director::getInstance()->getTextureCache()->addImage("lightmap,png"); state->setUniformTexture("lightmap",lightmap); addChild(mesh); // reset the shader if renderer gets dumped by the OS #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID || CC_TARGET_PLATFORM == CC_PLATFORM_WP8 || CC_TARGET_PLATFORM == CC_PLATFORM_WINRT) _backToForegroundListener = EventListenerCustom::create(EVENT_RENDERER_RECREATED, [state](EventCustom*) { auto glProgram = state->getGLProgram(); glProgram->reset(); glProgram->initWithFilenames("shaders/lightmap.vert","shaders/lightmap.frag"); glProgram->link(); glProgram->updateUniforms(); } ); Director::getInstance()->getEventDispatcher()->addEventListenerWithFixedPriority(_backToForegroundListener, -1); #endif ...