如果每次在商业项目中使用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(