先上图吧。使用opengl前后的对比:
1.在cpp中使用openGL shader。 (1)添加gray.vsh和gray.fsh到资源目录(见附件)。
(2)添加如下代码:
[cpp] view plain copy boolHelloWorld::init() { if(!Layer::init()) returnfalse; } izevisibleSize=Director::getInstance()->getVisibleSize(); autosprite=Sprite::create("HelloWorld.png"); sprite->setAnchorPoint(Point(0.5,0.5)); sprite->setposition(Point(visibleSize.wIDth/3,visibleSize.height/3)); this->addChild(sprite); graySprite(sprite); true; voIDHelloWorld::graySprite(Sprite*sprite) { if(sprite) GLProgram*p=newGLProgram(); p->initWithfilenames("gray.vsh","gray.fsh"); p->bindAttribLocation(GLProgram::ATTRIBUTE_name_position,GLProgram::VERTEX_ATTRIB_position); p->bindAttribLocation(GLProgram::ATTRIBUTE_name_color,GLProgram::VERTEX_ATTRIB_color); p->bindAttribLocation(GLProgram::ATTRIBUTE_name_TEX_COORD,GLProgram::VERTEX_ATTRIB_TEX_COORDS); p->link(); p->updateUniforms(); sprite->setShaderProgram(p); }
2.在Js中使用opengl shader. (1)添加gray.vsh和gray.fsh到资源目录。
(2)添加如下代码:
[JavaScript] varHelloWorldLayer=cc.Layer.extend({ sprite:null, ctor:function() this._super(); varsize=cc.director.getWinSize(); this.sprite=cc.Sprite.create(res.HelloWorld_png); this.sprite.attr({ x:size.wIDth/2, y:size.height/2, scale:0.5,248)"> rotation:180 }); this.addChild(this.sprite,0); graySprite(this.sprite); true; } }); functiongraySprite(sprite) varshader=newcc.GLProgram();//cc.GLProgram.create("gray.vsh","gray.fsh"); shader.retain(); //shader.initWithByteArrays("res/gray.vsh","res/gray.fsh"); shader.initWithfilenames("res/gray.vsh","res/gray.fsh"); shader.addAttribute(cc.ATTRIBUTE_name_position,cc.VERTEX_ATTRIB_position); shader.addAttribute(cc.ATTRIBUTE_name_color,cc.VERTEX_ATTRIB_color); shader.addAttribute(cc.ATTRIBUTE_name_TEX_COORD,cc.VERTEX_ATTRIB_TEX_COORDS); shader.link(); shader.updateUniforms(); sprite.setShaderProgram(shader); }
附件:gray.vsh和gray.fsh
翻译自:
1. http://www.cocos2d-x.org/forums/6/topics/49035
2. http://www.cocos2d-x.org/forums/19/topics/49038 总结
以上是内存溢出为你收集整理的【玩转cocos2d-x之四十】如何在Cocos2d-x 3.0中使用opengl shader?全部内容,希望文章能够帮你解决【玩转cocos2d-x之四十】如何在Cocos2d-x 3.0中使用opengl shader?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)