Yeahhhhhh

it is working
This commit is contained in:
Ricardo Quesada 2014-05-08 12:34:26 -07:00
parent a2361b30de
commit 8f2ba467ff
21 changed files with 201 additions and 180 deletions

View File

@ -178,8 +178,8 @@ bool DrawNode::init()
glEnableVertexAttribArray(GLProgram::VERTEX_ATTRIB_COLOR); glEnableVertexAttribArray(GLProgram::VERTEX_ATTRIB_COLOR);
glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_COLOR, 4, GL_UNSIGNED_BYTE, GL_TRUE, sizeof(V2F_C4B_T2F), (GLvoid *)offsetof(V2F_C4B_T2F, colors)); glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_COLOR, 4, GL_UNSIGNED_BYTE, GL_TRUE, sizeof(V2F_C4B_T2F), (GLvoid *)offsetof(V2F_C4B_T2F, colors));
glEnableVertexAttribArray(GLProgram::VERTEX_ATTRIB_TEX_COORDS); glEnableVertexAttribArray(GLProgram::VERTEX_ATTRIB_TEX_COORD);
glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_TEX_COORDS, 2, GL_FLOAT, GL_FALSE, sizeof(V2F_C4B_T2F), (GLvoid *)offsetof(V2F_C4B_T2F, texCoords)); glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_TEX_COORD, 2, GL_FLOAT, GL_FALSE, sizeof(V2F_C4B_T2F), (GLvoid *)offsetof(V2F_C4B_T2F, texCoords));
glBindBuffer(GL_ARRAY_BUFFER, 0); glBindBuffer(GL_ARRAY_BUFFER, 0);
@ -241,7 +241,7 @@ void DrawNode::onDraw(const Matrix &transform, bool transformUpdated)
glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_COLOR, 4, GL_UNSIGNED_BYTE, GL_TRUE, sizeof(V2F_C4B_T2F), (GLvoid *)offsetof(V2F_C4B_T2F, colors)); glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_COLOR, 4, GL_UNSIGNED_BYTE, GL_TRUE, sizeof(V2F_C4B_T2F), (GLvoid *)offsetof(V2F_C4B_T2F, colors));
// texcood // texcood
glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_TEX_COORDS, 2, GL_FLOAT, GL_FALSE, sizeof(V2F_C4B_T2F), (GLvoid *)offsetof(V2F_C4B_T2F, texCoords)); glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_TEX_COORD, 2, GL_FLOAT, GL_FALSE, sizeof(V2F_C4B_T2F), (GLvoid *)offsetof(V2F_C4B_T2F, texCoords));
} }
glDrawArrays(GL_TRIANGLES, 0, _bufferCount); glDrawArrays(GL_TRIANGLES, 0, _bufferCount);

View File

@ -82,6 +82,7 @@ const char* GLProgram::UNIFORM_NAME_ALPHA_TEST_VALUE = "CC_alpha_value";
const char* GLProgram::ATTRIBUTE_NAME_COLOR = "a_color"; const char* GLProgram::ATTRIBUTE_NAME_COLOR = "a_color";
const char* GLProgram::ATTRIBUTE_NAME_POSITION = "a_position"; const char* GLProgram::ATTRIBUTE_NAME_POSITION = "a_position";
const char* GLProgram::ATTRIBUTE_NAME_TEX_COORD = "a_texCoord"; const char* GLProgram::ATTRIBUTE_NAME_TEX_COORD = "a_texCoord";
const char* GLProgram::ATTRIBUTE_NAME_NORMAL = "a_normal";
GLProgram* GLProgram::createWithByteArrays(const GLchar* vShaderByteArray, const GLchar* fShaderByteArray) GLProgram* GLProgram::createWithByteArrays(const GLchar* vShaderByteArray, const GLchar* fShaderByteArray)
@ -243,6 +244,26 @@ bool GLProgram::initWithFilenames(const std::string &vShaderFilename, const std:
return initWithByteArrays(vertexSource.c_str(), fragmentSource.c_str()); return initWithByteArrays(vertexSource.c_str(), fragmentSource.c_str());
} }
void GLProgram::bindPredefinedVertexAttribs()
{
static const struct {
const char *attributeName;
int location;
} attribute_locations[] =
{
{GLProgram::ATTRIBUTE_NAME_POSITION, GLProgram::VERTEX_ATTRIB_POSITION},
{GLProgram::ATTRIBUTE_NAME_COLOR, GLProgram::VERTEX_ATTRIB_COLOR},
{GLProgram::ATTRIBUTE_NAME_TEX_COORD, GLProgram::VERTEX_ATTRIB_TEX_COORD},
{GLProgram::ATTRIBUTE_NAME_NORMAL, GLProgram::VERTEX_ATTRIB_NORMAL},
};
const int size = sizeof(attribute_locations) / sizeof(attribute_locations[0]);
for(int i=0; i<size;i++) {
glBindAttribLocation(_program, attribute_locations[i].location, attribute_locations[i].attributeName);
}
}
void GLProgram::parseVertexAttribs() void GLProgram::parseVertexAttribs()
{ {
_attributesDictionary.clear(); _attributesDictionary.clear();
@ -449,7 +470,6 @@ bool GLProgram::link()
{ {
CCASSERT(_program != 0, "Cannot link invalid program"); CCASSERT(_program != 0, "Cannot link invalid program");
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT) || (CC_TARGET_PLATFORM == CC_PLATFORM_WP8) #if (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT) || (CC_TARGET_PLATFORM == CC_PLATFORM_WP8)
if(!_hasShaderCompiler) if(!_hasShaderCompiler)
{ {
@ -460,8 +480,9 @@ bool GLProgram::link()
GLint status = GL_TRUE; GLint status = GL_TRUE;
glLinkProgram(_program); bindPredefinedVertexAttribs();
glLinkProgram(_program);
parseVertexAttribs(); parseVertexAttribs();
parseUniforms(); parseUniforms();

View File

@ -56,6 +56,7 @@ typedef void (*GLLogFunction) (GLuint program, GLsizei bufsize, GLsizei* length,
class VertexAttrib class VertexAttrib
{ {
friend class GLProgram; friend class GLProgram;
friend class GLProgramState;
friend class VertexAttribValue; friend class VertexAttribValue;
friend class VertexAttribBind; friend class VertexAttribBind;
@ -102,9 +103,13 @@ public:
{ {
VERTEX_ATTRIB_POSITION, VERTEX_ATTRIB_POSITION,
VERTEX_ATTRIB_COLOR, VERTEX_ATTRIB_COLOR,
VERTEX_ATTRIB_TEX_COORDS, VERTEX_ATTRIB_TEX_COORD,
VERTEX_ATTRIB_NORMAL,
VERTEX_ATTRIB_MAX, VERTEX_ATTRIB_MAX,
// backward compatibility
VERTEX_ATTRIB_TEX_COORDS = VERTEX_ATTRIB_TEX_COORD,
}; };
enum enum
@ -155,6 +160,7 @@ public:
static const char* ATTRIBUTE_NAME_COLOR; static const char* ATTRIBUTE_NAME_COLOR;
static const char* ATTRIBUTE_NAME_POSITION; static const char* ATTRIBUTE_NAME_POSITION;
static const char* ATTRIBUTE_NAME_TEX_COORD; static const char* ATTRIBUTE_NAME_TEX_COORD;
static const char* ATTRIBUTE_NAME_NORMAL;
GLProgram(); GLProgram();
virtual ~GLProgram(); virtual ~GLProgram();
@ -313,6 +319,8 @@ public:
protected: protected:
bool updateUniformLocation(GLint location, const GLvoid* data, unsigned int bytes); bool updateUniformLocation(GLint location, const GLvoid* data, unsigned int bytes);
virtual std::string getDescription() const; virtual std::string getDescription() const;
void bindPredefinedVertexAttribs();
void parseVertexAttribs(); void parseVertexAttribs();
void parseUniforms(); void parseUniforms();

View File

@ -52,6 +52,10 @@ UniformValue::UniformValue(Uniform *uniform, GLProgram* glprogram)
{ {
} }
UniformValue::~UniformValue()
{
}
void UniformValue::apply() void UniformValue::apply()
{ {
if(_useCallback) { if(_useCallback) {
@ -158,10 +162,12 @@ VertexAttribValue::VertexAttribValue(VertexAttrib *vertexAttrib)
{ {
} }
VertexAttribValue::~VertexAttribValue()
{
}
void VertexAttribValue::apply() void VertexAttribValue::apply()
{ {
GL::enableVertexAttribs(0);
if(_useCallback) { if(_useCallback) {
_value.callback(_vertexAttrib); _value.callback(_vertexAttrib);
} }
@ -176,13 +182,13 @@ void VertexAttribValue::apply()
} }
} }
void VertexAttribValue::setValue(const std::function<void(VertexAttrib*)> callback) void VertexAttribValue::setCallback(const std::function<void(VertexAttrib*)> callback)
{ {
_value.callback = callback; _value.callback = callback;
_useCallback = true; _useCallback = true;
} }
void VertexAttribValue::setValue(GLint size, GLenum type, GLboolean normalized, GLsizei stride, GLvoid *pointer) void VertexAttribValue::setPointer(GLint size, GLenum type, GLboolean normalized, GLsizei stride, GLvoid *pointer)
{ {
_value.pointer.size = size; _value.pointer.size = size;
_value.pointer.type = type; _value.pointer.type = type;
@ -199,8 +205,6 @@ void VertexAttribValue::setValue(GLint size, GLenum type, GLboolean normalized,
GLProgramState* GLProgramState::create(GLProgram *glprogram) GLProgramState* GLProgramState::create(GLProgram *glprogram)
{ {
CCASSERT(glprogram, "invalid shader");
auto ret = new (std::nothrow) GLProgramState; auto ret = new (std::nothrow) GLProgramState;
if(ret && ret->init(glprogram)) { if(ret && ret->init(glprogram)) {
ret->autorelease(); ret->autorelease();
@ -210,6 +214,12 @@ GLProgramState* GLProgramState::create(GLProgram *glprogram)
return nullptr; return nullptr;
} }
GLProgramState::GLProgramState()
: _vertexAttribsFlags(0)
, _blendFunc(BlendFunc::ALPHA_NON_PREMULTIPLIED)
{
}
GLProgramState::~GLProgramState() GLProgramState::~GLProgramState()
{ {
_glprogram->release(); _glprogram->release();
@ -217,6 +227,8 @@ GLProgramState::~GLProgramState()
bool GLProgramState::init(GLProgram* glprogram) bool GLProgramState::init(GLProgram* glprogram)
{ {
CCASSERT(glprogram, "invalid shader");
_glprogram = glprogram; _glprogram = glprogram;
_glprogram->retain(); _glprogram->retain();
@ -232,13 +244,22 @@ bool GLProgramState::init(GLProgram* glprogram)
return true; return true;
} }
void GLProgramState::apply()
void GLProgramState::resetGLProgram()
{
CC_SAFE_RELEASE(_glprogram);
_uniforms.clear();
_attributes.clear();
}
void GLProgramState::apply(const Matrix& modelView)
{ {
CCASSERT(_glprogram, "invalid glprogram"); CCASSERT(_glprogram, "invalid glprogram");
// set shader // set shader
_glprogram->use(); _glprogram->use();
_glprogram->setUniformsForBuiltins(modelView);
// set texture // set texture
int i = 0; int i = 0;
@ -250,15 +271,36 @@ void GLProgramState::apply()
GL::blendFunc(_blendFunc.src, _blendFunc.dst); GL::blendFunc(_blendFunc.src, _blendFunc.dst);
// set uniforms // enable/disable vertex attribs
for(auto& uniform : _uniforms) { GL::enableVertexAttribs(_vertexAttribsFlags);
uniform.second.apply();
}
// set attributes // set attributes
for(auto &attribute : _attributes) { for(auto &attribute : _attributes) {
attribute.second.apply(); attribute.second.apply();
} }
// set uniforms
for(auto& uniform : _uniforms) {
uniform.second.apply();
}
}
void GLProgramState::setGLProgram(GLProgram *glprogram)
{
CCASSERT(glprogram, "invalid GLProgram");
if( _glprogram != glprogram) {
resetGLProgram();
init(glprogram);
}
}
void GLProgramState::setTexture(cocos2d::Texture2D *texture)
{
if(_textures.size()>0)
_textures.replace(0, texture);
else
_textures.pushBack(texture);
} }
UniformValue* GLProgramState::getUniformValue(const std::string &name) UniformValue* GLProgramState::getUniformValue(const std::string &name)
@ -277,4 +319,19 @@ VertexAttribValue* GLProgramState::getVertexAttribValue(const std::string &name)
return nullptr; return nullptr;
} }
void GLProgramState::setVertexAttribCallback(const std::string &name, const std::function<void(VertexAttrib*)> callback)
{
VertexAttribValue *v = getVertexAttribValue(name);
v->setCallback(callback);
_vertexAttribsFlags |= 1 << v->_vertexAttrib->_index;
}
void GLProgramState::setVertexAttribPointer(const std::string &name, GLint size, GLenum type, GLboolean normalized, GLsizei stride, GLvoid *pointer)
{
auto v = getVertexAttribValue(name);
v->setPointer(size, type, normalized, stride, pointer);
_vertexAttribsFlags |= 1 << v->_vertexAttrib->_index;
}
NS_CC_END NS_CC_END

View File

@ -95,14 +95,15 @@ protected:
class VertexAttribValue class VertexAttribValue
{ {
friend class GLProgram; friend class GLProgram;
friend class GLProgramState;
public: public:
VertexAttribValue(VertexAttrib *vertexAttrib); VertexAttribValue(VertexAttrib *vertexAttrib);
VertexAttribValue(); VertexAttribValue();
~VertexAttribValue(); ~VertexAttribValue();
void setValue(GLint size, GLenum type, GLboolean normalized, GLsizei stride, GLvoid *pointer); void setPointer(GLint size, GLenum type, GLboolean normalized, GLsizei stride, GLvoid *pointer);
void setValue(const std::function<void(VertexAttrib*)> callback); void setCallback(const std::function<void(VertexAttrib*)> callback);
void apply(); void apply();
protected: protected:
@ -137,26 +138,36 @@ protected:
class GLProgramState : public Ref class GLProgramState : public Ref
{ {
public: public:
GLProgramState* create(GLProgram* glprogram); static GLProgramState* create(GLProgram* glprogram);
void apply(); void apply(const Matrix& modelView);
void setTexture(Texture2D *texture) { _textures.insert(0, texture); }
void setGLProgram(GLProgram* glprogram);
GLProgram* getGLProgram() const { return _glprogram; }
void setTexture(Texture2D *texture);
Texture2D* getTexture() const { return _textures.at(0); } Texture2D* getTexture() const { return _textures.at(0); }
void setBlendFunc(const BlendFunc& blendFunc) { _blendFunc = blendFunc; } void setBlendFunc(const BlendFunc& blendFunc) { _blendFunc = blendFunc; }
const BlendFunc& getBlendFunc() const { return _blendFunc; } const BlendFunc& getBlendFunc() const { return _blendFunc; }
void setVertexAttribCallback(const std::string &name, const std::function<void(VertexAttrib*)> callback);
void setVertexAttribPointer(const std::string &name, GLint size, GLenum type, GLboolean normalized, GLsizei stride, GLvoid *pointer);
UniformValue* getUniformValue(const std::string &uniformName); UniformValue* getUniformValue(const std::string &uniformName);
VertexAttribValue* getVertexAttribValue(const std::string &attributeName);
protected: protected:
GLProgramState(); GLProgramState();
~GLProgramState(); ~GLProgramState();
bool init(GLProgram* program); bool init(GLProgram* program);
void resetGLProgram();
VertexAttribValue* getVertexAttribValue(const std::string &attributeName);
std::unordered_map<std::string, UniformValue> _uniforms; std::unordered_map<std::string, UniformValue> _uniforms;
std::unordered_map<std::string, VertexAttribValue> _attributes; std::unordered_map<std::string, VertexAttribValue> _attributes;
uint32_t _vertexAttribsFlags;
GLProgram *_glprogram; GLProgram *_glprogram;
Vector<Texture2D*> _textures; Vector<Texture2D*> _textures;
BlendFunc _blendFunc; BlendFunc _blendFunc;

View File

@ -330,7 +330,7 @@ void Grid3D::blit(void)
// texCoords // texCoords
setGLBufferData(_texCoordinates, numOfPoints * sizeof(Vector2), 1); setGLBufferData(_texCoordinates, numOfPoints * sizeof(Vector2), 1);
glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_TEX_COORDS, 2, GL_FLOAT, GL_FALSE, 0, 0); glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_TEX_COORD, 2, GL_FLOAT, GL_FALSE, 0, 0);
setGLIndexData(_indices, n * 12, 0); setGLIndexData(_indices, n * 12, 0);
glDrawElements(GL_TRIANGLES, (GLsizei) n*6, GL_UNSIGNED_SHORT, 0); glDrawElements(GL_TRIANGLES, (GLsizei) n*6, GL_UNSIGNED_SHORT, 0);
@ -339,7 +339,7 @@ void Grid3D::blit(void)
glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 3, GL_FLOAT, GL_FALSE, 0, _vertices); glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 3, GL_FLOAT, GL_FALSE, 0, _vertices);
// texCoords // texCoords
glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_TEX_COORDS, 2, GL_FLOAT, GL_FALSE, 0, _texCoordinates); glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_TEX_COORD, 2, GL_FLOAT, GL_FALSE, 0, _texCoordinates);
glDrawElements(GL_TRIANGLES, (GLsizei) n*6, GL_UNSIGNED_SHORT, _indices); glDrawElements(GL_TRIANGLES, (GLsizei) n*6, GL_UNSIGNED_SHORT, _indices);
#endif // EMSCRIPTEN #endif // EMSCRIPTEN
@ -545,7 +545,7 @@ void TiledGrid3D::blit(void)
// texCoords // texCoords
setGLBufferData(_texCoordinates, (numQuads*4*sizeof(Vector2)), 1); setGLBufferData(_texCoordinates, (numQuads*4*sizeof(Vector2)), 1);
glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_TEX_COORDS, 2, GL_FLOAT, GL_FALSE, 0, 0); glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_TEX_COORD, 2, GL_FLOAT, GL_FALSE, 0, 0);
setGLIndexData(_indices, n * 12, 0); setGLIndexData(_indices, n * 12, 0);
glDrawElements(GL_TRIANGLES, (GLsizei) n*6, GL_UNSIGNED_SHORT, 0); glDrawElements(GL_TRIANGLES, (GLsizei) n*6, GL_UNSIGNED_SHORT, 0);
@ -554,7 +554,7 @@ void TiledGrid3D::blit(void)
glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 3, GL_FLOAT, GL_FALSE, 0, _vertices); glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 3, GL_FLOAT, GL_FALSE, 0, _vertices);
// texCoords // texCoords
glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_TEX_COORDS, 2, GL_FLOAT, GL_FALSE, 0, _texCoordinates); glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_TEX_COORD, 2, GL_FLOAT, GL_FALSE, 0, _texCoordinates);
glDrawElements(GL_TRIANGLES, (GLsizei)n*6, GL_UNSIGNED_SHORT, _indices); glDrawElements(GL_TRIANGLES, (GLsizei)n*6, GL_UNSIGNED_SHORT, _indices);
#endif // EMSCRIPTEN #endif // EMSCRIPTEN

View File

@ -389,13 +389,13 @@ void MotionStreak::onDraw(const Matrix &transform, bool transformUpdated)
glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 2, GL_FLOAT, GL_FALSE, 0, 0); glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 2, GL_FLOAT, GL_FALSE, 0, 0);
setGLBufferData(_texCoords, (sizeof(Tex2F) * _maxPoints * 2), 1); setGLBufferData(_texCoords, (sizeof(Tex2F) * _maxPoints * 2), 1);
glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_TEX_COORDS, 2, GL_FLOAT, GL_FALSE, 0, 0); glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_TEX_COORD, 2, GL_FLOAT, GL_FALSE, 0, 0);
setGLBufferData(_colorPointer, (sizeof(GLubyte) * _maxPoints * 2 * 4), 2); setGLBufferData(_colorPointer, (sizeof(GLubyte) * _maxPoints * 2 * 4), 2);
glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_COLOR, 4, GL_UNSIGNED_BYTE, GL_TRUE, 0, 0); glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_COLOR, 4, GL_UNSIGNED_BYTE, GL_TRUE, 0, 0);
#else #else
glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 2, GL_FLOAT, GL_FALSE, 0, _vertices); glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 2, GL_FLOAT, GL_FALSE, 0, _vertices);
glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_TEX_COORDS, 2, GL_FLOAT, GL_FALSE, 0, _texCoords); glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_TEX_COORD, 2, GL_FLOAT, GL_FALSE, 0, _texCoords);
glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_COLOR, 4, GL_UNSIGNED_BYTE, GL_TRUE, 0, _colorPointer); glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_COLOR, 4, GL_UNSIGNED_BYTE, GL_TRUE, 0, _colorPointer);
#endif // EMSCRIPTEN #endif // EMSCRIPTEN

View File

@ -478,8 +478,8 @@ void ParticleSystemQuad::setupVBOandVAO()
glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_COLOR, 4, GL_UNSIGNED_BYTE, GL_TRUE, kQuadSize, (GLvoid*) offsetof( V3F_C4B_T2F, colors)); glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_COLOR, 4, GL_UNSIGNED_BYTE, GL_TRUE, kQuadSize, (GLvoid*) offsetof( V3F_C4B_T2F, colors));
// tex coords // tex coords
glEnableVertexAttribArray(GLProgram::VERTEX_ATTRIB_TEX_COORDS); glEnableVertexAttribArray(GLProgram::VERTEX_ATTRIB_TEX_COORD);
glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_TEX_COORDS, 2, GL_FLOAT, GL_FALSE, kQuadSize, (GLvoid*) offsetof( V3F_C4B_T2F, texCoords)); glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_TEX_COORD, 2, GL_FLOAT, GL_FALSE, kQuadSize, (GLvoid*) offsetof( V3F_C4B_T2F, texCoords));
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _buffersVBO[1]); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _buffersVBO[1]);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(_indices[0]) * _totalParticles * 6, _indices, GL_STATIC_DRAW); glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(_indices[0]) * _totalParticles * 6, _indices, GL_STATIC_DRAW);

View File

@ -521,10 +521,10 @@ void ProgressTimer::onDraw(const Matrix &transform, bool transformUpdated)
glVertexAttribPointer( GLProgram::VERTEX_ATTRIB_COLOR, 4, GL_UNSIGNED_BYTE, GL_TRUE, sizeof(V2F_C4B_T2F), (GLvoid*)offset); glVertexAttribPointer( GLProgram::VERTEX_ATTRIB_COLOR, 4, GL_UNSIGNED_BYTE, GL_TRUE, sizeof(V2F_C4B_T2F), (GLvoid*)offset);
offset += sizeof(Color4B); offset += sizeof(Color4B);
glVertexAttribPointer( GLProgram::VERTEX_ATTRIB_TEX_COORDS, 2, GL_FLOAT, GL_FALSE, sizeof(V2F_C4B_T2F), (GLvoid*)offset); glVertexAttribPointer( GLProgram::VERTEX_ATTRIB_TEX_COORD, 2, GL_FLOAT, GL_FALSE, sizeof(V2F_C4B_T2F), (GLvoid*)offset);
#else #else
glVertexAttribPointer( GLProgram::VERTEX_ATTRIB_POSITION, 2, GL_FLOAT, GL_FALSE, sizeof(_vertexData[0]) , &_vertexData[0].vertices); glVertexAttribPointer( GLProgram::VERTEX_ATTRIB_POSITION, 2, GL_FLOAT, GL_FALSE, sizeof(_vertexData[0]) , &_vertexData[0].vertices);
glVertexAttribPointer( GLProgram::VERTEX_ATTRIB_TEX_COORDS, 2, GL_FLOAT, GL_FALSE, sizeof(_vertexData[0]), &_vertexData[0].texCoords); glVertexAttribPointer( GLProgram::VERTEX_ATTRIB_TEX_COORD, 2, GL_FLOAT, GL_FALSE, sizeof(_vertexData[0]), &_vertexData[0].texCoords);
glVertexAttribPointer( GLProgram::VERTEX_ATTRIB_COLOR, 4, GL_UNSIGNED_BYTE, GL_TRUE, sizeof(_vertexData[0]), &_vertexData[0].colors); glVertexAttribPointer( GLProgram::VERTEX_ATTRIB_COLOR, 4, GL_UNSIGNED_BYTE, GL_TRUE, sizeof(_vertexData[0]), &_vertexData[0].colors);
#endif // EMSCRIPTEN #endif // EMSCRIPTEN

View File

@ -283,118 +283,51 @@ void ShaderCache::loadDefaultShader(GLProgram *p, int type)
switch (type) { switch (type) {
case kShaderType_PositionTextureColor: case kShaderType_PositionTextureColor:
p->initWithByteArrays(ccPositionTextureColor_vert, ccPositionTextureColor_frag); p->initWithByteArrays(ccPositionTextureColor_vert, ccPositionTextureColor_frag);
p->bindAttribLocation(GLProgram::ATTRIBUTE_NAME_POSITION, GLProgram::VERTEX_ATTRIB_POSITION);
p->bindAttribLocation(GLProgram::ATTRIBUTE_NAME_COLOR, GLProgram::VERTEX_ATTRIB_COLOR);
p->bindAttribLocation(GLProgram::ATTRIBUTE_NAME_TEX_COORD, GLProgram::VERTEX_ATTRIB_TEX_COORDS);
break; break;
case kShaderType_PositionTextureColor_noMVP: case kShaderType_PositionTextureColor_noMVP:
p->initWithByteArrays(ccPositionTextureColor_noMVP_vert, ccPositionTextureColor_noMVP_frag); p->initWithByteArrays(ccPositionTextureColor_noMVP_vert, ccPositionTextureColor_noMVP_frag);
p->bindAttribLocation(GLProgram::ATTRIBUTE_NAME_POSITION, GLProgram::VERTEX_ATTRIB_POSITION);
p->bindAttribLocation(GLProgram::ATTRIBUTE_NAME_COLOR, GLProgram::VERTEX_ATTRIB_COLOR);
p->bindAttribLocation(GLProgram::ATTRIBUTE_NAME_TEX_COORD, GLProgram::VERTEX_ATTRIB_TEX_COORDS);
break; break;
case kShaderType_PositionTextureColorAlphaTest: case kShaderType_PositionTextureColorAlphaTest:
p->initWithByteArrays(ccPositionTextureColor_vert, ccPositionTextureColorAlphaTest_frag); p->initWithByteArrays(ccPositionTextureColor_vert, ccPositionTextureColorAlphaTest_frag);
p->bindAttribLocation(GLProgram::ATTRIBUTE_NAME_POSITION, GLProgram::VERTEX_ATTRIB_POSITION);
p->bindAttribLocation(GLProgram::ATTRIBUTE_NAME_COLOR, GLProgram::VERTEX_ATTRIB_COLOR);
p->bindAttribLocation(GLProgram::ATTRIBUTE_NAME_TEX_COORD, GLProgram::VERTEX_ATTRIB_TEX_COORDS);
break; break;
case kShaderType_PositionTextureColorAlphaTestNoMV: case kShaderType_PositionTextureColorAlphaTestNoMV:
p->initWithByteArrays(ccPositionTextureColor_noMVP_vert, ccPositionTextureColorAlphaTest_frag); p->initWithByteArrays(ccPositionTextureColor_noMVP_vert, ccPositionTextureColorAlphaTest_frag);
p->bindAttribLocation(GLProgram::ATTRIBUTE_NAME_POSITION, GLProgram::VERTEX_ATTRIB_POSITION);
p->bindAttribLocation(GLProgram::ATTRIBUTE_NAME_COLOR, GLProgram::VERTEX_ATTRIB_COLOR);
p->bindAttribLocation(GLProgram::ATTRIBUTE_NAME_TEX_COORD, GLProgram::VERTEX_ATTRIB_TEX_COORDS);
break; break;
case kShaderType_PositionColor: case kShaderType_PositionColor:
p->initWithByteArrays(ccPositionColor_vert ,ccPositionColor_frag); p->initWithByteArrays(ccPositionColor_vert ,ccPositionColor_frag);
p->bindAttribLocation(GLProgram::ATTRIBUTE_NAME_POSITION, GLProgram::VERTEX_ATTRIB_POSITION);
p->bindAttribLocation(GLProgram::ATTRIBUTE_NAME_COLOR, GLProgram::VERTEX_ATTRIB_COLOR);
break; break;
case kShaderType_PositionColor_noMVP: case kShaderType_PositionColor_noMVP:
p->initWithByteArrays(ccPositionTextureColor_noMVP_vert ,ccPositionColor_frag); p->initWithByteArrays(ccPositionTextureColor_noMVP_vert ,ccPositionColor_frag);
p->bindAttribLocation(GLProgram::ATTRIBUTE_NAME_POSITION, GLProgram::VERTEX_ATTRIB_POSITION);
p->bindAttribLocation(GLProgram::ATTRIBUTE_NAME_COLOR, GLProgram::VERTEX_ATTRIB_COLOR);
break; break;
case kShaderType_PositionTexture: case kShaderType_PositionTexture:
p->initWithByteArrays(ccPositionTexture_vert ,ccPositionTexture_frag); p->initWithByteArrays(ccPositionTexture_vert ,ccPositionTexture_frag);
p->bindAttribLocation(GLProgram::ATTRIBUTE_NAME_POSITION, GLProgram::VERTEX_ATTRIB_POSITION);
p->bindAttribLocation(GLProgram::ATTRIBUTE_NAME_TEX_COORD, GLProgram::VERTEX_ATTRIB_TEX_COORDS);
break; break;
case kShaderType_PositionTexture_uColor: case kShaderType_PositionTexture_uColor:
p->initWithByteArrays(ccPositionTexture_uColor_vert, ccPositionTexture_uColor_frag); p->initWithByteArrays(ccPositionTexture_uColor_vert, ccPositionTexture_uColor_frag);
p->bindAttribLocation(GLProgram::ATTRIBUTE_NAME_POSITION, GLProgram::VERTEX_ATTRIB_POSITION);
p->bindAttribLocation(GLProgram::ATTRIBUTE_NAME_TEX_COORD, GLProgram::VERTEX_ATTRIB_TEX_COORDS);
break; break;
case kShaderType_PositionTextureA8Color: case kShaderType_PositionTextureA8Color:
p->initWithByteArrays(ccPositionTextureA8Color_vert, ccPositionTextureA8Color_frag); p->initWithByteArrays(ccPositionTextureA8Color_vert, ccPositionTextureA8Color_frag);
p->bindAttribLocation(GLProgram::ATTRIBUTE_NAME_POSITION, GLProgram::VERTEX_ATTRIB_POSITION);
p->bindAttribLocation(GLProgram::ATTRIBUTE_NAME_COLOR, GLProgram::VERTEX_ATTRIB_COLOR);
p->bindAttribLocation(GLProgram::ATTRIBUTE_NAME_TEX_COORD, GLProgram::VERTEX_ATTRIB_TEX_COORDS);
break; break;
case kShaderType_Position_uColor: case kShaderType_Position_uColor:
p->initWithByteArrays(ccPosition_uColor_vert, ccPosition_uColor_frag); p->initWithByteArrays(ccPosition_uColor_vert, ccPosition_uColor_frag);
p->bindAttribLocation("aVertex", GLProgram::VERTEX_ATTRIB_POSITION); p->bindAttribLocation("aVertex", GLProgram::VERTEX_ATTRIB_POSITION);
break; break;
case kShaderType_PositionLengthTexureColor: case kShaderType_PositionLengthTexureColor:
p->initWithByteArrays(ccPositionColorLengthTexture_vert, ccPositionColorLengthTexture_frag); p->initWithByteArrays(ccPositionColorLengthTexture_vert, ccPositionColorLengthTexture_frag);
p->bindAttribLocation(GLProgram::ATTRIBUTE_NAME_POSITION, GLProgram::VERTEX_ATTRIB_POSITION);
p->bindAttribLocation(GLProgram::ATTRIBUTE_NAME_TEX_COORD, GLProgram::VERTEX_ATTRIB_TEX_COORDS);
p->bindAttribLocation(GLProgram::ATTRIBUTE_NAME_COLOR, GLProgram::VERTEX_ATTRIB_COLOR);
break; break;
case kShaderType_LabelDistanceFieldNormal: case kShaderType_LabelDistanceFieldNormal:
p->initWithByteArrays(ccLabel_vert, ccLabelDistanceFieldNormal_frag); p->initWithByteArrays(ccLabel_vert, ccLabelDistanceFieldNormal_frag);
p->bindAttribLocation(GLProgram::ATTRIBUTE_NAME_POSITION, GLProgram::VERTEX_ATTRIB_POSITION);
p->bindAttribLocation(GLProgram::ATTRIBUTE_NAME_COLOR, GLProgram::VERTEX_ATTRIB_COLOR);
p->bindAttribLocation(GLProgram::ATTRIBUTE_NAME_TEX_COORD, GLProgram::VERTEX_ATTRIB_TEX_COORDS);
break; break;
case kShaderType_LabelDistanceFieldGlow: case kShaderType_LabelDistanceFieldGlow:
p->initWithByteArrays(ccLabel_vert, ccLabelDistanceFieldGlow_frag); p->initWithByteArrays(ccLabel_vert, ccLabelDistanceFieldGlow_frag);
p->bindAttribLocation(GLProgram::ATTRIBUTE_NAME_POSITION, GLProgram::VERTEX_ATTRIB_POSITION);
p->bindAttribLocation(GLProgram::ATTRIBUTE_NAME_COLOR, GLProgram::VERTEX_ATTRIB_COLOR);
p->bindAttribLocation(GLProgram::ATTRIBUTE_NAME_TEX_COORD, GLProgram::VERTEX_ATTRIB_TEX_COORDS);
break; break;
case kShaderType_LabelNormal: case kShaderType_LabelNormal:
p->initWithByteArrays(ccLabel_vert, ccLabelNormal_frag); p->initWithByteArrays(ccLabel_vert, ccLabelNormal_frag);
p->bindAttribLocation(GLProgram::ATTRIBUTE_NAME_POSITION, GLProgram::VERTEX_ATTRIB_POSITION);
p->bindAttribLocation(GLProgram::ATTRIBUTE_NAME_COLOR, GLProgram::VERTEX_ATTRIB_COLOR);
p->bindAttribLocation(GLProgram::ATTRIBUTE_NAME_TEX_COORD, GLProgram::VERTEX_ATTRIB_TEX_COORDS);
break; break;
case kShaderType_LabelOutline: case kShaderType_LabelOutline:
p->initWithByteArrays(ccLabel_vert, ccLabelOutline_frag); p->initWithByteArrays(ccLabel_vert, ccLabelOutline_frag);
p->bindAttribLocation(GLProgram::ATTRIBUTE_NAME_POSITION, GLProgram::VERTEX_ATTRIB_POSITION);
p->bindAttribLocation(GLProgram::ATTRIBUTE_NAME_COLOR, GLProgram::VERTEX_ATTRIB_COLOR);
p->bindAttribLocation(GLProgram::ATTRIBUTE_NAME_TEX_COORD, GLProgram::VERTEX_ATTRIB_TEX_COORDS);
break; break;
default: default:
CCLOG("cocos2d: %s:%d, error shader type", __FUNCTION__, __LINE__); CCLOG("cocos2d: %s:%d, error shader type", __FUNCTION__, __LINE__);

View File

@ -1181,10 +1181,10 @@ void Texture2D::drawAtPoint(const Vector2& point)
glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 2, GL_FLOAT, GL_FALSE, 0, 0); glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 2, GL_FLOAT, GL_FALSE, 0, 0);
setGLBufferData(coordinates, 8 * sizeof(GLfloat), 1); setGLBufferData(coordinates, 8 * sizeof(GLfloat), 1);
glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_TEX_COORDS, 2, GL_FLOAT, GL_FALSE, 0, 0); glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_TEX_COORD, 2, GL_FLOAT, GL_FALSE, 0, 0);
#else #else
glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 2, GL_FLOAT, GL_FALSE, 0, vertices); glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 2, GL_FLOAT, GL_FALSE, 0, vertices);
glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_TEX_COORDS, 2, GL_FLOAT, GL_FALSE, 0, coordinates); glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_TEX_COORD, 2, GL_FLOAT, GL_FALSE, 0, coordinates);
#endif // EMSCRIPTEN #endif // EMSCRIPTEN
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
@ -1214,10 +1214,10 @@ void Texture2D::drawInRect(const Rect& rect)
glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 2, GL_FLOAT, GL_FALSE, 0, 0); glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 2, GL_FLOAT, GL_FALSE, 0, 0);
setGLBufferData(coordinates, 8 * sizeof(GLfloat), 1); setGLBufferData(coordinates, 8 * sizeof(GLfloat), 1);
glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_TEX_COORDS, 2, GL_FLOAT, GL_FALSE, 0, 0); glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_TEX_COORD, 2, GL_FLOAT, GL_FALSE, 0, 0);
#else #else
glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 2, GL_FLOAT, GL_FALSE, 0, vertices); glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 2, GL_FLOAT, GL_FALSE, 0, vertices);
glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_TEX_COORDS, 2, GL_FLOAT, GL_FALSE, 0, coordinates); glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_TEX_COORD, 2, GL_FLOAT, GL_FALSE, 0, coordinates);
#endif // EMSCRIPTEN #endif // EMSCRIPTEN
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
} }

View File

@ -274,8 +274,8 @@ void TextureAtlas::setupVBOandVAO()
glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_COLOR, 4, GL_UNSIGNED_BYTE, GL_TRUE, kQuadSize, (GLvoid*) offsetof( V3F_C4B_T2F, colors)); glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_COLOR, 4, GL_UNSIGNED_BYTE, GL_TRUE, kQuadSize, (GLvoid*) offsetof( V3F_C4B_T2F, colors));
// tex coords // tex coords
glEnableVertexAttribArray(GLProgram::VERTEX_ATTRIB_TEX_COORDS); glEnableVertexAttribArray(GLProgram::VERTEX_ATTRIB_TEX_COORD);
glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_TEX_COORDS, 2, GL_FLOAT, GL_FALSE, kQuadSize, (GLvoid*) offsetof( V3F_C4B_T2F, texCoords)); glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_TEX_COORD, 2, GL_FLOAT, GL_FALSE, kQuadSize, (GLvoid*) offsetof( V3F_C4B_T2F, texCoords));
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _buffersVBO[1]); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _buffersVBO[1]);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(_indices[0]) * _capacity * 6, _indices, GL_STATIC_DRAW); glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(_indices[0]) * _capacity * 6, _indices, GL_STATIC_DRAW);
@ -673,7 +673,7 @@ void TextureAtlas::drawNumberOfQuads(ssize_t numberOfQuads, ssize_t start)
glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_COLOR, 4, GL_UNSIGNED_BYTE, GL_TRUE, kQuadSize, (GLvoid*) offsetof(V3F_C4B_T2F, colors)); glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_COLOR, 4, GL_UNSIGNED_BYTE, GL_TRUE, kQuadSize, (GLvoid*) offsetof(V3F_C4B_T2F, colors));
// tex coords // tex coords
glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_TEX_COORDS, 2, GL_FLOAT, GL_FALSE, kQuadSize, (GLvoid*) offsetof(V3F_C4B_T2F, texCoords)); glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_TEX_COORD, 2, GL_FLOAT, GL_FALSE, kQuadSize, (GLvoid*) offsetof(V3F_C4B_T2F, texCoords));
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _buffersVBO[1]); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _buffersVBO[1]);

View File

@ -33,17 +33,18 @@ THE SOFTWARE.
NS_CC_BEGIN NS_CC_BEGIN
static const int MAX_ATTRIBUTES = 16;
static const int MAX_ACTIVE_TEXTURE = 16;
namespace namespace
{ {
static GLuint s_currentProjectionMatrix = -1; static GLuint s_currentProjectionMatrix = -1;
static unsigned int s_attributeFlags = 0; static uint32_t s_attributeFlags = 0; // 32 attributes max
#if CC_ENABLE_GL_STATE_CACHE #if CC_ENABLE_GL_STATE_CACHE
#define kMaxActiveTexture 16
static GLuint s_currentShaderProgram = -1; static GLuint s_currentShaderProgram = -1;
static GLuint s_currentBoundTexture[kMaxActiveTexture] = {(GLuint)-1,(GLuint)-1,(GLuint)-1,(GLuint)-1, (GLuint)-1,(GLuint)-1,(GLuint)-1,(GLuint)-1, (GLuint)-1,(GLuint)-1,(GLuint)-1,(GLuint)-1, (GLuint)-1,(GLuint)-1,(GLuint)-1,(GLuint)-1, }; static GLuint s_currentBoundTexture[MAX_ACTIVE_TEXTURE] = {(GLuint)-1,(GLuint)-1,(GLuint)-1,(GLuint)-1, (GLuint)-1,(GLuint)-1,(GLuint)-1,(GLuint)-1, (GLuint)-1,(GLuint)-1,(GLuint)-1,(GLuint)-1, (GLuint)-1,(GLuint)-1,(GLuint)-1,(GLuint)-1, };
static GLenum s_blendingSource = -1; static GLenum s_blendingSource = -1;
static GLenum s_blendingDest = -1; static GLenum s_blendingDest = -1;
static int s_GLServerState = 0; static int s_GLServerState = 0;
@ -65,7 +66,7 @@ void invalidateStateCache( void )
#if CC_ENABLE_GL_STATE_CACHE #if CC_ENABLE_GL_STATE_CACHE
s_currentShaderProgram = -1; s_currentShaderProgram = -1;
for( int i=0; i < kMaxActiveTexture; i++ ) for( int i=0; i < MAX_ACTIVE_TEXTURE; i++ )
{ {
s_currentBoundTexture[i] = -1; s_currentBoundTexture[i] = -1;
} }
@ -147,7 +148,7 @@ void bindTexture2D(GLuint textureId)
void bindTexture2DN(GLuint textureUnit, GLuint textureId) void bindTexture2DN(GLuint textureUnit, GLuint textureId)
{ {
#if CC_ENABLE_GL_STATE_CACHE #if CC_ENABLE_GL_STATE_CACHE
CCASSERT(textureUnit < kMaxActiveTexture, "textureUnit is too big"); CCASSERT(textureUnit < MAX_ACTIVE_TEXTURE, "textureUnit is too big");
if (s_currentBoundTexture[textureUnit] != textureId) if (s_currentBoundTexture[textureUnit] != textureId)
{ {
s_currentBoundTexture[textureUnit] = textureId; s_currentBoundTexture[textureUnit] = textureId;
@ -210,12 +211,12 @@ void bindVAO(GLuint vaoId)
// GL Vertex Attrib functions // GL Vertex Attrib functions
void enableVertexAttribs( unsigned int flags ) void enableVertexAttribs(uint32_t flags)
{ {
bindVAO(0); bindVAO(0);
// hardcoded! // hardcoded!
for(int i=0; i < 16; i++) { for(int i=0; i < MAX_ATTRIBUTES; i++) {
unsigned int bit = 1 << i; unsigned int bit = 1 << i;
bool enabled = flags & bit; bool enabled = flags & bit;
bool enabledBefore = s_attributeFlags & bit; bool enabledBefore = s_attributeFlags & bit;

View File

@ -102,7 +102,7 @@ void CC_DLL setProjectionMatrixDirty(void);
@since v2.0.0 @since v2.0.0
*/ */
void CC_DLL enableVertexAttribs(unsigned int flags); void CC_DLL enableVertexAttribs(uint32_t flags);
/** If the texture is not already bound to texture unit 0, it binds it. /** If the texture is not already bound to texture unit 0, it binds it.
If CC_ENABLE_GL_STATE_CACHE is disabled, it will call glBindTexture() directly. If CC_ENABLE_GL_STATE_CACHE is disabled, it will call glBindTexture() directly.

View File

@ -806,7 +806,7 @@ CC_DEPRECATED_ATTRIBUTE typedef void* CCZone;
CC_DEPRECATED_ATTRIBUTE const int kCCVertexAttrib_Position = GLProgram::VERTEX_ATTRIB_POSITION; CC_DEPRECATED_ATTRIBUTE const int kCCVertexAttrib_Position = GLProgram::VERTEX_ATTRIB_POSITION;
CC_DEPRECATED_ATTRIBUTE const int kCCVertexAttrib_Color = GLProgram::VERTEX_ATTRIB_COLOR; CC_DEPRECATED_ATTRIBUTE const int kCCVertexAttrib_Color = GLProgram::VERTEX_ATTRIB_COLOR;
CC_DEPRECATED_ATTRIBUTE const int kCCVertexAttrib_TexCoords = GLProgram::VERTEX_ATTRIB_TEX_COORDS; CC_DEPRECATED_ATTRIBUTE const int kCCVertexAttrib_TexCoords = GLProgram::VERTEX_ATTRIB_TEX_COORD;
CC_DEPRECATED_ATTRIBUTE const int kCCVertexAttrib_MAX = GLProgram::VERTEX_ATTRIB_MAX; CC_DEPRECATED_ATTRIBUTE const int kCCVertexAttrib_MAX = GLProgram::VERTEX_ATTRIB_MAX;
CC_DEPRECATED_ATTRIBUTE const int kCCUniformPMatrix = GLProgram::UNIFORM_P_MATRIX; CC_DEPRECATED_ATTRIBUTE const int kCCUniformPMatrix = GLProgram::UNIFORM_P_MATRIX;

View File

@ -202,8 +202,8 @@ void Renderer::setupVBOAndVAO()
glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_COLOR, 4, GL_UNSIGNED_BYTE, GL_TRUE, sizeof(V3F_C4B_T2F), (GLvoid*) offsetof( V3F_C4B_T2F, colors)); glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_COLOR, 4, GL_UNSIGNED_BYTE, GL_TRUE, sizeof(V3F_C4B_T2F), (GLvoid*) offsetof( V3F_C4B_T2F, colors));
// tex coords // tex coords
glEnableVertexAttribArray(GLProgram::VERTEX_ATTRIB_TEX_COORDS); glEnableVertexAttribArray(GLProgram::VERTEX_ATTRIB_TEX_COORD);
glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_TEX_COORDS, 2, GL_FLOAT, GL_FALSE, sizeof(V3F_C4B_T2F), (GLvoid*) offsetof( V3F_C4B_T2F, texCoords)); glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_TEX_COORD, 2, GL_FLOAT, GL_FALSE, sizeof(V3F_C4B_T2F), (GLvoid*) offsetof( V3F_C4B_T2F, texCoords));
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _buffersVBO[1]); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _buffersVBO[1]);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(_indices[0]) * VBO_SIZE * 6, _indices, GL_STATIC_DRAW); glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(_indices[0]) * VBO_SIZE * 6, _indices, GL_STATIC_DRAW);
@ -444,7 +444,7 @@ void Renderer::drawBatchedQuads()
glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_COLOR, 4, GL_UNSIGNED_BYTE, GL_TRUE, kQuadSize, (GLvoid*) offsetof(V3F_C4B_T2F, colors)); glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_COLOR, 4, GL_UNSIGNED_BYTE, GL_TRUE, kQuadSize, (GLvoid*) offsetof(V3F_C4B_T2F, colors));
// tex coords // tex coords
glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_TEX_COORDS, 2, GL_FLOAT, GL_FALSE, kQuadSize, (GLvoid*) offsetof(V3F_C4B_T2F, texCoords)); glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_TEX_COORD, 2, GL_FLOAT, GL_FALSE, kQuadSize, (GLvoid*) offsetof(V3F_C4B_T2F, texCoords));
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _buffersVBO[1]); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _buffersVBO[1]);
} }

View File

@ -131,7 +131,7 @@ cc.VERTEX_ATTRIB_FLAG_TEX_COORDS = 0x4
cc.VERTEX_ATTRIB_COLOR = 0x1 cc.VERTEX_ATTRIB_COLOR = 0x1
cc.VERTEX_ATTRIB_MAX = 0x3 cc.VERTEX_ATTRIB_MAX = 0x3
cc.VERTEX_ATTRIB_POSITION = 0x0 cc.VERTEX_ATTRIB_POSITION = 0x0
cc.VERTEX_ATTRIB_TEX_COORDS = 0x2 cc.VERTEX_ATTRIB_TEX_COORD = 0x2
cc.VERTICAL_TEXT_ALIGNMENT_BOTTOM = 0x2 cc.VERTICAL_TEXT_ALIGNMENT_BOTTOM = 0x2
cc.VERTICAL_TEXT_ALIGNMENT_CENTER = 0x1 cc.VERTICAL_TEXT_ALIGNMENT_CENTER = 0x1
cc.VERTICAL_TEXT_ALIGNMENT_TOP = 0x0 cc.VERTICAL_TEXT_ALIGNMENT_TOP = 0x0

View File

@ -1027,7 +1027,7 @@ void MySprite::onDraw(const Matrix &transform, bool transformUpdated)
// texCoods // texCoods
diff = offsetof( V3F_C4B_T2F, texCoords); diff = offsetof( V3F_C4B_T2F, texCoords);
glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_TEX_COORDS, 2, GL_FLOAT, GL_FALSE, kQuadSize, (void*)(offset + diff)); glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_TEX_COORD, 2, GL_FLOAT, GL_FALSE, kQuadSize, (void*)(offset + diff));
// color // color
diff = offsetof( V3F_C4B_T2F, colors); diff = offsetof( V3F_C4B_T2F, colors);

View File

@ -116,6 +116,7 @@ ShaderNode::ShaderNode()
ShaderNode::~ShaderNode() ShaderNode::~ShaderNode()
{ {
CC_SAFE_RELEASE(_glProgramState);
} }
ShaderNode* ShaderNode::shaderNodeWithVertex(const char *vert, const char *frag) ShaderNode* ShaderNode::shaderNodeWithVertex(const char *vert, const char *frag)
@ -142,6 +143,7 @@ bool ShaderNode::initWithVertex(const char *vert, const char *frag)
_time = 0; _time = 0;
_resolution = Vector2(SIZE_X, SIZE_Y); _resolution = Vector2(SIZE_X, SIZE_Y);
_glProgramState->getUniformValue("resolution")->setValue(_resolution);
scheduleUpdate(); scheduleUpdate();
@ -164,6 +166,9 @@ void ShaderNode::loadShaderVertex(const char *vert, const char *frag)
shader->updateUniforms(); shader->updateUniforms();
this->setShaderProgram(shader); this->setShaderProgram(shader);
_glProgramState = GLProgramState::create(shader);
_glProgramState->retain();
} }
void ShaderNode::update(float dt) void ShaderNode::update(float dt)
@ -176,6 +181,7 @@ void ShaderNode::setPosition(const Vector2 &newPosition)
Node::setPosition(newPosition); Node::setPosition(newPosition);
auto position = getPosition(); auto position = getPosition();
_center = Vector2(position.x * CC_CONTENT_SCALE_FACTOR(), position.y * CC_CONTENT_SCALE_FACTOR()); _center = Vector2(position.x * CC_CONTENT_SCALE_FACTOR(), position.y * CC_CONTENT_SCALE_FACTOR());
_glProgramState->getUniformValue("center")->setValue(_center);
} }
void ShaderNode::draw(Renderer *renderer, const Matrix &transform, bool transformUpdated) void ShaderNode::draw(Renderer *renderer, const Matrix &transform, bool transformUpdated)
@ -187,25 +193,16 @@ void ShaderNode::draw(Renderer *renderer, const Matrix &transform, bool transfor
void ShaderNode::onDraw(const Matrix &transform, bool transformUpdated) void ShaderNode::onDraw(const Matrix &transform, bool transformUpdated)
{ {
auto shader = getShaderProgram();
shader->use();
shader->setUniformsForBuiltins(transform);
// TODO: riq FIXME
// shader->getUniform("center")->setValue(_center);
// shader->getUniform("resolution")->setValue(_resolution);
GL::enableVertexAttribs( cocos2d::GL::VERTEX_ATTRIB_FLAG_POSITION );
float w = SIZE_X, h = SIZE_Y; float w = SIZE_X, h = SIZE_Y;
GLfloat vertices[12] = {0,0, w,0, w,h, 0,0, 0,h, w,h}; GLfloat vertices[12] = {0,0, w,0, w,h, 0,0, 0,h, w,h};
glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 2, GL_FLOAT, GL_FALSE, 0, vertices); _glProgramState->setVertexAttribPointer("a_position", 2, GL_FLOAT, GL_FALSE, 0, vertices);
_glProgramState->apply(transform);
glDrawArrays(GL_TRIANGLES, 0, 6); glDrawArrays(GL_TRIANGLES, 0, 6);
CC_INCREMENT_GL_DRAWN_BATCHES_AND_VERTICES(1,6); CC_INCREMENT_GL_DRAWN_BATCHES_AND_VERTICES(1,6);
} }
/// ShaderMonjori /// ShaderMonjori
@ -435,10 +432,12 @@ protected:
float _weightSum; float _weightSum;
CustomCommand _customCommand; CustomCommand _customCommand;
GLProgramState *_glProgramState;
}; };
SpriteBlur::~SpriteBlur() SpriteBlur::~SpriteBlur()
{ {
CC_SAFE_RELEASE(_glProgramState);
} }
SpriteBlur* SpriteBlur::create(const char *pszFileName) SpriteBlur* SpriteBlur::create(const char *pszFileName)
@ -473,9 +472,12 @@ bool SpriteBlur::initWithTexture(Texture2D* texture, const Rect& rect)
auto s = getTexture()->getContentSizeInPixels(); auto s = getTexture()->getContentSizeInPixels();
_pixelSize = Vector2(1/s.width, 1/s.height); _pixelSize = Vector2(1/s.width, 1/s.height);
_samplingRadius = 0; _samplingRadius = 0;
this->initProgram(); this->initProgram();
_glProgramState->getUniformValue("onePixelSize")->setValue(_pixelSize);
return true; return true;
} }
@ -491,12 +493,6 @@ void SpriteBlur::initProgram()
CHECK_GL_ERROR_DEBUG(); CHECK_GL_ERROR_DEBUG();
program->bindAttribLocation(GLProgram::ATTRIBUTE_NAME_POSITION, GLProgram::VERTEX_ATTRIB_POSITION);
program->bindAttribLocation(GLProgram::ATTRIBUTE_NAME_COLOR, GLProgram::VERTEX_ATTRIB_COLOR);
program->bindAttribLocation(GLProgram::ATTRIBUTE_NAME_TEX_COORD, GLProgram::VERTEX_ATTRIB_TEX_COORDS);
CHECK_GL_ERROR_DEBUG();
program->link(); program->link();
CHECK_GL_ERROR_DEBUG(); CHECK_GL_ERROR_DEBUG();
@ -504,6 +500,22 @@ void SpriteBlur::initProgram()
program->updateUniforms(); program->updateUniforms();
CHECK_GL_ERROR_DEBUG(); CHECK_GL_ERROR_DEBUG();
_glProgramState = GLProgramState::create(program);
_glProgramState->retain();
#define kQuadSize sizeof(_quad.bl)
size_t offset = (size_t)&_quad;
// position
int diff = offsetof( V3F_C4B_T2F, vertices);
_glProgramState->setVertexAttribPointer("a_position", 3, GL_FLOAT, GL_FALSE, kQuadSize, (void*) (offset + diff));
// texcoord
diff = offsetof( V3F_C4B_T2F, texCoords);
_glProgramState->setVertexAttribPointer("a_texCoord", 2, GL_FLOAT, GL_FALSE, kQuadSize, (void*)(offset + diff));
// color
diff = offsetof( V3F_C4B_T2F, colors);
_glProgramState->setVertexAttribPointer("a_color", 4, GL_UNSIGNED_BYTE, GL_TRUE, kQuadSize, (void*)(offset + diff));
} }
void SpriteBlur::draw(Renderer *renderer, const Matrix &transform, bool transformUpdated) void SpriteBlur::draw(Renderer *renderer, const Matrix &transform, bool transformUpdated)
@ -515,37 +527,10 @@ void SpriteBlur::draw(Renderer *renderer, const Matrix &transform, bool transfor
void SpriteBlur::onDraw(const Matrix &transform, bool transformUpdated) void SpriteBlur::onDraw(const Matrix &transform, bool transformUpdated)
{ {
GL::enableVertexAttribs(cocos2d::GL::VERTEX_ATTRIB_FLAG_POS_COLOR_TEX ); _glProgramState->setTexture(getTexture());
BlendFunc blend = getBlendFunc(); _glProgramState->setBlendFunc(getBlendFunc());
GL::blendFunc(blend.src, blend.dst);
auto program = getShaderProgram(); _glProgramState->apply(transform);
program->use();
program->setUniformsForBuiltins(transform);
// TODO: riq FIXME
// program->getUniform("onePixelSize")->setValue(_pixelSize);
// program->getUniform("gaussianCoefficient")->setValue(Vector4(_samplingRadius, _scale, _cons, _weightSum));
GL::bindTexture2D( getTexture()->getName());
//
// Attributes
//
#define kQuadSize sizeof(_quad.bl)
size_t offset = (size_t)&_quad;
// vertex
int diff = offsetof( V3F_C4B_T2F, vertices);
glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 3, GL_FLOAT, GL_FALSE, kQuadSize, (void*) (offset + diff));
// texCoods
diff = offsetof( V3F_C4B_T2F, texCoords);
glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_TEX_COORDS, 2, GL_FLOAT, GL_FALSE, kQuadSize, (void*)(offset + diff));
// color
diff = offsetof( V3F_C4B_T2F, colors);
glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_COLOR, 4, GL_UNSIGNED_BYTE, GL_TRUE, kQuadSize, (void*)(offset + diff));
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
@ -585,6 +570,8 @@ void SpriteBlur::setBlurSize(float f)
} }
} }
log("_blurRadius:%d",_blurRadius); log("_blurRadius:%d",_blurRadius);
_glProgramState->getUniformValue("gaussianCoefficient")->setValue(Vector4(_samplingRadius, _scale, _cons, _weightSum));
} }
// ShaderBlur // ShaderBlur
@ -670,7 +657,7 @@ bool ShaderRetroEffect::init()
auto p = GLProgram::createWithByteArrays(ccPositionTexture_vert, fragSource); auto p = GLProgram::createWithByteArrays(ccPositionTexture_vert, fragSource);
p->bindAttribLocation(GLProgram::ATTRIBUTE_NAME_POSITION, GLProgram::VERTEX_ATTRIB_POSITION); p->bindAttribLocation(GLProgram::ATTRIBUTE_NAME_POSITION, GLProgram::VERTEX_ATTRIB_POSITION);
p->bindAttribLocation(GLProgram::ATTRIBUTE_NAME_TEX_COORD, GLProgram::VERTEX_ATTRIB_TEX_COORDS); p->bindAttribLocation(GLProgram::ATTRIBUTE_NAME_TEX_COORD, GLProgram::VERTEX_ATTRIB_TEX_COORD);
p->link(); p->link();
p->updateUniforms(); p->updateUniforms();

View File

@ -5,6 +5,8 @@
#include "extensions/cocos-ext.h" #include "extensions/cocos-ext.h"
#include "../BaseTest.h" #include "../BaseTest.h"
#include "2d/CCGLProgramState.h"
USING_NS_CC_EXT; USING_NS_CC_EXT;
class ShaderTestDemo : public BaseTest class ShaderTestDemo : public BaseTest
@ -134,6 +136,7 @@ protected:
std::string _vertFileName; std::string _vertFileName;
std::string _fragFileName; std::string _fragFileName;
CustomCommand _customCommand; CustomCommand _customCommand;
GLProgramState *_glProgramState;
}; };
class ShaderTestScene : public TestScene class ShaderTestScene : public TestScene

View File

@ -158,7 +158,7 @@ local function OpenGLTestMainLayer()
InitTitle(RetroEffectlayer) InitTitle(RetroEffectlayer)
local program = cc.GLProgram:create("Shaders/example_ColorBars.vsh", "Shaders/example_ColorBars.fsh") local program = cc.GLProgram:create("Shaders/example_ColorBars.vsh", "Shaders/example_ColorBars.fsh")
program:bindAttribLocation(cc.ATTRIBUTE_NAME_POSITION, cc.VERTEX_ATTRIB_POSITION) program:bindAttribLocation(cc.ATTRIBUTE_NAME_POSITION, cc.VERTEX_ATTRIB_POSITION)
program:bindAttribLocation(cc.ATTRIBUTE_NAME_TEX_COORD, cc.VERTEX_ATTRIB_TEX_COORDS) program:bindAttribLocation(cc.ATTRIBUTE_NAME_TEX_COORD, cc.VERTEX_ATTRIB_TEX_COORD)
program:link() program:link()
program:updateUniforms() program:updateUniforms()
@ -653,7 +653,7 @@ local function OpenGLTestMainLayer()
gl.bindBuffer(gl.ARRAY_BUFFER, squareVertexTextureBuffer.buffer_id) gl.bindBuffer(gl.ARRAY_BUFFER, squareVertexTextureBuffer.buffer_id)
gl.vertexAttribPointer(cc.VERTEX_ATTRIB_TEX_COORDS,2,gl.FLOAT,false,0,0) gl.vertexAttribPointer(cc.VERTEX_ATTRIB_TEX_COORD,2,gl.FLOAT,false,0,0)
gl.drawArrays(gl.TRIANGLE_STRIP,0,4) gl.drawArrays(gl.TRIANGLE_STRIP,0,4)