mirror of https://github.com/axmolengine/axmol.git
parent
a2361b30de
commit
8f2ba467ff
|
@ -178,8 +178,8 @@ bool DrawNode::init()
|
|||
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));
|
||||
|
||||
glEnableVertexAttribArray(GLProgram::VERTEX_ATTRIB_TEX_COORDS);
|
||||
glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_TEX_COORDS, 2, GL_FLOAT, GL_FALSE, sizeof(V2F_C4B_T2F), (GLvoid *)offsetof(V2F_C4B_T2F, texCoords));
|
||||
glEnableVertexAttribArray(GLProgram::VERTEX_ATTRIB_TEX_COORD);
|
||||
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);
|
||||
|
||||
|
@ -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));
|
||||
|
||||
// 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);
|
||||
|
|
|
@ -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_POSITION = "a_position";
|
||||
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)
|
||||
|
@ -243,6 +244,26 @@ bool GLProgram::initWithFilenames(const std::string &vShaderFilename, const std:
|
|||
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()
|
||||
{
|
||||
_attributesDictionary.clear();
|
||||
|
@ -448,7 +469,6 @@ void GLProgram::updateUniforms()
|
|||
bool GLProgram::link()
|
||||
{
|
||||
CCASSERT(_program != 0, "Cannot link invalid program");
|
||||
|
||||
|
||||
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT) || (CC_TARGET_PLATFORM == CC_PLATFORM_WP8)
|
||||
if(!_hasShaderCompiler)
|
||||
|
@ -459,9 +479,10 @@ bool GLProgram::link()
|
|||
#endif
|
||||
|
||||
GLint status = GL_TRUE;
|
||||
|
||||
glLinkProgram(_program);
|
||||
|
||||
bindPredefinedVertexAttribs();
|
||||
|
||||
glLinkProgram(_program);
|
||||
|
||||
parseVertexAttribs();
|
||||
parseUniforms();
|
||||
|
|
|
@ -56,6 +56,7 @@ typedef void (*GLLogFunction) (GLuint program, GLsizei bufsize, GLsizei* length,
|
|||
class VertexAttrib
|
||||
{
|
||||
friend class GLProgram;
|
||||
friend class GLProgramState;
|
||||
friend class VertexAttribValue;
|
||||
friend class VertexAttribBind;
|
||||
|
||||
|
@ -102,9 +103,13 @@ public:
|
|||
{
|
||||
VERTEX_ATTRIB_POSITION,
|
||||
VERTEX_ATTRIB_COLOR,
|
||||
VERTEX_ATTRIB_TEX_COORDS,
|
||||
|
||||
VERTEX_ATTRIB_TEX_COORD,
|
||||
VERTEX_ATTRIB_NORMAL,
|
||||
|
||||
VERTEX_ATTRIB_MAX,
|
||||
|
||||
// backward compatibility
|
||||
VERTEX_ATTRIB_TEX_COORDS = VERTEX_ATTRIB_TEX_COORD,
|
||||
};
|
||||
|
||||
enum
|
||||
|
@ -155,6 +160,7 @@ public:
|
|||
static const char* ATTRIBUTE_NAME_COLOR;
|
||||
static const char* ATTRIBUTE_NAME_POSITION;
|
||||
static const char* ATTRIBUTE_NAME_TEX_COORD;
|
||||
static const char* ATTRIBUTE_NAME_NORMAL;
|
||||
|
||||
GLProgram();
|
||||
virtual ~GLProgram();
|
||||
|
@ -313,6 +319,8 @@ public:
|
|||
protected:
|
||||
bool updateUniformLocation(GLint location, const GLvoid* data, unsigned int bytes);
|
||||
virtual std::string getDescription() const;
|
||||
|
||||
void bindPredefinedVertexAttribs();
|
||||
void parseVertexAttribs();
|
||||
void parseUniforms();
|
||||
|
||||
|
|
|
@ -52,6 +52,10 @@ UniformValue::UniformValue(Uniform *uniform, GLProgram* glprogram)
|
|||
{
|
||||
}
|
||||
|
||||
UniformValue::~UniformValue()
|
||||
{
|
||||
}
|
||||
|
||||
void UniformValue::apply()
|
||||
{
|
||||
if(_useCallback) {
|
||||
|
@ -158,10 +162,12 @@ VertexAttribValue::VertexAttribValue(VertexAttrib *vertexAttrib)
|
|||
{
|
||||
}
|
||||
|
||||
void VertexAttribValue::apply()
|
||||
VertexAttribValue::~VertexAttribValue()
|
||||
{
|
||||
GL::enableVertexAttribs(0);
|
||||
|
||||
}
|
||||
|
||||
void VertexAttribValue::apply()
|
||||
{
|
||||
if(_useCallback) {
|
||||
_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;
|
||||
_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.type = type;
|
||||
|
@ -199,8 +205,6 @@ void VertexAttribValue::setValue(GLint size, GLenum type, GLboolean normalized,
|
|||
|
||||
GLProgramState* GLProgramState::create(GLProgram *glprogram)
|
||||
{
|
||||
CCASSERT(glprogram, "invalid shader");
|
||||
|
||||
auto ret = new (std::nothrow) GLProgramState;
|
||||
if(ret && ret->init(glprogram)) {
|
||||
ret->autorelease();
|
||||
|
@ -210,6 +214,12 @@ GLProgramState* GLProgramState::create(GLProgram *glprogram)
|
|||
return nullptr;
|
||||
}
|
||||
|
||||
GLProgramState::GLProgramState()
|
||||
: _vertexAttribsFlags(0)
|
||||
, _blendFunc(BlendFunc::ALPHA_NON_PREMULTIPLIED)
|
||||
{
|
||||
}
|
||||
|
||||
GLProgramState::~GLProgramState()
|
||||
{
|
||||
_glprogram->release();
|
||||
|
@ -217,6 +227,8 @@ GLProgramState::~GLProgramState()
|
|||
|
||||
bool GLProgramState::init(GLProgram* glprogram)
|
||||
{
|
||||
CCASSERT(glprogram, "invalid shader");
|
||||
|
||||
_glprogram = glprogram;
|
||||
_glprogram->retain();
|
||||
|
||||
|
@ -232,13 +244,22 @@ bool GLProgramState::init(GLProgram* glprogram)
|
|||
|
||||
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");
|
||||
|
||||
// set shader
|
||||
_glprogram->use();
|
||||
|
||||
_glprogram->setUniformsForBuiltins(modelView);
|
||||
|
||||
// set texture
|
||||
int i = 0;
|
||||
|
@ -250,15 +271,36 @@ void GLProgramState::apply()
|
|||
GL::blendFunc(_blendFunc.src, _blendFunc.dst);
|
||||
|
||||
|
||||
// set uniforms
|
||||
for(auto& uniform : _uniforms) {
|
||||
uniform.second.apply();
|
||||
}
|
||||
// enable/disable vertex attribs
|
||||
GL::enableVertexAttribs(_vertexAttribsFlags);
|
||||
|
||||
// set attributes
|
||||
for(auto &attribute : _attributes) {
|
||||
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)
|
||||
|
@ -277,4 +319,19 @@ VertexAttribValue* GLProgramState::getVertexAttribValue(const std::string &name)
|
|||
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
|
||||
|
|
|
@ -95,14 +95,15 @@ protected:
|
|||
class VertexAttribValue
|
||||
{
|
||||
friend class GLProgram;
|
||||
friend class GLProgramState;
|
||||
|
||||
public:
|
||||
VertexAttribValue(VertexAttrib *vertexAttrib);
|
||||
VertexAttribValue();
|
||||
~VertexAttribValue();
|
||||
|
||||
void setValue(GLint size, GLenum type, GLboolean normalized, GLsizei stride, GLvoid *pointer);
|
||||
void setValue(const std::function<void(VertexAttrib*)> callback);
|
||||
void setPointer(GLint size, GLenum type, GLboolean normalized, GLsizei stride, GLvoid *pointer);
|
||||
void setCallback(const std::function<void(VertexAttrib*)> callback);
|
||||
void apply();
|
||||
|
||||
protected:
|
||||
|
@ -137,26 +138,36 @@ protected:
|
|||
class GLProgramState : public Ref
|
||||
{
|
||||
public:
|
||||
GLProgramState* create(GLProgram* glprogram);
|
||||
static GLProgramState* create(GLProgram* glprogram);
|
||||
|
||||
void apply();
|
||||
void setTexture(Texture2D *texture) { _textures.insert(0, texture); }
|
||||
void apply(const Matrix& modelView);
|
||||
|
||||
void setGLProgram(GLProgram* glprogram);
|
||||
GLProgram* getGLProgram() const { return _glprogram; }
|
||||
|
||||
void setTexture(Texture2D *texture);
|
||||
Texture2D* getTexture() const { return _textures.at(0); }
|
||||
|
||||
void setBlendFunc(const BlendFunc& blendFunc) { _blendFunc = 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);
|
||||
VertexAttribValue* getVertexAttribValue(const std::string &attributeName);
|
||||
|
||||
protected:
|
||||
GLProgramState();
|
||||
~GLProgramState();
|
||||
bool init(GLProgram* program);
|
||||
void resetGLProgram();
|
||||
VertexAttribValue* getVertexAttribValue(const std::string &attributeName);
|
||||
|
||||
std::unordered_map<std::string, UniformValue> _uniforms;
|
||||
std::unordered_map<std::string, VertexAttribValue> _attributes;
|
||||
|
||||
|
||||
uint32_t _vertexAttribsFlags;
|
||||
|
||||
GLProgram *_glprogram;
|
||||
Vector<Texture2D*> _textures;
|
||||
BlendFunc _blendFunc;
|
||||
|
|
|
@ -330,7 +330,7 @@ void Grid3D::blit(void)
|
|||
|
||||
// texCoords
|
||||
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);
|
||||
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);
|
||||
|
||||
// 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);
|
||||
#endif // EMSCRIPTEN
|
||||
|
@ -545,7 +545,7 @@ void TiledGrid3D::blit(void)
|
|||
|
||||
// texCoords
|
||||
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);
|
||||
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);
|
||||
|
||||
// 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);
|
||||
#endif // EMSCRIPTEN
|
||||
|
|
|
@ -389,13 +389,13 @@ void MotionStreak::onDraw(const Matrix &transform, bool transformUpdated)
|
|||
glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 2, GL_FLOAT, GL_FALSE, 0, 0);
|
||||
|
||||
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);
|
||||
glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_COLOR, 4, GL_UNSIGNED_BYTE, GL_TRUE, 0, 0);
|
||||
#else
|
||||
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);
|
||||
#endif // EMSCRIPTEN
|
||||
|
||||
|
|
|
@ -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));
|
||||
|
||||
// tex coords
|
||||
glEnableVertexAttribArray(GLProgram::VERTEX_ATTRIB_TEX_COORDS);
|
||||
glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_TEX_COORDS, 2, GL_FLOAT, GL_FALSE, kQuadSize, (GLvoid*) offsetof( V3F_C4B_T2F, texCoords));
|
||||
glEnableVertexAttribArray(GLProgram::VERTEX_ATTRIB_TEX_COORD);
|
||||
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]);
|
||||
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(_indices[0]) * _totalParticles * 6, _indices, GL_STATIC_DRAW);
|
||||
|
|
|
@ -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);
|
||||
|
||||
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
|
||||
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);
|
||||
#endif // EMSCRIPTEN
|
||||
|
||||
|
|
|
@ -283,118 +283,51 @@ void ShaderCache::loadDefaultShader(GLProgram *p, int type)
|
|||
switch (type) {
|
||||
case kShaderType_PositionTextureColor:
|
||||
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;
|
||||
case kShaderType_PositionTextureColor_noMVP:
|
||||
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;
|
||||
|
||||
case kShaderType_PositionTextureColorAlphaTest:
|
||||
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;
|
||||
case kShaderType_PositionTextureColorAlphaTestNoMV:
|
||||
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;
|
||||
|
||||
case kShaderType_PositionColor:
|
||||
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;
|
||||
case kShaderType_PositionColor_noMVP:
|
||||
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;
|
||||
case kShaderType_PositionTexture:
|
||||
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;
|
||||
case kShaderType_PositionTexture_uColor:
|
||||
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;
|
||||
case kShaderType_PositionTextureA8Color:
|
||||
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;
|
||||
case kShaderType_Position_uColor:
|
||||
p->initWithByteArrays(ccPosition_uColor_vert, ccPosition_uColor_frag);
|
||||
|
||||
p->bindAttribLocation("aVertex", GLProgram::VERTEX_ATTRIB_POSITION);
|
||||
|
||||
break;
|
||||
case kShaderType_PositionLengthTexureColor:
|
||||
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;
|
||||
case kShaderType_LabelDistanceFieldNormal:
|
||||
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;
|
||||
case kShaderType_LabelDistanceFieldGlow:
|
||||
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;
|
||||
case kShaderType_LabelNormal:
|
||||
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;
|
||||
case kShaderType_LabelOutline:
|
||||
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;
|
||||
default:
|
||||
CCLOG("cocos2d: %s:%d, error shader type", __FUNCTION__, __LINE__);
|
||||
|
|
|
@ -1181,10 +1181,10 @@ void Texture2D::drawAtPoint(const Vector2& point)
|
|||
glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 2, GL_FLOAT, GL_FALSE, 0, 0);
|
||||
|
||||
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
|
||||
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
|
||||
|
||||
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);
|
||||
|
||||
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
|
||||
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
|
||||
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
|
||||
}
|
||||
|
|
|
@ -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));
|
||||
|
||||
// tex coords
|
||||
glEnableVertexAttribArray(GLProgram::VERTEX_ATTRIB_TEX_COORDS);
|
||||
glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_TEX_COORDS, 2, GL_FLOAT, GL_FALSE, kQuadSize, (GLvoid*) offsetof( V3F_C4B_T2F, texCoords));
|
||||
glEnableVertexAttribArray(GLProgram::VERTEX_ATTRIB_TEX_COORD);
|
||||
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]);
|
||||
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));
|
||||
|
||||
// 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]);
|
||||
|
||||
|
|
|
@ -33,17 +33,18 @@ THE SOFTWARE.
|
|||
|
||||
NS_CC_BEGIN
|
||||
|
||||
static const int MAX_ATTRIBUTES = 16;
|
||||
static const int MAX_ACTIVE_TEXTURE = 16;
|
||||
|
||||
namespace
|
||||
{
|
||||
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
|
||||
|
||||
#define kMaxActiveTexture 16
|
||||
|
||||
|
||||
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_blendingDest = -1;
|
||||
static int s_GLServerState = 0;
|
||||
|
@ -65,7 +66,7 @@ void invalidateStateCache( void )
|
|||
|
||||
#if CC_ENABLE_GL_STATE_CACHE
|
||||
s_currentShaderProgram = -1;
|
||||
for( int i=0; i < kMaxActiveTexture; i++ )
|
||||
for( int i=0; i < MAX_ACTIVE_TEXTURE; i++ )
|
||||
{
|
||||
s_currentBoundTexture[i] = -1;
|
||||
}
|
||||
|
@ -147,7 +148,7 @@ void bindTexture2D(GLuint textureId)
|
|||
void bindTexture2DN(GLuint textureUnit, GLuint textureId)
|
||||
{
|
||||
#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)
|
||||
{
|
||||
s_currentBoundTexture[textureUnit] = textureId;
|
||||
|
@ -210,12 +211,12 @@ void bindVAO(GLuint vaoId)
|
|||
|
||||
// GL Vertex Attrib functions
|
||||
|
||||
void enableVertexAttribs( unsigned int flags )
|
||||
void enableVertexAttribs(uint32_t flags)
|
||||
{
|
||||
bindVAO(0);
|
||||
|
||||
// hardcoded!
|
||||
for(int i=0; i < 16; i++) {
|
||||
for(int i=0; i < MAX_ATTRIBUTES; i++) {
|
||||
unsigned int bit = 1 << i;
|
||||
bool enabled = flags & bit;
|
||||
bool enabledBefore = s_attributeFlags & bit;
|
||||
|
|
|
@ -102,7 +102,7 @@ void CC_DLL setProjectionMatrixDirty(void);
|
|||
|
||||
@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 CC_ENABLE_GL_STATE_CACHE is disabled, it will call glBindTexture() directly.
|
||||
|
|
|
@ -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_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 kCCUniformPMatrix = GLProgram::UNIFORM_P_MATRIX;
|
||||
|
|
|
@ -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));
|
||||
|
||||
// tex coords
|
||||
glEnableVertexAttribArray(GLProgram::VERTEX_ATTRIB_TEX_COORDS);
|
||||
glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_TEX_COORDS, 2, GL_FLOAT, GL_FALSE, sizeof(V3F_C4B_T2F), (GLvoid*) offsetof( V3F_C4B_T2F, texCoords));
|
||||
glEnableVertexAttribArray(GLProgram::VERTEX_ATTRIB_TEX_COORD);
|
||||
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]);
|
||||
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));
|
||||
|
||||
// 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]);
|
||||
}
|
||||
|
|
|
@ -131,7 +131,7 @@ cc.VERTEX_ATTRIB_FLAG_TEX_COORDS = 0x4
|
|||
cc.VERTEX_ATTRIB_COLOR = 0x1
|
||||
cc.VERTEX_ATTRIB_MAX = 0x3
|
||||
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_CENTER = 0x1
|
||||
cc.VERTICAL_TEXT_ALIGNMENT_TOP = 0x0
|
||||
|
|
|
@ -1027,7 +1027,7 @@ void MySprite::onDraw(const Matrix &transform, bool transformUpdated)
|
|||
|
||||
// texCoods
|
||||
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
|
||||
diff = offsetof( V3F_C4B_T2F, colors);
|
||||
|
|
|
@ -116,6 +116,7 @@ ShaderNode::ShaderNode()
|
|||
|
||||
ShaderNode::~ShaderNode()
|
||||
{
|
||||
CC_SAFE_RELEASE(_glProgramState);
|
||||
}
|
||||
|
||||
ShaderNode* ShaderNode::shaderNodeWithVertex(const char *vert, const char *frag)
|
||||
|
@ -142,6 +143,7 @@ bool ShaderNode::initWithVertex(const char *vert, const char *frag)
|
|||
|
||||
_time = 0;
|
||||
_resolution = Vector2(SIZE_X, SIZE_Y);
|
||||
_glProgramState->getUniformValue("resolution")->setValue(_resolution);
|
||||
|
||||
scheduleUpdate();
|
||||
|
||||
|
@ -164,6 +166,9 @@ void ShaderNode::loadShaderVertex(const char *vert, const char *frag)
|
|||
shader->updateUniforms();
|
||||
|
||||
this->setShaderProgram(shader);
|
||||
|
||||
_glProgramState = GLProgramState::create(shader);
|
||||
_glProgramState->retain();
|
||||
}
|
||||
|
||||
void ShaderNode::update(float dt)
|
||||
|
@ -176,6 +181,7 @@ void ShaderNode::setPosition(const Vector2 &newPosition)
|
|||
Node::setPosition(newPosition);
|
||||
auto position = getPosition();
|
||||
_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)
|
||||
|
@ -187,25 +193,16 @@ void ShaderNode::draw(Renderer *renderer, const Matrix &transform, bool transfor
|
|||
|
||||
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;
|
||||
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);
|
||||
|
||||
CC_INCREMENT_GL_DRAWN_BATCHES_AND_VERTICES(1,6);
|
||||
|
||||
}
|
||||
|
||||
/// ShaderMonjori
|
||||
|
@ -435,10 +432,12 @@ protected:
|
|||
float _weightSum;
|
||||
|
||||
CustomCommand _customCommand;
|
||||
GLProgramState *_glProgramState;
|
||||
};
|
||||
|
||||
SpriteBlur::~SpriteBlur()
|
||||
{
|
||||
CC_SAFE_RELEASE(_glProgramState);
|
||||
}
|
||||
|
||||
SpriteBlur* SpriteBlur::create(const char *pszFileName)
|
||||
|
@ -473,9 +472,12 @@ bool SpriteBlur::initWithTexture(Texture2D* texture, const Rect& rect)
|
|||
auto s = getTexture()->getContentSizeInPixels();
|
||||
|
||||
_pixelSize = Vector2(1/s.width, 1/s.height);
|
||||
|
||||
_samplingRadius = 0;
|
||||
this->initProgram();
|
||||
|
||||
_glProgramState->getUniformValue("onePixelSize")->setValue(_pixelSize);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
@ -490,20 +492,30 @@ void SpriteBlur::initProgram()
|
|||
setShaderProgram(program);
|
||||
|
||||
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();
|
||||
|
||||
CHECK_GL_ERROR_DEBUG();
|
||||
|
||||
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)
|
||||
|
@ -515,38 +527,11 @@ void SpriteBlur::draw(Renderer *renderer, const Matrix &transform, bool transfor
|
|||
|
||||
void SpriteBlur::onDraw(const Matrix &transform, bool transformUpdated)
|
||||
{
|
||||
GL::enableVertexAttribs(cocos2d::GL::VERTEX_ATTRIB_FLAG_POS_COLOR_TEX );
|
||||
BlendFunc blend = getBlendFunc();
|
||||
GL::blendFunc(blend.src, blend.dst);
|
||||
_glProgramState->setTexture(getTexture());
|
||||
_glProgramState->setBlendFunc(getBlendFunc());
|
||||
|
||||
_glProgramState->apply(transform);
|
||||
|
||||
auto program = getShaderProgram();
|
||||
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);
|
||||
|
||||
CC_INCREMENT_GL_DRAWN_BATCHES_AND_VERTICES(1,4);
|
||||
|
@ -585,6 +570,8 @@ void SpriteBlur::setBlurSize(float f)
|
|||
}
|
||||
}
|
||||
log("_blurRadius:%d",_blurRadius);
|
||||
|
||||
_glProgramState->getUniformValue("gaussianCoefficient")->setValue(Vector4(_samplingRadius, _scale, _cons, _weightSum));
|
||||
}
|
||||
|
||||
// ShaderBlur
|
||||
|
@ -670,7 +657,7 @@ bool ShaderRetroEffect::init()
|
|||
auto p = GLProgram::createWithByteArrays(ccPositionTexture_vert, fragSource);
|
||||
|
||||
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->updateUniforms();
|
||||
|
|
|
@ -5,6 +5,8 @@
|
|||
#include "extensions/cocos-ext.h"
|
||||
#include "../BaseTest.h"
|
||||
|
||||
#include "2d/CCGLProgramState.h"
|
||||
|
||||
USING_NS_CC_EXT;
|
||||
|
||||
class ShaderTestDemo : public BaseTest
|
||||
|
@ -134,6 +136,7 @@ protected:
|
|||
std::string _vertFileName;
|
||||
std::string _fragFileName;
|
||||
CustomCommand _customCommand;
|
||||
GLProgramState *_glProgramState;
|
||||
};
|
||||
|
||||
class ShaderTestScene : public TestScene
|
||||
|
|
|
@ -158,7 +158,7 @@ local function OpenGLTestMainLayer()
|
|||
InitTitle(RetroEffectlayer)
|
||||
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_TEX_COORD, cc.VERTEX_ATTRIB_TEX_COORDS)
|
||||
program:bindAttribLocation(cc.ATTRIBUTE_NAME_TEX_COORD, cc.VERTEX_ATTRIB_TEX_COORD)
|
||||
program:link()
|
||||
program:updateUniforms()
|
||||
|
||||
|
@ -653,7 +653,7 @@ local function OpenGLTestMainLayer()
|
|||
|
||||
|
||||
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)
|
||||
|
||||
|
|
Loading…
Reference in New Issue