Last active
May 30, 2016 06:35
-
-
Save prespondek/5b642b55910071a7d5b4 to your computer and use it in GitHub Desktop.
Apply Shader Cocos2DX
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
... | |
// 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 | |
... |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment