fix rendering bug

This commit is contained in:
yangxiao 2014-06-11 14:05:35 +08:00
parent cddad40ec0
commit 5b05ab6353
4 changed files with 20 additions and 14 deletions

View File

@ -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<void(Uniform*)> &callback)
void UniformValue::setCallback(const std::function<void(GLProgram*, Uniform*)> &callback)
{
// delete previously set callback
// XXX TODO: memory will leak if the user does:
@ -115,7 +115,7 @@ void UniformValue::setCallback(const std::function<void(Uniform*)> &callback)
if (_useCallback)
delete _value.callback;
_value.callback = new std::function<void(Uniform*)>();
_value.callback = new std::function<void(GLProgram*, Uniform*)>();
*_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<void(Uniform*)> &callback)
void GLProgramState::setUniformCallback(const std::string &uniformName, const std::function<void(GLProgram*, Uniform*)> &callback)
{
auto v = getUniformValue(uniformName);
if (v)

View File

@ -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<void(Uniform*)> &callback);
void setCallback(const std::function<void(GLProgram*, Uniform*)> &callback);
void setTexture(GLuint textureId, GLuint activeTexture);
void apply();
@ -81,7 +81,7 @@ protected:
GLuint textureId;
GLuint textureUnit;
} tex;
std::function<void(Uniform*)> *callback;
std::function<void(GLProgram*, Uniform*)> *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<void(Uniform*)> &callback);
void setUniformCallback(const std::string &uniformName, const std::function<void(GLProgram*, Uniform*)> &callback);
void setUniformTexture(const std::string &uniformName, Texture2D *texture);
void setUniformTexture(const std::string &uniformName, GLuint textureId);

View File

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

View File

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