OSDN Git Service

first
[psychlops/cpp.git] / osx / test / Shader / ShaderImage.cpp
1 #include <psychlops.h>
2 using namespace Psychlops;
3
4 void psychlops_main() {
5         Canvas cnvs(1024,768,Canvas::window);
6         cnvs.watchFPS();
7         cnvs.showFPS();
8         Mouse::show();
9         Color::setGammaValue(1.0,1.0,1.0);
10
11         Figures::ShaderImage img;
12 /*      img.setFunction(
13                 "uniform float frequency;"
14                 "void main(void){"
15                 "float sigma_ = 2.0/frequency;"
16                 "float freq_ = frequency;"
17                 "float r = length(vec2(xp()-RECT_WIDTH/2.0, yp()-RECT_HEIGHT/2.0));"
18                 "float level = 0.5+0.5*cos(r*freq_) * GaussianM0(r, sigma_);"
19                 "gl_FragColor = vec4(level, 0.0,0.0,1.0);"
20                 "}"
21         );*/
22 /*
23         img.setFunction(
24                 "uniform float frequency;"
25                 "vec4 GaussianConvolute(in float frequency_) {"
26                 "float sigma_ = 2.0/frequency_;"
27                 "int lim = int(sigma_*3.0);"
28                 "vec4 color = vec4(0.0,0.0,0.0,0.0);"
29                 "float factor = 0.0, cur=0.0, r=0.0;"
30                 "for(int y=-lim; y<=lim; y++) { for(int x=-lim; x<=lim; x++) {"
31                 "     r = length(vec2(x,y));"
32                 "     cur = cos(r*frequency_) * GaussianM0(r, sigma_);"
33                 "     factor += cur;"
34                 "     color += cur*getPix(x,y);"
35                 "} }"
36                 "color /= factor;"
37                 "return color;"
38                 "}"
39                 "void main(void){"
40 //              "float _y = abs(xi()-0.5)*frequency;"
41                 "gl_FragColor = GaussianConvolute(frequency);"
42                 "}"
43         );*/
44 /*      img.setFunction(
45                 "uniform float sigma;"
46                 "vec4 GaussianConvolute(in float sigma_) {"
47                 "int lim = int(sigma_*3.0);"
48                 "vec4 color = vec4(0.0,0.0,0.0,0.0);"
49                 "float factor = 0.0, cur=0.0;"
50                 "for(int y=-lim; y<=lim; y++) { for(int x=-lim; x<=lim; x++) {"
51                 "     cur = GaussianM0(length(ivec2(x,y)), sigma_);"
52                 "     factor += cur;"
53                 "     color += cur*getPix(x,y);"
54                 "} }"
55                 "return color / factor;"
56                 "}"
57                 "void main(void){"
58                 "float _y = abs(xi()-0.5)*sigma;"
59                 "gl_FragColor = GaussianConvolute(_y);"
60                 "}"
61         );
62 */      img.setFunction(
63                 "uniform float dh, sigma, MX, MY;"
64                 "void main(void){"
65                 "vec4 hsv = RGBtoHSV(getPix());"
66                 "hsv[0] = mod(hsv[0]+dh, 360.0);"
67                 //"hsv[1] = hsv[1]*GaussianM0(length(vec2(xfp()-MX, yfp()-MY)), sigma);"
68                 //"hsv[2] = 0.5+(hsv[2]-0.5)*GaussianM0(length(vec2(xfp()-MX, yfp()-MY)), sigma);"
69                 "gl_FragColor = HSVtoRGB(hsv);"
70                 "}"
71         );
72         img.orig_vars.push_back("dh");
73         img.orig_vars.push_back("sigma");
74         img.orig_vars.push_back("MX");
75         img.orig_vars.push_back("MY");
76         Image orig_img("01.png");
77         orig_img.cache();
78         img.cache();
79
80
81
82         double val = 0, sigma = 100.0;
83         Widgets::Dial dial1, dial2;
84         dial1.link(val,   360).set(50).centering(50,700);
85         dial2.link(sigma, 100).set(50).centering(150,700);
86
87
88         while(!Keyboard::esc.pushed()) {
89                 img.var(img.orig_vars[2], cnvs.mouse().x-orig_img.getDatum().x);
90                 img.var(img.orig_vars[3], cnvs.mouse().y-orig_img.getDatum().y);
91                 if(dial1.changed()) img.var(img.orig_vars[0], val);
92                 if(dial2.changed()) img.var(img.orig_vars[1], sigma);
93                 cnvs.clear(Color::gray);
94 //              orig_img.centering(300,300);
95                 orig_img.centering();
96                 img.draw(orig_img);
97 //              orig_img.centering(700,550).draw();
98                 dial1.draw();
99                 dial2.draw();
100                 cnvs.flip();
101         }
102 }
103