void ShaderSprite::initShader() { GLchar * fragSource = (GLchar*) String::createWithContentsOfFile( FileUtils::getInstance()->fullPathForFilename(_fragSourceFile).c_str())->getCString(); auto program = new GLProgram(); program->initWithByteArrays(ccPositionTextureColor_vert, fragSource); setShaderProgram(program); program->release(); CHECK_GL_ERROR_DEBUG(); program->bindAttribLocation(GLProgram::ATTRIBUTE_NAME_POSITION, GLProgram::VERTEX_ATTRIB_POSITION); program->bindAttribLocation(GLProgram::ATTRIBUTE_NAME_COLOR, GLProgram::VERTEX_ATTRIB_COLOR); program->bindAttribLocation(GLProgram::ATTRIBUTE_NAME_TEX_COORD, GLProgram::VERTEX_ATTRIB_TEX_COORDS); CHECK_GL_ERROR_DEBUG(); program->link(); CHECK_GL_ERROR_DEBUG(); program->updateUniforms(); CHECK_GL_ERROR_DEBUG(); buildCustomUniforms(); CHECK_GL_ERROR_DEBUG(); }
bool GLProgram::initWithFilenames(const std::string& vShaderFilename, const std::string& fShaderFilename, const std::string& compileTimeDefines) { auto fileUtils = FileUtils::getInstance(); std::string vertexSource = fileUtils->getStringFromFile(FileUtils::getInstance()->fullPathForFilename(vShaderFilename)); std::string fragmentSource = fileUtils->getStringFromFile(FileUtils::getInstance()->fullPathForFilename(fShaderFilename)); return initWithByteArrays(vertexSource.c_str(), fragmentSource.c_str(), compileTimeDefines); }
GLProgram* GLProgram::createWithByteArrays(const GLchar* vShaderByteArray, const GLchar* fShaderByteArray, const std::string& compileTimeDefines) { auto ret = new (std::nothrow) GLProgram(); if(ret && ret->initWithByteArrays(vShaderByteArray, fShaderByteArray, compileTimeDefines)) { ret->link(); ret->updateUniforms(); ret->autorelease(); return ret; } CC_SAFE_DELETE(ret); return nullptr; }
Effect::Effect():_glProgramState(nullptr) { #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID) _backgroundListener = EventListenerCustom::create(EVENT_RENDERER_RECREATED, [this](EventCustom*) { auto glProgram = _glprogramstate->getGLProgram(); glProgram->reset(); glProgram->initWithByteArrays(ccPositionTextureColor_noMVP_vert, _fragSource.c_str()); glProgram->link(); glProgram->updateUniforms(); } ); Director::getInstance()->getEventDispatcher()->addEventListenerWithFixedPriority(_backgroundListener, -1); #endif }
BaseEffect3D::BaseEffect3D() : m_pGLprogramstate(nullptr) { #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID) m_pBackgroundListener = EventListenerCustom::create(EVENT_RENDERER_RECREATED, [this](EventCustom*) { auto glProgram = m_pGLprogramstate->getGLProgram(); glProgram->reset(); glProgram->initWithByteArrays(m_strVertSource.c_str(), m_strFragSource.c_str()); glProgram->link(); glProgram->updateUniforms(); } ); Director::getInstance()->getEventDispatcher()->addEventListenerWithFixedPriority(m_pBackgroundListener, -1); #endif }
bool GLProgram::initWithByteArrays(const GLchar* vShaderByteArray, const GLchar* fShaderByteArray) { return initWithByteArrays(vShaderByteArray, fShaderByteArray, EMPTY_DEFINE); }
bool GLProgram::initWithByteArrays(const GLchar* vShaderByteArray, const GLchar* fShaderByteArray, const std::string& compileTimeDefines) { return initWithByteArrays(vShaderByteArray, fShaderByteArray, "", compileTimeDefines); }