最近看了下CCDrawNode的实现,这里笔记下实现方式。
参考
CCDrawNode需要很多基础知识,之前记录过,这里贴下网址:
VBO的讲解: 【转】OpenGL ES使用VBO:顶点缓存
opengl混合: 【转】OpenGL ES 混合的理解
cocos2dx封装的一些opengl函数:cocos2dx学习笔记:CCGLProgram
cocos2dx自带的shaders:cocos2dx学习笔记:shaders
代码笔记
记的比较乱。
static ccVertex2F v2fzero = {0.0f,0.0f}; |
m_sBlendFunc.src = CC_BLEND_SRC; |
m_sBlendFunc.dst = CC_BLEND_DST; |
CCDrawNode::~CCDrawNode() |
glDeleteBuffers(1, &m_uVbo); |
#if CC_TEXTURE_ATLAS_USE_VAO |
glDeleteVertexArrays(1, &m_uVao); |
#if CC_ENABLE_CACHE_TEXTURE_DATA |
CCNotificationCenter::sharedNotificationCenter()->removeObserver( this , EVENT_COME_TO_FOREGROUND); |
CCDrawNode* CCDrawNode::create() |
CCDrawNode* pRet = new CCDrawNode(); |
if (pRet && pRet->init()) |
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)); |
m_sBlendFunc.src = CC_BLEND_SRC; |
m_sBlendFunc.dst = CC_BLEND_DST; |
setShaderProgram(CCShaderCache::sharedShaderCache()->programForKey(kCCShader_PositionLengthTexureColor)); |
#if CC_TEXTURE_ATLAS_USE_VAO |
glGenVertexArrays(1, &m_uVao); |
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); |
glEnableVertexAttribArray(kCCVertexAttrib_Position); |
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)); |
glBindBuffer(GL_ARRAY_BUFFER, 0); |
#if CC_TEXTURE_ATLAS_USE_VAO |
#if CC_ENABLE_CACHE_TEXTURE_DATA |
CCNotificationCenter::sharedNotificationCenter()->addObserver( this , |
callfuncO_selector(CCDrawNode::listenBackToForeground), |
EVENT_COME_TO_FOREGROUND, |
void CCDrawNode::render() |
glBindBuffer(GL_ARRAY_BUFFER, m_uVbo); |
glBufferData(GL_ARRAY_BUFFER, sizeof (ccV2F_C4B_T2F)*m_uBufferCapacity, m_pBuffer, GL_STREAM_DRAW); |
#if CC_TEXTURE_ATLAS_USE_VAO |
ccGLEnableVertexAttribs(kCCVertexAttribFlag_PosColorTex); |
glBindBuffer(GL_ARRAY_BUFFER, m_uVbo); |
glVertexAttribPointer(kCCVertexAttrib_Position, 2, GL_FLOAT, GL_FALSE, sizeof (ccV2F_C4B_T2F), (GLvoid *)offsetof(ccV2F_C4B_T2F, vertices)); |
glVertexAttribPointer(kCCVertexAttrib_Color, 4, GL_UNSIGNED_BYTE, GL_TRUE, sizeof (ccV2F_C4B_T2F), (GLvoid *)offsetof(ccV2F_C4B_T2F, colors)); |
glVertexAttribPointer(kCCVertexAttrib_TexCoords, 2, GL_FLOAT, GL_FALSE, sizeof (ccV2F_C4B_T2F), (GLvoid *)offsetof(ccV2F_C4B_T2F, texCoords)); |
glDrawArrays(GL_TRIANGLES, 0, m_nBufferCount); |
glBindBuffer(GL_ARRAY_BUFFER, 0); |
CC_INCREMENT_GL_DRAWS(1); |
ccGLBlendFunc(m_sBlendFunc.src, m_sBlendFunc.dst); |
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} }; |
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; |
void CCDrawNode::drawSegment( const CCPoint &from, const CCPoint &to, float radius, const ccColor4F &color) |
void CCDrawNode::drawPolygon(CCPoint *verts, unsigned int count, const ccColor4F &fillColor, float borderWidth, const ccColor4F &borderColor) |
ccBlendFunc CCDrawNode::getBlendFunc() const |
void CCDrawNode::setBlendFunc( const ccBlendFunc &blendFunc) |
m_sBlendFunc = blendFunc; |
void CCDrawNode::listenBackToForeground(CCObject *obj) |
发表评论