closed #5369: refactor Effect3d/EffectSprite3D framework to the pattern similar to Effect/EffectSprite

This commit is contained in:
Huabing.Xu 2014-05-27 14:50:46 +08:00
parent ba7c2cc133
commit 453ff81d2a
2 changed files with 44 additions and 24 deletions

View File

@ -258,9 +258,10 @@ void EffectSprite3D::addEffect(Effect3DOutline* effect, ssize_t order)
{
if(nullptr == effect) return;
effect->retain();
effect->setTarget(this);
_effects.push_back(std::make_tuple(order,effect,CustomCommand()));
std::sort(std::begin(_effects), std::end(_effects), tuple_sort);
}
@ -317,12 +318,14 @@ bool Effect3DOutline::init()
Effect3DOutline::Effect3DOutline()
: _outlineWidth(1.0f)
, _outlineColor(1, 1, 1)
, _sprite(nullptr)
{
}
Effect3DOutline::~Effect3DOutline()
{
CC_SAFE_RELEASE_NULL(_sprite);
}
void Effect3DOutline::setOutlineColor(const Vec3& color)
@ -343,33 +346,48 @@ void Effect3DOutline::setOutlineWidth(float width)
}
}
void Effect3DOutline::drawWithSprite(EffectSprite3D* sprite, const Mat4 &transform)
void Effect3DOutline::setTarget(EffectSprite3D *sprite)
{
auto mesh = sprite->getMesh();
long offset = 0;
for (auto i = 0; i < mesh->getMeshVertexAttribCount(); i++)
CCASSERT(nullptr != sprite || nullptr != sprite->getMesh(),"Error: Setting a null pointer or a null mesh EffectSprite3D to Effect3D");
if(sprite != _sprite)
{
auto meshvertexattrib = mesh->getMeshVertexAttribute(i);
CC_SAFE_RETAIN(sprite);
CC_SAFE_RELEASE_NULL(_sprite);
_sprite = sprite;
_glProgramState->setVertexAttribPointer(s_attributeNames[meshvertexattrib.vertexAttrib],
meshvertexattrib.size,
meshvertexattrib.type,
GL_FALSE,
mesh->getVertexSizeInBytes(),
(void*)offset);
offset += meshvertexattrib.attribSizeBytes;
auto mesh = sprite->getMesh();
long offset = 0;
for (auto i = 0; i < mesh->getMeshVertexAttribCount(); i++)
{
auto meshvertexattrib = mesh->getMeshVertexAttribute(i);
_glProgramState->setVertexAttribPointer(s_attributeNames[meshvertexattrib.vertexAttrib],
meshvertexattrib.size,
meshvertexattrib.type,
GL_FALSE,
mesh->getVertexSizeInBytes(),
(void*)offset);
offset += meshvertexattrib.attribSizeBytes;
}
Color4F color(_sprite->getDisplayedColor());
color.a = _sprite->getDisplayedOpacity() / 255.0f;
_glProgramState->setUniformVec4("u_color", Vec4(color.r, color.g, color.b, color.a));
}
}
void Effect3DOutline::draw(const Mat4 &transform)
{
//draw
if(_sprite && _sprite->getMesh())
{
glEnable(GL_CULL_FACE);
glCullFace(GL_FRONT);
glEnable(GL_DEPTH_TEST);
Color4F color(sprite->getDisplayedColor());
color.a = sprite->getDisplayedOpacity() / 255.0f;
_glProgramState->setUniformVec4("u_color", Vec4(color.r, color.g, color.b, color.a));
auto mesh = sprite->getMesh();
auto mesh = _sprite->getMesh();
glBindBuffer(GL_ARRAY_BUFFER, mesh->getVertexBuffer());
_glProgramState->apply(transform);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mesh->getIndexBuffer());
@ -390,7 +408,7 @@ void EffectSprite3D::draw(cocos2d::Renderer *renderer, const cocos2d::Mat4 &tran
if(std::get<0>(effect) >=0)
break;
CustomCommand &cc = std::get<2>(effect);
cc.func = CC_CALLBACK_0(Effect3D::drawWithSprite,std::get<1>(effect),this,transform);
cc.func = CC_CALLBACK_0(Effect3D::draw,std::get<1>(effect),transform);
renderer->addCommand(&cc);
}
@ -402,7 +420,7 @@ void EffectSprite3D::draw(cocos2d::Renderer *renderer, const cocos2d::Mat4 &tran
else
{
_command.init(_globalZOrder);
_command.func = CC_CALLBACK_0(Effect3D::drawWithSprite, _defaultEffect, this, transform);
_command.func = CC_CALLBACK_0(Effect3D::draw, _defaultEffect, transform);
renderer->addCommand(&_command);
}
@ -411,7 +429,7 @@ void EffectSprite3D::draw(cocos2d::Renderer *renderer, const cocos2d::Mat4 &tran
if(std::get<0>(effect) <=0)
continue;
CustomCommand &cc = std::get<2>(effect);
cc.func = CC_CALLBACK_0(Effect3D::drawWithSprite,std::get<1>(effect),this,transform);
cc.func = CC_CALLBACK_0(Effect3D::draw,std::get<1>(effect),transform);
renderer->addCommand(&cc);
}

View File

@ -65,7 +65,8 @@ class EffectSprite3D;
class Effect3D : public Ref
{
public:
virtual void drawWithSprite(EffectSprite3D* sprite, const Mat4 &transform) = 0;
virtual void draw(const Mat4 &transform) = 0;
virtual void setTarget(EffectSprite3D *sprite) = 0;
protected:
Effect3D() : _glProgramState(nullptr) {}
virtual ~Effect3D()
@ -85,8 +86,8 @@ public:
void setOutlineWidth(float width);
void drawWithSprite(EffectSprite3D* sprite, const Mat4 &transform);
virtual void draw(const Mat4 &transform) override;
virtual void setTarget(EffectSprite3D *sprite) override;
protected:
Effect3DOutline();
@ -96,6 +97,7 @@ protected:
Vec3 _outlineColor;
float _outlineWidth;
EffectSprite3D* _sprite;
protected:
static const std::string _vertShaderFile;