2012-03-21 17:27:13 +08:00
|
|
|
#include "ShaderTest.h"
|
|
|
|
#include "../testResource.h"
|
2012-06-19 13:50:11 +08:00
|
|
|
#include "cocos2d.h"
|
2012-03-21 17:27:13 +08:00
|
|
|
|
2015-04-09 12:23:47 +08:00
|
|
|
USING_NS_CC;
|
|
|
|
USING_NS_CC_EXT;
|
|
|
|
|
2015-04-03 14:31:03 +08:00
|
|
|
ShaderTests::ShaderTests()
|
2012-03-21 17:27:13 +08:00
|
|
|
{
|
2015-04-03 14:31:03 +08:00
|
|
|
ADD_TEST_CASE(ShaderLensFlare);
|
|
|
|
ADD_TEST_CASE(ShaderMandelbrot);
|
|
|
|
ADD_TEST_CASE(ShaderJulia);
|
|
|
|
ADD_TEST_CASE(ShaderHeart);
|
|
|
|
ADD_TEST_CASE(ShaderFlower);
|
|
|
|
ADD_TEST_CASE(ShaderPlasma);
|
|
|
|
ADD_TEST_CASE(ShaderBlur);
|
|
|
|
ADD_TEST_CASE(ShaderRetroEffect);
|
|
|
|
ADD_TEST_CASE(ShaderMonjori);
|
|
|
|
ADD_TEST_CASE(ShaderGlow);
|
|
|
|
ADD_TEST_CASE(ShaderMultiTexture);
|
2012-03-21 17:27:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
///---------------------------------------
|
|
|
|
//
|
|
|
|
// ShaderNode
|
|
|
|
//
|
|
|
|
///---------------------------------------
|
|
|
|
enum
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
SIZE_X = 256,
|
|
|
|
SIZE_Y = 256,
|
2012-03-21 17:27:13 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
ShaderNode::ShaderNode()
|
2014-05-15 01:07:09 +08:00
|
|
|
:_center(Vec2(0.0f, 0.0f))
|
|
|
|
,_resolution(Vec2(0.0f, 0.0f))
|
2013-06-15 14:03:30 +08:00
|
|
|
,_time(0.0f)
|
2012-03-21 17:27:13 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2012-10-18 10:15:46 +08:00
|
|
|
ShaderNode::~ShaderNode()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2014-05-13 10:12:56 +08:00
|
|
|
ShaderNode* ShaderNode::shaderNodeWithVertex(const std::string &vert, const std::string& frag)
|
2012-03-21 17:27:13 +08:00
|
|
|
{
|
2014-08-28 07:31:57 +08:00
|
|
|
auto node = new (std::nothrow) ShaderNode();
|
2012-04-19 14:35:52 +08:00
|
|
|
node->initWithVertex(vert, frag);
|
|
|
|
node->autorelease();
|
2012-03-21 17:27:13 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
return node;
|
2012-03-21 17:27:13 +08:00
|
|
|
}
|
|
|
|
|
2014-05-13 10:12:56 +08:00
|
|
|
bool ShaderNode::initWithVertex(const std::string &vert, const std::string &frag)
|
2012-03-21 17:27:13 +08:00
|
|
|
{
|
2013-12-31 10:54:37 +08:00
|
|
|
#if CC_ENABLE_CACHE_TEXTURE_DATA
|
2014-07-09 23:03:04 +08:00
|
|
|
auto listener = EventListenerCustom::create(EVENT_RENDERER_RECREATED, [this](EventCustom* event){
|
2014-05-13 10:12:56 +08:00
|
|
|
this->setGLProgramState(nullptr);
|
|
|
|
loadShaderVertex(_vertFileName, _fragFileName);
|
2013-12-31 10:54:37 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
|
|
|
|
#endif
|
2012-03-21 17:27:13 +08:00
|
|
|
|
2014-05-13 10:12:56 +08:00
|
|
|
_vertFileName = vert;
|
|
|
|
_fragFileName = frag;
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
loadShaderVertex(vert, frag);
|
2012-03-21 17:27:13 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
_time = 0;
|
2014-05-15 01:07:09 +08:00
|
|
|
_resolution = Vec2(SIZE_X, SIZE_Y);
|
2014-05-09 09:01:48 +08:00
|
|
|
getGLProgramState()->setUniformVec2("resolution", _resolution);
|
2012-03-21 17:27:13 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
scheduleUpdate();
|
2012-03-21 17:27:13 +08:00
|
|
|
|
2013-07-12 14:30:26 +08:00
|
|
|
setContentSize(Size(SIZE_X, SIZE_Y));
|
2014-05-15 01:07:09 +08:00
|
|
|
setAnchorPoint(Vec2(0.5f, 0.5f));
|
2012-10-18 10:15:46 +08:00
|
|
|
|
2012-03-21 17:27:13 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
return true;
|
2012-03-21 17:27:13 +08:00
|
|
|
}
|
|
|
|
|
2014-05-13 10:12:56 +08:00
|
|
|
void ShaderNode::loadShaderVertex(const std::string &vert, const std::string &frag)
|
2012-03-21 17:27:13 +08:00
|
|
|
{
|
2014-05-13 10:12:56 +08:00
|
|
|
auto fileUtiles = FileUtils::getInstance();
|
|
|
|
|
|
|
|
// frag
|
|
|
|
auto fragmentFilePath = fileUtiles->fullPathForFilename(frag);
|
|
|
|
auto fragSource = fileUtiles->getStringFromFile(fragmentFilePath);
|
|
|
|
|
|
|
|
// vert
|
|
|
|
std::string vertSource;
|
|
|
|
if (vert.empty()) {
|
|
|
|
vertSource = ccPositionTextureColor_vert;
|
|
|
|
} else {
|
|
|
|
std::string vertexFilePath = fileUtiles->fullPathForFilename(vert);
|
|
|
|
vertSource = fileUtiles->getStringFromFile(vertexFilePath);
|
|
|
|
}
|
|
|
|
|
|
|
|
auto glprogram = GLProgram::createWithByteArrays(vertSource.c_str(), fragSource.c_str());
|
2014-05-14 09:12:58 +08:00
|
|
|
auto glprogramstate = GLProgramState::getOrCreateWithGLProgram(glprogram);
|
2014-05-13 10:12:56 +08:00
|
|
|
setGLProgramState(glprogramstate);
|
2012-03-21 17:27:13 +08:00
|
|
|
}
|
|
|
|
|
2012-06-08 13:55:28 +08:00
|
|
|
void ShaderNode::update(float dt)
|
2012-03-21 17:27:13 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_time += dt;
|
2012-03-21 17:27:13 +08:00
|
|
|
}
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
void ShaderNode::setPosition(const Vec2 &newPosition)
|
2012-03-21 17:27:13 +08:00
|
|
|
{
|
2013-06-20 14:17:10 +08:00
|
|
|
Node::setPosition(newPosition);
|
2013-08-16 16:05:27 +08:00
|
|
|
auto position = getPosition();
|
2014-05-15 01:07:09 +08:00
|
|
|
_center = Vec2(position.x * CC_CONTENT_SCALE_FACTOR(), position.y * CC_CONTENT_SCALE_FACTOR());
|
2014-05-09 09:01:48 +08:00
|
|
|
getGLProgramState()->setUniformVec2("center", _center);
|
2012-03-21 17:27:13 +08:00
|
|
|
}
|
|
|
|
|
2014-05-31 07:42:05 +08:00
|
|
|
void ShaderNode::draw(Renderer *renderer, const Mat4 &transform, uint32_t flags)
|
2012-03-21 17:27:13 +08:00
|
|
|
{
|
2015-01-16 06:00:49 +08:00
|
|
|
_customCommand.init(_globalZOrder, transform, flags);
|
2014-05-31 07:42:05 +08:00
|
|
|
_customCommand.func = CC_CALLBACK_0(ShaderNode::onDraw, this, transform, flags);
|
2014-03-01 08:10:48 +08:00
|
|
|
renderer->addCommand(&_customCommand);
|
2013-12-25 16:38:43 +08:00
|
|
|
}
|
2012-03-21 17:27:13 +08:00
|
|
|
|
2014-05-31 07:42:05 +08:00
|
|
|
void ShaderNode::onDraw(const Mat4 &transform, uint32_t flags)
|
2013-12-25 16:38:43 +08:00
|
|
|
{
|
2014-03-06 07:49:08 +08:00
|
|
|
float w = SIZE_X, h = SIZE_Y;
|
|
|
|
GLfloat vertices[12] = {0,0, w,0, w,h, 0,0, 0,h, w,h};
|
|
|
|
|
2014-05-09 06:43:12 +08:00
|
|
|
auto glProgramState = getGLProgramState();
|
|
|
|
glProgramState->setVertexAttribPointer("a_position", 2, GL_FLOAT, GL_FALSE, 0, vertices);
|
|
|
|
glProgramState->apply(transform);
|
2014-05-09 03:34:26 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
glDrawArrays(GL_TRIANGLES, 0, 6);
|
|
|
|
|
2014-02-08 11:37:44 +08:00
|
|
|
CC_INCREMENT_GL_DRAWN_BATCHES_AND_VERTICES(1,6);
|
2012-03-21 17:27:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// ShaderMonjori
|
|
|
|
|
|
|
|
ShaderMonjori::ShaderMonjori()
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
init();
|
2012-03-21 17:27:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool ShaderMonjori::init()
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
if (ShaderTestDemo::init())
|
|
|
|
{
|
2014-05-13 10:12:56 +08:00
|
|
|
auto sn = ShaderNode::shaderNodeWithVertex("", "Shaders/example_Monjori.fsh");
|
2012-03-21 17:27:13 +08:00
|
|
|
|
2013-08-16 16:05:27 +08:00
|
|
|
auto s = Director::getInstance()->getWinSize();
|
2014-05-15 01:07:09 +08:00
|
|
|
sn->setPosition(Vec2(s.width/2, s.height/2));
|
2012-03-21 17:27:13 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
addChild(sn);
|
2012-03-21 17:27:13 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
return true;
|
|
|
|
}
|
2012-03-21 17:27:13 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
return false;
|
2012-03-21 17:27:13 +08:00
|
|
|
}
|
|
|
|
|
2013-12-19 05:52:10 +08:00
|
|
|
std::string ShaderMonjori::title() const
|
2012-03-21 17:27:13 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
return "Shader: Frag shader";
|
2012-03-21 17:27:13 +08:00
|
|
|
}
|
|
|
|
|
2013-12-19 05:52:10 +08:00
|
|
|
std::string ShaderMonjori::subtitle() const
|
2012-03-21 17:27:13 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
return "Monjori plane deformations";
|
2012-03-21 17:27:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// ShaderMandelbrot
|
|
|
|
ShaderMandelbrot::ShaderMandelbrot()
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
init();
|
2012-03-21 17:27:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool ShaderMandelbrot::init()
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
if (ShaderTestDemo::init())
|
|
|
|
{
|
2014-11-22 02:29:36 +08:00
|
|
|
#if CC_TARGET_PLATFORM != CC_PLATFORM_WINRT && CC_TARGET_PLATFORM != CC_PLATFORM_WP8
|
2014-05-13 10:12:56 +08:00
|
|
|
auto sn = ShaderNode::shaderNodeWithVertex("", "Shaders/example_Mandelbrot.fsh");
|
2012-03-21 17:27:13 +08:00
|
|
|
|
2013-08-16 16:05:27 +08:00
|
|
|
auto s = Director::getInstance()->getWinSize();
|
2014-05-15 01:07:09 +08:00
|
|
|
sn->setPosition(Vec2(s.width/2, s.height/2));
|
2012-03-21 17:27:13 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
addChild(sn);
|
2014-11-22 02:29:36 +08:00
|
|
|
#endif
|
2012-04-19 14:35:52 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
2012-03-21 17:27:13 +08:00
|
|
|
}
|
|
|
|
|
2013-12-19 05:52:10 +08:00
|
|
|
std::string ShaderMandelbrot::title() const
|
2012-03-21 17:27:13 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
return "Shader: Frag shader";
|
2012-03-21 17:27:13 +08:00
|
|
|
}
|
|
|
|
|
2013-12-19 05:52:10 +08:00
|
|
|
std::string ShaderMandelbrot::subtitle() const
|
2012-03-21 17:27:13 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
return "Mandelbrot shader with Zoom";
|
2012-03-21 17:27:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// ShaderJulia
|
|
|
|
ShaderJulia::ShaderJulia()
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
init();
|
2012-03-21 17:27:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool ShaderJulia::init()
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
if (ShaderTestDemo::init())
|
|
|
|
{
|
2014-11-22 02:29:36 +08:00
|
|
|
#if CC_TARGET_PLATFORM != CC_PLATFORM_WINRT && CC_TARGET_PLATFORM != CC_PLATFORM_WP8
|
2014-05-13 10:12:56 +08:00
|
|
|
auto sn = ShaderNode::shaderNodeWithVertex("", "Shaders/example_Julia.fsh");
|
2012-03-21 17:27:13 +08:00
|
|
|
|
2013-08-16 16:05:27 +08:00
|
|
|
auto s = Director::getInstance()->getWinSize();
|
2014-05-15 01:07:09 +08:00
|
|
|
sn->setPosition(Vec2(s.width/2, s.height/2));
|
2012-03-21 17:27:13 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
addChild(sn);
|
2014-11-22 02:29:36 +08:00
|
|
|
#endif
|
2012-04-19 14:35:52 +08:00
|
|
|
return true;
|
|
|
|
}
|
2012-03-21 17:27:13 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
return false;
|
2012-03-21 17:27:13 +08:00
|
|
|
}
|
|
|
|
|
2013-12-19 05:52:10 +08:00
|
|
|
std::string ShaderJulia::title() const
|
2012-03-21 17:27:13 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
return "Shader: Frag shader";
|
2012-03-21 17:27:13 +08:00
|
|
|
}
|
|
|
|
|
2013-12-19 05:52:10 +08:00
|
|
|
std::string ShaderJulia::subtitle() const
|
2012-03-21 17:27:13 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
return "Julia shader";
|
2012-03-21 17:27:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// ShaderHeart
|
|
|
|
ShaderHeart::ShaderHeart()
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
init();
|
2012-03-21 17:27:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool ShaderHeart::init()
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
if (ShaderTestDemo::init())
|
|
|
|
{
|
2014-05-13 10:12:56 +08:00
|
|
|
auto sn = ShaderNode::shaderNodeWithVertex("", "Shaders/example_Heart.fsh");
|
2012-03-21 17:27:13 +08:00
|
|
|
|
2013-08-16 16:05:27 +08:00
|
|
|
auto s = Director::getInstance()->getWinSize();
|
2014-05-15 01:07:09 +08:00
|
|
|
sn->setPosition(Vec2(s.width/2, s.height/2));
|
2012-03-21 17:27:13 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
addChild(sn);
|
2012-03-21 17:27:13 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
return true;
|
|
|
|
}
|
2012-03-21 17:27:13 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
return false;
|
2012-03-21 17:27:13 +08:00
|
|
|
}
|
|
|
|
|
2013-12-19 05:52:10 +08:00
|
|
|
std::string ShaderHeart::title() const
|
2012-03-21 17:27:13 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
return "Shader: Frag shader";
|
2012-03-21 17:27:13 +08:00
|
|
|
}
|
|
|
|
|
2013-12-19 05:52:10 +08:00
|
|
|
std::string ShaderHeart::subtitle() const
|
2012-03-21 17:27:13 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
return "Heart";
|
2012-03-21 17:27:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// ShaderFlower
|
|
|
|
ShaderFlower::ShaderFlower()
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
init();
|
2012-03-21 17:27:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool ShaderFlower::init()
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
if (ShaderTestDemo::init())
|
|
|
|
{
|
2014-05-13 10:12:56 +08:00
|
|
|
auto sn = ShaderNode::shaderNodeWithVertex("", "Shaders/example_Flower.fsh");
|
2012-03-21 17:27:13 +08:00
|
|
|
|
2013-08-16 16:05:27 +08:00
|
|
|
auto s = Director::getInstance()->getWinSize();
|
2014-05-15 01:07:09 +08:00
|
|
|
sn->setPosition(Vec2(s.width/2, s.height/2));
|
2012-03-21 17:27:13 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
addChild(sn);
|
2012-03-21 17:27:13 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
return true;
|
|
|
|
}
|
2012-03-21 17:27:13 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
return false;
|
2012-03-21 17:27:13 +08:00
|
|
|
}
|
|
|
|
|
2013-12-19 05:52:10 +08:00
|
|
|
std::string ShaderFlower::title() const
|
2012-03-21 17:27:13 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
return "Shader: Frag shader";
|
2012-03-21 17:27:13 +08:00
|
|
|
}
|
|
|
|
|
2013-12-19 05:52:10 +08:00
|
|
|
std::string ShaderFlower::subtitle() const
|
2012-03-21 17:27:13 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
return "Flower";
|
2012-03-21 17:27:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// ShaderPlasma
|
|
|
|
ShaderPlasma::ShaderPlasma()
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
init();
|
2012-03-21 17:27:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool ShaderPlasma::init()
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
if (ShaderTestDemo::init())
|
|
|
|
{
|
2014-05-13 10:12:56 +08:00
|
|
|
auto sn = ShaderNode::shaderNodeWithVertex("", "Shaders/example_Plasma.fsh");
|
2012-03-21 17:27:13 +08:00
|
|
|
|
2013-08-16 16:05:27 +08:00
|
|
|
auto s = Director::getInstance()->getWinSize();
|
2014-05-15 01:07:09 +08:00
|
|
|
sn->setPosition(Vec2(s.width/2, s.height/2));
|
2012-03-21 17:27:13 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
addChild(sn);
|
2012-03-21 17:27:13 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
return true;
|
|
|
|
}
|
2012-03-21 17:27:13 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
return false;
|
2012-03-21 17:27:13 +08:00
|
|
|
}
|
|
|
|
|
2013-12-19 05:52:10 +08:00
|
|
|
std::string ShaderPlasma::title() const
|
2012-03-21 17:27:13 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
return "Shader: Frag shader";
|
2012-03-21 17:27:13 +08:00
|
|
|
}
|
|
|
|
|
2013-12-19 05:52:10 +08:00
|
|
|
std::string ShaderPlasma::subtitle() const
|
2012-03-21 17:27:13 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
return "Plasma";
|
2012-03-21 17:27:13 +08:00
|
|
|
}
|
|
|
|
|
2012-04-17 18:31:31 +08:00
|
|
|
// ShaderBlur
|
|
|
|
|
2013-06-20 14:17:10 +08:00
|
|
|
class SpriteBlur : public Sprite
|
2012-04-17 18:31:31 +08:00
|
|
|
{
|
|
|
|
public:
|
2012-10-18 10:39:56 +08:00
|
|
|
~SpriteBlur();
|
2013-06-20 14:17:10 +08:00
|
|
|
bool initWithTexture(Texture2D* texture, const Rect& rect);
|
2014-05-13 10:12:56 +08:00
|
|
|
void initGLProgram();
|
2012-04-17 18:31:31 +08:00
|
|
|
|
2012-06-14 15:13:16 +08:00
|
|
|
static SpriteBlur* create(const char *pszFileName);
|
2014-06-12 09:49:48 +08:00
|
|
|
void setBlurRadius(float radius);
|
|
|
|
void setBlurSampleNum(float num);
|
2012-04-17 18:31:31 +08:00
|
|
|
|
2013-12-25 16:54:09 +08:00
|
|
|
protected:
|
2014-06-11 14:54:14 +08:00
|
|
|
float _blurRadius;
|
2014-06-12 09:49:48 +08:00
|
|
|
float _blurSampleNum;
|
2012-04-17 18:31:31 +08:00
|
|
|
};
|
|
|
|
|
2012-10-18 10:39:56 +08:00
|
|
|
SpriteBlur::~SpriteBlur()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2012-06-14 15:13:16 +08:00
|
|
|
SpriteBlur* SpriteBlur::create(const char *pszFileName)
|
2012-04-17 18:31:31 +08:00
|
|
|
{
|
2014-08-28 07:31:57 +08:00
|
|
|
SpriteBlur* pRet = new (std::nothrow) SpriteBlur();
|
2012-04-17 18:31:31 +08:00
|
|
|
if (pRet && pRet->initWithFile(pszFileName))
|
|
|
|
{
|
|
|
|
pRet->autorelease();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
CC_SAFE_DELETE(pRet);
|
|
|
|
}
|
|
|
|
|
|
|
|
return pRet;
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:17:10 +08:00
|
|
|
bool SpriteBlur::initWithTexture(Texture2D* texture, const Rect& rect)
|
2012-04-17 18:31:31 +08:00
|
|
|
{
|
2014-02-28 17:34:24 +08:00
|
|
|
_blurRadius = 0;
|
2013-06-20 14:17:10 +08:00
|
|
|
if( Sprite::initWithTexture(texture, rect) )
|
2012-04-17 18:31:31 +08:00
|
|
|
{
|
2013-12-31 10:54:37 +08:00
|
|
|
#if CC_ENABLE_CACHE_TEXTURE_DATA
|
2014-07-09 23:03:04 +08:00
|
|
|
auto listener = EventListenerCustom::create(EVENT_RENDERER_RECREATED, [this](EventCustom* event){
|
2014-05-09 07:42:36 +08:00
|
|
|
setGLProgram(nullptr);
|
2014-05-13 10:12:56 +08:00
|
|
|
initGLProgram();
|
2013-12-31 10:54:37 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
|
|
|
|
#endif
|
2012-10-18 10:39:56 +08:00
|
|
|
|
2014-06-11 14:54:14 +08:00
|
|
|
initGLProgram();
|
2014-05-09 03:34:26 +08:00
|
|
|
|
2012-04-17 18:31:31 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-05-13 10:12:56 +08:00
|
|
|
void SpriteBlur::initGLProgram()
|
2012-10-18 10:39:56 +08:00
|
|
|
{
|
2013-06-20 14:17:10 +08:00
|
|
|
GLchar * fragSource = (GLchar*) String::createWithContentsOfFile(
|
2014-02-28 17:34:24 +08:00
|
|
|
FileUtils::getInstance()->fullPathForFilename("Shaders/example_Blur.fsh").c_str())->getCString();
|
2014-05-13 10:12:56 +08:00
|
|
|
auto program = GLProgram::createWithByteArrays(ccPositionTextureColor_noMVP_vert, fragSource);
|
2014-05-09 03:34:26 +08:00
|
|
|
|
2014-05-14 09:12:58 +08:00
|
|
|
auto glProgramState = GLProgramState::getOrCreateWithGLProgram(program);
|
2014-05-13 10:12:56 +08:00
|
|
|
setGLProgramState(glProgramState);
|
2014-06-11 14:54:14 +08:00
|
|
|
|
|
|
|
auto size = getTexture()->getContentSizeInPixels();
|
|
|
|
getGLProgramState()->setUniformVec2("resolution", size);
|
|
|
|
getGLProgramState()->setUniformFloat("blurRadius", _blurRadius);
|
2014-06-12 09:49:48 +08:00
|
|
|
getGLProgramState()->setUniformFloat("sampleNum", 7.0f);
|
2012-04-17 18:31:31 +08:00
|
|
|
}
|
|
|
|
|
2014-06-11 14:54:14 +08:00
|
|
|
void SpriteBlur::setBlurRadius(float radius)
|
2012-04-17 18:31:31 +08:00
|
|
|
{
|
2014-06-11 14:54:14 +08:00
|
|
|
_blurRadius = radius;
|
|
|
|
getGLProgramState()->setUniformFloat("blurRadius", _blurRadius);
|
2012-04-17 18:31:31 +08:00
|
|
|
}
|
|
|
|
|
2014-06-12 09:49:48 +08:00
|
|
|
void SpriteBlur::setBlurSampleNum(float num)
|
|
|
|
{
|
|
|
|
_blurSampleNum = num;
|
|
|
|
getGLProgramState()->setUniformFloat("sampleNum", _blurSampleNum);
|
|
|
|
}
|
|
|
|
|
2012-04-17 18:31:31 +08:00
|
|
|
// ShaderBlur
|
|
|
|
|
|
|
|
ShaderBlur::ShaderBlur()
|
|
|
|
{
|
|
|
|
init();
|
|
|
|
}
|
|
|
|
|
2013-12-19 05:52:10 +08:00
|
|
|
std::string ShaderBlur::title() const
|
2012-04-17 18:31:31 +08:00
|
|
|
{
|
|
|
|
return "Shader: Frag shader";
|
|
|
|
}
|
|
|
|
|
2013-12-19 05:52:10 +08:00
|
|
|
std::string ShaderBlur::subtitle() const
|
2012-04-17 18:31:31 +08:00
|
|
|
{
|
|
|
|
return "Gaussian blur";
|
|
|
|
}
|
|
|
|
|
2014-06-12 09:49:48 +08:00
|
|
|
void ShaderBlur::createSliderCtls()
|
2012-04-17 18:31:31 +08:00
|
|
|
{
|
2013-08-16 16:05:27 +08:00
|
|
|
auto screenSize = Director::getInstance()->getWinSize();
|
2014-02-28 17:34:24 +08:00
|
|
|
|
2014-06-12 09:49:48 +08:00
|
|
|
{
|
|
|
|
ControlSlider *slider = ControlSlider::create("extensions/sliderTrack.png","extensions/sliderProgress.png" ,"extensions/sliderThumb.png");
|
|
|
|
slider->setAnchorPoint(Vec2(0.5f, 1.0f));
|
|
|
|
slider->setMinimumValue(0.0f);
|
|
|
|
slider->setMaximumValue(25.0f);
|
|
|
|
slider->setScale(0.6f);
|
|
|
|
slider->setPosition(Vec2(screenSize.width / 4.0f, screenSize.height / 3.0f));
|
|
|
|
slider->addTargetWithActionForControlEvents(this, cccontrol_selector(ShaderBlur::onRadiusChanged), Control::EventType::VALUE_CHANGED);
|
|
|
|
slider->setValue(2.0f);
|
|
|
|
addChild(slider);
|
|
|
|
_sliderRadiusCtl = slider;
|
|
|
|
|
|
|
|
auto label = Label::createWithTTF("Blur Radius", "fonts/arial.ttf", 12.0f);
|
|
|
|
addChild(label);
|
|
|
|
label->setPosition(Vec2(screenSize.width / 4.0f, screenSize.height / 3.0f - 24.0f));
|
|
|
|
}
|
2014-02-28 17:34:24 +08:00
|
|
|
|
2014-06-12 09:49:48 +08:00
|
|
|
{
|
|
|
|
ControlSlider *slider = ControlSlider::create("extensions/sliderTrack.png","extensions/sliderProgress.png" ,"extensions/sliderThumb.png");
|
|
|
|
slider->setAnchorPoint(Vec2(0.5f, 1.0f));
|
|
|
|
slider->setMinimumValue(0.0f);
|
|
|
|
slider->setMaximumValue(11.0f);
|
|
|
|
slider->setScale(0.6f);
|
|
|
|
slider->setPosition(Vec2(screenSize.width * 3 / 4.0f, screenSize.height / 3.0f));
|
|
|
|
slider->addTargetWithActionForControlEvents(this, cccontrol_selector(ShaderBlur::onSampleNumChanged), Control::EventType::VALUE_CHANGED);
|
|
|
|
slider->setValue(7.0f);
|
|
|
|
addChild(slider);
|
|
|
|
_sliderNumCtrl = slider;
|
|
|
|
|
|
|
|
auto label = Label::createWithTTF("Blur Sample Num", "fonts/arial.ttf", 12.0f);
|
|
|
|
addChild(label);
|
|
|
|
label->setPosition(Vec2(screenSize.width * 3 / 4.0f, screenSize.height / 3.0f - 24.0f));
|
|
|
|
}
|
2012-04-17 18:31:31 +08:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ShaderBlur::init()
|
|
|
|
{
|
|
|
|
if( ShaderTestDemo::init() )
|
|
|
|
{
|
2014-11-22 02:29:36 +08:00
|
|
|
#if CC_TARGET_PLATFORM != CC_PLATFORM_WINRT && CC_TARGET_PLATFORM != CC_PLATFORM_WP8
|
2013-06-15 14:03:30 +08:00
|
|
|
_blurSprite = SpriteBlur::create("Images/grossini.png");
|
2013-08-16 16:05:27 +08:00
|
|
|
auto sprite = Sprite::create("Images/grossini.png");
|
|
|
|
auto s = Director::getInstance()->getWinSize();
|
2014-05-15 01:07:09 +08:00
|
|
|
_blurSprite->setPosition(Vec2(s.width/3, s.height/2));
|
|
|
|
sprite->setPosition(Vec2(2*s.width/3, s.height/2));
|
2012-04-17 18:31:31 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
addChild(_blurSprite);
|
2012-04-17 18:31:31 +08:00
|
|
|
addChild(sprite);
|
|
|
|
|
2014-06-12 09:49:48 +08:00
|
|
|
createSliderCtls();
|
2014-11-22 02:29:36 +08:00
|
|
|
#endif
|
2012-04-17 18:31:31 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-06-12 09:49:48 +08:00
|
|
|
void ShaderBlur::onRadiusChanged(Ref* sender, Control::EventType)
|
2012-04-17 18:31:31 +08:00
|
|
|
{
|
2014-05-13 10:12:56 +08:00
|
|
|
ControlSlider* slider = (ControlSlider*)sender;
|
2014-06-11 14:54:14 +08:00
|
|
|
_blurSprite->setBlurRadius(slider->getValue());
|
2012-04-18 13:58:24 +08:00
|
|
|
}
|
|
|
|
|
2014-06-12 09:49:48 +08:00
|
|
|
void ShaderBlur::onSampleNumChanged(Ref* sender, Control::EventType)
|
|
|
|
{
|
|
|
|
ControlSlider* slider = (ControlSlider*)sender;
|
|
|
|
_blurSprite->setBlurSampleNum(slider->getValue());
|
|
|
|
}
|
|
|
|
|
2012-04-18 13:58:24 +08:00
|
|
|
// ShaderRetroEffect
|
|
|
|
|
|
|
|
ShaderRetroEffect::ShaderRetroEffect()
|
2014-04-09 22:53:59 +08:00
|
|
|
: _label(nullptr)
|
2013-06-15 14:03:30 +08:00
|
|
|
, _accum(0.0f)
|
2012-04-18 13:58:24 +08:00
|
|
|
{
|
|
|
|
init();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ShaderRetroEffect::init()
|
|
|
|
{
|
|
|
|
if( ShaderTestDemo::init() ) {
|
|
|
|
|
2014-05-08 05:38:41 +08:00
|
|
|
GLchar * fragSource = (GLchar*) String::createWithContentsOfFile(FileUtils::getInstance()->fullPathForFilename("Shaders/example_HorizontalColor.fsh"))->getCString();
|
|
|
|
auto p = GLProgram::createWithByteArrays(ccPositionTexture_vert, fragSource);
|
2012-04-18 13:58:24 +08:00
|
|
|
|
2013-08-16 16:05:27 +08:00
|
|
|
auto director = Director::getInstance();
|
|
|
|
auto s = director->getWinSize();
|
2012-04-18 13:58:24 +08:00
|
|
|
|
2014-03-05 14:41:13 +08:00
|
|
|
_label = Label::createWithBMFont("fonts/west_england-64.fnt","RETRO EFFECT");
|
2014-05-15 01:07:09 +08:00
|
|
|
_label->setAnchorPoint(Vec2::ANCHOR_MIDDLE);
|
2014-05-09 07:04:59 +08:00
|
|
|
_label->setGLProgram(p);
|
2012-04-18 13:58:24 +08:00
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
_label->setPosition(Vec2(s.width/2,s.height/2));
|
2012-04-18 13:58:24 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
addChild(_label);
|
2012-04-18 13:58:24 +08:00
|
|
|
|
|
|
|
scheduleUpdate();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-06-08 13:55:28 +08:00
|
|
|
void ShaderRetroEffect::update(float dt)
|
2012-04-18 13:58:24 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_accum += dt;
|
2014-03-07 14:58:44 +08:00
|
|
|
int letterCount = _label->getStringLength();
|
2014-03-05 14:41:13 +08:00
|
|
|
for (int i = 0; i < letterCount; ++i)
|
|
|
|
{
|
|
|
|
auto sprite = _label->getLetter(i);
|
2013-08-16 16:05:27 +08:00
|
|
|
auto oldPosition = sprite->getPosition();
|
2014-05-15 01:07:09 +08:00
|
|
|
sprite->setPosition(Vec2( oldPosition.x, sinf( _accum * 2 + i/2.0) * 20 ));
|
2013-11-29 10:37:40 +08:00
|
|
|
|
2012-04-18 13:58:24 +08:00
|
|
|
// add fabs() to prevent negative scaling
|
2013-06-15 14:03:30 +08:00
|
|
|
float scaleY = ( sinf( _accum * 2 + i/2.0 + 0.707) );
|
2013-11-29 10:37:40 +08:00
|
|
|
|
2012-04-18 13:58:24 +08:00
|
|
|
sprite->setScaleY(scaleY);
|
2013-12-20 05:34:41 +08:00
|
|
|
}
|
2012-04-18 13:58:24 +08:00
|
|
|
}
|
|
|
|
|
2013-12-19 05:52:10 +08:00
|
|
|
std::string ShaderRetroEffect::title() const
|
2012-04-18 13:58:24 +08:00
|
|
|
{
|
|
|
|
return "Shader: Retro test";
|
|
|
|
}
|
|
|
|
|
2013-12-19 05:52:10 +08:00
|
|
|
std::string ShaderRetroEffect::subtitle() const
|
2012-04-18 13:58:24 +08:00
|
|
|
{
|
|
|
|
return "sin() effect with moving colors";
|
2012-04-17 18:31:31 +08:00
|
|
|
}
|
|
|
|
|
2014-05-10 02:48:11 +08:00
|
|
|
|
2014-05-10 11:34:15 +08:00
|
|
|
ShaderLensFlare::ShaderLensFlare()
|
2014-05-10 02:48:11 +08:00
|
|
|
{
|
|
|
|
init();
|
|
|
|
}
|
|
|
|
|
2014-05-10 11:34:15 +08:00
|
|
|
std::string ShaderLensFlare::title() const
|
2014-05-10 02:48:11 +08:00
|
|
|
{
|
|
|
|
return "ShaderToy Test";
|
|
|
|
}
|
|
|
|
|
2014-05-10 11:34:15 +08:00
|
|
|
std::string ShaderLensFlare::subtitle() const
|
2014-05-10 02:48:11 +08:00
|
|
|
{
|
2014-05-10 11:34:15 +08:00
|
|
|
return "Lens Flare ";
|
2014-05-10 02:48:11 +08:00
|
|
|
}
|
|
|
|
|
2014-05-10 11:34:15 +08:00
|
|
|
bool ShaderLensFlare::init()
|
2014-05-10 02:48:11 +08:00
|
|
|
{
|
|
|
|
if (ShaderTestDemo::init())
|
|
|
|
{
|
2014-05-13 10:12:56 +08:00
|
|
|
auto sn = ShaderNode::shaderNodeWithVertex("", "Shaders/shadertoy_LensFlare.fsh");
|
2014-05-10 02:48:11 +08:00
|
|
|
|
|
|
|
auto s = Director::getInstance()->getWinSize();
|
2014-05-15 01:07:09 +08:00
|
|
|
sn->setPosition(Vec2(s.width/2, s.height/2));
|
2014-05-10 11:34:15 +08:00
|
|
|
sn->setContentSize(Size(s.width/2,s.height/2));
|
2014-05-10 02:48:11 +08:00
|
|
|
addChild(sn);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-05-14 03:55:34 +08:00
|
|
|
//
|
|
|
|
// ShaderGlow
|
|
|
|
//
|
|
|
|
ShaderGlow::ShaderGlow()
|
2014-05-10 02:48:11 +08:00
|
|
|
{
|
|
|
|
init();
|
|
|
|
}
|
|
|
|
|
2014-05-14 03:55:34 +08:00
|
|
|
std::string ShaderGlow::title() const
|
2014-05-10 02:48:11 +08:00
|
|
|
{
|
|
|
|
return "ShaderToy Test";
|
|
|
|
}
|
|
|
|
|
2014-05-14 03:55:34 +08:00
|
|
|
std::string ShaderGlow::subtitle() const
|
2014-05-10 02:48:11 +08:00
|
|
|
{
|
2014-05-14 03:55:34 +08:00
|
|
|
return "Glow";
|
2014-05-10 02:48:11 +08:00
|
|
|
}
|
|
|
|
|
2014-05-14 03:55:34 +08:00
|
|
|
bool ShaderGlow::init()
|
2014-05-10 02:48:11 +08:00
|
|
|
{
|
|
|
|
if (ShaderTestDemo::init())
|
|
|
|
{
|
2014-05-14 03:55:34 +08:00
|
|
|
auto sn = ShaderNode::shaderNodeWithVertex("", "Shaders/shadertoy_Glow.fsh");
|
2014-05-10 02:48:11 +08:00
|
|
|
|
|
|
|
auto s = Director::getInstance()->getWinSize();
|
2014-05-15 01:07:09 +08:00
|
|
|
sn->setPosition(Vec2(s.width/2, s.height/2));
|
2014-05-10 11:34:15 +08:00
|
|
|
sn->setContentSize(Size(s.width/2,s.height/2));
|
2014-05-10 02:48:11 +08:00
|
|
|
addChild(sn);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-05-14 03:55:34 +08:00
|
|
|
//
|
|
|
|
// ShaderMultiTexture
|
|
|
|
//
|
2014-06-09 11:39:19 +08:00
|
|
|
ShaderMultiTexture::ShaderMultiTexture():_changedTextureId(0)
|
2014-05-10 02:48:11 +08:00
|
|
|
{
|
|
|
|
init();
|
|
|
|
}
|
|
|
|
|
2014-05-14 03:55:34 +08:00
|
|
|
std::string ShaderMultiTexture::title() const
|
2014-05-10 02:48:11 +08:00
|
|
|
{
|
2014-05-14 03:55:34 +08:00
|
|
|
return "MultiTexture test";
|
2014-05-10 02:48:11 +08:00
|
|
|
}
|
|
|
|
|
2014-05-14 03:55:34 +08:00
|
|
|
std::string ShaderMultiTexture::subtitle() const
|
2014-05-10 02:48:11 +08:00
|
|
|
{
|
2014-05-14 03:55:34 +08:00
|
|
|
return "MultiTexture";
|
2014-05-10 02:48:11 +08:00
|
|
|
}
|
|
|
|
|
2014-05-16 07:45:46 +08:00
|
|
|
ui::Slider* ShaderMultiTexture::createSliderCtl()
|
|
|
|
{
|
|
|
|
auto screenSize = Director::getInstance()->getWinSize();
|
|
|
|
|
|
|
|
ui::Slider* slider = ui::Slider::create();
|
|
|
|
slider->loadBarTexture("cocosui/sliderTrack.png");
|
|
|
|
slider->loadSlidBallTextures("cocosui/sliderThumb.png", "cocosui/sliderThumb.png", "");
|
|
|
|
slider->loadProgressBarTexture("cocosui/sliderProgress.png");
|
2014-05-16 07:52:15 +08:00
|
|
|
slider->setPercent(50);
|
2014-05-16 07:45:46 +08:00
|
|
|
|
|
|
|
slider->setPosition(Vec2(screenSize.width / 2.0f, screenSize.height / 3.0f));
|
|
|
|
addChild(slider);
|
|
|
|
|
|
|
|
slider->addEventListener([&](Ref* sender, ui::Slider::EventType type) {
|
|
|
|
|
|
|
|
if (type == ui::Slider::EventType::ON_PERCENTAGE_CHANGED)
|
|
|
|
{
|
|
|
|
ui::Slider* slider = dynamic_cast<ui::Slider*>(sender);
|
|
|
|
float p = slider->getPercent() / 100.0f;
|
|
|
|
_sprite->getGLProgramState()->setUniformFloat("u_interpolate",p);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return slider;
|
|
|
|
}
|
|
|
|
|
2014-05-14 03:55:34 +08:00
|
|
|
bool ShaderMultiTexture::init()
|
2014-05-10 02:48:11 +08:00
|
|
|
{
|
|
|
|
if (ShaderTestDemo::init())
|
|
|
|
{
|
|
|
|
auto s = Director::getInstance()->getWinSize();
|
2014-05-14 03:55:34 +08:00
|
|
|
|
2014-05-16 07:45:46 +08:00
|
|
|
// Left: normal sprite
|
|
|
|
auto left = Sprite::create("Images/grossinis_sister1.png");
|
|
|
|
addChild(left);
|
|
|
|
left->setPosition(s.width*1/4, s.height/2);
|
|
|
|
|
|
|
|
// Right: normal sprite
|
|
|
|
auto right = Sprite::create("Images/grossinis_sister2.png");
|
2014-06-09 10:58:28 +08:00
|
|
|
addChild(right, 0, rightSpriteTag);
|
2014-05-16 07:45:46 +08:00
|
|
|
right->setPosition(s.width*3/4, s.height/2);
|
|
|
|
|
|
|
|
|
|
|
|
// Center: MultiTexture
|
2014-06-09 10:58:28 +08:00
|
|
|
_sprite = Sprite::createWithTexture(left->getTexture());
|
2014-05-16 07:45:46 +08:00
|
|
|
addChild(_sprite);
|
|
|
|
_sprite->setPosition(Vec2(s.width/2, s.height/2));
|
2014-05-14 03:55:34 +08:00
|
|
|
|
|
|
|
auto glprogram = GLProgram::createWithFilenames("Shaders/example_MultiTexture.vsh", "Shaders/example_MultiTexture.fsh");
|
2014-05-14 09:12:58 +08:00
|
|
|
auto glprogramstate = GLProgramState::getOrCreateWithGLProgram(glprogram);
|
2014-05-16 07:45:46 +08:00
|
|
|
_sprite->setGLProgramState(glprogramstate);
|
2014-05-14 03:55:34 +08:00
|
|
|
|
2014-06-09 10:58:28 +08:00
|
|
|
glprogramstate->setUniformTexture("u_texture1", right->getTexture());
|
2014-05-16 07:52:15 +08:00
|
|
|
glprogramstate->setUniformFloat("u_interpolate",0.5);
|
2014-05-16 07:45:46 +08:00
|
|
|
|
|
|
|
// slider
|
|
|
|
createSliderCtl();
|
2014-06-09 10:58:28 +08:00
|
|
|
|
|
|
|
// menu
|
|
|
|
auto label = Label::createWithTTF(TTFConfig("fonts/arial.ttf"), "change");
|
|
|
|
auto mi = MenuItemLabel::create(label, CC_CALLBACK_1(ShaderMultiTexture::changeTexture, this));
|
|
|
|
auto menu = Menu::create(mi, nullptr);
|
|
|
|
addChild(menu);
|
|
|
|
menu->setPosition(s.width * 7 / 8, s.height / 2);
|
2014-05-16 07:45:46 +08:00
|
|
|
|
2014-05-10 02:48:11 +08:00
|
|
|
return true;
|
|
|
|
}
|
2014-05-14 03:55:34 +08:00
|
|
|
|
2014-05-10 02:48:11 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-06-09 10:58:28 +08:00
|
|
|
void ShaderMultiTexture::changeTexture(Ref*)
|
|
|
|
{
|
|
|
|
static const int textureFilesCount = 3;
|
|
|
|
static const std::string textureFiles[textureFilesCount] = {
|
|
|
|
"Images/grossini.png",
|
|
|
|
"Images/grossinis_sister1.png",
|
|
|
|
"Images/grossinis_sister2.png"
|
|
|
|
};
|
|
|
|
auto textrue = Director::getInstance()->getTextureCache()->addImage(textureFiles[_changedTextureId++ % textureFilesCount]);
|
|
|
|
Sprite* right = dynamic_cast<Sprite*>(getChildByTag(rightSpriteTag));
|
|
|
|
right->setTexture(textrue);
|
|
|
|
auto programState = _sprite->getGLProgramState();
|
|
|
|
programState->setUniformTexture("u_texture1", right->getTexture());
|
|
|
|
}
|