示例#1
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();
}
//--------------------------------------------------------
void CShaderNode::initShader( const char *vert, const char *frag)
{
	CCGLProgram* shader = new CCGLProgram();  
	shader->initWithVertexShaderByteArray(vert, frag);   //载入着色器程序  
	//使用着色器程序  
	this->setShaderProgram(shader);  
	shader->release();  
	CHECK_GL_ERROR_DEBUG(); 

	//绑定attribute变量  
	this->getShaderProgram()->addAttribute(kCCAttributeNamePosition, kCCVertexAttrib_Position);  
	this->getShaderProgram()->addAttribute(kCCAttributeNameColor, kCCVertexAttrib_Color);  
	this->getShaderProgram()->link();  
	CHECK_GL_ERROR_DEBUG(); 

	//获取attribute变量标识  
	m_unAttributeColor = glGetAttribLocation(this->getShaderProgram()->getProgram(), kCCAttributeNameColor);  
	m_unAttributePosition = glGetAttribLocation(this->getShaderProgram()->getProgram(), kCCAttributeNamePosition);  
	this->getShaderProgram()->updateUniforms();  
	CHECK_GL_ERROR_DEBUG(); 

	//获取uniform变量标识  
	m_unUniformResolution = glGetUniformLocation(this->getShaderProgram()->getProgram(), "resolution");  
	m_unUniformTime = glGetUniformLocation(this->getShaderProgram()->getProgram(), "time");  
	m_unUniformTex0 = glGetUniformLocation(this->getShaderProgram()->getProgram(), "tex0");  

}
示例#3
0
/**
@brief pszGLProgramName shader文件名
@param
*/
CCGLProgram* CShaderManager::getGLProgram( const char* pszGLProgramName )
{
	if( pszGLProgramName == NULL )
	{
		return NULL;
	}

	CCGLProgram* pProgram = CCShaderCache::sharedShaderCache()->programForKey( pszGLProgramName );
	if( pProgram )
	{
		return pProgram;
	}

	pProgram = new CCGLProgram();
	if( pProgram == NULL )
	{
		return NULL;
	}

	loadGLProgram( pProgram, pszGLProgramName );
	CCShaderCache::sharedShaderCache()->addProgram( pProgram, pszGLProgramName );
	m_vecGLPrograme.push_back( pszGLProgramName );

	pProgram->release();
	CHECK_GL_ERROR_DEBUG();

	return pProgram;
}
bool BloodFlashSprite::initWithTexture(CCTexture2D *pTexture, const CCRect& rect)
{
    if (CCSprite::initWithTexture(pTexture, rect)) 
    {
        CCGLProgram *program = new CCGLProgram;
        GLchar *fragSrc = (GLchar *)CCString::createWithContentsOfFile(
            CCFileUtils::sharedFileUtils()->fullPathForFilename(_fragShaderName.c_str()).c_str())->getCString();
        program->initWithVertexShaderByteArray(ccPositionTextureColor_vert, fragSrc);
        setShaderProgram(program);
        program->release();

        CHECK_GL_ERROR_DEBUG();

        getShaderProgram()->link();

        CHECK_GL_ERROR_DEBUG();

        getShaderProgram()->updateUniforms();

        CHECK_GL_ERROR_DEBUG();

        return true;
    }
    return false;
}
void SpriteMask::initProgram()
{
	std::string sharderStr = CCFileUtils::getInstance()->fullPathForFilename("Shaders/shader_mask.fsh");
	GLchar * fragSource = (GLchar*)CCString::createWithContentsOfFile(sharderStr.c_str())->getCString();
	CCGLProgram* pProgram = new CCGLProgram();
	pProgram->initWithVertexShaderByteArray(ccPositionTextureColor_vert, fragSource);
	setShaderProgram(pProgram);
	pProgram->release();

	CHECK_GL_ERROR_DEBUG();

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

	CHECK_GL_ERROR_DEBUG();

	getShaderProgram()->link();

	CHECK_GL_ERROR_DEBUG();

	getShaderProgram()->updateUniforms();

	CHECK_GL_ERROR_DEBUG();

	_maskLocation = glGetUniformLocation(getShaderProgram()->getProgram(), "u_mask");
	_offsetLocation = glGetUniformLocation(getShaderProgram()->getProgram(), "v_offset");

	CHECK_GL_ERROR_DEBUG();
}
示例#6
0
void SpriteBlur::initProgram()
{
    GLchar * fragSource = (GLchar*) CCString::createWithContentsOfFile(
                                CCFileUtils::sharedFileUtils()->fullPathForFilename("Shaders/example_Blur.fsh").c_str())->getCString();
    CCGLProgram* pProgram = new CCGLProgram();
    pProgram->initWithVertexShaderByteArray(ccPositionTextureColor_vert, fragSource);
    setShaderProgram(pProgram);
    pProgram->release();
    
    CHECK_GL_ERROR_DEBUG();
    
    getShaderProgram()->addAttribute(kCCAttributeNamePosition, kCCVertexAttrib_Position);
    getShaderProgram()->addAttribute(kCCAttributeNameColor, kCCVertexAttrib_Color);
    getShaderProgram()->addAttribute(kCCAttributeNameTexCoord, kCCVertexAttrib_TexCoords);
    
    CHECK_GL_ERROR_DEBUG();
    
    getShaderProgram()->link();
    
    CHECK_GL_ERROR_DEBUG();
    
    getShaderProgram()->updateUniforms();
    
    CHECK_GL_ERROR_DEBUG();
    
    subLocation = glGetUniformLocation( getShaderProgram()->getProgram(), "substract");
    blurLocation = glGetUniformLocation( getShaderProgram()->getProgram(), "blurSize");
    
    CHECK_GL_ERROR_DEBUG();
}
CCGLProgram * GAFStencilMaskSprite::programShaderForMask()
{
	CCGLProgram *program = CCShaderCache::sharedShaderCache()->programForKey(kPCStencilMaskAlphaFilterProgramCacheKey);
	
    if (!program)
    {
        program = GAFShaderManager::createWithFragmentFilename(ccPositionTextureColor_vert, kPCStencilMaskAlphaFilterFragmentShaderFilename);
        if (program)
        {
			program->addAttribute(kCCAttributeNamePosition, kCCVertexAttrib_Position);
			program->addAttribute(kCCAttributeNameColor,    kCCVertexAttrib_Color);
			program->addAttribute(kCCAttributeNameTexCoord, kCCVertexAttrib_TexCoords);
            program->link();
			program->updateUniforms();
            CHECK_GL_ERROR_DEBUG();
			CCShaderCache::sharedShaderCache()->addProgram(program, kPCStencilMaskAlphaFilterProgramCacheKey);
            program->release();
        }
        else
        {
            CCLOGERROR("Cannot load program for programShaderForMask.");
            return NULL;
        }
    }
	
    program->use();
	return program;
}
示例#8
0
void CSpriteRGBA::shaderMulColor(float r, float g, float b, float a)
{
    CCGLProgram *pShaders = new CCGLProgram;
    pShaders->initWithVertexShaderByteArray(ccPositionTextureColor_vert, SPRITERGBA_SHADER_MUL_COLOR_FSH);
    CHECK_GL_ERROR_DEBUG();

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

    pShaders->link();
    CHECK_GL_ERROR_DEBUG();

    pShaders->updateUniforms();
    CHECK_GL_ERROR_DEBUG();

    GLint nDotColorLocation = pShaders->getUniformLocationForName("v_dotcolor");
    pShaders->setUniformLocationWith4f(nDotColorLocation, r, g, b, a);

    scale9Image->setShaderProgram(pShaders);
    CHECK_GL_ERROR_DEBUG();

    pShaders->release();
}
void DynamicBackground::initProgram()
{
	std::string sharderStr = CCFileUtils::getInstance()->fullPathForFilename("Shaders/shader_dynamic_bg.fsh");
	GLchar * fragSource = (GLchar*)CCString::createWithContentsOfFile(sharderStr.c_str())->getCString();

	sharderStr = CCFileUtils::getInstance()->fullPathForFilename("Shaders/shader_very_pos_color.vsh");
	GLchar * vertexSource = (GLchar*)CCString::createWithContentsOfFile(sharderStr.c_str())->getCString();

	CCGLProgram* pProgram = new CCGLProgram();
	pProgram->initWithVertexShaderByteArray(vertexSource, fragSource);
	setShaderProgram(pProgram);
	pProgram->release();

	CHECK_GL_ERROR_DEBUG();

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

	CHECK_GL_ERROR_DEBUG();

	getShaderProgram()->link();

	CHECK_GL_ERROR_DEBUG();

	getShaderProgram()->updateUniforms();

	CHECK_GL_ERROR_DEBUG();

	_bgLocation = glGetUniformLocation(getShaderProgram()->getProgram(), "u_bg_texture");
	_bgSize = glGetUniformLocation(getShaderProgram()->getProgram(), "u_bg_size");
	_bgTextureCoordOffset = glGetUniformLocation(getShaderProgram()->getProgram(), "u_bg_CoordOffset");

	CHECK_GL_ERROR_DEBUG();
}
void EXGrayUtility::enableGray(CCSprite* sp)
{
    const GLchar* pszFragSource =
    "#ifdef GL_ES \n \
    precision mediump float; \n \
    #endif \n \
    uniform sampler2D u_texture; \n \
    varying vec2 v_texCoord; \n \
    varying vec4 v_fragmentColor; \n \
    void main(void) \n \
    { \n \
    // Convert to greyscale using NTSC weightings \n \
    vec4 col = texture2D(u_texture, v_texCoord); \n \
    float grey = dot(col.rgb, vec3(0.299, 0.587, 0.114)); \n \
    gl_FragColor = vec4(grey, grey, grey, col.a); \n \
    }";
    
    CCGLProgram* pProgram = new CCGLProgram();
    pProgram->initWithVertexShaderByteArray(ccPositionTextureColor_vert, pszFragSource);
    sp->setShaderProgram(pProgram);
    pProgram->release();
    
    sp->getShaderProgram()->addAttribute(kCCAttributeNamePosition, kCCVertexAttrib_Position);
    sp->getShaderProgram()->addAttribute(kCCAttributeNameColor, kCCVertexAttrib_Color);
    sp->getShaderProgram()->addAttribute(kCCAttributeNameTexCoord, kCCVertexAttrib_TexCoords);
    
    sp->getShaderProgram()->link();
    sp->getShaderProgram()->updateUniforms();
}
示例#11
0
bool ShaderRetroEffect::init()
{
    if( ShaderTestDemo::init() ) {

        GLchar * fragSource = (GLchar*) CCString::createWithContentsOfFile(CCFileUtils::sharedFileUtils()->fullPathFromRelativePath("Shaders/example_HorizontalColor.fsh"))->getCString();
        CCGLProgram *p = new CCGLProgram();
        p->initWithVertexShaderByteArray(ccPositionTexture_vert, fragSource);

        p->addAttribute(kCCAttributeNamePosition, kCCVertexAttrib_Position);
        p->addAttribute(kCCAttributeNameTexCoord, kCCVertexAttrib_TexCoords);

        p->link();
        p->updateUniforms();


        CCDirector *director = CCDirector::sharedDirector();
        CCSize s = director->getWinSize();

        m_pLabel = CCLabelBMFont::create("RETRO EFFECT", "fonts/west_england-64.fnt");

        m_pLabel->setShaderProgram(p);

        p->release();


        m_pLabel->setPosition(ccp(s.width/2,s.height/2));

        addChild(m_pLabel);

        scheduleUpdate();
        return true;
    }

    return false;
}
示例#12
0
void Ship::setShader(int what){
	do{ 

        GLchar* pszFragSource;
		if(what == 0){
			pszFragSource =  
		   "#ifdef GL_ES \n "
			"precision mediump float; \n "  
			"#endif \n "  
			"uniform sampler2D u_texture; \n " 
			"varying vec2 v_texCoord; \n "  
			"varying vec4 v_fragmentColor; \n " 
			"void main(void) \n "  
			"{ \n "  
			"// Convert to greyscale using NTSC weightings \n "  
			"vec4 col = texture2D(u_texture, v_texCoord); \n "  
			"gl_FragColor = vec4(col.r, col.g, col.b, col.a); \n " 
			"}";
		}else{
			pszFragSource =  
		   "#ifdef GL_ES \n "
			"precision mediump float; \n "  
			"#endif \n "  
			"uniform sampler2D u_texture; \n " 
			"varying vec2 v_texCoord; \n "  
			"varying vec4 v_fragmentColor; \n " 
			"void main(void) \n "  
			"{ \n "  
			"// Convert to greyscale using NTSC weightings \n "  
			"float grey = dot(texture2D(u_texture, v_texCoord).rgba, vec4(0.5, 0.0, 0.0,0.7)); \n "  
			"gl_FragColor = vec4(grey, 0.0, 0.0, 0.0); \n " 
			"}";
		}
		
        
        CCGLProgram* pProgram = new CCGLProgram();  
        pProgram->initWithVertexShaderByteArray(ccPositionTextureColor_vert, pszFragSource);  
        this->setShaderProgram(pProgram);  
        pProgram->release();  
        CHECK_GL_ERROR_DEBUG();  
          
        this->getShaderProgram()->addAttribute(kCCAttributeNamePosition, kCCVertexAttrib_Position);  
        this->getShaderProgram()->addAttribute(kCCAttributeNameColor, kCCVertexAttrib_Color);  
        this->getShaderProgram()->addAttribute(kCCAttributeNameTexCoord, kCCVertexAttrib_TexCoords);  
        CHECK_GL_ERROR_DEBUG();  
          
        this->getShaderProgram()->link();  
        CHECK_GL_ERROR_DEBUG();  
          
        this->getShaderProgram()->updateUniforms();  
        CHECK_GL_ERROR_DEBUG();   
    } while (0);  
}
void LoadSuperAnimShader(){
	CCGLProgram* aProgram = new CCGLProgram();
	
	aProgram->initWithVertexShaderByteArray(ccSuperAnim_vert, ccPositionTextureColor_frag);
	aProgram->addAttribute(kCCAttributeNamePosition, kCCVertexAttrib_Position);
	aProgram->addAttribute(kCCAttributeNameColor, kCCVertexAttrib_Color);
	aProgram->addAttribute(kCCAttributeNameTexCoord, kCCVertexAttrib_TexCoords);
	aProgram->link();
    aProgram->updateUniforms();
	
	CCShaderCache::sharedShaderCache()->addProgram(aProgram, kCCShaderSuperAnimation);
	aProgram->release();
}
示例#14
0
void QRSprite::loadShaderVertex(const char *vert, const char *frag)
{
    CCGLProgram *shader = new CCGLProgram();
    shader->initWithVertexShaderByteArray(vert, frag);
    
    shader->addAttribute(kCCAttributeNamePosition, kCCVertexAttrib_Position);
    shader->addAttribute(kCCAttributeNameColor, kCCVertexAttrib_Color);
    shader->addAttribute(kCCAttributeNameTexCoord, kCCVertexAttrib_TexCoords);
    
    shader->link();
    
    shader->updateUniforms();
    
    this->setShaderProgram(shader);
    
    shader->release();
}
示例#15
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();
}
//-------------------------------------------------------------------------
bool FKCW_RenderEx_SwayNode::init( const char* p_szSpriteFile )
{
	do{
		m_fTime = 0.0f;

		m_pSprite = CCSprite::create( p_szSpriteFile );
		if( m_pSprite == NULL )
		{
			break;
		}
		this->addChild( m_pSprite );

		// 加载顶点着色器和片元着色器
		CCGLProgram* pShader = new  CCGLProgram();
		pShader->initWithVertexShaderByteArray(ccPositionTextureA8Color_vert, szSwayShader );
		m_pSprite->setShaderProgram( pShader );
		pShader->release();
		CHECK_GL_ERROR_DEBUG();

		// 启用顶点着色器的attribute变量,坐标、纹理坐标、颜色
		m_pSprite->getShaderProgram()->addAttribute(kCCAttributeNamePosition, kCCVertexAttrib_Position);
		m_pSprite->getShaderProgram()->addAttribute(kCCAttributeNameTexCoord, kCCVertexAttrib_TexCoords);
		CHECK_GL_ERROR_DEBUG();

		// 自定义着色器链接
		m_pSprite->getShaderProgram()->link();
		CHECK_GL_ERROR_DEBUG();

		// 设置移动、缩放、旋转矩阵
		m_pSprite->getShaderProgram()->updateUniforms();
		CHECK_GL_ERROR_DEBUG();

		m_nTimeUniformLocation = glGetUniformLocation( m_pSprite->getShaderProgram()->getProgram(), "u_time" );

		m_pSprite->getShaderProgram()->use();

		// 开启帧更新
		this->schedule(schedule_selector(FKCW_RenderEx_SwayNode::UpdatePos));

		return true;
	}while(0);
	return false;
}
void SpriteBlur::initProgram()
{
	GLchar * fragSource = (GLchar*) CCString::createWithContentsOfFile(
		CCFileUtils::sharedFileUtils()->fullPathForFilename("Shaders/example_Blur.fsh").c_str())->getCString();
	CCGLProgram* pProgram = new CCGLProgram();
	pProgram->initWithVertexShaderByteArray(ccPositionTextureColor_vert, fragSource);
	setShaderProgram(pProgram);
	pProgram->release();

	CHECK_GL_ERROR_DEBUG();

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

	CHECK_GL_ERROR_DEBUG();

	getShaderProgram()->link();

	CHECK_GL_ERROR_DEBUG();

	getShaderProgram()->updateUniforms();

	CHECK_GL_ERROR_DEBUG();

	subLocation = glGetUniformLocation( getShaderProgram()->getProgram(), "substract");
	blurLocation = glGetUniformLocation( getShaderProgram()->getProgram(), "blurSize");

	CHECK_GL_ERROR_DEBUG();
    
    ccGLEnableVertexAttribs(kCCVertexAttribFlag_PosColorTex );
	ccBlendFunc blend = getBlendFunc();
	ccGLBlendFunc(blend.src, blend.dst);
    
	getShaderProgram()->use();
	getShaderProgram()->setUniformsForBuiltins();
	getShaderProgram()->setUniformLocationWith2f(blurLocation, blur_.x, blur_.y);
	getShaderProgram()->setUniformLocationWith4fv(subLocation, sub_, 1);
    
	ccGLBindTexture2D( getTexture()->getName());    
}
bool EXGraySprite::initWithTexture( cocos2d::CCTexture2D* pTexture, const cocos2d::CCRect& tRect )
{
	do{
		CC_BREAK_IF(!CCSprite::initWithTexture(pTexture, tRect));
        
        GLchar* pszFragSource =
        "#ifdef GL_ES \n \
        precision mediump float; \n \
        #endif \n \
        uniform sampler2D u_texture; \n \
        varying vec2 v_texCoord; \n \
        varying vec4 v_fragmentColor; \n \
        void main(void) \n \
        { \n \
        // Convert to greyscale using NTSC weightings \n \
        vec4 col = texture2D(u_texture, v_texCoord); \n \
        float grey = dot(col.rgb, vec3(0.299, 0.587, 0.114)); \n \
        gl_FragColor = vec4(grey, grey, grey, col.a); \n \
        }";
        
		CCGLProgram* pProgram = new CCGLProgram();
		pProgram->initWithVertexShaderByteArray(ccPositionTextureColor_vert, pszFragSource);
		this->setShaderProgram(pProgram);
		pProgram->release();
		CHECK_GL_ERROR_DEBUG();
        
		this->getShaderProgram()->addAttribute(kCCAttributeNamePosition, kCCVertexAttrib_Position);
		this->getShaderProgram()->addAttribute(kCCAttributeNameColor, kCCVertexAttrib_Color);
		this->getShaderProgram()->addAttribute(kCCAttributeNameTexCoord, kCCVertexAttrib_TexCoords);
		CHECK_GL_ERROR_DEBUG();
        
		this->getShaderProgram()->link();
		CHECK_GL_ERROR_DEBUG();
        
		this->getShaderProgram()->updateUniforms();
		CHECK_GL_ERROR_DEBUG();
        
		return true;
	} while (0);
	return false;
}
示例#19
0
bool SpriteBlur::initWithTexture(CCTexture2D* texture, const CCRect& rect)
{
    if( CCSprite::initWithTexture(texture, rect) ) 
    {
        CCSize s = getTexture()->getContentSizeInPixels();

        blur_ = ccp(1/s.width, 1/s.height);
        sub_[0] = sub_[1] = sub_[2] = sub_[3] = 0;

        GLchar * fragSource = (GLchar*) CCString::createWithContentsOfFile(
            CCFileUtils::sharedFileUtils()->fullPathFromRelativePath("Shaders/example_Blur.fsh"))->getCString();
        CCGLProgram* pProgram = new CCGLProgram();
        pProgram->initWithVertexShaderByteArray(ccPositionTextureColor_vert, fragSource);
        setShaderProgram(pProgram);
        pProgram->release();

        CHECK_GL_ERROR_DEBUG();

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

        CHECK_GL_ERROR_DEBUG();

        getShaderProgram()->link();

        CHECK_GL_ERROR_DEBUG();

        getShaderProgram()->updateUniforms();

        CHECK_GL_ERROR_DEBUG();

        subLocation = glGetUniformLocation( getShaderProgram()->getProgram(), "substract");
        blurLocation = glGetUniformLocation( getShaderProgram()->getProgram(), "blurSize");

        CHECK_GL_ERROR_DEBUG();
        return true;
    }

    return false;
}
示例#20
0
void LightLayer::initProgram()
{
	std::string sharderStr = CCFileUtils::getInstance()->fullPathForFilename("Shaders/shader_spot.fsh");
	GLchar * fragSource = (GLchar*)CCString::createWithContentsOfFile(sharderStr.c_str())->getCString();

	sharderStr = CCFileUtils::getInstance()->fullPathForFilename("Shaders/shader_very_pos_color.vsh");
	GLchar * vertexSource = (GLchar*)CCString::createWithContentsOfFile(sharderStr.c_str())->getCString();

	CCGLProgram* pProgram = new CCGLProgram();
	pProgram->initWithVertexShaderByteArray(vertexSource, fragSource);
	setShaderProgram(pProgram);
	pProgram->release();

	CHECK_GL_ERROR_DEBUG();

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

	CHECK_GL_ERROR_DEBUG();

	getShaderProgram()->link();

	CHECK_GL_ERROR_DEBUG();

	getShaderProgram()->updateUniforms();

	CHECK_GL_ERROR_DEBUG();

	_spotLocation = glGetUniformLocation(getShaderProgram()->getProgram(), "v_spot_pos");
	_innerColor = glGetUniformLocation(getShaderProgram()->getProgram(), "v_spot_innerColor");
	_spotInnerRadius = glGetUniformLocation(getShaderProgram()->getProgram(), "v_spot_innerRadius");
	_spotOuterRadius = glGetUniformLocation(getShaderProgram()->getProgram(), "v_spot_outerRadius");

	CHECK_GL_ERROR_DEBUG();

	_spotPoint = CCPoint(100, 100);
}
int FragmentEffect::addEffectNode(CCNode* pNode)
{
	do
	{
		CCGLProgram* program = new CCGLProgram();

		GLchar* pszFragSource =
				"#ifdef GL_ES													  \n \
				precision mediump float;										  \n \
				#endif															  \n \
				uniform sampler2D u_texture;									  \n \
				varying vec2 v_texCoord;										  \n \
				varying vec4 v_fragmentColor;									  \n \
				uniform mat4 matrixEffect;										  \n \
				void main(void)													  \n \
				{																  \n \
					gl_FragColor = texture2D(u_texture, v_texCoord)*matrixEffect; \n \
				}";

		program->initWithVertexShaderByteArray(ccPositionTextureColor_vert, //顶点找色器,这里是引擎自带的
												 pszFragSource);            //像素找色器,这里是自己写的

		//将生成的shader程序传给CCSprite类
		pNode->setShaderProgram(program);
		
		//要release一次,在上面的setShaderProgram将pProgram托管给CCSprite
		//setShaderProgram只调用了pProgram->retain(),没有release(),现在pProgram的m_uReference是2
		program->release();
		
		CHECK_GL_ERROR_DEBUG();
		
		//启用顶点着色器的attribute变量,坐标、纹理坐标、颜色 ,去掉其中一个或两个还能工作
		program->addAttribute(kCCAttributeNamePosition, kCCVertexAttrib_Position);
		program->addAttribute(kCCAttributeNameColor,    kCCVertexAttrib_Color);
		program->addAttribute(kCCAttributeNameTexCoord, kCCVertexAttrib_TexCoords);
		
		CHECK_GL_ERROR_DEBUG();

		//自定义着色器链接
		program->link();
		CHECK_GL_ERROR_DEBUG();

		// 设置移动、缩放、旋转矩阵  
		program->updateUniforms();
		CHECK_GL_ERROR_DEBUG();

		EffectListNode listNode;
		listNode._pNode         = pNode;
		listNode._effectSelect  = 0;
		listNode._uniformMatrix = program->getUniformLocationForName("matrixEffect");

		program->use();
		program->setUniformLocationWithMatrix4fv(listNode._uniformMatrix,
												 m_matrices[0]._matrix,
												 1);
		m_nodeList.push_back(listNode);

		return m_nodeList.size()-1;

	} while (0);
	
	return -1;
}
示例#22
0
void CCShaderCache::loadDefaultShaders()
{
    // Position Texture Color shader
    CCGLProgram *p = new CCGLProgram();
    loadDefaultShader(p, kCCShaderType_PositionTextureColor);

    m_pPrograms->setObject(p, kCCShader_PositionTextureColor);
    p->release();

    // Position Texture Color alpha test
    p = new CCGLProgram();
    loadDefaultShader(p, kCCShaderType_PositionTextureColorAlphaTest);

    m_pPrograms->setObject(p, kCCShader_PositionTextureColorAlphaTest);
    p->release();

    //
    // Position, Color shader
    //
    p = new CCGLProgram();
    loadDefaultShader(p, kCCShaderType_PositionColor);

    m_pPrograms->setObject(p, kCCShader_PositionColor);
    p->release();

    //
    // Position Texture shader
    //
    p = new CCGLProgram();
    loadDefaultShader(p, kCCShaderType_PositionTexture);

    m_pPrograms->setObject(p, kCCShader_PositionTexture);
    p->release();

    //
    // Position, Texture attribs, 1 Color as uniform shader
    //
    p = new CCGLProgram();
    loadDefaultShader(p, kCCShaderType_PositionTexture_uColor);

    m_pPrograms->setObject(p ,kCCShader_PositionTexture_uColor);
    p->release();

    //
    // Position Texture A8 Color shader
    //
    p = new CCGLProgram();
    loadDefaultShader(p, kCCShaderType_PositionTextureA8Color);
    
    m_pPrograms->setObject(p, kCCShader_PositionTextureA8Color);
    p->release();

    //
    // Position and 1 color passed as a uniform (to simulate glColor4ub )
    //
    p = new CCGLProgram();
    loadDefaultShader(p, kCCShaderType_Position_uColor);
    
    m_pPrograms->setObject(p, kCCShader_Position_uColor);
    p->release();    
}
示例#23
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;
}
void CCShaderCache::loadDefaultShaders()
{
    // Position Texture Color shader
    CCGLProgram *p = new CCGLProgram();
    loadDefaultShader(p, kCCShaderType_PositionTextureColor);

    m_pPrograms->setObject(p, kCCShader_PositionTextureColor);
    p->release();

    // Position Texture Gray shader
    p = new CCGLProgram();
    loadDefaultShader(p, kCCShaderType_PositionTextureGray);

    m_pPrograms->setObject(p, kCCShader_PositionTextureGray);
    p->release();

    // Position Texture Color alpha test
    p = new CCGLProgram();
    loadDefaultShader(p, kCCShaderType_PositionTextureColorAlphaTest);

    m_pPrograms->setObject(p, kCCShader_PositionTextureColorAlphaTest);
    p->release();

    //
    // Position, Color shader
    //
    p = new CCGLProgram();
    loadDefaultShader(p, kCCShaderType_PositionColor);

    m_pPrograms->setObject(p, kCCShader_PositionColor);
    p->release();

    //
    // Position Texture shader
    //
    p = new CCGLProgram();
    loadDefaultShader(p, kCCShaderType_PositionTexture);

    m_pPrograms->setObject(p, kCCShader_PositionTexture);
    p->release();

    //
    // Position, Texture attribs, 1 Color as uniform shader
    //
    p = new CCGLProgram();
    loadDefaultShader(p, kCCShaderType_PositionTexture_uColor);

    m_pPrograms->setObject(p ,kCCShader_PositionTexture_uColor);
    p->release();

    //
    // Position Texture A8 Color shader
    //
    p = new CCGLProgram();
    loadDefaultShader(p, kCCShaderType_PositionTextureA8Color);
    
    m_pPrograms->setObject(p, kCCShader_PositionTextureA8Color);
    p->release();

    //
    // Position and 1 color passed as a uniform (to simulate glColor4ub )
    //
    p = new CCGLProgram();
    loadDefaultShader(p, kCCShaderType_Position_uColor);
    
    m_pPrograms->setObject(p, kCCShader_Position_uColor);
    p->release();
    
    //
	// Position, Legth(TexCoords, Color (used by Draw Node basically )
	//
    p = new CCGLProgram();
    loadDefaultShader(p, kCCShaderType_PositionLengthTexureColor);
    
    m_pPrograms->setObject(p, kCCShader_PositionLengthTexureColor);
    p->release();

    //
	// ControlSwitch
	//
    p = new CCGLProgram();
    loadDefaultShader(p, kCCShaderType_ControlSwitch);
    
    m_pPrograms->setObject(p, kCCShader_ControlSwitch);
    p->release();
}
示例#25
0
bool SceneNode::initWithTexture(std::string textureName) 
{
    //enable touch
	setTouchEnabled( true );
    //set projection is 2D (default is 3D). if use 3D projection, projection error accumulation may cause ripple effect mess.
    CCDirector::sharedDirector()->setProjection(kCCDirectorProjection2D);
    //get adaptedViewport. adaptedViewport is calculated by cocos2d-x
    //so long as we use this adaptedViewport, we just writting code based on designResolutionSize, no need to worry about the screen adaptation.
    glGetIntegerv(GL_VIEWPORT,adaptedViewport);
    //get screenSize
    //screenSize is the real size of simulator/device screen
    screenSize=CCEGLView::sharedOpenGLView()->getFrameSize();
    CCLOG("screenSize:%f,%f",screenSize.width,screenSize.height);
    //get winSize
    //winSize is equals to designResolutionSize. we only need to writting code based on designResolutionSize (and forget the real screenSize).
    winSize=CCDirector::sharedDirector()->getWinSize();
    CCLOG("winSize:%f,%f",winSize.width,winSize.height);
    //determine bufferTexSize based on winSize
    bufferTexSize=CCSize(winSize.width*0.4,winSize.height*0.4);
    //use bufferTexSize to calculate step_s and step_t
    step_s=1.0/bufferTexSize.width;
	step_t=1.0/bufferTexSize.height;
	//create textures
    texBackGround = CCTextureCache::sharedTextureCache()->addImage(textureName.c_str()) ;
    bufferTexSource=createCCTexture2DWithSize(bufferTexSize,kCCTexture2DPixelFormat_RGBA8888,0.5,0.5,0.5,1);
    bufferTexDest=createCCTexture2DWithSize(bufferTexSize,kCCTexture2DPixelFormat_RGBA8888,0.5,0.5,0.5,1);
    bufferTexTemp=createCCTexture2DWithSize(bufferTexSize,kCCTexture2DPixelFormat_RGBA8888,0.5,0.5,0.5,1);
    //set texture params
    ccGLBindTexture2D(bufferTexSource->getName());
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);//GL_NEAREST
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);//GL_NEAREST
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
    ccGLBindTexture2D(bufferTexDest->getName());
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
    ccGLBindTexture2D(bufferTexTemp->getName());
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
	//create shaders
	//updateRipple shader
	{
		GLchar * fragSource = (GLchar*) CCString::createWithContentsOfFile(CCFileUtils::sharedFileUtils()->fullPathForFilename("updateRipple.fsh").c_str())->getCString();
		CCGLProgram* pProgram = new CCGLProgram();
		pProgram->initWithVertexShaderByteArray(ccPositionTextureColor_vert, fragSource);
        //bind attribute
		pProgram->addAttribute(kCCAttributeNamePosition, kCCVertexAttrib_Position);
		pProgram->addAttribute(kCCAttributeNameColor, kCCVertexAttrib_Color);
		pProgram->addAttribute(kCCAttributeNameTexCoord, kCCVertexAttrib_TexCoords);
        //link  (must after bindAttribute)
		pProgram->link();
        //get cocos2d-x build-in uniforms
		pProgram->updateUniforms();
        //get my own uniforms
		map<string,GLint> myUnifoMap;
        myUnifoMap["texSource"] =glGetUniformLocation(pProgram->getProgram(),"texSource");
        myUnifoMap["texDest"] = glGetUniformLocation(pProgram->getProgram(),"texDest");
        myUnifoMap["step_s"] = glGetUniformLocation(pProgram->getProgram(),"step_s");
        myUnifoMap["step_t"] = glGetUniformLocation(pProgram->getProgram(),"step_t");
        myUnifoMap["touchPos_winSpace"] = glGetUniformLocation(pProgram->getProgram(),"touchPos_winSpace");
        myUnifoMap["touchValid"] = glGetUniformLocation(pProgram->getProgram(),"touchValid");
        myUnifoMap["winSize"] = glGetUniformLocation(pProgram->getProgram(),"winSize");
        myUnifoMap["bufferTexSize"] = glGetUniformLocation(pProgram->getProgram(),"bufferTexSize");
		//make program_updateRipple
		program_updateRipple.myUnifoMap=myUnifoMap;
		program_updateRipple.setProgram(pProgram);
         //program can be released
        pProgram->release();
        //check gl error
		CHECK_GL_ERROR_DEBUG();
	}
	//renderRipple shader
	{
		GLchar * fragSource = (GLchar*) CCString::createWithContentsOfFile(CCFileUtils::sharedFileUtils()->fullPathForFilename("renderRipple.fsh").c_str())->getCString();
		CCGLProgram* pProgram = new CCGLProgram();
		pProgram->initWithVertexShaderByteArray(ccPositionTextureColor_vert, fragSource);
        //bind attribute
		pProgram->addAttribute(kCCAttributeNamePosition, kCCVertexAttrib_Position);
		pProgram->addAttribute(kCCAttributeNameColor, kCCVertexAttrib_Color);
		pProgram->addAttribute(kCCAttributeNameTexCoord, kCCVertexAttrib_TexCoords);
        //link  (must after bindAttribute)
		pProgram->link();
        //get cocos2d-x build-in uniforms
		pProgram->updateUniforms();
        //get my own uniforms
		map<string,GLint> myUnifoMap;
        myUnifoMap["texSource"] = glGetUniformLocation(pProgram->getProgram(),"texSource");
        myUnifoMap["step_s"] = glGetUniformLocation(pProgram->getProgram(),"step_s");
        myUnifoMap["step_t"] = glGetUniformLocation(pProgram->getProgram(),"step_t");
        //make program_renderRipple
        program_renderRipple.myUnifoMap=myUnifoMap;
        program_renderRipple.setProgram(pProgram);
        //program can be released
        pProgram->release();
        //check gl error
		CHECK_GL_ERROR_DEBUG();
	}
	//create FBO
	glGenFramebuffers(1,&hFBO);
    // create model
	float posArray[8]={0,0,winSize.width,0,winSize.width,winSize.height,0,winSize.height};
	int indexArray[6]={0,1,2,2,3,0};
    float texCoordArray[8]={0,1,1,1,1,0,0,0};
	//create indexVBO
	CindexVBO::enableAttribArrays();
	_indexVBO=new CindexVBO();
	_indexVBO->genBuffers();
	//submit data to VBO
	_indexVBO->submitPos(posArray,8,GL_STATIC_DRAW);
	_indexVBO->submitIndex(indexArray,6,GL_STATIC_DRAW);
    _indexVBO->submitTexCoord(texCoordArray,8,GL_STATIC_DRAW);
	//check gl error
	CHECK_GL_ERROR_DEBUG();

	return true ;
}