Cocos2d-x   发布时间:2022-05-02  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了DrawPrimitive真是一个好类!!cocos的开发组干了好事。大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如果每次在商业项目中使用opengl命令去绘制特效,工作效率真心低,所以官方包装了这个接口,真实好东西。

draw函数的接口以及改了,新接口不允许重载原先的void draw(void)。

命令模式已是过去,显示列表模式则是将命令放入缓冲池中,在opengl状态机执行绘制命令时从中读取才去绘制,不再是每次绘制就调用opengl状态机立即绘制。



这就要求引擎全局组织绘制命令,即openglcocos2d::Customcatommand


class Customcatommand : public RenderCommand
{
public:
    Customcatommand();
    ~Customcatommand();
    
public:

    void init(float depth);

    void execute();

    inline bool isTranslucent() { return true; }
    std::function<void()> func;

protected:
};


render执行单个RenderCommand时就是调用execute函数,最后访问额func成员,从而执行下面的onDraw。

最后产生一堆绘制命令。等待真正opengl去执行,实际上每条绘制函数的命令都会去flush一次。


代码是演示效果:VisibleRect来自TESTCpp,或者说下面整段来自TESTCpp。

void HelloWorld::draw(Renderer *renderer,const Mat4 &transform,uint32_t flags)
{
    m_customcatommand.init(_globalZOrder);
    m_customcatommand.func = CC_CALLBACK_0(HelloWorld::onDraw,this,transform,flags);
    renderer->addCommand(&m_customcatommand);
}


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 (white,non-transparent)
    // Anti-Aliased
    //  glEnable(GL_LINE_SMOOTH);
    DrawPrimitives::drawLine( VisibleRect::leftBottom(),VisibleRect::rightTop() );
    
    checK_GL_ERROR_DEBUG();
    
    // line: color,width,aliased
    // glLineWidth > 1 and GL_LINE_SMOOTH are not compatible
    // GL_SMOOTH_LINE_WIDTH_RANGE = (1,1) on iPhone
    //  glDisable(GL_LINE_SMOOTH);
    glLineWidth( 5.0f );
    DrawPrimitives::setDrawColor4B(255,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(0,128);
    DrawPrimitives::drawPoint( VisibleRect::center() );//中心的圆点,蓝色正方向
    
    checK_GL_ERROR_DEBUG();
    
    // draw 4 small points
    Vec2 points[] = { Vec2(60,60),Vec2(70,70),Vec2(60,60) };
    DrawPrimitives::setPointSize(4);
    DrawPrimitives::setDrawColor4B(0,255);
    DrawPrimitives::drawPoints( points,4);//四个青色点
    
    checK_GL_ERROR_DEBUG();
    
    // draw a green circle with 10 segments
    glLineWidth(16);
    DrawPrimitives::setDrawColor4B(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);
    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,255);
    DrawPrimitives::drawSolidCircle( VisibleRect::center() + Vec2(140,0),40,1.0f,1.0f);
    
    checK_GL_ERROR_DEBUG();
    
    // open yellow poly
    DrawPrimitives::setDrawColor4B(255,255);
    glLineWidth(10);
    Vec2 vertices[] = { Vec2(0,Vec2(50,50),Vec2(100,100),100) };
    DrawPrimitives::drawPoly( vertices,5,falsE);//左下角的方形
    
    checK_GL_ERROR_DEBUG();
    
    // filled poly
    glLineWidth(1);//五角星
    Vec2 filledVertices[] = { Vec2(0,120),170),Vec2(25,200),Vec2(0,170) };
    DrawPrimitives::drawSolidPoly(filledVertices,Color4F(0.5f,0.5f,1,1 ) );
    
    
    // closed purble poly
    DrawPrimitives::setDrawColor4B(255,255);
    glLineWidth(2);
    Vec2 vertices2[] = { Vec2(30,130),Vec2(30,230),200) };
    DrawPrimitives::drawPoly( vertices2,3,truE);
    
    checK_GL_ERROR_DEBUG();
    
    // draw quad bezier path 中间这条线
    DrawPrimitives::drawQuadBezier(VisibleRect::leftTop(),VisibleRect::center(),VisibleRect::rightTop(),50);
    
    checK_GL_ERROR_DEBUG();
    
    // draw cubic bezier path 上面的线
    DrawPrimitives::drawCubicBezier(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),190),Vec2(90,160)};
    DrawPrimitives::drawSolidPoly( vertices3,4,Color4F(1,1) );
    
    // restore original values
    glLineWidth(1);
    DrawPrimitives::setDrawColor4B(255,255);
    DrawPrimitives::setPointSize(1);
    
    checK_GL_ERROR_DEBUG();
    
    //end draw
    director->popMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW);
}



西。

const Vec2& center,

float radius,

float angle,---------这个参数有什么用?没看懂,测试了也感觉不出区别。。终于找到了,相对于x轴正方向旋转方向。比如:从90*pi/180度即y轴正方向与圆交点的位置

unsigned int segments,:圆边被切成多少段线段

bool drawLineToCenter:圆心到起始绘制点是否绘制线段


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

大佬总结

以上是大佬教程为你收集整理的DrawPrimitive真是一个好类!!cocos的开发组干了好事。全部内容,希望文章能够帮你解决DrawPrimitive真是一个好类!!cocos的开发组干了好事。所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。