void ExampleBase::refreshShader() { if(_currentIndex < 0 || _currentIndex >= _examples.size()) { return; } _exampleSprite->setTexture("res/Logo-hd.png"); auto shaderInfo = _examples.at(_currentIndex); auto vertFilename = std::string("res/shaders/").append(shaderInfo.name).append(".vsh"); auto fragFilename = std::string("res/shaders/").append(shaderInfo.name).append(".fsh"); CCLOG("refresh shader with files: %s, %s", vertFilename.c_str(), fragFilename.c_str()); // Load the fragment shader in the file name E01_SimplestShader.fsh. // Also loads E01_SimplestShader.vsh as the vertex shader if it exists. auto glprogram = GLProgram::createWithFilenames(vertFilename, fragFilename); GLProgramState* glprogramstate = GLProgramState::getOrCreateWithGLProgram(glprogram); auto winsize = Director::getInstance()->getWinSizeInPixels(); glprogramstate->setUniformVec2("cc_ViewSizeInPixels", winsize); if(! shaderInfo.texture2.empty()) { // add extra texture auto textureCache = Director::getInstance()->getTextureCache(); auto tex1 = textureCache->getTextureForKey(shaderInfo.texture1); if(! tex1) { tex1 = textureCache->addImage(shaderInfo.texture1); } if(tex1) { // TODO: should store in shaderinfo _exampleSprite->setTexture("res/Rocks-hd.png"); glprogramstate->setUniformTexture("u_CausticTexture", tex1); } // 2nd Extra Texture auto tex2 = textureCache->getTextureForKey(shaderInfo.texture2); if(! tex2) { tex2 = textureCache->addImage(shaderInfo.texture2); } if(tex2) { Texture2D::TexParams texParams = {GL_LINEAR, GL_LINEAR, GL_REPEAT, GL_REPEAT}; tex2->setTexParameters(texParams); auto locid = glprogram->getUniformLocationForName("uNoiseTextureSize"); glprogram->setUniformLocationWith2f(locid, 30, 30); glprogramstate->setUniformVec2("u_NoiseTextureSize", tex2->getContentSizeInPixels()); glprogramstate->setUniformTexture("u_NoiseTexture", tex2); } } // attach the ProgramState to a Node _exampleSprite->setGLProgram(glprogram); }
void ShaderBrush::movePaint(Sprite *pTarget, RenderTexture *pCanvas, Point worldPos) { Point localPos = pCanvas->getSprite()->convertToNodeSpace(worldPos); localPos.y = pCanvas->getContentSize().height - localPos.y; GLProgramState* pGLPS = _brush->getGLProgramState(); _brush->setPosition(worldPos); CCLOG("movePaint worldPos%f %f", worldPos.x, worldPos.y); // set brush texture GL::bindTexture2DN(0, _brush->getTexture()->getName()); pGLPS->setUniformTexture("u_brushTexture", _brush->getTexture()->getName()); pGLPS->setUniformFloat("u_brushOpacity", ((float)_brush->getOpacity()) / 255); pGLPS->setUniformVec2("u_brushSize", Vec2(_brush->getContentSize())); // set target glstate && texture coord GL::bindTexture2DN(1, pTarget->getTexture()->getName()); pGLPS->setUniformTexture("u_targetTexture", pTarget->getTexture()->getName()); float lX = (localPos.x - _brush->getContentSize().width / 2.0) / pTarget->getContentSize().width; float lY = (localPos.y - _brush->getContentSize().height / 2.0) / pTarget->getContentSize().height; pGLPS->setUniformVec2("v_targetCoord", Vec2(lX, lY)); pGLPS->setUniformVec2("u_targetSize", Vec2(pTarget->getContentSize())); // set canvas glstate //static CCTexture2D *pCanvasTex = NULL; //if (pCanvasTex) { // delete pCanvasTex; // pCanvasTex = new Texture2D(); //} //Image *img = pCanvas->newImage(); //pCanvasTex->initWithImage(img); //delete img; //GL::bindTexture2DN(2, pCanvasTex->getName()); //_brush->getGLProgramState()->setUniformTexture("u_canvasTexture", pCanvasTex->getName()); // draw pCanvas->begin(); _brush->visit(); //CCLOG("brush pos:%f %f", _brush->getPosition().x, _brush->getPosition().y); //CCLOG("canvase pos%f %f", pCanvas->getPosition().x, pCanvas->getPosition().y); pCanvas->end(); }
void FSprite::InitShader(const std::string& alpha_file) { #ifdef ETC1 //Shader GLProgram* glp = new GLProgram(); glp->autorelease(); glp->initWithFilenames("testv.vsh", "test.fsh"); glp->bindAttribLocation(GLProgram::ATTRIBUTE_NAME_POSITION, GLProgram::VERTEX_ATTRIB_POSITION); glp->bindAttribLocation(GLProgram::ATTRIBUTE_NAME_COLOR, GLProgram::VERTEX_ATTRIB_COLOR); glp->bindAttribLocation(GLProgram::ATTRIBUTE_NAME_TEX_COORD, GLProgram::VERTEX_ATTRIB_TEX_COORD); glp->link(); glp->updateUniforms(); opacity_location_ = glGetUniformLocation(glp->getProgram(), "u_opacity"); current_alpha_file_ = alpha_file; // #pragma message WARN("getTextureForKey or someth else, texture should be added on loading scene, do not add (cocos should check that anyway but still dont do it) all the time!!") Texture2D* tex = Director::getInstance()->getTextureCache()->addImage(alpha_file); setBlendFunc(BlendFunc({ GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA })); //setBlendFunc((BlendFunc) { GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA }); GLProgramState* glprogramstate = GLProgramState::getOrCreateWithGLProgram(glp); setGLProgramState(glprogramstate); glprogramstate->setUniformFloat(opacity_location_, (float)getOpacity()/255.0); glprogramstate->setUniformTexture("u_texture1", tex); setGLProgram(glp); #endif }