Cocos2dx 绘制流程总结
1. 创建CCDirector.
代码:CCDirector *pDirector = CCDirector::sharedDirector();
2. 设置openglView.
代码:
pDirector->setOpenGLView(CCEGLView::sharedOpenGLView());
重要内容:setOpenglGLView->setGLDefaultValues,其中设置了视口大小,颜色混合,深度检测,清屏颜色,摄像机透视类型等。
3.主循环mainloop->drawScene
visit方法访问了所有子节点的draw方法。
4.单个节点绘制
以CCSprite为例:
void CCSprite::draw(void)
{
CC_PROFILER_START_CATEGORY(kCCProfilerCategorySprite,"CCSprite - draw");
CCAssert(!m_pobBatchNode,"If CCSprite is being rendered byCCSpriteBatchNode, CCSprite#draw SHOULD NOT be called");
CC_NODE_DRAW_SETUP();
ccGLBlendFunc( m_sBlendFunc.src, m_sBlendFunc.dst );
if (m_pobTexture !=NULL)
{
ccGLBindTexture2D( m_pobTexture->getName() );
}
else
{
ccGLBindTexture2D(0);
}
//
// Attributes
//
ccGLEnableVertexAttribs( kCCVertexAttribFlag_PosColorTex );
#definekQuadSize sizeof(m_sQuad.bl)
long offset= (long)&m_sQuad;
// vertex
int diff = offsetof( ccV3F_C4B_T2F, vertices);
glVertexAttribPointer(kCCVertexAttrib_Position,3, GL_FLOAT,GL_FALSE, kQuadSize, (void*)(offset + diff));
// texCoods
diff = offsetof( ccV3F_C4B_T2F, texCoords);
glVertexAttribPointer(kCCVertexAttrib_TexCoords,2, GL_FLOAT,GL_FALSE, kQuadSize, (void*)(offset+ diff));
// color
diff = offsetof( ccV3F_C4B_T2F, colors);
glVertexAttribPointer(kCCVertexAttrib_Color,4, GL_UNSIGNED_BYTE,GL_TRUE, kQuadSize, (void*)(offset+ diff));
glDrawArrays(GL_TRIANGLE_STRIP,0, 4);
CHECK_GL_ERROR_DEBUG();
#ifCC_SPRITE_DEBUG_DRAW == 1
// draw bounding box
CCPoint vertices[4]={
ccp(m_sQuad.tl.vertices.x,m_sQuad.tl.vertices.y),
ccp(m_sQuad.bl.vertices.x,m_sQuad.bl.vertices.y),
ccp(m_sQuad.br.vertices.x,m_sQuad.br.vertices.y),
ccp(m_sQuad.tr.vertices.x,m_sQuad.tr.vertices.y),
};
ccDrawPoly(vertices, 4, true);
#elifCC_SPRITE_DEBUG_DRAW == 2
// draw texture box
CCSize s = this->getTextureRect().size;
CCPoint offsetPix = this->getOffsetPosition();
CCPoint vertices[4] = {
ccp(offsetPix.x,offsetPix.y),ccp(offsetPix.x+s.width,offsetPix.y),
ccp(offsetPix.x+s.width,offsetPix.y+s.height),ccp(offsetPix.x,offsetPix.y+s.height)
};
ccDrawPoly(vertices, 4, true);
#endif// CC_SPRITE_DEBUG_DRAW
CC_INCREMENT_GL_DRAWS(1);
CC_PROFILER_STOP_CATEGORY(kCCProfilerCategorySprite,"CCSprite - draw");
}
本文总结了Cocos2d-x的绘制流程,包括创建CCDirector,设置OpenGLView,主循环中的drawScene以及单个节点如CCSprite的绘制方法。详细解释了visit方法、draw方法中的GL指令及顶点、纹理坐标和颜色的设置,以及如何进行GL状态管理和错误检查。

3146

被折叠的 条评论
为什么被折叠?



