Created
August 31, 2014 13:38
-
-
Save t-takasaka/a4b4df96d47b4b1b50bc to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "HelloWorldScene.h" | |
USING_NS_CC; | |
Scene *HelloWorld::createScene(){ | |
auto scene = Scene::create(); | |
auto layer = HelloWorld::create(); | |
scene->addChild(layer); | |
return scene; | |
} | |
bool HelloWorld::init(){ | |
if(!Layer::init()){ return false; } | |
m_visibleSize = Director::getInstance()->getVisibleSize(); | |
//スプライトを作成 | |
m_sprite1 = Sprite::create("HelloWorld512a.png"); //※mipmap使うときは2のN乗の画像じゃないとコケる | |
m_textureSize = m_sprite1->getTexture()->getContentSizeInPixels(); | |
m_sprite1->setPosition(m_visibleSize.width / 2.0f, m_visibleSize.height / 2.0f); | |
m_sprite1->setScale(2.0); | |
m_sprite1->retain(); | |
//スプライトにミップマップを作成 | |
m_sprite1->getTexture()->generateMipmap(); | |
Texture2D::TexParams texParams = { GL_LINEAR_MIPMAP_LINEAR, GL_LINEAR, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE }; | |
m_sprite1->getTexture()->setTexParameters(texParams); | |
//シェーダをスプライトに紐付け | |
GLProgram *shader1 = new GLProgram(); | |
m_sprite1->setShaderProgram(shader1); | |
shader1->initWithFilenames("shaders/lerpbloom.vsh", "shaders/lerpbloom.fsh"); | |
shader1->bindAttribLocation(GLProgram::ATTRIBUTE_NAME_POSITION, GLProgram::VERTEX_ATTRIB_POSITION); | |
shader1->bindAttribLocation(GLProgram::ATTRIBUTE_NAME_TEX_COORD, GLProgram::VERTEX_ATTRIB_TEX_COORDS); | |
shader1->bindAttribLocation(GLProgram::ATTRIBUTE_NAME_COLOR, GLProgram::VERTEX_ATTRIB_COLOR); | |
shader1->link(); | |
shader1->updateUniforms(); | |
shader1->setUniformLocationWith1f(shader1->getUniformLocationForName("weight"), 1.0f); | |
addChild(m_sprite1); | |
scheduleUpdate(); | |
return true; | |
} | |
void HelloWorld::update(float delta){ | |
timeval tv; | |
gettimeofday(&tv, nullptr); | |
long now = tv.tv_sec * 1000 + tv.tv_usec / 1000; | |
int repeat = 10000; | |
now = abs((abs(now) % (repeat * 2)) - repeat); | |
m_sprite1->getTexture()->generateMipmap(); | |
Texture2D::TexParams texParams = { GL_LINEAR_MIPMAP_LINEAR, GL_LINEAR, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE }; | |
m_sprite1->getTexture()->setTexParameters(texParams); | |
GLProgram *shader1 = m_sprite1->getShaderProgram(); | |
shader1->updateUniforms(); | |
shader1->setUniformLocationWith1f(shader1->getUniformLocationForName("weight"), now * (2.0 / repeat) + 0.01); //0.1~2.1 | |
} | |
void HelloWorld::menuCloseCallback(Ref* pSender){ | |
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WP8) || (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT) | |
MessageBox("You pressed the close button. Windows Store Apps do not implement a close button.","Alert"); | |
return; | |
#endif | |
Director::getInstance()->end(); | |
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS) | |
exit(0); | |
#endif | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
attribute vec4 a_position; | |
attribute vec2 a_texCoord; | |
attribute vec4 a_color; | |
varying vec2 v_texCoord; | |
varying vec4 v_color; | |
void main() { | |
gl_Position = CC_PMatrix * a_position; | |
v_color = a_color; | |
v_texCoord = a_texCoord; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifdef GL_ES | |
precision mediump float; | |
#endif | |
varying vec2 v_texCoord; | |
varying lowp vec4 v_color; | |
uniform sampler2D u_texture; | |
uniform vec2 u_texSize; | |
uniform float weight; | |
void main(){ | |
int rad = 1, diam = rad + rad + 1; | |
float area = float(diam * diam); | |
float dist = 1.0 / 512.0; | |
vec4 sum = vec4(0.0); | |
for(int x = -rad; x <= rad; x++){ | |
for(int y = -rad; y <= rad; y++){ | |
float tmpx = v_texCoord.x + float(x) * dist; | |
float tmpy = v_texCoord.y + float(y) * dist; | |
sum += texture2D(u_texture, vec2(tmpx, tmpy), 0.0); | |
// sum += texture2D(u_texture, vec2(tmpx, tmpy), 1.5); | |
sum += texture2D(u_texture, vec2(tmpx, tmpy), 2.5); | |
// sum += texture2D(u_texture, vec2(tmpx, tmpy), 3.5); | |
sum += texture2D(u_texture, vec2(tmpx, tmpy), 4.5); | |
// sum += texture2D(u_texture, vec2(tmpx, tmpy), 5.5); | |
// sum += texture2D(u_texture, vec2(tmpx, tmpy), 6.5); | |
// sum += texture2D(u_texture, vec2(tmpx, tmpy), 7.5); | |
} | |
} | |
gl_FragColor = (sum / area) / weight; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment