Get CCGrid to work.

This commit is contained in:
James Gregory 2013-04-08 17:51:30 -07:00
parent 662b8b595f
commit f42254125f
6 changed files with 90 additions and 21 deletions

View File

@ -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)
{

View File

@ -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

View File

@ -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);

View File

@ -27,6 +27,10 @@ THE SOFTWARE.
#include "platform/CCPlatformMacros.h"
#ifdef EMSCRIPTEN
#include <GLES2/gl2.h>
#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
};

View File

@ -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);
}

View File

@ -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 );