axmol/tests/cpp-tests/Classes/ShaderTest/ShaderTest2.cpp

646 lines
20 KiB
C++
Raw Normal View History

2014-05-09 06:43:12 +08:00
/****************************************************************************
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.
****************************************************************************/
2014-05-09 06:43:12 +08:00
#include "ShaderTest2.h"
2013-09-09 17:49:13 +08:00
#include "ShaderTest.h"
#include "../testResource.h"
#include "cocos2d.h"
#include <tuple>
2013-09-09 17:49:13 +08:00
namespace ShaderTest2
{
static std::function<Layer*()> createFunctions[] =
{
2014-05-15 04:09:33 +08:00
CL(EffectSpriteTest),
CL(EffectSpriteLamp),
2013-09-09 17:49:13 +08:00
};
static unsigned int TEST_CASE_COUNT = sizeof(ShaderTest2::createFunctions) / sizeof(ShaderTest2::createFunctions[0]);
static int sceneIdx=-1;
Layer* createTest(int index)
{
auto layer = (createFunctions[index])();;
return layer;
}
Layer* nextAction();
Layer* backAction();
Layer* restartAction();
Layer* nextAction()
{
sceneIdx++;
sceneIdx = sceneIdx % TEST_CASE_COUNT;
return createTest(sceneIdx);
}
Layer* backAction()
{
sceneIdx--;
if( sceneIdx < 0 )
sceneIdx = TEST_CASE_COUNT -1;
return createTest(sceneIdx);
}
Layer* restartAction()
{
return createTest(sceneIdx);
}
}
ShaderTestDemo2::ShaderTestDemo2()
{
}
void ShaderTestDemo2::backCallback(Ref* sender)
2013-09-09 17:49:13 +08:00
{
2013-11-14 09:36:33 +08:00
auto s = ShaderTestScene2::create();
2013-09-09 17:49:13 +08:00
s->addChild( ShaderTest2::backAction() );
Director::getInstance()->replaceScene(s);
}
void ShaderTestDemo2::nextCallback(Ref* sender)
2013-09-09 17:49:13 +08:00
{
2013-11-14 09:36:33 +08:00
auto s = ShaderTestScene2::create();
2013-09-09 17:49:13 +08:00
s->addChild( ShaderTest2::nextAction() );
Director::getInstance()->replaceScene(s);
}
void ShaderTestDemo2::restartCallback(Ref* sender)
2013-09-09 17:49:13 +08:00
{
2013-11-14 09:36:33 +08:00
auto s = ShaderTestScene2::create();
s->addChild(ShaderTest2::restartAction());
2013-09-09 17:49:13 +08:00
Director::getInstance()->replaceScene(s);
}
void ShaderTestScene2::runThisTest()
{
auto layer = ShaderTest2::nextAction();
addChild(layer);
Director::getInstance()->replaceScene(this);
}
2014-05-14 09:12:58 +08:00
//
2014-05-15 04:09:33 +08:00
// EffectSprite
2014-05-14 09:12:58 +08:00
//
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);
}
2014-05-15 04:09:33 +08:00
class Effect;
class EffectSprite : public Sprite
2014-05-14 09:12:58 +08:00
{
public:
2014-05-15 04:09:33 +08:00
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;
}
2014-05-14 09:12:58 +08:00
void setEffect(Effect* effect) {
if(_defaultEffect != effect) {
2014-05-15 04:09:33 +08:00
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
{
// Don't do calculate the culling if the transform was not updated
2014-05-31 12:50:39 +08:00
_insideBounds = (flags & FLAGS_TRANSFORM_DIRTY) ? renderer->checkVisibility(transform, _contentSize) : _insideBounds;
if(_insideBounds)
{
// 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);
renderer->addCommand(&q);
idx++;
}
// normal effect: order == 0
_quadCommand.init(_globalZOrder, _texture->getName(), getGLProgramState(), _blendFunc, &_quad, 1, transform);
renderer->addCommand(&_quadCommand);
// 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);
renderer->addCommand(&q);
idx++;
}
2014-05-15 04:09:33 +08:00
}
}
2014-05-14 09:12:58 +08:00
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;
2014-05-14 09:12:58 +08:00
};
2014-05-15 04:09:33 +08:00
//
// Effect
//
2014-05-14 09:12:58 +08:00
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)
_fragSource = fragSource;
#endif
2014-05-14 09:12:58 +08:00
_glprogramstate = GLProgramState::getOrCreateWithGLProgram(glprogram);
_glprogramstate->retain();
return _glprogramstate != nullptr;
}
Effect::Effect()
: _glprogramstate(nullptr)
{
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
_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)
Director::getInstance()->getEventDispatcher()->removeEventListener(_backgroundListener);
#endif
}
2014-05-15 04:09:33 +08:00
// Blur
2014-05-14 09:12:58 +08:00
class EffectBlur : public Effect
{
public:
2014-05-15 04:09:33 +08:00
CREATE_FUNC(EffectBlur);
virtual void setTarget(EffectSprite *sprite) override;
void setBlurRadius(float radius);
void setBlurSampleNum(float num);
2014-05-14 09:12:58 +08:00
protected:
bool init(float blurRadius = 10.0f, float sampleNum = 5.0f);
float _blurRadius;
float _blurSampleNum;
2014-05-14 09:12:58 +08:00
};
2014-05-15 04:09:33 +08:00
void EffectBlur::setTarget(EffectSprite *sprite)
2014-05-14 09:12:58 +08:00
{
Size size = sprite->getTexture()->getContentSizeInPixels();
_glprogramstate->setUniformVec2("resolution", size);
_glprogramstate->setUniformFloat("blurRadius", _blurRadius);
_glprogramstate->setUniformFloat("sampleNum", _blurSampleNum);
2014-05-14 09:12:58 +08:00
}
bool EffectBlur::init(float blurRadius, float sampleNum)
2014-05-14 09:12:58 +08:00
{
initGLProgramState("Shaders/example_Blur.fsh");
_blurRadius = blurRadius;
_blurSampleNum = sampleNum;
2014-05-14 09:12:58 +08:00
return true;
}
void EffectBlur::setBlurRadius(float radius)
2014-05-14 09:12:58 +08:00
{
_blurRadius = radius;
2014-05-14 09:12:58 +08:00
}
void EffectBlur::setBlurSampleNum(float num)
{
_blurSampleNum = num;
}
2014-05-15 04:09:33 +08:00
// Outline
class EffectOutline : public Effect
{
public:
CREATE_FUNC(EffectOutline);
bool init()
{
initGLProgramState("Shaders/example_outline.fsh");
2014-05-14 09:12:58 +08:00
Vec3 color(1.0f, 0.2f, 0.3f);
GLfloat radius = 0.01f;
2014-05-15 04:09:33 +08:00
GLfloat threshold = 1.75;
_glprogramstate->setUniformVec3("u_outlineColor", color);
_glprogramstate->setUniformFloat("u_radius", radius);
_glprogramstate->setUniformFloat("u_threshold", threshold);
return true;
}
};
// Noise
class EffectNoise : public Effect
2014-05-14 09:12:58 +08:00
{
public:
2014-05-15 04:09:33 +08:00
CREATE_FUNC(EffectNoise);
protected:
bool init() {
initGLProgramState("Shaders/example_Noisy.fsh");
return true;
2014-05-14 09:12:58 +08:00
}
2014-05-15 07:55:57 +08:00
virtual void setTarget(EffectSprite* sprite) override
2014-05-15 04:09:33 +08:00
{
auto s = sprite->getTexture()->getContentSizeInPixels();
getGLProgramState()->setUniformVec2("resolution", Vec2(s.width, s.height));
2014-05-14 09:12:58 +08:00
}
2014-05-15 04:09:33 +08:00
};
// Edge Detect
class EffectEdgeDetect : public Effect
{
public:
CREATE_FUNC(EffectEdgeDetect);
2014-05-14 09:12:58 +08:00
protected:
2014-05-15 04:09:33 +08:00
bool init() {
initGLProgramState("Shaders/example_edgeDetection.fsh");
return true;
}
2014-05-15 07:55:57 +08:00
virtual void setTarget(EffectSprite* sprite) override
2014-05-15 04:09:33 +08:00
{
auto s = sprite->getTexture()->getContentSizeInPixels();
getGLProgramState()->setUniformVec2("resolution", Vec2(s.width, s.height));
}
};
// Grey
class EffectGreyScale : public Effect
{
public:
CREATE_FUNC(EffectGreyScale);
protected:
bool init() {
initGLProgramState("Shaders/example_greyScale.fsh");
return true;
}
2014-05-14 09:12:58 +08:00
};
2014-05-15 07:55:57 +08:00
// Sepia
class EffectSepia : public Effect
2013-09-09 17:49:13 +08:00
{
public:
2014-05-15 07:55:57 +08:00
CREATE_FUNC(EffectSepia);
2013-09-09 17:49:13 +08:00
protected:
2014-05-15 07:55:57 +08:00
bool init() {
initGLProgramState("Shaders/example_sepia.fsh");
return true;
2014-04-14 11:52:17 +08:00
}
2013-09-09 17:49:13 +08:00
};
2014-05-15 07:55:57 +08:00
// bloom
class EffectBloom : public Effect
2013-09-09 17:49:13 +08:00
{
public:
2014-05-15 07:55:57 +08:00
CREATE_FUNC(EffectBloom);
2013-09-09 17:49:13 +08:00
protected:
2014-05-15 07:55:57 +08:00
bool init() {
initGLProgramState("Shaders/example_bloom.fsh");
return true;
}
2014-05-09 06:43:12 +08:00
2014-05-15 07:55:57 +08:00
virtual void setTarget(EffectSprite* sprite) override
{
auto s = sprite->getTexture()->getContentSizeInPixels();
getGLProgramState()->setUniformVec2("resolution", Vec2(s.width, s.height));
}
2013-09-09 17:49:13 +08:00
};
2014-05-15 07:55:57 +08:00
// cel shading
class EffectCelShading : public Effect
{
public:
2014-05-15 07:55:57 +08:00
CREATE_FUNC(EffectCelShading);
2014-05-09 06:43:12 +08:00
protected:
2014-05-15 07:55:57 +08:00
bool init() {
initGLProgramState("Shaders/example_celShading.fsh");
return true;
}
2014-05-09 06:43:12 +08:00
2014-05-15 07:55:57 +08:00
virtual void setTarget(EffectSprite* sprite) override
{
auto s = sprite->getTexture()->getContentSizeInPixels();
getGLProgramState()->setUniformVec2("resolution", Vec2(s.width, s.height));
}
};
2014-05-15 07:55:57 +08:00
// Lens Flare
class EffectLensFlare : public Effect
{
2014-05-15 07:55:57 +08:00
public:
CREATE_FUNC(EffectLensFlare);
2014-05-15 07:55:57 +08:00
protected:
bool init() {
initGLProgramState("Shaders/example_lensFlare.fsh");
return true;
2013-09-09 17:49:13 +08:00
}
2014-05-15 07:55:57 +08:00
virtual void setTarget(EffectSprite* sprite) override
2013-09-09 17:49:13 +08:00
{
2014-05-15 07:55:57 +08:00
auto s = sprite->getTexture()->getContentSizeInPixels();
getGLProgramState()->setUniformVec2("textureResolution", Vec2(s.width, s.height));
s = Director::getInstance()->getWinSize();
getGLProgramState()->setUniformVec2("resolution", Vec2(s.width, s.height));
}
2014-05-15 07:55:57 +08:00
};
class EffectNormalMapped : public Effect
{
public:
CREATE_FUNC(EffectNormalMapped);
static EffectNormalMapped* create(const std::string&normalMapFileName)
{
EffectNormalMapped *normalMappedSprite = new (std::nothrow) EffectNormalMapped();
if (normalMappedSprite && normalMappedSprite->init() && normalMappedSprite->initNormalMap(normalMapFileName))
{
normalMappedSprite->autorelease();
return normalMappedSprite;
}
CC_SAFE_DELETE(normalMappedSprite);
return nullptr;
}
void setKBump(float value);
void setLightPos(const Vec3& pos);
void setLightColor(const Color4F& color);
float getKBump()const{return _kBump;}
protected:
bool init();
bool initNormalMap(const std::string&normalMapFileName);
virtual void setTarget(EffectSprite* sprite) override;
EffectSprite* _sprite;
Vec3 _lightPos;
Color4F _lightColor;
float _kBump;
};
bool EffectNormalMapped::init()
{
initGLProgramState("Shaders3D/Normal.frag");
_kBump = 2;
return true;
}
bool EffectNormalMapped::initNormalMap(const std::string&normalMapFileName)
{
auto normalMapTextrue = TextureCache::getInstance()->addImage(normalMapFileName.c_str());
getGLProgramState()->setUniformTexture("u_normalMap", normalMapTextrue);
return true;
}
void EffectNormalMapped::setTarget(EffectSprite* sprite)
{
_sprite = sprite;
getGLProgramState()->setUniformFloat("u_kBump", _kBump);
getGLProgramState()->setUniformVec2("u_contentSize", Vec2(sprite->getContentSize().width,sprite->getContentSize().height));
}
void EffectNormalMapped::setKBump(float value)
{
_kBump = value;
auto glProgramState = getGLProgramState();
if(glProgramState) glProgramState->setUniformFloat("u_kBump", _kBump);
}
void EffectNormalMapped::setLightPos(const Vec3& pos)
{
_lightPos = pos;
auto glProgramState = getGLProgramState();
if(glProgramState) glProgramState->setUniformVec4("u_lightPosInLocalSpace", Vec4(_lightPos.x,_lightPos.y,_lightPos.z,1));
}
void EffectNormalMapped::setLightColor(const Color4F& color)
{
_lightColor = color;
auto glProgramState = getGLProgramState();
if(glProgramState) getGLProgramState()->setUniformVec3("u_diffuseL", Vec3(_lightColor.r,_lightColor.g,_lightColor.b));
}
2014-04-14 11:52:17 +08:00
2014-05-15 04:09:33 +08:00
EffectSpriteTest::EffectSpriteTest()
2014-04-14 11:52:17 +08:00
{
if (ShaderTestDemo2::init()) {
2014-05-15 04:09:33 +08:00
2014-04-14 11:52:17 +08:00
auto s = Director::getInstance()->getWinSize();
2014-05-07 02:19:51 +08:00
2014-05-15 07:55:57 +08:00
auto itemPrev = MenuItemImage::create("Images/b1.png", "Images/b2.png",
[&](Ref *sender) {
_vectorIndex--;
if(_vectorIndex<0)
_vectorIndex = _effects.size()-1;
_sprite->setEffect(_effects.at(_vectorIndex));
});
auto itemNext = MenuItemImage::create("Images/f1.png", "Images/f2.png",
2014-05-15 04:09:33 +08:00
[&](Ref *sender) {
_vectorIndex++;
if(_vectorIndex>=_effects.size())
_vectorIndex = 0;
_sprite->setEffect(_effects.at(_vectorIndex));
});
2014-05-14 09:12:58 +08:00
auto menu = Menu::create(itemPrev, itemNext, nullptr);
2014-05-15 07:55:57 +08:00
menu->alignItemsHorizontally();
menu->setScale(0.5);
menu->setAnchorPoint(Vec2(0,0));
menu->setPosition(Vec2(s.width/2,70));
2014-05-15 04:09:33 +08:00
addChild(menu);
_sprite = EffectSprite::create("Images/grossini.png");
_sprite->setPosition(Vec2(0, s.height/2));
addChild(_sprite);
2014-05-14 09:12:58 +08:00
Squashed commit of the following: commit a9572b8913f3a38b59adbd7b4017ab9848a6b2b5 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed May 14 10:03:44 2014 -0700 math renames `Vector2` -> `Vec2` `Vector3` -> `Vec3` `Vector4` -> `Vec4` `Matrix` -> `Mat4` commit 4e107f4bd854c26bfceb52b063d6bd9cea02d6a3 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 09:24:28 2014 -0700 raw version of rename Vector3 commit 1d115573ebe96a5fc815fa44fbe6417ea7dba841 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 09:07:14 2014 -0700 rename Vector2 after merge commit ab2ed58c129dbc30a4c0970ed94568c5d271657b Merge: 1978d2d 86fb75a Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 09:05:30 2014 -0700 Merge branch 'v3' into v3_renameMathClassName Conflicts: tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UIButtonTest/UIButtonTest_Editor.cpp tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UICheckBoxTest/UICheckBoxTest_Editor.cpp tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UISliderTest/UISliderTest_Editor.cpp tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UITextFieldTest/UITextFieldTest.cpp tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UITextFieldTest/UITextFieldTest_Editor.cpp commit 1978d2d174877172ccddc083020a1bbf43ad3b39 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 08:51:45 2014 -0700 rename vector2 in tests/cpp-empty-test folder commit d4e0ff13dcce62724d2fece656543f26aa28e467 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:58:23 2014 -0700 rename vector2 in tests/cpp-tests cpp files commit be50ca2ec75e0fd32a6fcdaa15fe1ebb4cafe79f Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:52:57 2014 -0700 rename vector2 in tests/cpp-tests head files commit 6daef564400d4e28c4ce20859a68e0f583fed125 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:49:48 2014 -0700 rename vector2 in extension folder commit 8f3f0f65ceea92c9e7a0d87ab54e62220c5572e2 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:47:22 2014 -0700 rename vector2 in cocos/2d cpp files commit e1f3105aae06d595661a3030f519f7cc13aefbed Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:44:39 2014 -0700 rename vector2 in cocos/2d head files commit 6708d890bfe486109120c3cd4b9fe5c078b7108f Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:40:59 2014 -0700 rename vector2 in cocos/base folder commit d3978fa5447c31ea2f3ece5469b7e746dfba4248 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:40:43 2014 -0700 rename vector2 in cocos/deprecated folder commit 4bff45139363d6b9706edbbcf9f322d48b4fd019 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:40:26 2014 -0700 rename vector2 in cocos/editor-support folder commit 353d244c995f8b5d14f635c52aed8bc5e5fc1a6f Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:36:48 2014 -0700 rename vector2 in cocos/ui folder commit 758b8f4d513084b9922d7242e9b8f2c7f316de6c Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:32:39 2014 -0700 rename vector2 in cocos/renderer folder commit 0bd2710dd8714cecb993880bc37affd9ecb05c27 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:32:15 2014 -0700 rename vector2 in cocos/physics folder commit b7f0581c4587348bdbc1478d5374c2325735f21d Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:25:01 2014 -0700 rename vector2 in cocos/math folder commit a8631a8e1a4e2740807ccd9be9d70de6ecaad7dd Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:16:55 2014 -0700 rename Vector2 to Vec2 deprecate typedef Vector2
2014-05-15 01:07:09 +08:00
auto jump = JumpBy::create(4, Vec2(s.width,0), 100, 4);
2014-05-14 09:12:58 +08:00
auto rot = RotateBy::create(4, 720);
auto spawn = Spawn::create(jump, rot, nullptr);
2014-05-14 09:12:58 +08:00
auto rev = spawn->reverse();
auto seq = Sequence::create(spawn, rev, nullptr);
2014-05-14 09:12:58 +08:00
auto repeat = RepeatForever::create(seq);
2014-05-15 04:09:33 +08:00
_sprite->runAction(repeat);
// set the Effects
_effects.pushBack(EffectBlur::create());
_effects.pushBack(EffectOutline::create());
_effects.pushBack(EffectNoise::create());
_effects.pushBack(EffectEdgeDetect::create());
_effects.pushBack(EffectGreyScale::create());
2014-05-15 07:55:57 +08:00
_effects.pushBack(EffectSepia::create());
_effects.pushBack(EffectBloom::create());
_effects.pushBack(EffectCelShading::create());
_effects.pushBack(EffectLensFlare::create());
2014-05-14 09:12:58 +08:00
2014-05-15 04:09:33 +08:00
_vectorIndex = 0;
_sprite->setEffect( _effects.at(_vectorIndex) );
// _sprite->addEffect( _effects.at(8), -10 );
// _sprite->addEffect( _effects.at(1), 1 );
2014-05-14 09:12:58 +08:00
}
}
EffectSpriteLamp::EffectSpriteLamp()
{
if (ShaderTestDemo2::init()) {
auto s = Director::getInstance()->getWinSize();
_sprite = EffectSprite::create("Images/elephant1_Diffuse.png");
//auto contentSize = _sprite->getContentSize();
_sprite->setPosition(Vec2(s.width/2, s.height/2));
addChild(_sprite);
auto lampEffect = EffectNormalMapped::create("Images/elephant1_Normal.png");
Vec3 pos(150,150, 50);
_lightSprite = Sprite::create("Images/ball.png");
this->addChild(_lightSprite);
_lightSprite->setPosition(Vec2(pos.x, s.height- pos.y));
Mat4 mat = _sprite->getNodeToWorldTransform();
Point lightPosInLocalSpace=PointApplyAffineTransform(Vec2(pos.x, pos.y),_sprite->worldToNodeTransform());
lampEffect->setLightColor(Color4F(1,1,1,1));
lampEffect->setLightPos(Vec3(lightPosInLocalSpace.x, lightPosInLocalSpace.y, 50));
lampEffect->setKBump(2);
_sprite->setEffect(lampEffect);
_effect = lampEffect;
auto listerner = EventListenerTouchAllAtOnce::create();
listerner->onTouchesBegan = CC_CALLBACK_2(EffectSpriteLamp::onTouchesBegan, this);
listerner->onTouchesMoved = CC_CALLBACK_2(EffectSpriteLamp::onTouchesMoved, this);
listerner->onTouchesEnded = CC_CALLBACK_2(EffectSpriteLamp::onTouchesEnded, this);
_eventDispatcher->addEventListenerWithSceneGraphPriority(listerner, this);
}
}
void EffectSpriteLamp::onTouchesBegan(const std::vector<Touch*>& touches, Event *unused_event)
{
for ( auto &item: touches )
{
auto touch = item;
auto s = Director::getInstance()->getWinSize();
Point loc_winSpace = touch->getLocationInView();
_lightSprite->setPosition(Vec2( loc_winSpace.x, s.height - loc_winSpace.y));
Vec3 pos(loc_winSpace.x,loc_winSpace.y, 50);
Mat4 mat = _sprite->getNodeToWorldTransform();
Point lightPosInLocalSpace=PointApplyAffineTransform(Vec2(pos.x, pos.y),_sprite->worldToNodeTransform());
((EffectNormalMapped*)_effect)->setLightPos(Vec3(lightPosInLocalSpace.x, lightPosInLocalSpace.y, 50));
}
}
void EffectSpriteLamp::onTouchesMoved(const std::vector<Touch*>& touches, Event *unused_event)
{
for ( auto &item: touches )
{
auto touch = item;
auto s = Director::getInstance()->getWinSize();
Point loc_winSpace = touch->getLocationInView();
_lightSprite->setPosition(Vec2( loc_winSpace.x, s.height - loc_winSpace.y));
Vec3 pos(loc_winSpace.x,loc_winSpace.y, 50);
Mat4 mat = _sprite->getNodeToWorldTransform();
Point lightPosInLocalSpace=PointApplyAffineTransform(Vec2(pos.x, pos.y),_sprite->worldToNodeTransform());
((EffectNormalMapped*)_effect)->setLightPos(Vec3(lightPosInLocalSpace.x, lightPosInLocalSpace.y, 50));
}
}
void EffectSpriteLamp::onTouchesEnded(const std::vector<Touch*>& touches, Event *unused_event)
{
for ( auto &item: touches )
{
auto touch = item;
auto s = Director::getInstance()->getWinSize();
Point loc_winSpace = touch->getLocationInView();
_lightSprite->setPosition(Vec2( loc_winSpace.x, s.height - loc_winSpace.y));
Vec3 pos(loc_winSpace.x,loc_winSpace.y, 50);
Mat4 mat = _sprite->getNodeToWorldTransform();
Point lightPosInLocalSpace=PointApplyAffineTransform(Vec2(pos.x, pos.y),_sprite->worldToNodeTransform());
((EffectNormalMapped*)_effect)->setLightPos(Vec3(lightPosInLocalSpace.x, lightPosInLocalSpace.y, 50));
}
}