Skip to content

Commit

Permalink
fix bug. add two functions
Browse files Browse the repository at this point in the history
  • Loading branch information
zhangyufei49 committed Aug 20, 2014
1 parent 430890a commit 3dd2baf
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 35 deletions.
36 changes: 22 additions & 14 deletions Classes/FilterNode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ void FilterNode::capture()
}

/*********************| GuassianBlur class |*********************/
static GLchar* hblurSrc = NULL;
static GLchar* vblurSrc = NULL;
extern GLchar* hblurSrc = NULL;
extern GLchar* vblurSrc = NULL;
static GaussianBlur* _scSizeBlurNode = NULL;

GaussianBlur::GaussianBlur():f1(NULL), f2(NULL), _showing(false)
Expand Down Expand Up @@ -106,6 +106,24 @@ void GaussianBlur::do_free()
CC_SAFE_RELEASE_NULL(_scSizeBlurNode);
}

CCGLProgram*GaussianBlur::blurProgram(bool isV, GLfloat ratio, GLfloat hvsize)
{
CCGLProgram* p = FilterNode::commonPrograme(isV ? vblurSrc : hblurSrc);
setBlurData(p, isV, ratio, hvsize);
return p;
}

void GaussianBlur::setBlurData(CCGLProgram* p, bool isV, GLfloat ratio, GLfloat hvsize)
{
if (p) {
p->use();
GLint blurSLoc = glGetUniformLocation(p->getProgram(), "ratio");
p->setUniformLocationWith1f(blurSLoc, ratio);
blurSLoc = glGetUniformLocation(p->getProgram(), isV ? "vsize" : "hsize");
p->setUniformLocationWith1f(blurSLoc, hvsize);
}
}

GaussianBlur*GaussianBlur::create(CCNode* src, const CCSize& size)
{
GaussianBlur* ret = new GaussianBlur();
Expand Down Expand Up @@ -164,21 +182,11 @@ bool GaussianBlur::init(CCNode* src, const CCSize& size, bool reused)

void GaussianBlur::setBlurSize(const CCSize& size, const int which , GLfloat ratio)
{
CCGLProgram* s = NULL;
GLint blurSLoc = 0;
if (!which || 1 == which) {
s = f1->getSprite()->getShaderProgram();
blurSLoc = glGetUniformLocation(s->getProgram(), "ratio");
s->setUniformLocationWith1f(blurSLoc, ratio);
blurSLoc = glGetUniformLocation(s->getProgram(), "hsize");
s->setUniformLocationWith1f(blurSLoc, (GLfloat)(size.width));
setBlurData(f1->getSprite()->getShaderProgram(), false, ratio, size.width);
}
if (!which || 2 == which) {
s = f2->getSprite()->getShaderProgram();
blurSLoc = glGetUniformLocation(s->getProgram(), "ratio");
s->setUniformLocationWith1f(blurSLoc, ratio);
blurSLoc = glGetUniformLocation(s->getProgram(), "vsize");
s->setUniformLocationWith1f(blurSLoc, (GLfloat)(size.height));
setBlurData(f2->getSprite()->getShaderProgram(), true, ratio, size.height);
}
}

Expand Down
31 changes: 10 additions & 21 deletions Classes/FilterNode.h
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
#ifndef FILETERSPRITE_H
#define FILETERSPRITE_H

/**
* Desc: 特效node
* Auth: 张宇飞
* Date: 2014-08-19
*/

#include "cocos2d.h"
USING_NS_CC;

Expand Down Expand Up @@ -52,13 +46,13 @@ class FilterNode : public CCRenderTexture, public FilterProto
}
public:
bool init(CCNode* src, const CCSize& size);
/* set filter */
/* 设定filter */
inline void filter(CCGLProgram* shader) {
this->getSprite()->setShaderProgram(shader);
}
inline void changeNode(CCNode* src) {_srcNode = src; capture();}

/* take a capture means do draw elements once 快照 */
/* 快照 */
void capture();
UPCAP_FUNC(FilterNode, freshCapture)
private:
Expand All @@ -69,51 +63,46 @@ class FilterNode : public CCRenderTexture, public FilterProto
};

/**
* @brief The GaussianBlur class use two FilterNode, one with horizontal blur and
* send result to next FilerNode with vertical blur .Then sencond result will show
* as Gaussian blur effect
* 中文:这个类用了两个FilterNode来实现高斯模糊。第一个用横向的模糊,结果作为第二个的
* 输入进行纵向模糊,得到的结果就是需要的了。
* @brief The GaussianBlur class
*/
class GaussianBlur : public CCNode, public FilterProto
{
public:
GaussianBlur();
~GaussianBlur();
public:
/* call this at global init time */
static bool do_ready();
/* call this at global end time */
static bool do_ready();
static void do_free();

static CCGLProgram* blurProgram(bool isV, GLfloat ratio, GLfloat hvsize);
static void setBlurData(CCGLProgram* p, bool isV, GLfloat ratio, GLfloat hvsize);
static GaussianBlur* create(CCNode* src, const CCSize& size);
inline static GaussianBlur* create(CCNode* src) {
if (src) return create(src, src->getContentSize());
return NULL;
}
/**
* @brief screenBlurNode get a blur node instance with screen size(中文:获取一个屏幕大小的模糊单例)
* @brief screenBlurNode 获取一个屏幕大小的模糊单例
*/
static GaussianBlur* screenBlurNodeInstance();
public:
bool init(CCNode* src, const CCSize& size, bool reused = false);
/**
* @brief setBlurSize 设置模糊程度
* @param size 模糊范围
* @param which 0 all , 1 horizontal blur, 2 vertical blur(中文:0横纵向同时模糊,1横向模糊设定,2纵向模糊设定)
* @param which 0横纵向同时设置,1横向设置,2纵向设置
* @param ratio 模糊半径
*/
void setBlurSize(const CCSize& size, const int which = 0, GLfloat ratio = 3.0);
bool reset(CCNode* src);
void cleanFromWorld();
/* @brief show try use runningScene()->addChild to show this(中文:尝试用getRunningScene()->addChild来显示当前节点 */
void show();
/* 快照 */
void capture();
UPCAP_FUNC(GaussianBlur, freshCapture)
private:
void freshCapture(float dt) { capture();}
private:
FilterNode *f1, *f2;
FilterNode *f1, *f2;
bool _showing;
};

Expand Down

0 comments on commit 3dd2baf

Please sign in to comment.