Created
December 6, 2013 01:28
-
-
Save SimpleDrunk/7817172 to your computer and use it in GitHub Desktop.
『openframeworks』shader制作六边形马赛克效果
http://blog.csdn.net/simpledrunk/article/details/17095821
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
//testApp.h | |
#pragma once | |
#include "ofMain.h" | |
class testApp : public ofBaseApp{ | |
public: | |
void setup(); | |
void update(); | |
void draw(); | |
void keyPressed (int key); | |
void keyReleased(int key); | |
void mouseMoved(int x, int y ); | |
void mouseDragged(int x, int y, int button); | |
void mousePressed(int x, int y, int button); | |
void mouseReleased(int x, int y, int button); | |
void windowResized(int w, int h); | |
void dragEvent(ofDragInfo dragInfo); | |
void gotMessage(ofMessage msg); | |
ofImage img; | |
ofShader shader; | |
ofFbo fbo; | |
float len; | |
bool bAdd; | |
}; | |
//testApp.cpp | |
#include "testApp.h" | |
//-------------------------------------------------------------- | |
void testApp::setup(){ | |
img.loadImage("bg.jpg"); | |
fbo.allocate(640, 480); | |
shader.load("hexagon.vert", "hexagon.frag"); | |
len = 8.0f; | |
bAdd = false; | |
} | |
//-------------------------------------------------------------- | |
void testApp::update(){ | |
if(len < 10.0f) | |
bAdd = true; | |
if(len > 20.0f) | |
bAdd = false; | |
len += bAdd ? (0.005f):(-0.005f); | |
} | |
//-------------------------------------------------------------- | |
void testApp::draw(){ | |
ofEnableAlphaBlending(); | |
img.draw(0, 0, 640, 480); | |
fbo.begin(); | |
img.draw(0, 0, 640, 480); | |
fbo.end(); | |
shader.begin(); | |
shader.setUniform1f("len", len); | |
shader.setUniformTexture("colorTex0", fbo.getTextureReference(), 0); | |
fbo.draw(0, 0, 640, 480); | |
shader.end(); | |
ofDisableAlphaBlending(); | |
} | |
//-------------------------------------------------------------- | |
void testApp::keyPressed(int key){ | |
} | |
//-------------------------------------------------------------- | |
void testApp::keyReleased(int key){ | |
} | |
//-------------------------------------------------------------- | |
void testApp::mouseMoved(int x, int y ){ | |
} | |
//-------------------------------------------------------------- | |
void testApp::mouseDragged(int x, int y, int button){ | |
} | |
//-------------------------------------------------------------- | |
void testApp::mousePressed(int x, int y, int button){ | |
} | |
//-------------------------------------------------------------- | |
void testApp::mouseReleased(int x, int y, int button){ | |
} | |
//-------------------------------------------------------------- | |
void testApp::windowResized(int w, int h){ | |
} | |
//-------------------------------------------------------------- | |
void testApp::gotMessage(ofMessage msg){ | |
} | |
//-------------------------------------------------------------- | |
void testApp::dragEvent(ofDragInfo dragInfo){ | |
} | |
//hexagon.frag | |
#version 400 | |
#extension GL_ARB_texture_rectangle : enable | |
uniform float len; | |
uniform sampler2DRect colorTex0; | |
void main (void){ | |
float TR = 0.866025f; | |
float x = gl_TexCoord[0].x; | |
float y = gl_TexCoord[0].y; | |
int wx = int(x/1.5f/len); | |
int wy = int(y/TR/len); | |
vec2 v1, v2, vn; | |
if(wx/2 * 2 == wx) { | |
if(wy/2 * 2 == wy) { | |
v1 = vec2(len*1.5f*wx, len*TR*wy); | |
v2 = vec2(len*1.5f*(wx+1), len*TR*(wy+1)); | |
} else { | |
v1 = vec2(len*1.5f*wx, len*TR*(wy+1)); | |
v2 = vec2(len*1.5f*(wx+1), len*TR*wy); | |
} | |
} else { | |
if(wy/2 * 2 == wy) { | |
v1 = vec2(len*1.5f*wx, len*TR*(wy+1)); | |
v2 = vec2(len*1.5f*(wx+1), len*TR*wy); | |
} else { | |
v1 = vec2(len*1.5f*wx, len*TR*wy); | |
v2 = vec2(len*1.5f*(wx+1), len*TR*(wy+1)); | |
} | |
} | |
float s1 = sqrt( pow(v1.x-x, 2) + pow(v1.y-y, 2) ); | |
float s2 = sqrt( pow(v2.x-x, 2) + pow(v2.y-y, 2) ); | |
if(s1 < s2) | |
vn = v1; | |
else | |
vn = v2; | |
vec4 color = texture2DRect(colorTex0, vn); | |
gl_FragColor = color; | |
} | |
//hexagon.vert | |
#version 400 | |
#extension GL_ARB_texture_rectangle : enable | |
void main(){ | |
gl_TexCoord[0] = gl_MultiTexCoord0; | |
gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
//三角形马赛克frag script
//triangle.frag
version 400
extension GL_ARB_texture_rectangle : enable
uniform float len;
uniform sampler2DRect colorTex0;
void main (void){
float TR = 0.866025f;
float PI6 = 0.523599f; //PI/12
float x = gl_TexCoord[0].x;
float y = gl_TexCoord[0].y;
int wx = int(x/1.5f/len);
int wy = int(y/TR/len);
vec2 v1, v2, vn;
if(wx/2 * 2 == wx) {
if(wy/2 * 2 == wy) {
v1 = vec2(len_1.5f_wx, len_TR_wy);
v2 = vec2(len_1.5f_(wx+1), len_TR_(wy+1));
} else {
v1 = vec2(len_1.5f_wx, len_TR_(wy+1));
v2 = vec2(len_1.5f_(wx+1), len_TR_wy);
}
} else {
if(wy/2 * 2 == wy) {
v1 = vec2(len_1.5f_wx, len_TR_(wy+1));
v2 = vec2(len_1.5f_(wx+1), len_TR_wy);
} else {
v1 = vec2(len_1.5f_wx, len_TR_wy);
v2 = vec2(len_1.5f_(wx+1), len_TR_(wy+1));
}
}
float s1 = sqrt( pow(v1.x-x, 2) + pow(v1.y-y, 2) );
float s2 = sqrt( pow(v2.x-x, 2) + pow(v2.y-y, 2) );
if(s1 < s2)
vn = v1;
else
vn = v2;
float a = atan((x-vn.x)/(y-vn.y));//夹角
vec2 area1 = vec2(vn.x, vn.y-len/TR/2);
vec2 area2 = vec2(vn.x+len/2, vn.y-len/TR/4);
vec2 area3 = vec2(vn.x+len/2, vn.y+len/TR/4);
vec2 area4 = vec2(vn.x, vn.y+len/TR/2);
vec2 area5 = vec2(vn.x-len/2, vn.y+len/TR/4);
vec2 area6 = vec2(vn.x-len/2, vn.y-len/TR/4);
if(a>=PI6 && a<PI63)
vn = area1;
else if(a>=PI6_3 && a<PI6_5)
vn = area2;
else if((a>=PI6_5 && a<=PI6_6) || (a<-PI6_5 && a>-PI6_6))
vn = area3;
else if(a<-PI6_3 && a>=-PI6_5)
vn = area4;
else if(a<=-PI6 && a>-PI63)
vn = area5;
else if(a>-PI6 && a<PI6)
vn = area6;
vec4 color = texture2DRect(colorTex0, vn);
gl_FragColor = color;
}