Exemple #1
0
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();
}
Exemple #2
0
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
}
// on "init" you need to initialize your instance
bool HelloWorld::init()
{
    //////////////////////////////
    // 1. super init first
    if ( !Layer::init() )
    {
        return false;
    }

    Size visibleSize = Director::getInstance()->getVisibleSize();
    Vec2 origin = Director::getInstance()->getVisibleOrigin();



    auto center = Point(visibleSize.width / 2 + origin.x, visibleSize.height / 2 + origin.y);

    auto bg = Sprite::create("bg.png");
    bg->setPosition(center);
    auto bgsize = bg->getContentSize();
    float scaleX = visibleSize.width / bgsize.width;
    float scaleY = visibleSize.height / bgsize.height;
    bg->setScaleX(scaleX);
    bg->setScaleY(scaleY);
    addChild(bg);

    auto grass = Sprite::create("grass.png");
    grass->setAnchorPoint(ccp(0.5, 0.5));
    grass->setPosition(center);
    addChild(grass);

    Texture2D::TexParams p;
    p.minFilter = GL_LINEAR;
    p.magFilter = GL_LINEAR;
    p.wrapS = GL_REPEAT;
    p.wrapT = GL_REPEAT;

    glActiveTexture(GL_TEXTURE0);

    auto textureCache = Director::getInstance()->getTextureCache();
    auto tex1 = textureCache->addImage("grass.png");
    //tex1->setTexParameters(p);

    GLProgram* prog = new GLProgram();
    prog->initWithFilenames("perlin_wind.vert", "perlin_wind.frag");
    prog->retain();
    prog->link();
    prog->use();
    prog->updateUniforms();


    float gtime = Director::getInstance()->getDeltaTime();
    float ctime = Director::getInstance()->getTotalFrames() * Director::getInstance()->getAnimationInterval();

    //CCLog("Wind is %f", pnoise);

    ShaderCache::getInstance()->addGLProgram(prog, "perlin_wind");

    grass->setGLProgram(prog);

    GLProgramState* state = GLProgramState::getOrCreateWithGLProgram(prog);

    //state->setUniformFloat("u_gtime", 1.0f);
    //state->setUniformFloat("u_ctime",0.5f);
    //state->setUniformFloat("u_color", m_cloudAmount);
    state->setUniformFloat("u_wind", 0);
    schedule(schedule_selector(HelloWorld::setWind));
    prog->release();

    schedule(schedule_selector(HelloWorld::setWind));
    return true;


}