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-04-19 14:35:52 +08:00
|
|
|
#define MAX_LAYER 8
|
2012-03-21 17:27:13 +08:00
|
|
|
|
|
|
|
static CCLayer* createShaderLayer(int nIndex)
|
|
|
|
{
|
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-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
|
|
|
}
|
|
|
|
|
|
|
|
static CCLayer* nextAction(void)
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
sceneIdx++;
|
|
|
|
sceneIdx = sceneIdx % MAX_LAYER;
|
2012-03-21 17:27:13 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
CCLayer* pLayer = createShaderLayer(sceneIdx);
|
|
|
|
pLayer->autorelease();
|
2012-03-21 17:27:13 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
return pLayer;
|
2012-03-21 17:27:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static CCLayer* backAction(void)
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
sceneIdx--;
|
|
|
|
int total = MAX_LAYER;
|
|
|
|
if( sceneIdx < 0 )
|
|
|
|
sceneIdx += total;
|
|
|
|
|
|
|
|
CCLayer* pLayer = createShaderLayer(sceneIdx);
|
|
|
|
pLayer->autorelease();
|
2012-03-21 17:27:13 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
return pLayer;
|
2012-03-21 17:27:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static CCLayer* restartAction(void)
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
CCLayer* pLayer = createShaderLayer(sceneIdx);
|
|
|
|
pLayer->autorelease();
|
2012-03-21 17:27:13 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
return pLayer;
|
2012-03-21 17:27:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ShaderTestDemo::ShaderTestDemo()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ShaderTestDemo::init()
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
CCSize s = CCDirector::sharedDirector()->getWinSize();
|
2012-03-21 17:27:13 +08:00
|
|
|
|
2012-06-14 15:13:16 +08:00
|
|
|
CCLabelTTF *label = CCLabelTTF::create(title().c_str(), "Arial", 26);
|
2012-04-19 14:35:52 +08:00
|
|
|
addChild(label, 1);
|
|
|
|
label->setPosition(ccp(s.width/2, s.height-50));
|
|
|
|
label->setColor(ccRED);
|
2012-03-21 17:27:13 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
std::string subtitle = this->subtitle();
|
|
|
|
if (subtitle.length() > 0)
|
|
|
|
{
|
2012-06-14 15:13:16 +08:00
|
|
|
CCLabelTTF *l = CCLabelTTF::create(subtitle.c_str(), "Thonburi", 16);
|
2012-04-19 14:35:52 +08:00
|
|
|
addChild(l, 1);
|
|
|
|
l->setPosition(ccp(s.width/2, s.height-80));
|
|
|
|
}
|
2012-03-21 17:27:13 +08:00
|
|
|
|
2012-06-14 15:13:16 +08:00
|
|
|
CCMenuItemImage *item1 = CCMenuItemImage::create(s_pPathB1, s_pPathB2, this, menu_selector(ShaderTestDemo::backCallback));
|
|
|
|
CCMenuItemImage *item2 = CCMenuItemImage::create(s_pPathR1, s_pPathR2, this, menu_selector(ShaderTestDemo::restartCallback));
|
|
|
|
CCMenuItemImage *item3 = CCMenuItemImage::create(s_pPathF1, s_pPathF2, this, menu_selector(ShaderTestDemo::nextCallback));
|
2012-03-21 17:27:13 +08:00
|
|
|
|
2012-06-14 15:13:16 +08:00
|
|
|
CCMenu *menu = CCMenu::create(item1, item2, item3, NULL);
|
2012-03-21 17:27:13 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
menu->setPosition(ccp(0, 0));
|
2012-06-13 10:41:04 +08:00
|
|
|
item1->setPosition(s.width/2- item2->getContentSize().width*2, item2->getContentSize().height/2);
|
|
|
|
item2->setPosition(s.width/2, item2->getContentSize().height/2);
|
|
|
|
item3->setPosition(s.width/2 + item2->getContentSize().width*2, item2->getContentSize().height/2);
|
2012-04-19 14:35:52 +08:00
|
|
|
addChild(menu, 1);
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void ShaderTestDemo::backCallback(CCObject* pSender)
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
CCScene* s = new ShaderTestScene();
|
|
|
|
s->addChild( backAction() );
|
|
|
|
CCDirector::sharedDirector()->replaceScene(s);
|
2012-03-21 17:27:13 +08:00
|
|
|
s->release();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ShaderTestDemo::nextCallback(CCObject* pSender)
|
|
|
|
{
|
2012-06-14 15:13:16 +08:00
|
|
|
CCScene* s = new ShaderTestScene();//CCScene::create();
|
2012-04-19 14:35:52 +08:00
|
|
|
s->addChild( nextAction() );
|
|
|
|
CCDirector::sharedDirector()->replaceScene(s);
|
2012-03-21 17:27:13 +08:00
|
|
|
s->release();
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string ShaderTestDemo::title()
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
return "No title";
|
2012-03-21 17:27:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string ShaderTestDemo::subtitle()
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
return "";
|
2012-03-21 17:27:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void ShaderTestDemo::restartCallback(CCObject* pSender)
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
CCScene* s = new ShaderTestScene();
|
|
|
|
s->addChild(restartAction());
|
2012-03-21 17:27:13 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
CCDirector::sharedDirector()->replaceScene(s);
|
|
|
|
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()
|
|
|
|
:m_center(vertex2(0.0f, 0.0f))
|
|
|
|
,m_resolution(vertex2(0.0f, 0.0f))
|
|
|
|
,m_time(0.0f)
|
|
|
|
,m_uniformCenter(0)
|
|
|
|
,m_uniformResolution(0)
|
|
|
|
,m_uniformTime(0)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2012-10-18 10:15:46 +08:00
|
|
|
ShaderNode::~ShaderNode()
|
|
|
|
{
|
|
|
|
CCNotificationCenter::sharedNotificationCenter()->removeObserver(this, EVNET_COME_TO_FOREGROUND);
|
|
|
|
}
|
|
|
|
|
2012-03-21 17:27:13 +08:00
|
|
|
ShaderNode* ShaderNode::shaderNodeWithVertex(const char *vert, const char *frag)
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
ShaderNode *node = new ShaderNode();
|
|
|
|
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)
|
|
|
|
{
|
2012-10-18 10:15:46 +08:00
|
|
|
CCNotificationCenter::sharedNotificationCenter()->addObserver(this,
|
|
|
|
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
|
|
|
|
|
|
|
m_time = 0;
|
2012-04-19 14:35:52 +08:00
|
|
|
m_resolution = vertex2(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
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
setContentSize(CCSizeMake(SIZE_X, SIZE_Y));
|
|
|
|
setAnchorPoint(ccp(0.5f, 0.5f));
|
2012-10-18 10:15:46 +08:00
|
|
|
|
|
|
|
m_vertFileName = vert;
|
|
|
|
m_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
|
|
|
}
|
|
|
|
|
2012-10-18 10:15:46 +08:00
|
|
|
void ShaderNode::listenBackToForeground(CCObject *obj)
|
|
|
|
{
|
|
|
|
this->setShaderProgram(NULL);
|
|
|
|
loadShaderVertex(m_vertFileName.c_str(), m_fragFileName.c_str());
|
|
|
|
}
|
|
|
|
|
2012-03-21 17:27:13 +08:00
|
|
|
void ShaderNode::loadShaderVertex(const char *vert, const char *frag)
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
CCGLProgram *shader = new CCGLProgram();
|
|
|
|
shader->initWithVertexShaderFilename(vert, frag);
|
2012-03-21 17:27:13 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
shader->addAttribute("aVertex", kCCVertexAttrib_Position);
|
|
|
|
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
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
m_uniformCenter = glGetUniformLocation(shader->getProgram(), "center");
|
|
|
|
m_uniformResolution = glGetUniformLocation(shader->getProgram(), "resolution");
|
|
|
|
m_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
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
m_time += dt;
|
2012-03-21 17:27:13 +08:00
|
|
|
}
|
|
|
|
|
2012-03-21 22:49:58 +08:00
|
|
|
void ShaderNode::setPosition(const CCPoint &newPosition)
|
2012-03-21 17:27:13 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
CCNode::setPosition(newPosition);
|
|
|
|
CCPoint position = getPosition();
|
|
|
|
m_center = vertex2(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
|
|
|
|
//
|
|
|
|
getShaderProgram()->setUniformLocationWith2f(m_uniformCenter, m_center.x, m_center.y);
|
|
|
|
getShaderProgram()->setUniformLocationWith2f(m_uniformResolution, m_resolution.x, m_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
|
2012-04-19 14:35:52 +08:00
|
|
|
glUniform1f(m_uniformTime, m_time);
|
2012-03-21 17:27:13 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
ccGLEnableVertexAttribs( kCCVertexAttribFlag_Position );
|
2012-03-21 17:27:13 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
glVertexAttribPointer(kCCVertexAttrib_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())
|
|
|
|
{
|
|
|
|
ShaderNode *sn = ShaderNode::shaderNodeWithVertex("Shaders/example_Monjori.vsh", "Shaders/example_Monjori.fsh");
|
2012-03-21 17:27:13 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
CCSize s = CCDirector::sharedDirector()->getWinSize();
|
|
|
|
sn->setPosition(ccp(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
|
|
|
}
|
|
|
|
|
|
|
|
std::string ShaderMonjori::title()
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
return "Shader: Frag shader";
|
2012-03-21 17:27:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string ShaderMonjori::subtitle()
|
|
|
|
{
|
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())
|
|
|
|
{
|
|
|
|
ShaderNode *sn = ShaderNode::shaderNodeWithVertex("Shaders/example_Mandelbrot.vsh", "Shaders/example_Mandelbrot.fsh");
|
2012-03-21 17:27:13 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
CCSize s = CCDirector::sharedDirector()->getWinSize();
|
|
|
|
sn->setPosition(ccp(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
|
|
|
}
|
|
|
|
|
|
|
|
std::string ShaderMandelbrot::title()
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
return "Shader: Frag shader";
|
2012-03-21 17:27:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string ShaderMandelbrot::subtitle()
|
|
|
|
{
|
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())
|
|
|
|
{
|
|
|
|
ShaderNode *sn = ShaderNode::shaderNodeWithVertex("Shaders/example_Julia.vsh", "Shaders/example_Julia.fsh");
|
2012-03-21 17:27:13 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
CCSize s = CCDirector::sharedDirector()->getWinSize();
|
|
|
|
sn->setPosition(ccp(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
|
|
|
}
|
|
|
|
|
|
|
|
std::string ShaderJulia::title()
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
return "Shader: Frag shader";
|
2012-03-21 17:27:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string ShaderJulia::subtitle()
|
|
|
|
{
|
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())
|
|
|
|
{
|
|
|
|
ShaderNode *sn = ShaderNode::shaderNodeWithVertex("Shaders/example_Heart.vsh", "Shaders/example_Heart.fsh");
|
2012-03-21 17:27:13 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
CCSize s = CCDirector::sharedDirector()->getWinSize();
|
|
|
|
sn->setPosition(ccp(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
|
|
|
}
|
|
|
|
|
|
|
|
std::string ShaderHeart::title()
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
return "Shader: Frag shader";
|
2012-03-21 17:27:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string ShaderHeart::subtitle()
|
|
|
|
{
|
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())
|
|
|
|
{
|
|
|
|
ShaderNode *sn = ShaderNode::shaderNodeWithVertex("Shaders/example_Flower.vsh", "Shaders/example_Flower.fsh");
|
2012-03-21 17:27:13 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
CCSize s = CCDirector::sharedDirector()->getWinSize();
|
|
|
|
sn->setPosition(ccp(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
|
|
|
}
|
|
|
|
|
|
|
|
std::string ShaderFlower::title()
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
return "Shader: Frag shader";
|
2012-03-21 17:27:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string ShaderFlower::subtitle()
|
|
|
|
{
|
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())
|
|
|
|
{
|
|
|
|
ShaderNode *sn = ShaderNode::shaderNodeWithVertex("Shaders/example_Plasma.vsh", "Shaders/example_Plasma.fsh");
|
2012-03-21 17:27:13 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
CCSize s = CCDirector::sharedDirector()->getWinSize();
|
|
|
|
sn->setPosition(ccp(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
|
|
|
}
|
|
|
|
|
|
|
|
std::string ShaderPlasma::title()
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
return "Shader: Frag shader";
|
2012-03-21 17:27:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string ShaderPlasma::subtitle()
|
|
|
|
{
|
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
|
|
|
|
|
|
|
|
class SpriteBlur : public CCSprite
|
|
|
|
{
|
|
|
|
public:
|
2012-10-18 10:39:56 +08:00
|
|
|
~SpriteBlur();
|
2012-08-01 15:30:12 +08:00
|
|
|
void setBlurSize(float f);
|
2012-04-17 18:31:31 +08:00
|
|
|
bool initWithTexture(CCTexture2D* texture, const CCRect& rect);
|
|
|
|
void draw();
|
2012-10-18 10:39:56 +08:00
|
|
|
void initProgram();
|
|
|
|
void listenBackToForeground(CCObject *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
|
|
|
|
|
|
|
CCPoint 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()
|
|
|
|
{
|
|
|
|
CCNotificationCenter::sharedNotificationCenter()->removeObserver(this, EVNET_COME_TO_FOREGROUND);
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2012-10-18 10:39:56 +08:00
|
|
|
void SpriteBlur::listenBackToForeground(CCObject *obj)
|
|
|
|
{
|
|
|
|
setShaderProgram(NULL);
|
|
|
|
initProgram();
|
|
|
|
}
|
|
|
|
|
2012-04-17 18:31:31 +08:00
|
|
|
bool SpriteBlur::initWithTexture(CCTexture2D* texture, const CCRect& rect)
|
|
|
|
{
|
|
|
|
if( CCSprite::initWithTexture(texture, rect) )
|
|
|
|
{
|
2012-10-18 10:39:56 +08:00
|
|
|
CCNotificationCenter::sharedNotificationCenter()->addObserver(this,
|
|
|
|
callfuncO_selector(ShaderNode::listenBackToForeground),
|
|
|
|
EVNET_COME_TO_FOREGROUND,
|
|
|
|
NULL);
|
|
|
|
|
2012-04-17 18:31:31 +08:00
|
|
|
CCSize s = getTexture()->getContentSizeInPixels();
|
|
|
|
|
|
|
|
blur_ = ccp(1/s.width, 1/s.height);
|
|
|
|
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()
|
|
|
|
{
|
|
|
|
GLchar * fragSource = (GLchar*) CCString::createWithContentsOfFile(
|
|
|
|
CCFileUtils::sharedFileUtils()->fullPathFromRelativePath("Shaders/example_Blur.fsh"))->getCString();
|
|
|
|
CCGLProgram* pProgram = new CCGLProgram();
|
|
|
|
pProgram->initWithVertexShaderByteArray(ccPositionTextureColor_vert, fragSource);
|
|
|
|
setShaderProgram(pProgram);
|
|
|
|
pProgram->release();
|
|
|
|
|
|
|
|
CHECK_GL_ERROR_DEBUG();
|
|
|
|
|
|
|
|
getShaderProgram()->addAttribute(kCCAttributeNamePosition, kCCVertexAttrib_Position);
|
|
|
|
getShaderProgram()->addAttribute(kCCAttributeNameColor, kCCVertexAttrib_Color);
|
|
|
|
getShaderProgram()->addAttribute(kCCAttributeNameTexCoord, kCCVertexAttrib_TexCoords);
|
|
|
|
|
|
|
|
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()
|
|
|
|
{
|
|
|
|
ccGLEnableVertexAttribs(kCCVertexAttribFlag_PosColorTex );
|
|
|
|
ccGLBlendFunc( m_sBlendFunc.src, m_sBlendFunc.dst );
|
|
|
|
|
|
|
|
getShaderProgram()->use();
|
|
|
|
getShaderProgram()->setUniformForModelViewProjectionMatrix();
|
|
|
|
getShaderProgram()->setUniformLocationWith2f(blurLocation, blur_.x, blur_.y);
|
|
|
|
getShaderProgram()->setUniformLocationWith4fv(subLocation, sub_, 1);
|
|
|
|
|
|
|
|
ccGLBindTexture2D( getTexture()->getName() );
|
|
|
|
|
|
|
|
//
|
|
|
|
// Attributes
|
|
|
|
//
|
|
|
|
#define kQuadSize sizeof(m_sQuad.bl)
|
|
|
|
long offset = (long)&m_sQuad;
|
|
|
|
|
|
|
|
// vertex
|
|
|
|
int diff = offsetof( ccV3F_C4B_T2F, vertices);
|
|
|
|
glVertexAttribPointer(kCCVertexAttrib_Position, 3, GL_FLOAT, GL_FALSE, kQuadSize, (void*) (offset + diff));
|
|
|
|
|
|
|
|
// texCoods
|
|
|
|
diff = offsetof( ccV3F_C4B_T2F, texCoords);
|
|
|
|
glVertexAttribPointer(kCCVertexAttrib_TexCoords, 2, GL_FLOAT, GL_FALSE, kQuadSize, (void*)(offset + diff));
|
|
|
|
|
|
|
|
// color
|
|
|
|
diff = offsetof( ccV3F_C4B_T2F, colors);
|
|
|
|
glVertexAttribPointer(kCCVertexAttrib_Color, 4, GL_UNSIGNED_BYTE, GL_TRUE, kQuadSize, (void*)(offset + diff));
|
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
{
|
|
|
|
CCSize s = getTexture()->getContentSizeInPixels();
|
|
|
|
|
|
|
|
blur_ = ccp(1/s.width, 1/s.height);
|
|
|
|
blur_ = ccpMult(blur_,f);
|
|
|
|
}
|
|
|
|
|
|
|
|
// ShaderBlur
|
|
|
|
|
|
|
|
ShaderBlur::ShaderBlur()
|
|
|
|
{
|
|
|
|
init();
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string ShaderBlur::title()
|
|
|
|
{
|
|
|
|
return "Shader: Frag shader";
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string ShaderBlur::subtitle()
|
|
|
|
{
|
|
|
|
return "Gaussian blur";
|
|
|
|
}
|
|
|
|
|
|
|
|
CCControlSlider* ShaderBlur::createSliderCtl()
|
|
|
|
{
|
|
|
|
CCSize screenSize = CCDirector::sharedDirector()->getWinSize();
|
|
|
|
|
2012-06-14 15:13:16 +08:00
|
|
|
CCControlSlider *slider = CCControlSlider::create("extensions/sliderTrack.png","extensions/sliderProgress.png" ,"extensions/sliderThumb.png");
|
2012-04-17 18:31:31 +08:00
|
|
|
slider->setAnchorPoint(ccp(0.5f, 1.0f));
|
|
|
|
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);
|
2012-04-17 18:31:31 +08:00
|
|
|
slider->setPosition(ccp(screenSize.width / 2.0f, screenSize.height / 3.0f));
|
|
|
|
|
|
|
|
// When the value of the slider will change, the given selector will be call
|
2012-06-12 05:57:30 +08:00
|
|
|
slider->addTargetWithActionForControlEvents(this, cccontrol_selector(ShaderBlur::sliderAction), CCControlEventValueChanged);
|
2012-04-17 18:31:31 +08:00
|
|
|
|
|
|
|
return slider;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ShaderBlur::init()
|
|
|
|
{
|
|
|
|
if( ShaderTestDemo::init() )
|
|
|
|
{
|
2012-06-14 15:13:16 +08:00
|
|
|
m_pBlurSprite = SpriteBlur::create("Images/grossini.png");
|
2012-04-17 18:31:31 +08:00
|
|
|
|
2012-06-14 15:13:16 +08:00
|
|
|
CCSprite *sprite = CCSprite::create("Images/grossini.png");
|
2012-04-17 18:31:31 +08:00
|
|
|
|
|
|
|
CCSize s = CCDirector::sharedDirector()->getWinSize();
|
2012-04-18 13:58:24 +08:00
|
|
|
m_pBlurSprite->setPosition(ccp(s.width/3, s.height/2));
|
2012-04-17 18:31:31 +08:00
|
|
|
sprite->setPosition(ccp(2*s.width/3, s.height/2));
|
|
|
|
|
2012-04-18 13:58:24 +08:00
|
|
|
addChild(m_pBlurSprite);
|
2012-04-17 18:31:31 +08:00
|
|
|
addChild(sprite);
|
|
|
|
|
2012-04-18 13:58:24 +08:00
|
|
|
m_pSliderCtl = createSliderCtl();
|
2012-04-17 18:31:31 +08:00
|
|
|
|
2012-04-18 13:58:24 +08:00
|
|
|
addChild(m_pSliderCtl);
|
2012-04-17 18:31:31 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-06-14 18:32:44 +08:00
|
|
|
void ShaderBlur::sliderAction(CCObject* sender, CCControlEvent controlEvent)
|
2012-04-17 18:31:31 +08:00
|
|
|
{
|
|
|
|
CCControlSlider* pSlider = (CCControlSlider*)sender;
|
2012-04-18 13:58:24 +08:00
|
|
|
m_pBlurSprite->setBlurSize(pSlider->getValue());
|
|
|
|
}
|
|
|
|
|
|
|
|
// ShaderRetroEffect
|
|
|
|
|
|
|
|
ShaderRetroEffect::ShaderRetroEffect()
|
|
|
|
: m_pLabel(NULL)
|
|
|
|
, m_fAccum(0.0f)
|
|
|
|
{
|
|
|
|
init();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ShaderRetroEffect::init()
|
|
|
|
{
|
|
|
|
if( ShaderTestDemo::init() ) {
|
|
|
|
|
2012-06-27 17:08:50 +08:00
|
|
|
GLchar * fragSource = (GLchar*) CCString::createWithContentsOfFile(CCFileUtils::sharedFileUtils()->fullPathFromRelativePath("Shaders/example_HorizontalColor.fsh"))->getCString();
|
2012-04-18 13:58:24 +08:00
|
|
|
CCGLProgram *p = new CCGLProgram();
|
|
|
|
p->initWithVertexShaderByteArray(ccPositionTexture_vert, fragSource);
|
|
|
|
|
|
|
|
p->addAttribute(kCCAttributeNamePosition, kCCVertexAttrib_Position);
|
|
|
|
p->addAttribute(kCCAttributeNameTexCoord, kCCVertexAttrib_TexCoords);
|
|
|
|
|
|
|
|
p->link();
|
|
|
|
p->updateUniforms();
|
|
|
|
|
|
|
|
|
|
|
|
CCDirector *director = CCDirector::sharedDirector();
|
|
|
|
CCSize s = director->getWinSize();
|
|
|
|
|
2012-06-14 15:13:16 +08:00
|
|
|
m_pLabel = CCLabelBMFont::create("RETRO EFFECT", "fonts/west_england-64.fnt");
|
2012-04-18 13:58:24 +08:00
|
|
|
|
|
|
|
m_pLabel->setShaderProgram(p);
|
|
|
|
|
|
|
|
p->release();
|
|
|
|
|
|
|
|
|
|
|
|
m_pLabel->setPosition(ccp(s.width/2,s.height/2));
|
|
|
|
|
|
|
|
addChild(m_pLabel);
|
|
|
|
|
|
|
|
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
|
|
|
{
|
|
|
|
m_fAccum += dt;
|
|
|
|
|
|
|
|
CCArray* pArray = m_pLabel->getChildren();
|
|
|
|
|
|
|
|
int i=0;
|
|
|
|
CCObject* pObj = NULL;
|
|
|
|
CCARRAY_FOREACH(pArray, pObj)
|
|
|
|
{
|
|
|
|
CCSprite *sprite = (CCSprite*)pObj;
|
|
|
|
i++;
|
|
|
|
CCPoint oldPosition = sprite->getPosition();
|
|
|
|
sprite->setPosition(ccp( oldPosition.x, sinf( m_fAccum * 2 + i/2.0) * 20 ));
|
|
|
|
|
|
|
|
// add fabs() to prevent negative scaling
|
|
|
|
float scaleY = ( sinf( m_fAccum * 2 + i/2.0 + 0.707) );
|
|
|
|
|
|
|
|
sprite->setScaleY(scaleY);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string ShaderRetroEffect::title()
|
|
|
|
{
|
|
|
|
return "Shader: Retro test";
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string ShaderRetroEffect::subtitle()
|
|
|
|
{
|
|
|
|
return "sin() effect with moving colors";
|
2012-04-17 18:31:31 +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());
|
|
|
|
|
|
|
|
CCDirector::sharedDirector()->replaceScene(this);
|
|
|
|
}
|