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
|
|
|
|
|
|
|
static int sceneIdx = -1;
|
|
|
|
|
2012-11-20 16:16:16 +08:00
|
|
|
#define MAX_LAYER 9
|
2012-03-21 17:27:13 +08:00
|
|
|
|
2013-06-20 14:17:10 +08:00
|
|
|
static Layer* createShaderLayer(int nIndex)
|
2012-03-21 17:27:13 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
switch (sceneIdx)
|
|
|
|
{
|
|
|
|
case 0: return new ShaderMonjori();
|
|
|
|
case 1: return new ShaderMandelbrot();
|
|
|
|
case 2: return new ShaderJulia();
|
|
|
|
case 3: return new ShaderHeart();
|
|
|
|
case 4: return new ShaderFlower();
|
|
|
|
case 5: return new ShaderPlasma();
|
2012-04-17 18:31:31 +08:00
|
|
|
case 6: return new ShaderBlur();
|
2012-04-18 13:58:24 +08:00
|
|
|
case 7: return new ShaderRetroEffect();
|
2012-11-20 16:16:16 +08:00
|
|
|
case 8: return new ShaderFail();
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
2012-03-21 17:27:13 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
return NULL;
|
2012-03-21 17:27:13 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:17:10 +08:00
|
|
|
static Layer* nextAction(void)
|
2012-03-21 17:27:13 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
sceneIdx++;
|
|
|
|
sceneIdx = sceneIdx % MAX_LAYER;
|
2012-03-21 17:27:13 +08:00
|
|
|
|
2013-08-16 16:05:27 +08:00
|
|
|
auto layer = createShaderLayer(sceneIdx);
|
2013-07-23 08:25:44 +08:00
|
|
|
layer->autorelease();
|
2012-03-21 17:27:13 +08:00
|
|
|
|
2013-07-23 08:25:44 +08:00
|
|
|
return layer;
|
2012-03-21 17:27:13 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:17:10 +08:00
|
|
|
static Layer* backAction(void)
|
2012-03-21 17:27:13 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
sceneIdx--;
|
|
|
|
int total = MAX_LAYER;
|
|
|
|
if( sceneIdx < 0 )
|
|
|
|
sceneIdx += total;
|
|
|
|
|
2013-08-16 16:05:27 +08:00
|
|
|
auto layer = createShaderLayer(sceneIdx);
|
2013-07-23 08:25:44 +08:00
|
|
|
layer->autorelease();
|
2012-03-21 17:27:13 +08:00
|
|
|
|
2013-07-23 08:25:44 +08:00
|
|
|
return layer;
|
2012-03-21 17:27:13 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:17:10 +08:00
|
|
|
static Layer* restartAction(void)
|
2012-03-21 17:27:13 +08:00
|
|
|
{
|
2013-08-16 16:05:27 +08:00
|
|
|
auto layer = createShaderLayer(sceneIdx);
|
2013-07-23 08:25:44 +08:00
|
|
|
layer->autorelease();
|
2012-03-21 17:27:13 +08:00
|
|
|
|
2013-07-23 08:25:44 +08:00
|
|
|
return layer;
|
2012-03-21 17:27:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ShaderTestDemo::ShaderTestDemo()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-07-26 06:53:24 +08:00
|
|
|
void ShaderTestDemo::backCallback(Object* sender)
|
2012-03-21 17:27:13 +08:00
|
|
|
{
|
2013-08-16 16:05:27 +08:00
|
|
|
auto s = new ShaderTestScene();
|
2012-04-19 14:35:52 +08:00
|
|
|
s->addChild( backAction() );
|
2013-07-12 06:24:23 +08:00
|
|
|
Director::getInstance()->replaceScene(s);
|
2012-03-21 17:27:13 +08:00
|
|
|
s->release();
|
|
|
|
}
|
|
|
|
|
2013-07-26 06:53:24 +08:00
|
|
|
void ShaderTestDemo::nextCallback(Object* sender)
|
2012-03-21 17:27:13 +08:00
|
|
|
{
|
2013-08-16 16:05:27 +08:00
|
|
|
auto s = new ShaderTestScene();//CCScene::create();
|
2012-04-19 14:35:52 +08:00
|
|
|
s->addChild( nextAction() );
|
2013-07-12 06:24:23 +08:00
|
|
|
Director::getInstance()->replaceScene(s);
|
2012-03-21 17:27:13 +08:00
|
|
|
s->release();
|
|
|
|
}
|
|
|
|
|
2013-12-19 05:52:10 +08:00
|
|
|
std::string ShaderTestDemo::title() const
|
2012-03-21 17:27:13 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
return "No title";
|
2012-03-21 17:27:13 +08:00
|
|
|
}
|
|
|
|
|
2013-12-19 05:52:10 +08:00
|
|
|
std::string ShaderTestDemo::subtitle() const
|
2012-03-21 17:27:13 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
return "";
|
2012-03-21 17:27:13 +08:00
|
|
|
}
|
|
|
|
|
2013-07-26 06:53:24 +08:00
|
|
|
void ShaderTestDemo::restartCallback(Object* sender)
|
2012-03-21 17:27:13 +08:00
|
|
|
{
|
2013-08-16 16:05:27 +08:00
|
|
|
auto s = new ShaderTestScene();
|
2012-04-19 14:35:52 +08:00
|
|
|
s->addChild(restartAction());
|
2012-03-21 17:27:13 +08:00
|
|
|
|
2013-07-12 06:24:23 +08:00
|
|
|
Director::getInstance()->replaceScene(s);
|
2012-04-19 14:35:52 +08:00
|
|
|
s->release();
|
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()
|
2013-07-05 16:49:22 +08:00
|
|
|
:_center(Vertex2F(0.0f, 0.0f))
|
|
|
|
,_resolution(Vertex2F(0.0f, 0.0f))
|
2013-06-15 14:03:30 +08:00
|
|
|
,_time(0.0f)
|
|
|
|
,_uniformCenter(0)
|
|
|
|
,_uniformResolution(0)
|
|
|
|
,_uniformTime(0)
|
2012-03-21 17:27:13 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2012-10-18 10:15:46 +08:00
|
|
|
ShaderNode::~ShaderNode()
|
|
|
|
{
|
2013-07-12 06:24:23 +08:00
|
|
|
NotificationCenter::getInstance()->removeObserver(this, EVNET_COME_TO_FOREGROUND);
|
2012-10-18 10:15:46 +08:00
|
|
|
}
|
|
|
|
|
2012-03-21 17:27:13 +08:00
|
|
|
ShaderNode* ShaderNode::shaderNodeWithVertex(const char *vert, const char *frag)
|
|
|
|
{
|
2013-08-16 16:05:27 +08:00
|
|
|
auto node = new 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
|
|
|
}
|
|
|
|
|
|
|
|
bool ShaderNode::initWithVertex(const char *vert, const char *frag)
|
|
|
|
{
|
2013-07-12 06:24:23 +08:00
|
|
|
NotificationCenter::getInstance()->addObserver(this,
|
2012-10-18 10:15:46 +08:00
|
|
|
callfuncO_selector(ShaderNode::listenBackToForeground),
|
|
|
|
EVNET_COME_TO_FOREGROUND,
|
|
|
|
NULL);
|
2012-03-21 17:27:13 +08:00
|
|
|
|
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;
|
2013-07-05 16:49:22 +08:00
|
|
|
_resolution = Vertex2F(SIZE_X, SIZE_Y);
|
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));
|
2013-07-12 14:11:55 +08:00
|
|
|
setAnchorPoint(Point(0.5f, 0.5f));
|
2012-10-18 10:15:46 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
_vertFileName = vert;
|
|
|
|
_fragFileName = frag;
|
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
|
|
|
}
|
|
|
|
|
2013-06-20 14:17:10 +08:00
|
|
|
void ShaderNode::listenBackToForeground(Object *obj)
|
2012-10-18 10:15:46 +08:00
|
|
|
{
|
|
|
|
this->setShaderProgram(NULL);
|
2013-06-15 14:03:30 +08:00
|
|
|
loadShaderVertex(_vertFileName.c_str(), _fragFileName.c_str());
|
2012-10-18 10:15:46 +08:00
|
|
|
}
|
|
|
|
|
2012-03-21 17:27:13 +08:00
|
|
|
void ShaderNode::loadShaderVertex(const char *vert, const char *frag)
|
|
|
|
{
|
2013-08-16 16:05:27 +08:00
|
|
|
auto shader = new GLProgram();
|
2012-04-19 14:35:52 +08:00
|
|
|
shader->initWithVertexShaderFilename(vert, frag);
|
2012-03-21 17:27:13 +08:00
|
|
|
|
2013-07-25 17:48:22 +08:00
|
|
|
shader->addAttribute("aVertex", GLProgram::VERTEX_ATTRIB_POSITION);
|
2012-04-19 14:35:52 +08:00
|
|
|
shader->link();
|
2012-03-21 17:27:13 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
shader->updateUniforms();
|
2012-03-21 17:27:13 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
_uniformCenter = glGetUniformLocation(shader->getProgram(), "center");
|
|
|
|
_uniformResolution = glGetUniformLocation(shader->getProgram(), "resolution");
|
|
|
|
_uniformTime = glGetUniformLocation(shader->getProgram(), "time");
|
2012-03-21 17:27:13 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
this->setShaderProgram(shader);
|
2012-03-21 17:27:13 +08:00
|
|
|
|
|
|
|
shader->release();
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2013-06-20 14:17:10 +08:00
|
|
|
void ShaderNode::setPosition(const Point &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();
|
2013-07-05 16:49:22 +08:00
|
|
|
_center = Vertex2F(position.x * CC_CONTENT_SCALE_FACTOR(), position.y * CC_CONTENT_SCALE_FACTOR());
|
2012-03-21 17:27:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void ShaderNode::draw()
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
CC_NODE_DRAW_SETUP();
|
2012-03-21 17:27:13 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
float w = SIZE_X, h = SIZE_Y;
|
|
|
|
GLfloat vertices[12] = {0,0, w,0, w,h, 0,0, 0,h, w,h};
|
2012-03-21 17:27:13 +08:00
|
|
|
|
|
|
|
//
|
2012-04-19 14:35:52 +08:00
|
|
|
// Uniforms
|
|
|
|
//
|
2013-06-15 14:03:30 +08:00
|
|
|
getShaderProgram()->setUniformLocationWith2f(_uniformCenter, _center.x, _center.y);
|
|
|
|
getShaderProgram()->setUniformLocationWith2f(_uniformResolution, _resolution.x, _resolution.y);
|
2012-03-21 17:27:13 +08:00
|
|
|
|
|
|
|
// time changes all the time, so it is Ok to call OpenGL directly, and not the "cached" version
|
2013-06-15 14:03:30 +08:00
|
|
|
glUniform1f(_uniformTime, _time);
|
2012-03-21 17:27:13 +08:00
|
|
|
|
2013-07-26 09:42:53 +08:00
|
|
|
GL::enableVertexAttribs( cocos2d::GL::VERTEX_ATTRIB_FLAG_POSITION );
|
2012-03-21 17:27:13 +08:00
|
|
|
|
2013-07-25 17:48:22 +08:00
|
|
|
glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 2, GL_FLOAT, GL_FALSE, 0, vertices);
|
2012-03-21 17:27:13 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
glDrawArrays(GL_TRIANGLES, 0, 6);
|
|
|
|
|
|
|
|
CC_INCREMENT_GL_DRAWS(1);
|
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())
|
|
|
|
{
|
2013-08-16 16:05:27 +08:00
|
|
|
auto sn = ShaderNode::shaderNodeWithVertex("Shaders/example_Monjori.vsh", "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();
|
2013-07-12 14:11:55 +08:00
|
|
|
sn->setPosition(Point(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())
|
|
|
|
{
|
2013-08-16 16:05:27 +08:00
|
|
|
auto sn = ShaderNode::shaderNodeWithVertex("Shaders/example_Mandelbrot.vsh", "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();
|
2013-07-12 14:11:55 +08:00
|
|
|
sn->setPosition(Point(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;
|
|
|
|
}
|
|
|
|
|
|
|
|
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())
|
|
|
|
{
|
2013-08-16 16:05:27 +08:00
|
|
|
auto sn = ShaderNode::shaderNodeWithVertex("Shaders/example_Julia.vsh", "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();
|
2013-07-12 14:11:55 +08:00
|
|
|
sn->setPosition(Point(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 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())
|
|
|
|
{
|
2013-08-16 16:05:27 +08:00
|
|
|
auto sn = ShaderNode::shaderNodeWithVertex("Shaders/example_Heart.vsh", "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();
|
2013-07-12 14:11:55 +08:00
|
|
|
sn->setPosition(Point(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())
|
|
|
|
{
|
2013-08-16 16:05:27 +08:00
|
|
|
auto sn = ShaderNode::shaderNodeWithVertex("Shaders/example_Flower.vsh", "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();
|
2013-07-12 14:11:55 +08:00
|
|
|
sn->setPosition(Point(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())
|
|
|
|
{
|
2013-08-16 16:05:27 +08:00
|
|
|
auto sn = ShaderNode::shaderNodeWithVertex("Shaders/example_Plasma.vsh", "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();
|
2013-07-12 14:11:55 +08:00
|
|
|
sn->setPosition(Point(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();
|
2012-08-01 15:30:12 +08:00
|
|
|
void setBlurSize(float f);
|
2013-06-20 14:17:10 +08:00
|
|
|
bool initWithTexture(Texture2D* texture, const Rect& rect);
|
2012-04-17 18:31:31 +08:00
|
|
|
void draw();
|
2012-10-18 10:39:56 +08:00
|
|
|
void initProgram();
|
2013-06-20 14:17:10 +08:00
|
|
|
void listenBackToForeground(Object *obj);
|
2012-04-17 18:31:31 +08:00
|
|
|
|
2012-06-14 15:13:16 +08:00
|
|
|
static SpriteBlur* create(const char *pszFileName);
|
2012-04-17 18:31:31 +08:00
|
|
|
|
2013-06-20 14:17:10 +08:00
|
|
|
Point blur_;
|
2012-04-19 14:35:52 +08:00
|
|
|
GLfloat sub_[4];
|
2012-04-17 18:31:31 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
GLuint blurLocation;
|
|
|
|
GLuint subLocation;
|
2012-04-17 18:31:31 +08:00
|
|
|
};
|
|
|
|
|
2012-10-18 10:39:56 +08:00
|
|
|
SpriteBlur::~SpriteBlur()
|
|
|
|
{
|
2013-07-12 06:24:23 +08:00
|
|
|
NotificationCenter::getInstance()->removeObserver(this, EVNET_COME_TO_FOREGROUND);
|
2012-10-18 10:39:56 +08:00
|
|
|
}
|
|
|
|
|
2012-06-14 15:13:16 +08:00
|
|
|
SpriteBlur* SpriteBlur::create(const char *pszFileName)
|
2012-04-17 18:31:31 +08:00
|
|
|
{
|
|
|
|
SpriteBlur* pRet = new SpriteBlur();
|
|
|
|
if (pRet && pRet->initWithFile(pszFileName))
|
|
|
|
{
|
|
|
|
pRet->autorelease();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
CC_SAFE_DELETE(pRet);
|
|
|
|
}
|
|
|
|
|
|
|
|
return pRet;
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:17:10 +08:00
|
|
|
void SpriteBlur::listenBackToForeground(Object *obj)
|
2012-10-18 10:39:56 +08:00
|
|
|
{
|
|
|
|
setShaderProgram(NULL);
|
|
|
|
initProgram();
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:17:10 +08:00
|
|
|
bool SpriteBlur::initWithTexture(Texture2D* texture, const Rect& rect)
|
2012-04-17 18:31:31 +08:00
|
|
|
{
|
2013-06-20 14:17:10 +08:00
|
|
|
if( Sprite::initWithTexture(texture, rect) )
|
2012-04-17 18:31:31 +08:00
|
|
|
{
|
2013-07-12 06:24:23 +08:00
|
|
|
NotificationCenter::getInstance()->addObserver(this,
|
2012-10-18 11:00:44 +08:00
|
|
|
callfuncO_selector(SpriteBlur::listenBackToForeground),
|
2012-10-18 10:39:56 +08:00
|
|
|
EVNET_COME_TO_FOREGROUND,
|
|
|
|
NULL);
|
|
|
|
|
2013-08-16 16:05:27 +08:00
|
|
|
auto s = getTexture()->getContentSizeInPixels();
|
2012-04-17 18:31:31 +08:00
|
|
|
|
2013-07-12 14:11:55 +08:00
|
|
|
blur_ = Point(1/s.width, 1/s.height);
|
2012-04-17 18:31:31 +08:00
|
|
|
sub_[0] = sub_[1] = sub_[2] = sub_[3] = 0;
|
|
|
|
|
2012-10-18 10:39:56 +08:00
|
|
|
this->initProgram();
|
|
|
|
|
2012-04-17 18:31:31 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-10-18 10:39:56 +08:00
|
|
|
void SpriteBlur::initProgram()
|
|
|
|
{
|
2013-06-20 14:17:10 +08:00
|
|
|
GLchar * fragSource = (GLchar*) String::createWithContentsOfFile(
|
2013-07-12 06:24:23 +08:00
|
|
|
FileUtils::getInstance()->fullPathForFilename("Shaders/example_Blur.fsh").c_str())->getCString();
|
2013-08-16 16:05:27 +08:00
|
|
|
auto pProgram = new GLProgram();
|
2012-10-18 10:39:56 +08:00
|
|
|
pProgram->initWithVertexShaderByteArray(ccPositionTextureColor_vert, fragSource);
|
|
|
|
setShaderProgram(pProgram);
|
|
|
|
pProgram->release();
|
|
|
|
|
|
|
|
CHECK_GL_ERROR_DEBUG();
|
|
|
|
|
2013-07-25 17:48:22 +08:00
|
|
|
getShaderProgram()->addAttribute(GLProgram::ATTRIBUTE_NAME_POSITION, GLProgram::VERTEX_ATTRIB_POSITION);
|
|
|
|
getShaderProgram()->addAttribute(GLProgram::ATTRIBUTE_NAME_COLOR, GLProgram::VERTEX_ATTRIB_COLOR);
|
|
|
|
getShaderProgram()->addAttribute(GLProgram::ATTRIBUTE_NAME_TEX_COORD, GLProgram::VERTEX_ATTRIB_TEX_COORDS);
|
2012-10-18 10:39:56 +08:00
|
|
|
|
|
|
|
CHECK_GL_ERROR_DEBUG();
|
|
|
|
|
|
|
|
getShaderProgram()->link();
|
|
|
|
|
|
|
|
CHECK_GL_ERROR_DEBUG();
|
|
|
|
|
|
|
|
getShaderProgram()->updateUniforms();
|
|
|
|
|
|
|
|
CHECK_GL_ERROR_DEBUG();
|
|
|
|
|
|
|
|
subLocation = glGetUniformLocation( getShaderProgram()->getProgram(), "substract");
|
|
|
|
blurLocation = glGetUniformLocation( getShaderProgram()->getProgram(), "blurSize");
|
|
|
|
|
|
|
|
CHECK_GL_ERROR_DEBUG();
|
|
|
|
}
|
|
|
|
|
2012-04-17 18:31:31 +08:00
|
|
|
void SpriteBlur::draw()
|
|
|
|
{
|
2013-07-26 09:42:53 +08:00
|
|
|
GL::enableVertexAttribs(cocos2d::GL::VERTEX_ATTRIB_FLAG_POS_COLOR_TEX );
|
2013-07-05 16:49:22 +08:00
|
|
|
BlendFunc blend = getBlendFunc();
|
2013-07-26 09:42:53 +08:00
|
|
|
GL::blendFunc(blend.src, blend.dst);
|
2012-04-17 18:31:31 +08:00
|
|
|
|
|
|
|
getShaderProgram()->use();
|
2012-11-20 16:34:55 +08:00
|
|
|
getShaderProgram()->setUniformsForBuiltins();
|
2012-04-17 18:31:31 +08:00
|
|
|
getShaderProgram()->setUniformLocationWith2f(blurLocation, blur_.x, blur_.y);
|
|
|
|
getShaderProgram()->setUniformLocationWith4fv(subLocation, sub_, 1);
|
|
|
|
|
2013-07-26 09:42:53 +08:00
|
|
|
GL::bindTexture2D( getTexture()->getName());
|
2012-04-17 18:31:31 +08:00
|
|
|
|
|
|
|
//
|
|
|
|
// Attributes
|
|
|
|
//
|
2013-06-15 14:03:30 +08:00
|
|
|
#define kQuadSize sizeof(_quad.bl)
|
|
|
|
long offset = (long)&_quad;
|
2012-04-17 18:31:31 +08:00
|
|
|
|
|
|
|
// vertex
|
2013-07-05 16:49:22 +08:00
|
|
|
int diff = offsetof( V3F_C4B_T2F, vertices);
|
2013-07-25 17:48:22 +08:00
|
|
|
glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 3, GL_FLOAT, GL_FALSE, kQuadSize, (void*) (offset + diff));
|
2012-04-17 18:31:31 +08:00
|
|
|
|
|
|
|
// texCoods
|
2013-07-05 16:49:22 +08:00
|
|
|
diff = offsetof( V3F_C4B_T2F, texCoords);
|
2013-07-25 17:48:22 +08:00
|
|
|
glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_TEX_COORDS, 2, GL_FLOAT, GL_FALSE, kQuadSize, (void*)(offset + diff));
|
2012-04-17 18:31:31 +08:00
|
|
|
|
|
|
|
// color
|
2013-07-05 16:49:22 +08:00
|
|
|
diff = offsetof( V3F_C4B_T2F, colors);
|
2013-07-25 17:48:22 +08:00
|
|
|
glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_COLOR, 4, GL_UNSIGNED_BYTE, GL_TRUE, kQuadSize, (void*)(offset + diff));
|
2012-04-17 18:31:31 +08:00
|
|
|
|
|
|
|
|
|
|
|
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
|
|
|
|
|
|
|
|
CC_INCREMENT_GL_DRAWS(1);
|
|
|
|
}
|
|
|
|
|
2012-08-01 15:30:12 +08:00
|
|
|
void SpriteBlur::setBlurSize(float f)
|
2012-04-17 18:31:31 +08:00
|
|
|
{
|
2013-08-16 16:05:27 +08:00
|
|
|
auto s = getTexture()->getContentSizeInPixels();
|
2012-04-17 18:31:31 +08:00
|
|
|
|
2013-07-12 14:11:55 +08:00
|
|
|
blur_ = Point(1/s.width, 1/s.height);
|
2013-07-11 16:38:58 +08:00
|
|
|
blur_ = blur_ * f;
|
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";
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:17:10 +08:00
|
|
|
ControlSlider* ShaderBlur::createSliderCtl()
|
2012-04-17 18:31:31 +08:00
|
|
|
{
|
2013-08-16 16:05:27 +08:00
|
|
|
auto screenSize = Director::getInstance()->getWinSize();
|
2012-04-17 18:31:31 +08:00
|
|
|
|
2013-06-20 14:17:10 +08:00
|
|
|
ControlSlider *slider = ControlSlider::create("extensions/sliderTrack.png","extensions/sliderProgress.png" ,"extensions/sliderThumb.png");
|
2013-07-12 14:11:55 +08:00
|
|
|
slider->setAnchorPoint(Point(0.5f, 1.0f));
|
2012-04-17 18:31:31 +08:00
|
|
|
slider->setMinimumValue(0.0f); // Sets the min value of range
|
|
|
|
slider->setMaximumValue(3.0f); // Sets the max value of range
|
2012-04-18 13:58:24 +08:00
|
|
|
slider->setValue(1.0f);
|
2013-07-12 14:11:55 +08:00
|
|
|
slider->setPosition(Point(screenSize.width / 2.0f, screenSize.height / 3.0f));
|
2012-04-17 18:31:31 +08:00
|
|
|
|
|
|
|
// When the value of the slider will change, the given selector will be call
|
2013-07-26 14:37:26 +08:00
|
|
|
slider->addTargetWithActionForControlEvents(this, cccontrol_selector(ShaderBlur::sliderAction), Control::EventType::VALUE_CHANGED);
|
2012-04-17 18:31:31 +08:00
|
|
|
|
|
|
|
return slider;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ShaderBlur::init()
|
|
|
|
{
|
|
|
|
if( ShaderTestDemo::init() )
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_blurSprite = SpriteBlur::create("Images/grossini.png");
|
2012-04-17 18:31:31 +08:00
|
|
|
|
2013-08-16 16:05:27 +08:00
|
|
|
auto sprite = Sprite::create("Images/grossini.png");
|
2012-04-17 18:31:31 +08:00
|
|
|
|
2013-08-16 16:05:27 +08:00
|
|
|
auto s = Director::getInstance()->getWinSize();
|
2013-07-12 14:11:55 +08:00
|
|
|
_blurSprite->setPosition(Point(s.width/3, s.height/2));
|
|
|
|
sprite->setPosition(Point(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);
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
_sliderCtl = createSliderCtl();
|
2012-04-17 18:31:31 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
addChild(_sliderCtl);
|
2012-04-17 18:31:31 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-07-26 14:37:26 +08:00
|
|
|
void ShaderBlur::sliderAction(Object* sender, Control::EventType controlEvent)
|
2012-04-17 18:31:31 +08:00
|
|
|
{
|
2013-06-20 14:17:10 +08:00
|
|
|
ControlSlider* pSlider = (ControlSlider*)sender;
|
2013-06-15 14:03:30 +08:00
|
|
|
_blurSprite->setBlurSize(pSlider->getValue());
|
2012-04-18 13:58:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// ShaderRetroEffect
|
|
|
|
|
|
|
|
ShaderRetroEffect::ShaderRetroEffect()
|
2013-06-15 14:03:30 +08:00
|
|
|
: _label(NULL)
|
|
|
|
, _accum(0.0f)
|
2012-04-18 13:58:24 +08:00
|
|
|
{
|
|
|
|
init();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ShaderRetroEffect::init()
|
|
|
|
{
|
|
|
|
if( ShaderTestDemo::init() ) {
|
|
|
|
|
2013-07-12 06:24:23 +08:00
|
|
|
GLchar * fragSource = (GLchar*) String::createWithContentsOfFile(FileUtils::getInstance()->fullPathForFilename("Shaders/example_HorizontalColor.fsh").c_str())->getCString();
|
2013-08-16 16:05:27 +08:00
|
|
|
auto p = new GLProgram();
|
2012-04-18 13:58:24 +08:00
|
|
|
p->initWithVertexShaderByteArray(ccPositionTexture_vert, fragSource);
|
|
|
|
|
2013-07-25 17:48:22 +08:00
|
|
|
p->addAttribute(GLProgram::ATTRIBUTE_NAME_POSITION, GLProgram::VERTEX_ATTRIB_POSITION);
|
|
|
|
p->addAttribute(GLProgram::ATTRIBUTE_NAME_TEX_COORD, GLProgram::VERTEX_ATTRIB_TEX_COORDS);
|
2012-04-18 13:58:24 +08:00
|
|
|
|
|
|
|
p->link();
|
|
|
|
p->updateUniforms();
|
|
|
|
|
|
|
|
|
2013-08-16 16:05:27 +08:00
|
|
|
auto director = Director::getInstance();
|
|
|
|
auto s = director->getWinSize();
|
2012-04-18 13:58:24 +08:00
|
|
|
|
2013-06-20 14:17:10 +08:00
|
|
|
_label = LabelBMFont::create("RETRO EFFECT", "fonts/west_england-64.fnt");
|
2012-04-18 13:58:24 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
_label->setShaderProgram(p);
|
2012-04-18 13:58:24 +08:00
|
|
|
|
|
|
|
p->release();
|
|
|
|
|
|
|
|
|
2013-07-12 14:11:55 +08:00
|
|
|
_label->setPosition(Point(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;
|
2012-04-18 13:58:24 +08:00
|
|
|
|
|
|
|
int i=0;
|
2013-12-17 17:45:29 +08:00
|
|
|
auto& children = _label->getChildren();
|
|
|
|
std::for_each(children.begin(), children.end(), [&i, this](Node* sprite){
|
2012-04-18 13:58:24 +08:00
|
|
|
i++;
|
2013-08-16 16:05:27 +08:00
|
|
|
auto oldPosition = sprite->getPosition();
|
2013-07-12 14:11:55 +08:00
|
|
|
sprite->setPosition(Point( 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-11-29 10:37:40 +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
|
|
|
}
|
|
|
|
|
2012-11-20 16:16:16 +08:00
|
|
|
// ShaderFail
|
|
|
|
const GLchar *shader_frag_fail = "\n\
|
|
|
|
#ifdef GL_ES \n\
|
|
|
|
precision lowp float; \n\
|
|
|
|
#endif \n\
|
|
|
|
\n\
|
|
|
|
varying vec2 v_texCoord; \n\
|
|
|
|
uniform sampler2D CC_Texture0; \n\
|
|
|
|
\n\
|
|
|
|
vec4 colors[10]; \n\
|
|
|
|
\n\
|
|
|
|
void main(void) \n\
|
|
|
|
{ \n\
|
|
|
|
colors[0] = vec4(1,0,0,1); \n\
|
|
|
|
colors[1] = vec4(0,1,0,1); \n\
|
|
|
|
colors[2] = vec4(0,0,1,1); \n\
|
|
|
|
colors[3] = vec4(0,1,1,1); \n\
|
|
|
|
colors[4] = vec4(1,0,1,1); \n\
|
|
|
|
colors[5] = vec4(1,1,0,1); \n\
|
|
|
|
colors[6] = vec4(1,1,1,1); \n\
|
|
|
|
colors[7] = vec4(1,0.5,0,1); \n\
|
|
|
|
colors[8] = vec4(1,0.5,0.5,1); \n\
|
|
|
|
colors[9] = vec4(0.5,0.5,1,1); \n\
|
|
|
|
\n\
|
|
|
|
int y = int( mod(gl_FragCoord.y / 3.0, 10.0 ) ); \n\
|
|
|
|
gl_FragColor = colors[z] * texture2D(CC_Texture0, v_texCoord); \n\
|
|
|
|
} \n\
|
|
|
|
\n";
|
|
|
|
|
|
|
|
ShaderFail::ShaderFail()
|
|
|
|
{
|
2013-08-16 16:05:27 +08:00
|
|
|
auto p = new GLProgram();
|
2012-11-20 16:16:16 +08:00
|
|
|
p->initWithVertexShaderByteArray(ccPositionTexture_vert, shader_frag_fail);
|
|
|
|
|
2013-07-25 17:48:22 +08:00
|
|
|
p->addAttribute(GLProgram::ATTRIBUTE_NAME_POSITION, GLProgram::VERTEX_ATTRIB_POSITION);
|
|
|
|
p->addAttribute(GLProgram::ATTRIBUTE_NAME_TEX_COORD, GLProgram::VERTEX_ATTRIB_TEX_COORDS);
|
2012-11-20 16:16:16 +08:00
|
|
|
|
|
|
|
p->link();
|
|
|
|
p->updateUniforms();
|
|
|
|
p->release();
|
|
|
|
}
|
|
|
|
|
2013-12-19 05:52:10 +08:00
|
|
|
std::string ShaderFail::title() const
|
2012-11-20 16:16:16 +08:00
|
|
|
{
|
|
|
|
return "Shader: Invalid shader";
|
|
|
|
}
|
|
|
|
|
2013-12-19 05:52:10 +08:00
|
|
|
std::string ShaderFail::subtitle() const
|
2012-11-20 16:16:16 +08:00
|
|
|
{
|
|
|
|
return "See console for output with useful error log";
|
|
|
|
}
|
|
|
|
|
2012-03-21 17:27:13 +08:00
|
|
|
///---------------------------------------
|
2012-11-20 16:16:16 +08:00
|
|
|
//
|
2012-03-21 17:27:13 +08:00
|
|
|
// ShaderTestScene
|
|
|
|
//
|
|
|
|
///---------------------------------------
|
|
|
|
void ShaderTestScene::runThisTest()
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
sceneIdx = -1;
|
2012-03-21 17:27:13 +08:00
|
|
|
addChild(nextAction());
|
|
|
|
|
2013-07-12 06:24:23 +08:00
|
|
|
Director::getInstance()->replaceScene(this);
|
2012-03-21 17:27:13 +08:00
|
|
|
}
|