Call OpenGL in cocos2dx (3.X)
I have been using pomelo to write back-end data for the past few months. Now the project is coming to an end and I have finally had time to study OpenGL-related things.
OpenGL itself is cross-platform, but the development environment of each platform is not the same. It is much easier to put OpenGL code in cocos to run it.
The following is a simple example. In cocos, call the OpenGL method and set the window to blue.
Create a cocos project, delete unnecessary sample code, and override the draw and onDraw methods.
Add code to the header file:
public: virtual void draw(cocos2d::Renderer *renderer, const cocos2d::Mat4 &transform, uint32_t flags) override; void onDraw(const cocos2d::Mat4& transform, uint32_t flags);private: cocos2d::CustomCommand _customCommand;
Code in the source file:
Void HelloWorld: draw (Renderer * renderer, const Mat4 & transform, uint32_t flags) {_ customCommand. init (_ globalZOrder, transform, flags); _ customCommand. func = CC_CALLBACK_0 (HelloWorld: onDraw, this, transform, flags); renderer-> addCommand (& _ customCommand);} void HelloWorld: onDraw (const Mat4 & transform, uint32_t flags) {// call the opengl code glClearColor (0.1f, 0.1f, 0.5f, 1.0f); // specify the RGBA glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) for the background color ); // clear the buffer with the value specified by glClearColor}
In this case, you can see a blue window,