展会信息港展会大全

DrawPrimitive真是一个好类!!cocos的开发组干了好事。
来源:互联网   发布日期:2015-09-27 14:58:09   浏览:1823次  

导读: 如果每次在商业项目中使用opengl命令去绘制特效,工作效率真心低,所以官方包装了这个接口,真实好东西。 draw函数的接口以及改了,新接口不允许重载原先的void draw(void)。 命...

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

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

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

这就要求引擎全局组织绘制命令,即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:

};()>

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

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

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

void HelloWorld::draw(Renderer *renderer, const Mat4 &transform, uint32_t flags)

{

m_customCommand.init(_globalZOrder);

m_customCommand.func = CC_CALLBACK_0(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

//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,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(0,0,255,128);

DrawPrimitives::drawPoint( VisibleRect::center() );//中心的圆点,蓝色正方向

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);//四个青色点

CHECK_GL_ERROR_DEBUG();

// 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(140,0), 40, CC_DEGREES_TO_RADIANS(90), 50, 1.0f, 1.0f);

CHECK_GL_ERROR_DEBUG();

// open yellow poly

DrawPrimitives::setDrawColor4B(255, 255, 0, 255);

glLineWidth(10);

Vec2 vertices[] = { Vec2(0,0), Vec2(50,50), Vec2(100,50), Vec2(100,100), Vec2(50,100) };

DrawPrimitives::drawPoly( vertices, 5, false);//左下角的方形

CHECK_GL_ERROR_DEBUG();

// filled poly

glLineWidth(1);//五角星

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();

// 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), Vec2(70,190), Vec2(100,190), Vec2(90,160)};

DrawPrimitives::drawSolidPoly( vertices3, 4, Color4F(1,1,0,1) );

// restore original values

glLineWidth(1);

DrawPrimitives::setDrawColor4B(255,255,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, 1.0f);

}

赞助本站

人工智能实验室
相关内容
AiLab云推荐
展开

热门栏目HotCates

Copyright © 2010-2024 AiLab Team. 人工智能实验室 版权所有    关于我们 | 联系我们 | 广告服务 | 公司动态 | 免责声明 | 隐私条款 | 工作机会 | 展会港