From f42254125fad69183c1c08d111004a7ba9193bb8 Mon Sep 17 00:00:00 2001 From: James Gregory Date: Mon, 8 Apr 2013 17:51:30 -0700 Subject: [PATCH] Get CCGrid to work. --- cocos2dx/base_nodes/CCNode.cpp | 21 ---------- cocos2dx/base_nodes/CCNode.h | 1 + cocos2dx/cocoa/CCObject.cpp | 42 +++++++++++++++++++ cocos2dx/cocoa/CCObject.h | 24 +++++++++++ cocos2dx/effects/CCGrid.cpp | 15 +++++++ .../CCLayer.cpp | 8 ++++ 6 files changed, 90 insertions(+), 21 deletions(-) diff --git a/cocos2dx/base_nodes/CCNode.cpp b/cocos2dx/base_nodes/CCNode.cpp index e2f0466ee2..13e1a745c6 100644 --- a/cocos2dx/base_nodes/CCNode.cpp +++ b/cocos2dx/base_nodes/CCNode.cpp @@ -88,10 +88,6 @@ CCNode::CCNode(void) , m_bReorderChildDirty(false) , m_nScriptHandler(0) , m_nUpdateScriptHandler(0) -#ifdef EMSCRIPTEN -, m_bufferObject(0) -, m_bufferSize(0) -#endif // EMSCRIPTEN { // set default scheduler and actionManager CCDirector *director = CCDirector::sharedDirector(); @@ -1196,23 +1192,6 @@ void CCNode::setAdditionalTransform(const CCAffineTransform& additionalTransform m_bAdditionalTransformDirty = true; } -#ifdef EMSCRIPTEN -void CCNode::setGLBufferData(void *buf, GLuint bufSize) -{ - // WebGL doesn't support client-side arrays, so generate a buffer and load the data first. - if(m_bufferSize < bufSize) - { - if(m_bufferObject) - { - glDeleteBuffers(1, &m_bufferObject); - } - glGenBuffers(1, &m_bufferObject); - m_bufferSize = bufSize; - } - glBindBuffer(GL_ARRAY_BUFFER, m_bufferObject); - glBufferData(GL_ARRAY_BUFFER, bufSize, buf, GL_STATIC_DRAW); -} -#endif // EMSCRIPTEN CCAffineTransform CCNode::parentToNodeTransform(void) { diff --git a/cocos2dx/base_nodes/CCNode.h b/cocos2dx/base_nodes/CCNode.h index 79b6a3657e..1fb68bc444 100644 --- a/cocos2dx/base_nodes/CCNode.h +++ b/cocos2dx/base_nodes/CCNode.h @@ -1376,6 +1376,7 @@ protected: int m_nScriptHandler; ///< script handler for onEnter() & onExit(), used in Javascript binding and Lua binding. int m_nUpdateScriptHandler; ///< script handler for update() callback per frame, which is invoked from lua & javascript. ccScriptType m_eScriptType; ///< type of script binding, lua or javascript + }; //#pragma mark - CCNodeRGBA diff --git a/cocos2dx/cocoa/CCObject.cpp b/cocos2dx/cocoa/CCObject.cpp index 40546ded8a..58dc0d5f86 100644 --- a/cocos2dx/cocoa/CCObject.cpp +++ b/cocos2dx/cocoa/CCObject.cpp @@ -45,6 +45,16 @@ CCObject::CCObject(void) static unsigned int uObjectCount = 0; m_uID = ++uObjectCount; + +#ifdef EMSCRIPTEN + for(int i = 0; i < BUFFER_SLOTS; i++) + { + m_bufferObject[i] = 0; + m_bufferSize[i] = 0; + m_indexBufferObject[i] = 0; + m_indexBufferSize[i] = 0; + } +#endif //EMSCRIPTEN } CCObject::~CCObject(void) @@ -71,6 +81,38 @@ CCObject::~CCObject(void) } } +#ifdef EMSCRIPTEN +void CCObject::setGLBufferData(void *buf, GLuint bufSize, int slot) +{ + // WebGL doesn't support client-side arrays, so generate a buffer and load the data first. + if(m_bufferSize[slot] < bufSize) + { + if(m_bufferObject[slot]) + { + glDeleteBuffers(1, &(m_bufferObject[slot])); + } + glGenBuffers(1, &(m_bufferObject[slot])); + m_bufferSize[slot] = bufSize; + } + glBindBuffer(GL_ARRAY_BUFFER, m_bufferObject[slot]); + glBufferData(GL_ARRAY_BUFFER, bufSize, buf, GL_STATIC_DRAW); +} +void CCObject::setGLIndexData(void *buf, GLuint bufSize, int slot) +{ + // WebGL doesn't support client-side arrays, so generate a buffer and load the data first. + if(m_indexBufferSize[slot] < bufSize) + { + if(m_indexBufferObject[slot]) + { + glDeleteBuffers(1, &(m_indexBufferObject[slot])); + } + glGenBuffers(1, &(m_indexBufferObject[slot])); + m_indexBufferSize[slot] = bufSize; + } + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_indexBufferObject[slot]); + glBufferData(GL_ELEMENT_ARRAY_BUFFER, bufSize, buf, GL_STATIC_DRAW); +} +#endif // EMSCRIPTEN CCObject* CCObject::copy() { return copyWithZone(0); diff --git a/cocos2dx/cocoa/CCObject.h b/cocos2dx/cocoa/CCObject.h index 010e55df62..d9dd1792a0 100644 --- a/cocos2dx/cocoa/CCObject.h +++ b/cocos2dx/cocoa/CCObject.h @@ -27,6 +27,10 @@ THE SOFTWARE. #include "platform/CCPlatformMacros.h" +#ifdef EMSCRIPTEN +#include +#endif // EMSCRIPTEN + NS_CC_BEGIN /** @@ -72,6 +76,26 @@ public: virtual void update(float dt) {CC_UNUSED_PARAM(dt);}; friend class CCAutoreleasePool; + +#ifdef EMSCRIPTEN +#define BUFFER_SLOTS 4 + // Moving GL Buffer management code up to CCObject as it is used both via + // the CCNode and CCGrid inheritance trees. + + /** + * Load the given data into this CCNode's GL Buffer. Needed for WebGL, as it does not support client-side arrays. + */ + void setGLBufferData(void *buf, GLuint bufSize, int slot); + void setGLIndexData(void *buf, GLuint bufSize, int slot); + + // We allocate 4 buffer objs per node, and index into them as slots. + GLuint m_bufferObject[BUFFER_SLOTS]; + GLuint m_bufferSize[BUFFER_SLOTS]; + + GLuint m_indexBufferObject[BUFFER_SLOTS]; + GLuint m_indexBufferSize[BUFFER_SLOTS]; +#endif // EMSCRIPTEN + }; diff --git a/cocos2dx/effects/CCGrid.cpp b/cocos2dx/effects/CCGrid.cpp index a3e18980ce..a97154f5e5 100644 --- a/cocos2dx/effects/CCGrid.cpp +++ b/cocos2dx/effects/CCGrid.cpp @@ -323,7 +323,21 @@ void CCGrid3D::blit(void) // // Attributes // +#ifdef EMSCRIPTEN + // Size calculations from calculateVertexPoints(). + unsigned int numOfPoints = (m_sGridSize.width+1) * (m_sGridSize.height+1); + // position + setGLBufferData(m_pVertices, numOfPoints * sizeof(ccVertex3F), 0); + glVertexAttribPointer(kCCVertexAttrib_Position, 3, GL_FLOAT, GL_FALSE, 0, 0); + + // texCoords + setGLBufferData(m_pTexCoordinates, numOfPoints * sizeof(ccVertex2F), 1); + glVertexAttribPointer(kCCVertexAttrib_TexCoords, 2, GL_FLOAT, GL_FALSE, 0, 0); + + setGLIndexData(m_pIndices, n * 12, 0); + glDrawElements(GL_TRIANGLES, (GLsizei) n*6, GL_UNSIGNED_SHORT, 0); +#else // position glVertexAttribPointer(kCCVertexAttrib_Position, 3, GL_FLOAT, GL_FALSE, 0, m_pVertices); @@ -331,6 +345,7 @@ void CCGrid3D::blit(void) glVertexAttribPointer(kCCVertexAttrib_TexCoords, 2, GL_FLOAT, GL_FALSE, 0, m_pTexCoordinates); glDrawElements(GL_TRIANGLES, (GLsizei) n*6, GL_UNSIGNED_SHORT, m_pIndices); +#endif // EMSCRIPTEN CC_INCREMENT_GL_DRAWS(1); } diff --git a/cocos2dx/layers_scenes_transitions_nodes/CCLayer.cpp b/cocos2dx/layers_scenes_transitions_nodes/CCLayer.cpp index 692623bd23..e203941117 100644 --- a/cocos2dx/layers_scenes_transitions_nodes/CCLayer.cpp +++ b/cocos2dx/layers_scenes_transitions_nodes/CCLayer.cpp @@ -778,8 +778,16 @@ void CCLayerColor::draw() // // Attributes // +#ifdef EMSCRIPTEN + setGLBufferData(m_pSquareVertices, 4 * sizeof(ccVertex2F), 0); + glVertexAttribPointer(kCCVertexAttrib_Position, 2, GL_FLOAT, GL_FALSE, 0, 0); + + setGLBufferData(m_pSquareColors, 4 * sizeof(ccColor4F), 1); + glVertexAttribPointer(kCCVertexAttrib_Color, 4, GL_FLOAT, GL_FALSE, 0, 0); +#else glVertexAttribPointer(kCCVertexAttrib_Position, 2, GL_FLOAT, GL_FALSE, 0, m_pSquareVertices); glVertexAttribPointer(kCCVertexAttrib_Color, 4, GL_FLOAT, GL_FALSE, 0, m_pSquareColors); +#endif // EMSCRIPTEN ccGLBlendFunc( m_tBlendFunc.src, m_tBlendFunc.dst );