Skip to content

Instantly share code, notes, and snippets.

@t-takasaka
Last active March 2, 2017 06:09
Show Gist options
  • Save t-takasaka/f319a1e28f8ee9393e61 to your computer and use it in GitHub Desktop.
Save t-takasaka/f319a1e28f8ee9393e61 to your computer and use it in GitHub Desktop.
#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("HelloWorld512.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(1.0f);
//スプライトにミップマップを作成
m_sprite1->getTexture()->generateMipmap();
Texture2D::TexParams texParams = { GL_LINEAR_MIPMAP_LINEAR, GL_LINEAR, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE };
// Texture2D::TexParams texParams = { GL_LINEAR_MIPMAP_NEAREST, GL_NEAREST, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE };
// Texture2D::TexParams texParams = { GL_NEAREST_MIPMAP_LINEAR, GL_NEAREST, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE };
// Texture2D::TexParams texParams = { GL_NEAREST_MIPMAP_NEAREST, GL_NEAREST, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE };
m_sprite1->getTexture()->setTexParameters(texParams);
//シェーダをスプライトに紐付け
GLProgram *shader1 = new GLProgram();
m_sprite1->setShaderProgram(shader1);
shader1->initWithFilenames("shaders/lerpblur.vsh", "shaders/lerpblur.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();
//※biasは0.5区切りでダウンサンプルされている(ように見える)
shader1->setUniformLocationWith1f(shader1->getUniformLocationForName("bias"), 0.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;
now = abs((abs(now) % 10000) - 5000);
//紐付け済みのシェーダを取得して、ミップマップのバイアスを更新
GLProgram *shader1 = m_sprite1->getShaderProgram();
shader1->updateUniforms();
shader1->setUniformLocationWith1f(shader1->getUniformLocationForName("bias"), now * 0.0016f);
}
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
}
#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 bias;
void main(){
vec4 texColor = texture2D(u_texture, v_texCoord, bias);
gl_FragColor = texColor * v_color;
}
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;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment