Пример #1
0
bool Assets::loadCustomShaders()
{
    /// Scrollable texture shader
    {
        CCGLProgram *shader = new CCGLProgram();
        shader->initWithVertexShaderFilename("shaders/shader_PositionTexture_uColor_uTime.vsh", "shaders/shader_PositionTexture_uColor_uTime.fsh");
        shader->addAttribute(kCCAttributeNamePosition, kCCVertexAttrib_Position);
        shader->addAttribute(kCCAttributeNameTexCoord, kCCVertexAttrib_TexCoords);
        shader->link();
        shader->updateUniforms();
        CCShaderCache::sharedShaderCache()->addProgram(shader, settings::kShader_PositionTexture_uColor_uTime);
    }

    /// Grayscale shader
    {
        CCGLProgram *shader = new CCGLProgram();
        shader->initWithVertexShaderFilename("shaders/shader_Grayscale.vsh", "shaders/shader_Grayscale.fsh");
        shader->addAttribute(kCCAttributeNamePosition, kCCVertexAttrib_Position);
        shader->addAttribute(kCCAttributeNameTexCoord, kCCVertexAttrib_TexCoords);
        shader->link();
        shader->updateUniforms();
        CCShaderCache::sharedShaderCache()->addProgram(shader, settings::kShader_Grayscale);
    }

    CHECK_GL_ERROR_DEBUG();

    return true;
}
Пример #2
0
CCGLProgram* CCWaveFilter::loadShader()
{
	CCGLProgram* program = new CCGLProgram();
	program->initWithVertexShaderFilename("wave.vsh","wave.fsh");

	return program;
}
Пример #3
0
void ShaderNode::loadShaderVertex(const char *vert, const char *frag)
{
	CCGLProgram* shader = new CCGLProgram();
	shader->initWithVertexShaderFilename(vert, frag);   //载入着色器程序  

	//绑定attribute变量  
	shader->addAttribute("a_position", 0);
	shader->addAttribute("a_color", 1);
	shader->link();

	//获取attribute变量标识  
	m_attributeColor = glGetAttribLocation(shader->getProgram(), "a_color");
	m_attributePosition = glGetAttribLocation(
		shader->getProgram(), "a_position");
	shader->updateUniforms();

	//获取uniform变量标识  
	m_uniformResolution = glGetUniformLocation(shader->getProgram(), "resolution");
	m_uniformTime = glGetUniformLocation(shader->getProgram(), "time");
	m_uniformTex0 = glGetUniformLocation(shader->getProgram(), "tex0");

	//使用着色器程序  
	this->setShaderProgram(shader);
	shader->release();
}
Пример #4
0
static int tolua_CCGLProgram_CCGLProgram_initWithVertexShaderFilename00(lua_State* tolua_S)
{
#ifndef TOLUA_RELEASE
 tolua_Error tolua_err;
 if (
     !tolua_isusertype(tolua_S,1,"CCGLProgram",0,&tolua_err) ||
     !tolua_isstring(tolua_S,2,0,&tolua_err) ||
     !tolua_isstring(tolua_S,3,0,&tolua_err) ||
     !tolua_isnoobj(tolua_S,4,&tolua_err)
 )
  goto tolua_lerror;
 else
#endif
 {
  CCGLProgram* self = (CCGLProgram*)  tolua_tousertype(tolua_S,1,0);
  const char* vShaderFilename = ((const char*)  tolua_tostring(tolua_S,2,0));
  const char* fShaderFilename = ((const char*)  tolua_tostring(tolua_S,3,0));
#ifndef TOLUA_RELEASE
  if (!self) tolua_error(tolua_S,"invalid 'self' in function 'initWithVertexShaderFilename'", NULL);
#endif
  {
   bool tolua_ret = (bool)  self->initWithVertexShaderFilename(vShaderFilename,fShaderFilename);
   tolua_pushboolean(tolua_S,(bool)tolua_ret);
  }
 }
 return 1;
#ifndef TOLUA_RELEASE
 tolua_lerror:
 tolua_error(tolua_S,"#ferror in function 'initWithVertexShaderFilename'.",&tolua_err);
 return 0;
#endif
}
Пример #5
0
bool Assets::reloadCustomShaders()
{
    CCLOG("reload custom shaders");
    CCGLProgram *shader = CCShaderCache::sharedShaderCache()->programForKey(settings::kShader_PositionTexture_uColor_uTime);
    shader->reset();
    shader->initWithVertexShaderFilename("shaders/shader_PositionTexture_uColor_uTime.vsh", "shaders/shader_PositionTexture_uColor_uTime.fsh");
    shader->addAttribute(kCCAttributeNamePosition, kCCVertexAttrib_Position);
    shader->addAttribute(kCCAttributeNameTexCoord, kCCVertexAttrib_TexCoords);
    shader->link();
    shader->updateUniforms();

    /// TODO

    CHECK_GL_ERROR_DEBUG();

    return true;
}
Пример #6
0
void ShaderNode::loadShaderVertex(const char *vert, const char *frag)
{
    CCGLProgram *shader = new CCGLProgram();
    shader->initWithVertexShaderFilename(vert, frag);

    shader->addAttribute("aVertex", kCCVertexAttrib_Position);
    shader->link();

    shader->updateUniforms();

    m_uniformCenter = glGetUniformLocation(shader->getProgram(), "center");
    m_uniformResolution = glGetUniformLocation(shader->getProgram(), "resolution");
    m_uniformTime = glGetUniformLocation(shader->getProgram(), "time");

    this->setShaderProgram(shader);

    shader->release();
}
Пример #7
0
// on "init" you need to initialize your instance
bool Stage::init()
{
	bool bRet = false;
	do 
	{
		//////////////////////////////////////////////////////////////////////////
		// super init first
		//////////////////////////////////////////////////////////////////////////

		CC_BREAK_IF(! CCLayer::init());

		//////////////////////////////////////////////////////////////////////////
		// add your codes below...
		//////////////////////////////////////////////////////////////////////////
		// Get window size and place the label upper. 
		CCSize size = CCDirector::sharedDirector()->getWinSize();
		CCSprite* pBgSprite;
		// 3. Add add a splash screen, show the cocos2d splash image.
		pBgSprite = CCSprite::create("bg.jpg");
		CC_BREAK_IF(! pBgSprite);

		// Place the sprite on the center of the screen
		pBgSprite->setPosition(ccp(size.width/2, size.height/2));
		// Add the sprite to HelloWorld layer as a child layer.
		this->addChild(pBgSprite, -5);

		//game logo
		CCSprite* pLogoSprite;
		// 3. Add add a splash screen, show the cocos2d splash image.
		pLogoSprite = CCSprite::create("glogo.png");
		CC_BREAK_IF(! pLogoSprite);

		// Place the sprite on the center of the screen
		pLogoSprite->setAnchorPoint(ccp(0.0f, 01.f));
		pLogoSprite->setPosition(ccp(0, size.height));
		// Add the sprite to HelloWorld layer as a child layer.
		this->addChild(pLogoSprite, -4);

		CCSprite* pBottleSprite = CCSprite::create("bottle.png");
		CC_BREAK_IF(! pBottleSprite);
		pBottleSprite->setAnchorPoint(ccp(0.0f, 0.0f));
		// Place the sprite on the center of the screen
		pBottleSprite->setPosition(ccp(0.0f, 0.0f));
		// Add the sprite to HelloWorld layer as a child layer.
		this->addChild(pBottleSprite, 4);

		SetupWorld();

		/*
		sn = ShaderNode::shaderNodeWithVertex("Shaders/example_Flower.vsh", "Shaders/example_Flower.fsh");
		sn->setPosition(ccp(size.width/2, size.height/2));
		addChild(sn, -1);
		*/

		rTexture = CCRenderTexture::create(WIN_WIDTH, WIN_HEIGHT,(CCTexture2DPixelFormat)kTexture2DPixelFormat_RGBA4444);
		gTexture = CCRenderTexture::create(WIN_WIDTH, WIN_HEIGHT,(CCTexture2DPixelFormat)kTexture2DPixelFormat_RGBA4444);
		bTexture = CCRenderTexture::create(WIN_WIDTH, WIN_HEIGHT,(CCTexture2DPixelFormat)kTexture2DPixelFormat_RGBA4444);
		yTexture = CCRenderTexture::create(WIN_WIDTH, WIN_HEIGHT,(CCTexture2DPixelFormat)kTexture2DPixelFormat_RGBA4444);
		if (NULL == rTexture)
		{
			CCLog("create renderTex fail");
			return false;
		}
		mainTexture = CCRenderTexture::create(WIN_WIDTH, WIN_HEIGHT, (CCTexture2DPixelFormat)kTexture2DPixelFormat_RGBA4444);
		
		
		if (NULL == mainTexture)
		{
			CCLog("create mainTexture fail");
			return false;
		}

		
		///
		 //renderTexture.sprite.shaderProgram = [[CCGLProgram alloc] initWithVertexShaderByteArray:ccPositionTextureA8Color_vert
//fragmentShaderByteArray:ccPositionTextureColorAlphaTest_frag];

		 shader = new CCGLProgram();
		 //shader->initWithVertexShaderByteArray(ccPositionTextureA8Color_vert, ccPositionTextureColorAlphaTest_frag);
		 shader->initWithVertexShaderFilename("Shaders/mix.vsh", "Shaders/mix.fsh");
		 rTexture->getSprite()->setShaderProgram(shader);
		 gTexture->getSprite()->setShaderProgram(shader);
		 bTexture->getSprite()->setShaderProgram(shader);
		 yTexture->getSprite()->setShaderProgram(shader);
		 shader->release();
        //[renderTexture.sprite.shaderProgram addAttribute:kCCAttributeNamePosition index:kCCVertexAttrib_Position];
		shader->addAttribute(kCCAttributeNamePosition, kCCVertexAttrib_Position);
        //[renderTexture.sprite.shaderProgram addAttribute:kCCAttributeNameColor index:kCCVertexAttrib_Color];
		shader->addAttribute(kCCAttributeNameColor, kCCVertexAttrib_Color);
        //[renderTexture.sprite.shaderProgram addAttribute:kCCAttributeNameTexCoord index:kCCVertexAttrib_TexCoords];
		shader->addAttribute(kCCAttributeNameTexCoord, kCCVertexAttrib_TexCoords);
		shader->link();
		shader->updateUniforms();

		 alphaValueLocation = glGetUniformLocation(shader->getProgram(), "u_alpha_value");
		 colorValueLocation = glGetUniformLocation(shader->getProgram(), "u_color_value");
		 //m_uMaskLocation    = glGetUniformLocation( shader->getProgram(), "u_mask");
		shader->setUniformLocationWith1f(alphaValueLocation, THROLD);
		shader->use();
		///
		CCGLProgram* pProgram = new CCGLProgram();
        pProgram->initWithVertexShaderFilename("Shaders/mix.vsh", "Shaders/mix2.fsh");
		mainTexture->getSprite()->setShaderProgram(pProgram);
        pProgram->release();

        getShaderProgram()->addAttribute(kCCAttributeNamePosition, kCCVertexAttrib_Position);
        getShaderProgram()->addAttribute(kCCAttributeNameColor, kCCVertexAttrib_Color);
        getShaderProgram()->addAttribute(kCCAttributeNameTexCoord, kCCVertexAttrib_TexCoords);

        getShaderProgram()->link();
        getShaderProgram()->updateUniforms();
		m_uMaskLocation    = glGetUniformLocation( shader->getProgram(), "u_mask");
		m_u2MaskLocation    = glGetUniformLocation( shader->getProgram(), "u_mask2");
		m_u3MaskLocation    = glGetUniformLocation( shader->getProgram(), "u_mask3");
		m_u4MaskLocation    = glGetUniformLocation( shader->getProgram(), "u_mask4");
		///
		//renderTexture->setAnchorPoint(ccp(0.0, -1));
		rTexture->setPosition(ccp(WIN_WIDTH/2.0, WIN_HEIGHT/2.0));
		rTexture->retain();

		gTexture->setPosition(ccp(WIN_WIDTH/2.0, WIN_HEIGHT/2.0));
		gTexture->retain();

		bTexture->setPosition(ccp(WIN_WIDTH/2.0, WIN_HEIGHT/2.0));
		bTexture->retain();

		yTexture->setPosition(ccp(WIN_WIDTH/2.0, WIN_HEIGHT/2.0));
		yTexture->retain();

		//mainTexture->getSprite()->setShaderProgram(shader);
		mainTexture->setAnchorPoint(ccp(0.5,0.5));
		mainTexture->setPosition(ccp(WIN_WIDTH/2, WIN_HEIGHT/2.0));


		

		addChild(mainTexture, -2);
		scheduleUpdate();
		bRet = true;
	} while (0);

	return bRet;
}