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

536 lines
15 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),
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, bool transformUpdated) override
{
// Don't do calculate the culling if the transform was not updated
_insideBounds = transformUpdated ? 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_COME_TO_FOREGROUND,
[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;
2014-05-14 09:12:58 +08:00
void setGaussian(float value);
void setCustomUniforms();
void setBlurSize(float f);
protected:
2014-05-15 04:09:33 +08:00
bool init(float blurSize=3.0);
2014-05-14 09:12:58 +08:00
int _blurRadius;
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
Vec2 _pixelSize;
2014-05-14 09:12:58 +08:00
int _samplingRadius;
float _scale;
float _cons;
float _weightSum;
};
2014-05-15 04:09:33 +08:00
void EffectBlur::setTarget(EffectSprite *sprite)
2014-05-14 09:12:58 +08:00
{
2014-05-15 04:09:33 +08:00
Size s = sprite->getTexture()->getContentSizeInPixels();
_pixelSize = Vec2(1/s.width, 1/s.height);
_glprogramstate->setUniformVec2("onePixelSize", _pixelSize);
2014-05-14 09:12:58 +08:00
}
bool EffectBlur::init(float blurSize)
{
initGLProgramState("Shaders/example_Blur.fsh");
auto s = Size(100,100);
_blurRadius = 0;
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
_pixelSize = Vec2(1/s.width, 1/s.height);
2014-05-14 09:12:58 +08:00
_samplingRadius = 0;
setBlurSize(blurSize);
_glprogramstate->setUniformVec2("onePixelSize", _pixelSize);
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
_glprogramstate->setUniformVec4("gaussianCoefficient", Vec4(_samplingRadius, _scale, _cons, _weightSum));
2014-05-14 09:12:58 +08:00
return true;
}
void EffectBlur::setBlurSize(float f)
{
if(_blurRadius == (int)f)
return;
_blurRadius = (int)f;
_samplingRadius = _blurRadius;
if (_samplingRadius > 10)
{
_samplingRadius = 10;
}
if (_blurRadius > 0)
{
float sigma = _blurRadius / 2.0f;
_scale = -0.5f / (sigma * sigma);
_cons = -1.0f * _scale / 3.141592f;
_weightSum = -_cons;
float weight;
int squareX;
for(int dx = 0; dx <= _samplingRadius; ++dx)
{
squareX = dx * dx;
weight = _cons * exp(squareX * _scale);
_weightSum += 2.0 * weight;
for (int dy = 1; dy <= _samplingRadius; ++dy)
{
weight = _cons * exp((squareX + dy * dy) * _scale);
_weightSum += 4.0 * weight;
}
}
}
}
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
2014-05-15 04:09:33 +08:00
Vec3 color(1.0, 0.2, 0.3);
GLfloat radius = 0.01;
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
};
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
2014-05-15 07:55:57 +08:00
auto menu = Menu::create(itemPrev, itemNext, NULL);
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, NULL);
auto rev = spawn->reverse();
auto seq = Sequence::create(spawn, rev, NULL);
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
}
}