最近看了下CCDrawNode的实现,这里笔记下实现方式。
CONTENTS
参考
CCDrawNode需要很多基础知识,之前记录过,这里贴下网址:
VBO的讲解: 【转】OpenGL ES使用VBO:顶点缓存
opengl混合: 【转】OpenGL ES 混合的理解
cocos2dx封装的一些opengl函数:cocos2dx学习笔记:CCGLProgram
cocos2dx自带的shaders:cocos2dx学习笔记:shaders
代码笔记
记的比较乱。
NS_CC_BEGIN // ccVertex2F == CGPoint in 32-bits, but not in 64-bits (OS X) // that's why the "v2f" functions are needed static ccVertex2F v2fzero = {0.0f,0.0f}; 。。。。。 //工具函数 。。。。。 // implementation of CCDrawNode CCDrawNode::CCDrawNode() : m_uVao(0) , m_uVbo(0) , m_uBufferCapacity(0) , m_nBufferCount(0) , m_pBuffer(NULL) , m_bDirty(false) { m_sBlendFunc.src = CC_BLEND_SRC; m_sBlendFunc.dst = CC_BLEND_DST; } CCDrawNode::~CCDrawNode() { free(m_pBuffer); m_pBuffer = NULL; //==>> 删除顶点缓存buffer(vbo) glDeleteBuffers(1, &m_uVbo); m_uVbo = 0; #if CC_TEXTURE_ATLAS_USE_VAO glDeleteVertexArrays(1, &m_uVao); ccGLBindVAO(0); m_uVao = 0; #endif #if CC_ENABLE_CACHE_TEXTURE_DATA CCNotificationCenter::sharedNotificationCenter()->removeObserver(this, EVENT_COME_TO_FOREGROUND); #endif } CCDrawNode* CCDrawNode::create() { CCDrawNode* pRet = new CCDrawNode(); if (pRet && pRet->init()) { pRet->autorelease(); } else { CC_SAFE_DELETE(pRet); } return pRet; } //==>> 扩容顶点缓存节点 void CCDrawNode::ensureCapacity(unsigned int count) { if(m_nBufferCount + count > m_uBufferCapacity) { m_uBufferCapacity += MAX(m_uBufferCapacity, count); m_pBuffer = (ccV2F_C4B_T2F*)realloc(m_pBuffer, m_uBufferCapacity*sizeof(ccV2F_C4B_T2F)); } } bool CCDrawNode::init() { /* ccMacros.h #define CC_BLEND_SRC GL_ONE #define CC_BLEND_DST GL_ONE_MINUS_SRC_ALPHA */ //通过宏的定义我们知道默认的混合方式是 要绘制上去颜色的100%绘制 //其余的地方100%绘制底色。 m_sBlendFunc.src = CC_BLEND_SRC; m_sBlendFunc.dst = CC_BLEND_DST; //==>>使用PositionLengthTexureColor这个shader setShaderProgram(CCShaderCache::sharedShaderCache()->programForKey(kCCShader_PositionLengthTexureColor)); //==>>先扩容到512个顶点缓存节点 ensureCapacity(512); #if CC_TEXTURE_ATLAS_USE_VAO glGenVertexArrays(1, &m_uVao); ccGLBindVAO(m_uVao); #endif //==>>申请一块vbo,并且将m_pBuffer刷入vbo中 glGenBuffers(1, &m_uVbo); glBindBuffer(GL_ARRAY_BUFFER, m_uVbo); glBufferData(GL_ARRAY_BUFFER, sizeof(ccV2F_C4B_T2F)* m_uBufferCapacity, m_pBuffer, GL_STREAM_DRAW); /* ccTypes.h: typedef struct _ccV2F_C4B_T2F { //! vertices (2F) ccVertex2F vertices; //! colors (4B) ccColor4B colors; //! tex coords (2F) ccTex2F texCoords; } ccV2F_C4B_T2F; */ //==>>允许设置位置坐标 glEnableVertexAttribArray(kCCVertexAttrib_Position); //==>>由于是vbo状态,那么glVertexAttribPointer的最后一个参数表示该种类型顶点数据(vertices)在顶点缓存(ccV2F_C4B_T2F)中的起始偏移量。 glVertexAttribPointer(kCCVertexAttrib_Position, 2, GL_FLOAT, GL_FALSE, sizeof(ccV2F_C4B_T2F), (GLvoid *)offsetof(ccV2F_C4B_T2F, vertices)); glEnableVertexAttribArray(kCCVertexAttrib_Color); glVertexAttribPointer(kCCVertexAttrib_Color, 4, GL_UNSIGNED_BYTE, GL_TRUE, sizeof(ccV2F_C4B_T2F), (GLvoid *)offsetof(ccV2F_C4B_T2F, colors)); glEnableVertexAttribArray(kCCVertexAttrib_TexCoords); glVertexAttribPointer(kCCVertexAttrib_TexCoords, 2, GL_FLOAT, GL_FALSE, sizeof(ccV2F_C4B_T2F), (GLvoid *)offsetof(ccV2F_C4B_T2F, texCoords)); //==>>设置好数据之后,将vbo解绑,防止对opengl上下文产生影响。 glBindBuffer(GL_ARRAY_BUFFER, 0); #if CC_TEXTURE_ATLAS_USE_VAO ccGLBindVAO(0); #endif CHECK_GL_ERROR_DEBUG(); //==>> 设置数据需要刷新 m_bDirty = true; #if CC_ENABLE_CACHE_TEXTURE_DATA // Need to listen the event only when not use batchnode, because it will use VBO CCNotificationCenter::sharedNotificationCenter()->addObserver(this, callfuncO_selector(CCDrawNode::listenBackToForeground), EVENT_COME_TO_FOREGROUND, NULL); #endif return true; } //==>>实际渲染实现类 void CCDrawNode::render() { //==>>如果数据需要刷新,那么首先重新绑定vbo,然后将m_pBuffer的数据刷入vbo中,这样做的原因主要是 //==>>避免每次都要刷新vbo,提高效率。 if (m_bDirty) { glBindBuffer(GL_ARRAY_BUFFER, m_uVbo); glBufferData(GL_ARRAY_BUFFER, sizeof(ccV2F_C4B_T2F)*m_uBufferCapacity, m_pBuffer, GL_STREAM_DRAW); m_bDirty = false; } #if CC_TEXTURE_ATLAS_USE_VAO ccGLBindVAO(m_uVao); #else //==>> 首先允许位置,颜色,纹理刷新 /* ccGLStateCache.h enum { kCCVertexAttribFlag_None = 0, kCCVertexAttribFlag_Position = 1 << 0, kCCVertexAttribFlag_Color = 1 << 1, kCCVertexAttribFlag_TexCoords = 1 << 2, kCCVertexAttribFlag_PosColorTex = ( kCCVertexAttribFlag_Position | kCCVertexAttribFlag_Color | kCCVertexAttribFlag_TexCoords ), }; */ //==>> 重新绑定vbo,并且使用vbo中的数据 ccGLEnableVertexAttribs(kCCVertexAttribFlag_PosColorTex); glBindBuffer(GL_ARRAY_BUFFER, m_uVbo); // vertex glVertexAttribPointer(kCCVertexAttrib_Position, 2, GL_FLOAT, GL_FALSE, sizeof(ccV2F_C4B_T2F), (GLvoid *)offsetof(ccV2F_C4B_T2F, vertices)); // color glVertexAttribPointer(kCCVertexAttrib_Color, 4, GL_UNSIGNED_BYTE, GL_TRUE, sizeof(ccV2F_C4B_T2F), (GLvoid *)offsetof(ccV2F_C4B_T2F, colors)); // texcood glVertexAttribPointer(kCCVertexAttrib_TexCoords, 2, GL_FLOAT, GL_FALSE, sizeof(ccV2F_C4B_T2F), (GLvoid *)offsetof(ccV2F_C4B_T2F, texCoords)); #endif //==>> 绘图 glDrawArrays(GL_TRIANGLES, 0, m_nBufferCount); //==>> 解绑vbo,防止对opengl上下文产生影响。 glBindBuffer(GL_ARRAY_BUFFER, 0); CC_INCREMENT_GL_DRAWS(1); CHECK_GL_ERROR_DEBUG(); } //==>> draw函数每帧都会被调用,因此需要在render里面绘制需要的东西,而且搞定后不能对opengl上下文造成影响。 void CCDrawNode::draw() { CC_NODE_DRAW_SETUP(); //==>> 设置混合模式 ccGLBlendFunc(m_sBlendFunc.src, m_sBlendFunc.dst); render(); } //==>> 画点的实际方式是将点的信息放入m_pBuffer中,然后设置m_bDirty,这样就可以在render中画出来了。 void CCDrawNode::drawDot(const CCPoint &pos, float radius, const ccColor4F &color) { unsigned int vertex_count = 2*3; ensureCapacity(vertex_count); ccV2F_C4B_T2F a = {{pos.x - radius, pos.y - radius}, ccc4BFromccc4F(color), {-1.0, -1.0} }; ccV2F_C4B_T2F b = {{pos.x - radius, pos.y + radius}, ccc4BFromccc4F(color), {-1.0, 1.0} }; ccV2F_C4B_T2F c = {{pos.x + radius, pos.y + radius}, ccc4BFromccc4F(color), { 1.0, 1.0} }; ccV2F_C4B_T2F d = {{pos.x + radius, pos.y - radius}, ccc4BFromccc4F(color), { 1.0, -1.0} }; //==>> 计算出需要更新的m_pBuffer ccV2F_C4B_T2F_Triangle *triangles = (ccV2F_C4B_T2F_Triangle *)(m_pBuffer + m_nBufferCount); ccV2F_C4B_T2F_Triangle triangle0 = {a, b, c}; ccV2F_C4B_T2F_Triangle triangle1 = {a, c, d}; triangles[0] = triangle0; triangles[1] = triangle1; m_nBufferCount += vertex_count; m_bDirty = true; } //==>> 和drawDot类似,计算数据>更新m_pBuffer中特定内存>设置m_bDirty void CCDrawNode::drawSegment(const CCPoint &from, const CCPoint &to, float radius, const ccColor4F &color) { 。。。。。 。。。。。 } //==>> 和drawDot类似,计算数据>更新m_pBuffer中特定内存>设置m_bDirty void CCDrawNode::drawPolygon(CCPoint *verts, unsigned int count, const ccColor4F &fillColor, float borderWidth, const ccColor4F &borderColor) { 。。。。。 。。。。。 } //==>> 设置m_pBuffer中有效数据的个数为0,然后设置m_bDirty,就会将vbo中的数据清空。 void CCDrawNode::clear() { m_nBufferCount = 0; m_bDirty = true; } ccBlendFunc CCDrawNode::getBlendFunc() const { return m_sBlendFunc; } void CCDrawNode::setBlendFunc(const ccBlendFunc &blendFunc) { m_sBlendFunc = blendFunc; } /** listen the event that coming to foreground on Android */ void CCDrawNode::listenBackToForeground(CCObject *obj) { init(); } NS_CC_END
发表评论