这里贴出具体的代码,比较简单。
HelloWorldScene.h
#ifndef __HELLOWORLD_SCENE_H__ #define __HELLOWORLD_SCENE_H__ #include "cocos2d.h" class HelloWorld : public cocos2d::Layer { public: // there's no 'id' in cpp, so we recommend returning the class instance pointer static cocos2d::Scene* createScene(); // Here's a difference. Method 'init' in cocos2d-x returns bool, instead of returning 'id' in cocos2d-iphone virtual bool init(); // implement the "static create()" method manually CREATE_FUNC(HelloWorld); virtual void visit(cocos2d::Renderer *renderer, const cocos2d::Mat4 &parentTransform, uint32_t parentFlags); void onDraw(); private: cocos2d::CustomCommand _command; }; #endif // __HELLOWORLD_SCENE_H__
#include "HelloWorldScene.h" USING_NS_CC; Scene* HelloWorld::createScene() { // 'scene' is an autorelease object auto scene = Scene::create(); // 'layer' is an autorelease object auto layer = HelloWorld::create(); // add layer as a child to scene scene->addChild(layer); // return the scene return scene; } // on "init" you need to initialize your instance bool HelloWorld::init() { ////////////////////////////// // 1. super init first if ( !Layer::init() ) { return false; } this->setGLProgram(GLProgramCache::getInstance()->getGLProgram(GLProgram::SHADER_NAME_POSITION_COLOR)); return true; } void HelloWorld::onDraw() { //获得当前HelloWorld的shader auto glProgram = getGLProgram(); //使用此shader glProgram->use(); //设置该shader的一些内置uniform,主要是MVP,即model-view-project矩阵 glProgram->setUniformsForBuiltins(); auto size = Director::getInstance()->getWinSize(); //指定将要绘制的三角形的三个顶点,分别位到屏幕左下角,右下角和正中间的顶端 float vertercies[] = {0 , 0 , size.width , 0 , size.width / 2 , size.height}; //指定每一个顶点的颜色,颜色值是RGBA格式的,取值范围是0-1 float color[] = {0 , 0 , 1 , 1, //第一个点的颜色值蓝色 1 , 0 , 0 ,1 , //第二个点的颜色值 红色 0 , 1 , 0 , 1}; //第三个点的颜色值 绿色 //激活名字为position和color的vertex attribute GL::enableVertexAttribs(GL::VERTEX_ATTRIB_FLAG_POSITION | GL::VERTEX_ATTRIB_FLAG_COLOR); //分别给position和color指定数据源 glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 2, GL_FLOAT, GL_FALSE, 0, vertercies); glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_COLOR, 4, GL_FLOAT, GL_FALSE, 0, color); //绘制三角形,所谓的draw call就是指这个函数调用 glDrawArrays(GL_TRIANGLES, 0, 3); //通知cocos2d-x 的renderer,让它在合适的时候调用这些OpenGL命令 CC_INCREMENT_GL_DRAWN_BATCHES_AND_VERTICES(1, 3); //如果出错了,可以使用这个函数来获取出错信息 CHECK_GL_ERROR_DEBUG(); } void HelloWorld::visit(cocos2d::Renderer *renderer, const cocos2d::Mat4 &parentTransform, uint32_t parentFlags) { Layer::visit(renderer, parentTransform, parentFlags); _command.init(_globalZOrder); _command.func = CC_CALLBACK_0(HelloWorld::onDraw, this); Director::getInstance()->getRenderer()->addCommand(&_command); }