Exemple #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();
}
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;
}