原文链接:http://blog.csdn.net/xufeng0991/article/details/47256583
attribute vec4 a_position; attribute vec4 a_color; varying vec4 v_fragmentColor; void main() { gl_Position = CC_MVPMatrix * a_position; v_fragmentColor = a_color; }
varying vec4 v_fragmentColor; void main() { gl_FragColor = v_fragmentColor; }
#ifndef __HELLOWORLD_SCENE_H__ #define __HELLOWORLD_SCENE_H__ #include "cocos2d.h" USING_NS_CC; 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(Renderer *renderer, const Mat4 &transform, uint32_t parentFlags) override; void onDraw(); private: CustomCommand _customCommand; GLuint VAO; GLuint vertexVBO; GLuint colorVBO; }; #endif // __HELLOWORLD_SCENE_H__
// .cpp
#include "HelloWorldScene.h" 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; } auto program = CCGLProgram::createWithFilenames("shaders/vert.vsh", "shaders/frag.fsh"); program->link(); program->updateUniforms(); this->setGLProgram(program); glGenVertexArrays(1, &VAO); glBindVertexArray(VAO); glGenBuffers(1, &vertexVBO); glBindBuffer(GL_ARRAY_BUFFER, vertexVBO); auto size = Director::getInstance()->getVisibleSize(); float vertercies[] = { 0, 0, size.width, 0, size.width / 2, size.height }; float color[] = { 0, 1, 0, 1, 1, 0, 0, 1, 0, 0, 1, 1 }; glBufferData(GL_ARRAY_BUFFER, sizeof(vertercies), vertercies, GL_STATIC_DRAW); GLuint positionLocation = glGetAttribLocation(program->getProgram(), "a_position"); glEnableVertexAttribArray(positionLocation); glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 2, GL_FLOAT, GL_FALSE, 0, (GLvoid*)0); glGenBuffers(1, &colorVBO); glBindBuffer(GL_ARRAY_BUFFER, colorVBO); glBufferData(GL_ARRAY_BUFFER, sizeof(color), color, GL_STATIC_DRAW); GLuint colorLocation = glGetAttribLocation(program->getProgram(), "a_color"); glEnableVertexAttribArray(colorLocation); glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_COLOR, 4, GL_FLOAT, GL_FALSE, 0, (GLvoid*)0); return true; } void HelloWorld::visit(cocos2d::Renderer *renderer, const Mat4 &transform, uint32_t parentFlags) { Layer::draw(renderer, transform, parentFlags); _customCommand.init(_globalZOrder); _customCommand.func = CC_CALLBACK_0(HelloWorld::onDraw, this); renderer->addCommand(&_customCommand); } void HelloWorld::onDraw() { auto glProgram = getGLProgram(); glProgram->use(); glProgram->setUniformsForBuiltins(); glBindVertexArray(VAO); glDrawArrays(GL_TRIANGLES, 0, 3); CC_INCREMENT_GL_DRAWN_BATCHES_AND_VERTICES(1, 3); CHECK_GL_ERROR_DEBUG(); }