From 5b05ab6353e2e2038e1794c8cfc5fb0484b8b262 Mon Sep 17 00:00:00 2001 From: yangxiao Date: Wed, 11 Jun 2014 14:05:35 +0800 Subject: [PATCH] fix rendering bug --- cocos/renderer/CCGLProgramState.cpp | 8 ++++---- cocos/renderer/CCGLProgramState.h | 6 +++--- cocos/renderer/CCMeshCommand.cpp | 16 +++++++++------- cocos/renderer/CCMeshCommand.h | 4 ++++ 4 files changed, 20 insertions(+), 14 deletions(-) diff --git a/cocos/renderer/CCGLProgramState.cpp b/cocos/renderer/CCGLProgramState.cpp index eadae38e75..68b6c27fb9 100644 --- a/cocos/renderer/CCGLProgramState.cpp +++ b/cocos/renderer/CCGLProgramState.cpp @@ -65,7 +65,7 @@ UniformValue::~UniformValue() void UniformValue::apply() { if(_useCallback) { - (*_value.callback)(_uniform); + (*_value.callback)(_glprogram, _uniform); } else { @@ -106,7 +106,7 @@ void UniformValue::apply() } } -void UniformValue::setCallback(const std::function &callback) +void UniformValue::setCallback(const std::function &callback) { // delete previously set callback // XXX TODO: memory will leak if the user does: @@ -115,7 +115,7 @@ void UniformValue::setCallback(const std::function &callback) if (_useCallback) delete _value.callback; - _value.callback = new std::function(); + _value.callback = new std::function(); *_value.callback = callback; _useCallback = true; @@ -390,7 +390,7 @@ void GLProgramState::setVertexAttribPointer(const std::string &name, GLint size, // Uniform Setters -void GLProgramState::setUniformCallback(const std::string &uniformName, const std::function &callback) +void GLProgramState::setUniformCallback(const std::string &uniformName, const std::function &callback) { auto v = getUniformValue(uniformName); if (v) diff --git a/cocos/renderer/CCGLProgramState.h b/cocos/renderer/CCGLProgramState.h index babfed8fed..11dd923d05 100644 --- a/cocos/renderer/CCGLProgramState.h +++ b/cocos/renderer/CCGLProgramState.h @@ -60,7 +60,7 @@ public: void setVec3(const Vec3& value); void setVec4(const Vec4& value); void setMat4(const Mat4& value); - void setCallback(const std::function &callback); + void setCallback(const std::function &callback); void setTexture(GLuint textureId, GLuint activeTexture); void apply(); @@ -81,7 +81,7 @@ protected: GLuint textureId; GLuint textureUnit; } tex; - std::function *callback; + std::function *callback; U() { memset( this, 0, sizeof(*this) ); } ~U(){} @@ -174,7 +174,7 @@ public: void setUniformVec3(const std::string &uniformName, const Vec3& value); void setUniformVec4(const std::string &uniformName, const Vec4& value); void setUniformMat4(const std::string &uniformName, const Mat4& value); - void setUniformCallback(const std::string &uniformName, const std::function &callback); + void setUniformCallback(const std::string &uniformName, const std::function &callback); void setUniformTexture(const std::string &uniformName, Texture2D *texture); void setUniformTexture(const std::string &uniformName, GLuint textureId); diff --git a/cocos/renderer/CCMeshCommand.cpp b/cocos/renderer/CCMeshCommand.cpp index 2269b05169..6b9dda2fbc 100644 --- a/cocos/renderer/CCMeshCommand.cpp +++ b/cocos/renderer/CCMeshCommand.cpp @@ -31,6 +31,7 @@ #include "renderer/CCRenderer.h" #include "renderer/CCTextureAtlas.h" #include "renderer/CCTexture2D.h" +#include "renderer/ccGLStateCache.h" NS_CC_BEGIN @@ -137,6 +138,11 @@ void MeshCommand::restoreRenderState() } } +void MeshCommand::MatrixPalleteCallBack( GLProgram* glProgram, Uniform* uniform) +{ + glProgram->setUniformLocationWith4fv(uniform->location, (const float*)_matrixPalette, _matrixPaletteSize); +} + void MeshCommand::execute() { // set render state @@ -148,15 +154,11 @@ void MeshCommand::execute() glBindBuffer(GL_ARRAY_BUFFER, _vertexBuffer); _glProgramState->setUniformVec4("u_color", _displayColor); + if (_matrixPaletteSize && _matrixPalette) { - auto glProgram = _glProgramState->getGLProgram(); - - auto uniform = glProgram->getUniform("u_matrixPalette"); - if (uniform) - { - glProgram->setUniformLocationWith4fv(uniform->location, (const float*)_matrixPalette, _matrixPaletteSize); - } + _glProgramState->setUniformCallback("u_matrixPalette", CC_CALLBACK_2(MeshCommand::MatrixPalleteCallBack, this)); + } _glProgramState->apply(_mv); diff --git a/cocos/renderer/CCMeshCommand.h b/cocos/renderer/CCMeshCommand.h index 2ba680cf72..031d8b9a51 100644 --- a/cocos/renderer/CCMeshCommand.h +++ b/cocos/renderer/CCMeshCommand.h @@ -33,6 +33,8 @@ NS_CC_BEGIN class GLProgramState; +class GLProgram; +class Uniform; //it is a common mesh class MeshCommand : public RenderCommand @@ -66,6 +68,8 @@ protected: //restore to all false void restoreRenderState(); + + void MatrixPalleteCallBack( GLProgram* glProgram, Uniform* uniform); GLuint _textureID; GLProgramState* _glProgramState;