Call OpenGL in cocos2dx (3.X)

Source: Internet
Author: User

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,

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.