Skip to content

Commit a75c8a8

Browse files
minggohuangwei1024
authored andcommitted
fix crash of cclayercolor and fix some other error logic (cocos2d#19177)
1 parent 1d41cc8 commit a75c8a8

File tree

11 files changed

+41
-23
lines changed

11 files changed

+41
-23
lines changed

cocos/2d/CCLayer.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -620,13 +620,13 @@ void LayerColor::onDraw(const Mat4& transform, uint32_t /*flags*/)
620620
getGLProgram()->use();
621621
getGLProgram()->setUniformsForBuiltins(transform);
622622

623-
glEnableVertexAttribArray(GLProgram::VERTEX_ATTRIB_POSITION);
624-
glEnableVertexAttribArray(GLProgram::VERTEX_ATTRIB_COLOR);
625-
626623
//
627624
// Attributes
628625
//
629626
glBindBuffer(GL_ARRAY_BUFFER, 0);
627+
glEnableVertexAttribArray(GLProgram::VERTEX_ATTRIB_POSITION);
628+
glDisableVertexAttribArray(GLProgram::VERTEX_ATTRIB_TEX_COORD);
629+
glEnableVertexAttribArray(GLProgram::VERTEX_ATTRIB_COLOR);
630630
glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 3, GL_FLOAT, GL_FALSE, 0, _noMVPVertices);
631631
glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_COLOR, 4, GL_FLOAT, GL_FALSE, 0, _squareColors);
632632

cocos/2d/CCMotionStreak.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -390,6 +390,12 @@ void MotionStreak::onDraw(const Mat4 &transform, uint32_t /*flags*/)
390390

391391
glActiveTexture(GL_TEXTURE0);
392392
glBindTexture(GL_TEXTURE_2D, _texture->getName());
393+
auto alphaTexID = _texture->getAlphaTextureName();
394+
if (alphaTexID > 0)
395+
{
396+
glActiveTexture(GL_TEXTURE0 + 1);
397+
glBindTexture(GL_TEXTURE_2D, alphaTexID);
398+
}
393399

394400
glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 2, GL_FLOAT, GL_FALSE, 0, _vertices);
395401
glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_TEX_COORD, 2, GL_FLOAT, GL_FALSE, 0, _texCoords);

cocos/2d/CCProgressTimer.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -517,6 +517,12 @@ void ProgressTimer::onDraw(const Mat4 &transform, uint32_t /*flags*/)
517517

518518
glActiveTexture(GL_TEXTURE0);
519519
glBindTexture(GL_TEXTURE_2D, _sprite->getTexture()->getName());
520+
auto alphaTexID = _sprite->getTexture()->getAlphaTextureName();
521+
if (alphaTexID > 0)
522+
{
523+
glActiveTexture(GL_TEXTURE0 + 1);
524+
glBindTexture(GL_TEXTURE_2D, alphaTexID);
525+
}
520526

521527
glVertexAttribPointer( GLProgram::VERTEX_ATTRIB_POSITION, 2, GL_FLOAT, GL_FALSE, sizeof(_vertexData[0]) , &_vertexData[0].vertices);
522528
glVertexAttribPointer( GLProgram::VERTEX_ATTRIB_TEX_COORD, 2, GL_FLOAT, GL_FALSE, sizeof(_vertexData[0]), &_vertexData[0].texCoords);

cocos/base/ccUtils.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -544,6 +544,18 @@ void setBlending(GLenum sfactor, GLenum dfactor)
544544
RenderState::StateBlock::_defaultState->setBlendDst((RenderState::Blend)dfactor);
545545
}
546546
}
547+
548+
void enableVertexAttributes(uint32_t flags)
549+
{
550+
for (int i = 0; flags > 0; i++)
551+
{
552+
int flag = 1 << i;
553+
if (flag & flags)
554+
glEnableVertexAttribArray(i);
555+
556+
flags &= ~flag;
557+
}
558+
}
547559

548560
}
549561

cocos/base/ccUtils.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,7 @@ namespace utils
190190
CC_DLL LanguageType getLanguageTypeByISO2(const char* code);
191191

192192
CC_DLL void setBlending(GLenum sfactor, GLenum dfactor);
193+
CC_DLL void enableVertexAttributes(uint32_t flags);
193194
}
194195

195196
NS_CC_END

cocos/renderer/CCTextureAtlas.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -614,6 +614,12 @@ void TextureAtlas::drawNumberOfQuads(ssize_t numberOfQuads, ssize_t start)
614614

615615
glActiveTexture(GL_TEXTURE0);
616616
glBindTexture(GL_TEXTURE_2D, _texture->getName());
617+
auto alphaTexID = _texture->getAlphaTextureName();
618+
if (alphaTexID > 0)
619+
{
620+
glActiveTexture(GL_TEXTURE0 + 1);
621+
glBindTexture(GL_TEXTURE_2D, alphaTexID);
622+
}
617623

618624
auto conf = Configuration::getInstance();
619625
if (conf->supportsShareableVAO() && conf->supportsMapBuffer())

cocos/renderer/CCVertexAttribBinding.cpp

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include "platform/CCGL.h"
2626
#include "base/CCConfiguration.h"
2727
#include "3d/CCMeshVertexIndexData.h"
28+
#include "base/ccUtils.h"
2829

2930
NS_CC_BEGIN
3031

@@ -147,7 +148,7 @@ bool VertexAttribBinding::init(MeshIndexData* meshIndexData, GLProgramState* glP
147148
glBindVertexArray(_handle);
148149
glBindBuffer(GL_ARRAY_BUFFER, meshVertexData->getVertexBuffer()->getVBO());
149150

150-
enableVertexAttributes(_vertexAttribsFlags);
151+
utils::enableVertexAttributes(_vertexAttribsFlags);
151152

152153
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, meshIndexData->getIndexBuffer()->getVBO());
153154

@@ -180,7 +181,7 @@ void VertexAttribBinding::bind()
180181
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _meshIndexData->getIndexBuffer()->getVBO());
181182

182183
// Software mode
183-
enableVertexAttributes(_vertexAttribsFlags);
184+
utils::enableVertexAttributes(_vertexAttribsFlags);
184185
// set attributes
185186
for(auto &attribute : _attributes)
186187
{
@@ -226,19 +227,6 @@ void VertexAttribBinding::parseAttributes()
226227
}
227228
}
228229

229-
void VertexAttribBinding::enableVertexAttributes(uint32_t flags) const
230-
{
231-
auto tmpFlags = flags;
232-
for (int i = 0; tmpFlags > 0; i++)
233-
{
234-
int flag = 1 << i;
235-
if (flag & tmpFlags)
236-
glEnableVertexAttribArray(i);
237-
238-
tmpFlags &= ~flag;
239-
}
240-
}
241-
242230
VertexAttribValue* VertexAttribBinding::getVertexAttribValue(const std::string& name)
243231
{
244232
const auto itr = _attributes.find(name);

cocos/renderer/CCVertexAttribBinding.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,8 +108,6 @@ class CC_DLL VertexAttribBinding : public Ref
108108
void setVertexAttribPointer(const std::string& name, GLint size, GLenum type, GLboolean normalized, GLsizei stride, GLvoid* pointer);
109109
VertexAttribValue* getVertexAttribValue(const std::string &name);
110110
void parseAttributes();
111-
void enableVertexAttributes(uint32_t flags) const;
112-
113111

114112
GLuint _handle;
115113

cocos/scripting/lua-bindings/manual/cocos2d/LuaOpengl.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
#include "2d/CCDrawingPrimitives.h"
3737
#include "renderer/CCRenderer.h"
3838
#include "platform/CCGL.h"
39+
#include "base/ccUtils.h"
3940

4041
using namespace cocos2d;
4142

@@ -4405,7 +4406,7 @@ static int tolua_Cocos2d_glEnableVertexAttribs00(lua_State* tolua_S)
44054406
#endif
44064407
{
44074408
int arg0 = (int)tolua_tonumber(tolua_S, 1, 0);
4408-
glEnableVertexAttribArray(arg0);
4409+
cocos2d::utils::enableVertexAttributes(arg0);
44094410
}
44104411
return 0;
44114412
#ifndef TOLUA_RELEASE

tests/js-tests/project/Classes/js_DrawNode3D_bindings.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ void DrawNode3D::onDraw(const Mat4 &transform, uint32_t flags)
224224
glProgram->use();
225225
glProgram->setUniformsForBuiltins(transform);
226226
glEnable(GL_DEPTH_TEST);
227-
glBlendFunc(_blendFunc.src, _blendFunc.dst);
227+
cocos2d::utils::setBlending(_blendFunc.src, _blendFunc.dst);
228228

229229
if (_dirty)
230230
{

0 commit comments

Comments
 (0)