cocos2d lua使用 shader

前端之家收集整理的这篇文章主要介绍了cocos2d lua使用 shader前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

使用步骤:

1:初始化GlPrograme的vert和frag [ 两种方式:createWithFilenames(一般扩展用)和createWithByteArrays(内置的可以采用)]
2:link和updateUniforms
3:GlPrograme加入到GLProgramCache缓存中
4:用program生成GLProgramState并加入缓存中
方式:GLProgramState* GLProgramStateCache::getGLProgramState(GLProgram* glprogram)
5:GLProgramState设置unifroms值
5:GLProgramState apply()


假设现在有shader_A.vert和shader_A.flag两个shader文件,用这个shader来渲染sprite控件

<span style="font-family:KaiTi_GB2312;font-size:14px;"><pre name="code" class="html"><span style="font-family:KaiTi_GB2312;font-size:18px;">local glprogram = _gLProgramCache:getGLProgram(key);
    if glprogram == nil then
        glprogram = _glProgram:createWithFilenames(shader_A.vert,shader_A.frag);
        glprogram:link();
		glprogram:updateUniforms();
        _gLProgramCache:addGLProgram(glprogram,key);
    end
	
	local glProgramState = _glProgramState:getOrCreateWithGLProgram(glprogram);
	
	local param1 = gl.getUniformLocation(glprogram,"color_r");
    glProgramState:setUniformFloat(param1,r);
	local param2 = gl.getUniformLocation(glprogram,"color_g");
	glProgramState:setUniformFloat(param2,g);	
	local param3 = gl.getUniformLocation(glprogram,"color_b");
	glProgramState:setUniformFloat(param3,b);
	
	sprite:setGLProgramState(glProgramState)</span></span>

猜你在找的Cocos2d-x相关文章