/****************************************************************************
Copyright (c) 2014 Chukong Technologies Inc.
http://www.cocos2d-x.org
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
#include "ShaderTest2.h"
#include "ShaderTest.h"
#include "../testResource.h"
#include "cocos2d.h"
#include <tuple>
USING_NS_CC;
Shader2Tests::Shader2Tests()
{
ADD_TEST_CASE(EffectSpriteTest);
ADD_TEST_CASE(EffectSpriteLamp);
}
//
// EffectSprite
//
static int tuple_sort( const std::tuple<ssize_t,Effect*,QuadCommand> &tuple1, const std::tuple<ssize_t,Effect*,QuadCommand> &tuple2 )
{
return std::get<0>(tuple1) < std::get<0>(tuple2);
}
class Effect;
class EffectSprite : public Sprite
{
public:
static EffectSprite *create(const std::string& filename) {
auto ret = new (std::nothrow) EffectSprite;
if(ret && ret->initWithFile(filename)) {
ret->autorelease();
return ret;
}
CC_SAFE_RELEASE(ret);
return nullptr;
}
void setEffect(Effect* effect) {
if(_defaultEffect != effect) {
effect->setTarget(this);
CC_SAFE_RELEASE(_defaultEffect);
_defaultEffect = effect;
CC_SAFE_RETAIN(_defaultEffect);
setGLProgramState(_defaultEffect->getGLProgramState());
}
}
void addEffect(Effect *effect, ssize_t order) {
effect->retain();
effect->setTarget(this);
_effects.push_back(std::make_tuple(order,effect,QuadCommand()));
std::sort(std::begin(_effects), std::end(_effects), tuple_sort);
}
void draw(Renderer *renderer, const Mat4 &transform, uint32_t flags) override
{
#if CC_USE_CULLING
// Don't do calculate the culling if the transform was not updated
_insideBounds = (flags & FLAGS_TRANSFORM_DIRTY) ? renderer->checkVisibility(transform, _contentSize) : _insideBounds;
if(_insideBounds)
#endif
{
// negative effects: order < 0
int idx=0;
for(auto &effect : _effects) {
if(std::get<0>(effect) >=0)
break;
QuadCommand &q = std::get<2>(effect);
q.init(_globalZOrder, _texture->getName(), std::get<1>(effect)->getGLProgramState(), _blendFunc, &_quad, 1, transform, flags);
renderer->addCommand(&q);
idx++;
}
// normal effect: order == 0
_trianglesCommand.init(_globalZOrder, _texture->getName(), getGLProgramState(), _blendFunc, _polyInfo.triangles, transform, flags);
renderer->addCommand(&_trianglesCommand);
// postive effects: oder >= 0
for(auto it = std::begin(_effects)+idx; it != std::end(_effects); ++it) {
QuadCommand &q = std::get<2>(*it);
q.init(_globalZOrder, _texture->getName(), std::get<1>(*it)->getGLProgramState(), _blendFunc, &_quad, 1, transform, flags);
renderer->addCommand(&q);
idx++;
}
}
}
protected:
EffectSprite() : _defaultEffect(nullptr)
{
_effects.reserve(2);
}
~EffectSprite() {
for(auto &tuple : _effects) {
std::get<1>(tuple)->release();
}
CC_SAFE_RELEASE(_defaultEffect);
}
std::vector<std::tuple<ssize_t,Effect*,QuadCommand>> _effects;
Effect* _defaultEffect;
};
//
// Effect
//
bool Effect::initGLProgramState(const std::string &fragmentFilename)
{
auto fileUtiles = FileUtils::getInstance();
auto fragmentFullPath = fileUtiles->fullPathForFilename(fragmentFilename);
auto fragSource = fileUtiles->getStringFromFile(fragmentFullPath);
auto glprogram = GLProgram::createWithByteArrays(ccPositionTextureColor_noMVP_vert, fragSource.c_str());
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID || CC_TARGET_PLATFORM == CC_PLATFORM_WINRT)
_fragSource = fragSource;
#endif
_glprogramstate = GLProgramState::getOrCreateWithGLProgram(glprogram);
_glprogramstate->retain();
return _glprogramstate != nullptr;
}
Effect::Effect()
: _glprogramstate(nullptr)
{
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID || CC_TARGET_PLATFORM == CC_PLATFORM_WINRT)
_backgroundListener = EventListenerCustom::create(EVENT_RENDERER_RECREATED,
[this](EventCustom*)
{
auto glProgram = _glprogramstate->getGLProgram();
glProgram->reset();
glProgram->initWithByteArrays(ccPositionTextureColor_noMVP_vert, _fragSource.c_str());
glProgram->link();
glProgram->updateUniforms();
}
);
Director::getInstance()->getEventDispatcher()->addEventListenerWithFixedPriority(_backgroundListener, -1);
#endif
}
Effect::~Effect()
{
CC_SAFE_RELEASE_NULL(_glprogramstate);
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID || CC_TARGET_PLATFORM == CC_PLATFORM_WINRT)
Director::getInstance()->getEventDispatcher()->removeEventListener(_backgroundListener);
#endif
}
// Blur
class EffectBlur : public Effect
{
public:
CREATE_FUNC(EffectBlur);
virtual void setTarget(EffectSprite *sprite) override;
void setBlurRadius(float radius);
void setBlurSampleNum(float num);
protected:
bool init(float blurRadius = 10.0f, float sampleNum = 5.0f);
float _blurRadius;
float _blurSampleNum;
};
void EffectBlur::setTarget(EffectSprite *sprite)
{
Size size = sprite->getTexture()->getContentSizeInPixels();
_glprogramstate->setUniformVec2("resolution", size);
#if (CC_TARGET_PLATFORM != CC_PLATFORM_WINRT)
_glprogramstate->setUniformFloat("blurRadius", _blurRadius);
_glprogramstate->setUniformFloat("sampleNum", _blurSampleNum);
#endif
}
bool EffectBlur::init(float blurRadius, float sampleNum)
{
#if (CC_TARGET_PLATFORM != CC_PLATFORM_WINRT)
initGLProgramState("Shaders/example_Blur.fsh");
#else
initGLProgramState("Shaders/example_Blur_winrt.fsh");
#endif
_blurRadius = blurRadius;
_blurSampleNum = sampleNum;
return true;
}
void EffectBlur::setBlurRadius(float radius)
{
_blurRadius = radius;
}
void EffectBlur::setBlurSampleNum(float num)
{
_blurSampleNum = num;
}
// Outline
class EffectOutline : public Effect
{
public:
CREATE_FUNC(EffectOutline);
bool init()
{
initGLProgramState("Shaders/example_Outline.fsh");
Vec3 color(1.0f, 0.2f, 0.3f);
GLfloat radius = 0.01f;
GLfloat threshold = 1.75;
_glprogramstate->setUniformVec3("u_o