Merge pull request #1471 from minggo/iss1466-shader-test

reloading shader when coming to foreground
This commit is contained in:
minggo 2012-10-17 20:03:22 -07:00
commit d68f7c76f6
2 changed files with 74 additions and 27 deletions

View File

@ -150,6 +150,11 @@ ShaderNode::ShaderNode()
{ {
} }
ShaderNode::~ShaderNode()
{
CCNotificationCenter::sharedNotificationCenter()->removeObserver(this, EVNET_COME_TO_FOREGROUND);
}
ShaderNode* ShaderNode::shaderNodeWithVertex(const char *vert, const char *frag) ShaderNode* ShaderNode::shaderNodeWithVertex(const char *vert, const char *frag)
{ {
ShaderNode *node = new ShaderNode(); ShaderNode *node = new ShaderNode();
@ -161,6 +166,10 @@ ShaderNode* ShaderNode::shaderNodeWithVertex(const char *vert, const char *frag)
bool ShaderNode::initWithVertex(const char *vert, const char *frag) bool ShaderNode::initWithVertex(const char *vert, const char *frag)
{ {
CCNotificationCenter::sharedNotificationCenter()->addObserver(this,
callfuncO_selector(ShaderNode::listenBackToForeground),
EVNET_COME_TO_FOREGROUND,
NULL);
loadShaderVertex(vert, frag); loadShaderVertex(vert, frag);
@ -172,9 +181,18 @@ bool ShaderNode::initWithVertex(const char *vert, const char *frag)
setContentSize(CCSizeMake(SIZE_X, SIZE_Y)); setContentSize(CCSizeMake(SIZE_X, SIZE_Y));
setAnchorPoint(ccp(0.5f, 0.5f)); setAnchorPoint(ccp(0.5f, 0.5f));
m_vertFileName = vert;
m_fragFileName = frag;
return true; return true;
} }
void ShaderNode::listenBackToForeground(CCObject *obj)
{
this->setShaderProgram(NULL);
loadShaderVertex(m_vertFileName.c_str(), m_fragFileName.c_str());
}
void ShaderNode::loadShaderVertex(const char *vert, const char *frag) void ShaderNode::loadShaderVertex(const char *vert, const char *frag)
{ {
CCGLProgram *shader = new CCGLProgram(); CCGLProgram *shader = new CCGLProgram();
@ -438,9 +456,12 @@ std::string ShaderPlasma::subtitle()
class SpriteBlur : public CCSprite class SpriteBlur : public CCSprite
{ {
public: public:
~SpriteBlur();
void setBlurSize(float f); void setBlurSize(float f);
bool initWithTexture(CCTexture2D* texture, const CCRect& rect); bool initWithTexture(CCTexture2D* texture, const CCRect& rect);
void draw(); void draw();
void initProgram();
void listenBackToForeground(CCObject *obj);
static SpriteBlur* create(const char *pszFileName); static SpriteBlur* create(const char *pszFileName);
@ -451,6 +472,11 @@ public:
GLuint subLocation; GLuint subLocation;
}; };
SpriteBlur::~SpriteBlur()
{
CCNotificationCenter::sharedNotificationCenter()->removeObserver(this, EVNET_COME_TO_FOREGROUND);
}
SpriteBlur* SpriteBlur::create(const char *pszFileName) SpriteBlur* SpriteBlur::create(const char *pszFileName)
{ {
SpriteBlur* pRet = new SpriteBlur(); SpriteBlur* pRet = new SpriteBlur();
@ -466,15 +492,36 @@ SpriteBlur* SpriteBlur::create(const char *pszFileName)
return pRet; return pRet;
} }
void SpriteBlur::listenBackToForeground(CCObject *obj)
{
setShaderProgram(NULL);
initProgram();
}
bool SpriteBlur::initWithTexture(CCTexture2D* texture, const CCRect& rect) bool SpriteBlur::initWithTexture(CCTexture2D* texture, const CCRect& rect)
{ {
if( CCSprite::initWithTexture(texture, rect) ) if( CCSprite::initWithTexture(texture, rect) )
{ {
CCNotificationCenter::sharedNotificationCenter()->addObserver(this,
callfuncO_selector(SpriteBlur::listenBackToForeground),
EVNET_COME_TO_FOREGROUND,
NULL);
CCSize s = getTexture()->getContentSizeInPixels(); CCSize s = getTexture()->getContentSizeInPixels();
blur_ = ccp(1/s.width, 1/s.height); blur_ = ccp(1/s.width, 1/s.height);
sub_[0] = sub_[1] = sub_[2] = sub_[3] = 0; sub_[0] = sub_[1] = sub_[2] = sub_[3] = 0;
this->initProgram();
return true;
}
return false;
}
void SpriteBlur::initProgram()
{
GLchar * fragSource = (GLchar*) CCString::createWithContentsOfFile( GLchar * fragSource = (GLchar*) CCString::createWithContentsOfFile(
CCFileUtils::sharedFileUtils()->fullPathFromRelativePath("Shaders/example_Blur.fsh"))->getCString(); CCFileUtils::sharedFileUtils()->fullPathFromRelativePath("Shaders/example_Blur.fsh"))->getCString();
CCGLProgram* pProgram = new CCGLProgram(); CCGLProgram* pProgram = new CCGLProgram();
@ -502,10 +549,6 @@ bool SpriteBlur::initWithTexture(CCTexture2D* texture, const CCRect& rect)
blurLocation = glGetUniformLocation( getShaderProgram()->getProgram(), "blurSize"); blurLocation = glGetUniformLocation( getShaderProgram()->getProgram(), "blurSize");
CHECK_GL_ERROR_DEBUG(); CHECK_GL_ERROR_DEBUG();
return true;
}
return false;
} }
void SpriteBlur::draw() void SpriteBlur::draw()

View File

@ -114,9 +114,11 @@ class ShaderNode : public CCNode
{ {
public: public:
ShaderNode(); ShaderNode();
~ShaderNode();
bool initWithVertex(const char *vert, const char *frag); bool initWithVertex(const char *vert, const char *frag);
void loadShaderVertex(const char *vert, const char *frag); void loadShaderVertex(const char *vert, const char *frag);
void listenBackToForeground(CCObject *obj);
virtual void update(float dt); virtual void update(float dt);
virtual void setPosition(const CCPoint &newPosition); virtual void setPosition(const CCPoint &newPosition);
@ -130,6 +132,8 @@ private:
ccVertex2F m_resolution; ccVertex2F m_resolution;
float m_time; float m_time;
GLuint m_uniformCenter, m_uniformResolution, m_uniformTime; GLuint m_uniformCenter, m_uniformResolution, m_uniformTime;
std::string m_vertFileName;
std::string m_fragFileName;
}; };
class ShaderTestScene : public TestScene class ShaderTestScene : public TestScene