DrawPrimitive is really a good class !! Cocos developers have done a good job.

Source: Internet
Author: User

DrawPrimitive is really a good class !! Cocos developers have done a good job.

If opengl commands are used to draw special effects in commercial projects each time, the work efficiency is really low, so the official packaging of this interface is really good.

The interface of the draw function is modified. The new interface cannot reload the original void draw (void ).

The command mode is past, and the display list mode is to put the command into the buffer pool. It is drawn only when the opengl state machine executes the drawing command. It is no longer to call the opengl state machine to draw immediately every draw.

 

 

This requires the engine to globally organize the drawing command, that is, openglcocos2d: CustomCommand

 

 

class CustomCommand : public RenderCommand{public:    CustomCommand();    ~CustomCommand();    public:    void init(float depth);    void execute();    inline bool isTranslucent() { return true; }    std::function
 
   func;protected:};
 


 

When executing a single RenderCommand, render calls the execute function and finally accesses the func member to execute the following onDraw.

Finally, a bunch of drawing commands are generated. Wait for the real opengl to execute. In fact, every command to draw a function will be flush once.

 

The Code demonstrates the effect: VisibleRect is from testCpp, or the entire section below is from testCpp.

 

Void HelloWorld: draw (Renderer * renderer, const Mat4 & transform, uint32_t flags) {m_customCommand.init (_ globalZOrder); region = transform (HelloWorld: onDraw, this, transform, flags); renderer-> addCommand (& m_customCommand);} void HelloWorld: onDraw (const Mat4 & transform, uint32_t flags) {Director * director = Director: getInstance (); director-> pushMatrix (MATRIX_STACK_TYPE: MATRIX_STACK_MODELVIEW); director-> loadMatrix (MATRIX_STACK_TYPE: MATRIX_STACK_MODELVIEW, transform); // draw CHECK_GL_ERROR_DEBUG (); // draw a simple line // The default state is: // Line Width: 1 // color: 255,255,255,255 (white, non-transparent) // Anti-Aliased // glable (GL_LINE_SMOOTH); DrawPrimitives: drawLine (VisibleRect: leftBottom (), VisibleRect: rightTop (); limit (); // line: color, width, aliased // glLineWidth> 1 and GL_LINE_SMOOTH are not compatible // ratio = () on iPhone // glDisable (GL_LINE_SMOOTH); glLineWidth (5.0f); DrawPrimitives :: setDrawColor4B (255, 0, 0,255); DrawPrimitives: drawLine (VisibleRect: leftTop (), VisibleRect: rightBottom (); CHECK_GL_ERROR_DEBUG (); // TIP: // If you are going to use always thde same color or width, you don't // need to call it before every draw // Remember: openGL is a state-machine. // draw big point in the center DrawPrimitives: setPointSize (64); DrawPrimitives: setDrawColor4B (255,128,); DrawPrimitives: drawPoint (VisibleRect: center ()); // The Center dot, in the blue direction toward CHECK_GL_ERROR_DEBUG (); // draw 4 small points Vec2 points [] = {Vec2 (60, 60), Vec2 (70, 70), Vec2 (60, 70 ), vec2 (70, 60)}; DrawPrimitives: setPointSize (4); DrawPrimitives: setDrawColor4B (0,255,255,255); DrawPrimitives: drawPoints (points, 4 ); // four blue dots rotate (); // draw a green circle with 10 segments glLineWidth (16); DrawPrimitives: setDrawColor4B (0,255, 0,255); DrawPrimitives: drawCircle (VisibleRect:: center (), 100, 2, 10, false); CHECK_GL_ERROR_DEBUG (); // draw a green circle with 50 segments with line to center glLineWidth (2); DrawPrimitives :: setDrawColor4B (0,255,255,255); DrawPrimitives: drawCircle (VisibleRect: center (), 200, CC_DEGREES_TO_RADIANS (90), 50, true); CHECK_GL_ERROR_DEBUG (); // draw a pink solid circle with 50 segments glLineWidth (2); DrawPrimitives: setDrawColor4B (255, 0,255,255); DrawPrimitives: drawSolidCircle (VisibleRect: center () + Vec2 (255,255), 40, rotate (90), 50, 1.0f, 1.0f); CHECK_GL_ERROR_DEBUG (); // open yellow poly DrawPrimitives: setDrawColor4B (0,255 ); glLineWidth (10); Vec2 vertices [] = {Vec2 (100,100), Vec2 (50, 50), Vec2 (50,100), Vec2 (), Vec2 ()}; DrawPrimitives :: drawPoly (vertices, 5, false); // The square CHECK_GL_ERROR_DEBUG () in the lower left corner; // filled poly glLineWidth (1 ); // star Vec2 filledVertices [] = {Vec2 (0,120), Vec2 (50,120), Vec2 (50,170), Vec2 (25,200), Vec2 (0,170)}; DrawPrimitives :: drawSolidPoly (filledVertices, 5, Color4F (0.5f, 0.5f, 1, 1); // closed purble poly DrawPrimitives: setDrawColor4B (255, 0,255,255); glLineWidth (2 ); vec2 vertices2 [] = {Vec2 (30,130), Vec2 (30,230), Vec2 (50,200)}; DrawPrimitives: drawPoly (vertices2, 3, true); CHECK_GL_ERROR_DEBUG (); // DrawPrimitives: drawquadbezr (VisibleRect: leftTop (), VisibleRect: center (), VisibleRect: rightTop (), 50 ); CHECK_GL_ERROR_DEBUG (); // The DrawPrimitives: drawcubicbezr (VisibleRect: center (), Vec2 (VisibleRect: center (). x + 30, VisibleRect: center (). y + 50), Vec2 (VisibleRect: center (). x + 60, VisibleRect: center (). y-50), VisibleRect: right (), 100); CHECK_GL_ERROR_DEBUG (); // draw a solid polygon Vec2 vertices3 [] = {Vec2 (60,160), Vec2 (70,190 ), vec2 (100,190), Vec2 (90,160)}; DrawPrimitives: drawSolidPoly (vertices3, 4, Color4F (,); // restore original values glLineWidth (1); DrawPrimitives:: setDrawColor4B (255,255,255,255); DrawPrimitives: setPointSize (1); CHECK_GL_ERROR_DEBUG (); // end draw ctor-> popMatrix (MATRIX_STACK_TYPE: MATRIX_STACK_MODELVIEW );}


 


West.

Const Vec2 & center,

Float radius,

Float angle, --------- what is the use of this parameter? I don't understand it. I don't feel any difference after testing .. Finally, it is found that it is in the direction of rotation relative to the positive side of the X axis. For example, from 90 * pi/180 degrees, that is, the point of intersection between the positive direction of the Y axis and the circle

Unsigned int segments: Number of segment segments to be cut by the circular edge

Bool drawLineToCenter: Specifies whether to draw a line segment from the center to the start point.

 

 

void drawCircle( const Vec2& center, float radius, float angle, unsigned int segments, bool drawLineToCenter){    drawCircle(center, radius, angle, segments, drawLineToCenter, 1.0f, 1.0f);}
 

 

 

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.