Blur쉐이더 , Desktop 용






원본은 https://www.shadertoy.com/view/4lXXWn 이다.

하지만 OF용으로 몇몇것들이 맞지 않아 수정을 해야 했다.



결과물 상세 이미지





쉐이더 1번에 최대한 효과적인 블러를 만들어야 겠다는 목표로.. 






#define STRINGIFY(A) #A

ofImage image1;
ofImage boy;
ofShader shader1;

//--------------------------------------------------------------
void ofApp::setup(){

//원본 : https://www.shadertoy.com/view/4lXXWn
string shaderProgram = STRINGIFY(

uniform sampler2DRect iChannel0;
uniform vec3 iResolution;

uniform float repeats;
uniform float amount; // 범위 0.0 ~ 0.1

vec4 texture(vec2 uv) {
return texture2DRect(iChannel0,vec2(uv.x * iResolution.x ,uv.y * iResolution.y));//.rgb;
}


//랜덤 이용
float rand(vec2 co){
return fract(sin(dot(co.xy ,vec2(12.9898,78.233))) * 43758.5453);
}

void mainImage( out vec4 fragColor, vec2 fragCoord )
{

vec2 uv = (fragCoord.xy / iResolution.xy);

vec4 blurred_image = vec4(0.);
for (float i = 0.0; i < repeats; i++) {
vec2 q = vec2(cos(degrees((i/repeats)*360.)),sin(degrees((i/repeats)*360.))) * (rand(vec2(i*uv.x ,i*uv.y ))+amount);
vec2 uv2 = uv+(q*amount);
blurred_image += texture(uv2)/2.;
q = vec2(cos(degrees((i/repeats)*360.)),sin(degrees((i/repeats)*360.))) * (rand(vec2(i*uv.x ,i*uv.y ))+amount);
uv2 = uv+(q*amount);
blurred_image += texture(uv2)/2.;
}
blurred_image /= repeats;
fragColor = blurred_image;
}


void main(void)
{
vec2 pos = gl_TexCoord[0].st;
vec4 color = vec4(0.0 , 0.0 , 0.0 , 0.0);
mainImage(color , pos);
gl_FragColor = color;
}

);

shader1.setupShaderFromSource(GL_FRAGMENT_SHADER , shaderProgram);
shader1.linkProgram();

boy.load("images/boy.png");
image1.load("images/scenery.png");
}

//--------------------------------------------------------------

float elapsed_time = 0.0;
void ofApp::draw(){

elapsed_time += ofGetLastFrameTime();

int repeats = ofMap(ofGetMouseX() ,0 , ofGetWidth() , 1 , 240);
float amount = ofMap(ofGetMouseY() , 0 , ofGetHeight() , 0.0 , 0.1);

boy.draw(0 , 0 , ofGetWidth() , ofGetHeight());

shader1.begin();
shader1.setUniform3f("iResolution" , image1.getWidth() , image1.getHeight() , 0.0f);
shader1.setUniform1f("repeats" , repeats);
shader1.setUniform1f("amount" , ofMap(ofGetMouseY() , 0 , ofGetHeight() , 0.0 , 0.1));
image1.draw(0,0);
shader1.end();

ofDrawBitmapStringHighlight(ofToString(ofGetFrameRate()) , 30,30);
ofDrawBitmapStringHighlight("repeats : " + ofToString(repeats) , 30 , 60);
ofDrawBitmapStringHighlight("amount : " + ofToString(amount) , 30 , 75);

boy.draw(30 , 90 , 50 , 50);

}

//--------------------------------------------------------------
void ofApp::update(){

}



//--------------------------------------------------------------
void ofApp::keyPressed(int key){

if(key == '1')
{
image1 = ofImage("images/scenery.png");

}
else if(key == '2')
{
image1 = ofImage("images/CircleAndRect1.png");
}

} 

저장저장


Yamecoder 야매코더_
C++(oF) 2016. 6. 18. 21:28

댓글을 달아 주세요

Powerd by Tistory, designed by criuce
rss