예제 #1
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();
}
예제 #2
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;
}
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;
}
예제 #4
0
void ShaderManager::loadShader(const char* key, const GLchar* vertexShader, const GLchar* fragShader)
{
	CCGLProgram * ccProgram = CCShaderCache::sharedShaderCache()->programForKey(key);
	if (ccProgram == NULL)
	{
		ccProgram = new CCGLProgram();
		ccProgram->autorelease();
		CCShaderCache::sharedShaderCache()->addProgram(ccProgram, key);
	}
	else
	{
		ccProgram->reset();
	}
	ccProgram->initWithVertexShaderByteArray(vertexShader, fragShader);
	CHECK_GL_ERROR_DEBUG();

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

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

	// 设置移动、缩放、旋转矩阵  
	ccProgram->updateUniforms();
	CHECK_GL_ERROR_DEBUG();
}
예제 #5
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;
}
예제 #6
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 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();
}
예제 #8
0
static int tolua_CCGLProgram_CCGLProgram_addAttribute00(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_isnumber(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* attributeName = ((const char*)  tolua_tostring(tolua_S,2,0));
  unsigned int index = ((unsigned int)  tolua_tonumber(tolua_S,3,0));
#ifndef TOLUA_RELEASE
  if (!self) tolua_error(tolua_S,"invalid 'self' in function 'addAttribute'", NULL);
#endif
  {
   self->addAttribute(attributeName,index);
  }
 }
 return 0;
#ifndef TOLUA_RELEASE
 tolua_lerror:
 tolua_error(tolua_S,"#ferror in function 'addAttribute'.",&tolua_err);
 return 0;
#endif
}
예제 #9
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;
}
예제 #10
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();
}
void BatchNodeManager::setBatchNodeAlphaTestValue(float _value)
{
    CCGLProgram* alphashader = CCShaderCache::sharedShaderCache()->programForKey(kCCShader_PositionTextureColorAlphaTest);
    CHECK_GL_ERROR_DEBUG();
    
    
    alphashader->initWithVertexShaderByteArray(ccPositionTextureColor_vert, ccPositionTextureColorAlphaTest_frag);
    
    alphashader->addAttribute(kCCAttributeNamePosition, kCCVertexAttrib_Position);
    alphashader->addAttribute(kCCAttributeNameColor, kCCVertexAttrib_Color);
    alphashader->addAttribute(kCCAttributeNameTexCoord, kCCVertexAttrib_TexCoords);
    alphashader->link();
    
    alphashader->updateUniforms();
    
    unsigned loc = glGetUniformLocation(alphashader->getProgram(), kCCUniformAlphaTestValue);
    CHECK_GL_ERROR_DEBUG();
    
    alphashader->setUniformLocationWith1f(loc, _value);
    CHECK_GL_ERROR_DEBUG();
}
예제 #12
0
void CCShaders::loadCustomShader(const string& key) {
    if(!CCShaderCache::sharedShaderCache()->programForKey(key.c_str())) {
		// load shader
        CCGLProgram* p = new CCGLProgram();
		p->autorelease();
		LOAD_PROGRAM_IF(flash);
		LOAD_PROGRAM_IF(blur);
		
		// add attribute
		if(false) {
			// non-default situation
		} else {
			p->addAttribute(kCCAttributeNamePosition, kCCVertexAttrib_Position);
			p->addAttribute(kCCAttributeNameColor, kCCVertexAttrib_Color);
			p->addAttribute(kCCAttributeNameTexCoord, kCCVertexAttrib_TexCoords);
		}

		// link it
        p->link();
		
		// add custom uniform
		if(key == kCCShader_flash) {
			ADD_UNIFORM(flashColor);
			ADD_UNIFORM(flashTime);
		} else if(key == kCCShader_blur) {
			ADD_UNIFORM(blurSize);
			ADD_UNIFORM(blurSubtract);
		}
		
		// add standard uniforms
        p->updateUniforms();
		
		// add program
        CCShaderCache::sharedShaderCache()->addProgram(p, key.c_str());
    }
}
예제 #13
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();
}
예제 #14
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 ;
}
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;
}