Skip to content

Commit f4dcbeb

Browse files
author
Jiang Feng
committed
make shader selectable by numeric keys
1 parent 55e07d0 commit f4dcbeb

File tree

8 files changed

+188
-23
lines changed

8 files changed

+188
-23
lines changed

hmiios2014/OpenglWindow.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,19 @@ bool OpenglWindow::event(QEvent* event)
227227
{
228228
close();
229229
}
230+
else if (l_keyevent->key() == Qt::Key_1
231+
|| l_keyevent->key() == Qt::Key_2
232+
|| l_keyevent->key() == Qt::Key_3
233+
|| l_keyevent->key() == Qt::Key_4
234+
|| l_keyevent->key() == Qt::Key_5
235+
|| l_keyevent->key() == Qt::Key_6
236+
|| l_keyevent->key() == Qt::Key_7
237+
|| l_keyevent->key() == Qt::Key_8
238+
|| l_keyevent->key() == Qt::Key_9
239+
|| l_keyevent->key() == Qt::Key_0)
240+
{
241+
selectShader((l_keyevent->key() - Qt::Key_1) + 1);
242+
}
230243
return true;
231244
}
232245
default:
@@ -469,3 +482,8 @@ void OpenglWindow::renderText(int posX, int posY, const QString& text, const QSt
469482
}
470483
//! [12]
471484

485+
void OpenglWindow::selectShader(uint shaderId)
486+
{
487+
qDebug() << "virtual parent, do nothing";
488+
}
489+

hmiios2014/OpenglWindow.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ public slots:
5050

5151
protected:
5252
bool event(QEvent* event);
53+
virtual void selectShader(uint shaderId);
5354
void exposeEvent(QExposeEvent* event);
5455
QTimer* timer;
5556
int m_fpsCounter, m_fps;

hmiios2014/TSDWindow.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,7 @@ void TSDWindow::initialize()
320320
m_resolution = m_program->uniformLocation("resolution");
321321
m_time = m_program->uniformLocation("time");
322322
m_colorId = m_program->uniformLocation("color_id");
323+
m_shaderId = m_program->uniformLocation("shader_id");
323324

324325
m_program->bind();
325326

@@ -581,6 +582,7 @@ void TSDWindow::render()
581582
m_program->setUniformValue(m_mouseDelta, mouseDelta[0] * devicePixelRatio(), -mouseDelta[1] * devicePixelRatio());
582583
m_program->setUniformValue(m_resolution, resolution[0], resolution[1]);
583584
m_program->setUniformValue(m_time, time);
585+
m_program->setUniformValue(m_shaderId, m_shader);
584586

585587
//glCallList(m_listIndex+m_sgCoastal.m_id);
586588
//if(SCALE>0.1)
@@ -779,3 +781,9 @@ void TSDWindow::setDisplayMask(DisplayMaskBits layer, bool b)
779781
else
780782
m_displayMask &= ~layer;
781783
}
784+
785+
void TSDWindow::selectShader(uint shaderId)
786+
{
787+
m_shader = shaderId;
788+
qDebug() << "set shader to " << shaderId;
789+
}

hmiios2014/TSDWindow.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,8 @@ class TSDWindow : public OpenglWindow
173173

174174
inline void setAutoSwing(bool value) { m_bAutoSwing = value; }
175175
inline bool getAutoSwing() { return m_bAutoSwing; }
176+
protected:
177+
virtual void selectShader(uint shaderId);
176178

177179
private:
178180
GLuint loadShader(GLenum type, const char* source);
@@ -190,8 +192,10 @@ class TSDWindow : public OpenglWindow
190192
GLuint m_resolution;
191193
GLuint m_time;
192194
GLuint m_colorId;
195+
GLuint m_shaderId;
193196

194197
GLuint m_listIndex;
198+
GLuint m_shader;
195199

196200
QOpenGLShaderProgram* m_program;
197201

hmiios2014/fshader.glsl

Lines changed: 155 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ uniform vec2 mouse;
33
uniform vec2 mouseDelta;
44
uniform float time;
55
uniform int color_id;
6+
uniform int shader_id;
67
/*uniform vec4 colors[23] = {
78
vec4(.2, .6, .1,1.) // land
89
,vec4(.0, .5, .0,1.0)
@@ -29,26 +30,166 @@ uniform int color_id;
2930
,vec4(.8, .0, .0,.6) // 21 not used
3031
,vec4(.0, .6, .0,1.)
3132
};*/
33+
34+
// shadertoy emulation
35+
#define iTime time
36+
#define iResolution resolution
37+
38+
// --------[ Original ShaderToy begins here ]---------- //
39+
#define EPS 0.001
40+
#define MAX_STEPS 32
41+
#define ITR 8
42+
43+
mat2 rot(float a){
44+
float s=sin(a);
45+
float c=cos(a);
46+
return mat2(c,s,-s,c);
47+
}
48+
49+
float scale;
50+
51+
float map(vec3 p){
52+
p+=vec3(1.0,1.0,iTime*0.2);
53+
p.xy*=rot(iTime*0.05);
54+
p.yz*=rot(iTime*0.05);
55+
float s=3.0;
56+
for(int i=0;i<ITR;i++){
57+
p=mod(p-1.0,2.0)-1.0;
58+
float r=1.53/dot(p,p);
59+
p*=r;
60+
s*=r;
61+
}
62+
scale=s;
63+
return dot(abs(p),normalize(vec3(0.0,1.0,1.0)))/s;
64+
}
65+
66+
vec3 GetNormal(vec3 p){
67+
float d=map(p);
68+
vec2 e=vec2(EPS,0.0);
69+
70+
vec3 n=d-vec3(
71+
map(p-e.xyy),
72+
map(p-e.yxy),
73+
map(p-e.yyx));
74+
return normalize(n);
75+
}
76+
77+
float circle(vec2 center, vec2 position, float radius) {
78+
vec2 d = center - position;
79+
d.x = mod(d.x+0.5, 1.0)-0.5;
80+
d.y = mod(d.y+0.5, 1.0)-0.5;
81+
return clamp((radius - length(d)) * resolution.y, 0.0, 1.0);
82+
}
83+
84+
float rnd(int seed) {
85+
return fract(sin(1.34232 + pow(float(seed), 1.014) * 89.72342433) * 328.2653653);
86+
}
87+
88+
#define VEL 1.5
89+
#define PI 3.14159265359
90+
91+
3292
void main() {
3393
//gl_FragColor = colors[color_id];
3494
if(color_id == 0)
3595
{
36-
const float Pi = 3.14159;
37-
const int complexity = 47; // More points of color.
38-
const float mouse_factor = 56.0; // Makes it more/less jumpy.
39-
const float mouse_offset = 0.0; // Drives complexity in the amount of curls/cuves. Zero is a single whirlpool.
40-
const float fluid_speed = 54.0; // Drives speed, higher number will make it slower.
41-
const float color_intensity = 0.8;
42-
vec2 p=(2.0*(gl_FragCoord.xy - mouseDelta.xy)-resolution)/max(resolution.x,resolution.y);
43-
for(int i=1;i<complexity;i++)
96+
if(shader_id == 1)
97+
{
98+
const float Pi = 3.14159;
99+
const int complexity = 47; // More points of color.
100+
const float mouse_factor = 56.0; // Makes it more/less jumpy.
101+
const float mouse_offset = 0.0; // Drives complexity in the amount of curls/cuves. Zero is a single whirlpool.
102+
const float fluid_speed = 27.0; // Drives speed, higher number will make it slower.
103+
const float color_intensity = 0.8;
104+
vec2 p=(2.0*(gl_FragCoord.xy - mouseDelta.xy)-resolution)/max(resolution.x,resolution.y);
105+
for(int i=1;i<complexity;i++)
106+
{
107+
vec2 newp=p + time*0.001;
108+
newp.x+=0.6/float(i)*sin(float(i)*p.y+time/fluid_speed+0.3*float(i)) + 0.5; // + mouse.y/mouse_factor+mouse_offset;
109+
newp.y+=0.6/float(i)*sin(float(i)*p.x+time/fluid_speed+0.3*float(i+10)) - 0.5; // - mouse.x/mouse_factor+mouse_offset;
110+
p=newp;
111+
}
112+
vec3 col=vec3(color_intensity*sin(3.0*p.x)+color_intensity,color_intensity*sin(3.0*p.y)+color_intensity,color_intensity*sin(p.x+p.y)+color_intensity);
113+
gl_FragColor=vec4(col, 1);
114+
}
115+
else if(shader_id == 2)
116+
{
117+
float v = VEL * abs(cos(time/5.0)) * PI* 0.01;
118+
vec3 rColor = vec3(1.0, 0.3, 1.0);
119+
rColor = vec3(0.6, 0.5, 10.0 * mouse.x);
120+
121+
vec2 p =((gl_FragCoord.xy - mouseDelta.xy) *2.0 -resolution);
122+
p /= min(resolution.x, resolution.y);
123+
p*=1.1;
124+
125+
float m = cos(time*0.1);
126+
127+
float d = cos(VEL*time*1.0 - p.x*4.0*m + 0.0*2.*PI/3.);
128+
float e = cos(-VEL*time*1.1 - p.x*4.0*m/4.0 + 1.0*2.*PI/3.);
129+
float f = cos(VEL*time*1.2 - p.x*4.0*m/2.0 + 2.0*2.*PI/3.);
130+
131+
float r = 1.09/abs(p.x + d);
132+
float g = 0.09/abs(p.y + e);
133+
float b = 0.09/abs(p.y + f);
134+
135+
136+
vec3 destColor = rColor*vec3(r, g, b);
137+
gl_FragColor =vec4(destColor, 1.0);
138+
}
139+
else if(shader_id == 3)
140+
{
141+
vec3 c = vec3(0.);
142+
vec4 o = vec4(0.);
143+
float t = iTime*.1,d;
144+
for(float i=0.; i<1.; i+=.06)
145+
{
146+
d = fract(i+.1*t);
147+
o = vec4( (gl_FragCoord.xy - mouseDelta.xy -iResolution.xy*.5)/iResolution.y*(1.-d) ,-i,0)*28.;
148+
for (int i=0 ; i<19;++i) o.xzyw = abs( o/dot(o,o) - vec4( 1.-.03*sin(t) , .9 , .1 , .15 -.14*cos(t*1.3)) );
149+
c+= o.xyz*o.yzw*(d-d*d);
150+
}
151+
o.rgb = c;
152+
gl_FragColor =vec4(o.xyz, 1.0);
153+
}
154+
else if(shader_id == 4)
155+
{
156+
vec2 uv=(2.0*(gl_FragCoord.xy- mouseDelta.xy)-resolution.xy)/resolution.y;
157+
vec3 col=vec3(0.0);
158+
vec3 rd=normalize(vec3(uv,1.0));
159+
vec3 p=vec3(0.0,0.0,iTime*0.05);
160+
float d;
161+
float emission=0.0;
162+
163+
for(int i=0;i<MAX_STEPS;i++){
164+
d=map(p);
165+
p+=rd*d;
166+
//normal=GetNormal(p);
167+
emission+=exp(d*-0.4);
168+
if(d<EPS)break;
169+
}
170+
171+
vec4 color=0.02*emission*vec4(sin(iTime),1.0,sin(iTime),1.0);
172+
gl_FragColor=vec4(color.xyz, 1.0);
173+
}
174+
else if(shader_id == 5)
175+
{
176+
vec2 position = ( (gl_FragCoord.xy - mouseDelta.xy) / resolution.xy );
177+
position -= 0.5;
178+
position.x *= resolution.x / resolution.y;
179+
vec3 o = vec3(0.0, 0.0, 0.0);
180+
181+
for (int i=0; i<300; i+=10) {
182+
vec2 center = vec2(rnd(i+0), rnd(i+1)) + time * 0.1 * vec2(rnd(i+2), rnd(i+3));
183+
float radius = 0.03 + 0.25 * rnd(i+4);
184+
vec3 color = vec3(rnd(i+5), rnd(i+6), rnd(i+7));
185+
o += circle(center, position, radius) * 0.5 * color;
186+
}
187+
gl_FragColor = vec4(o.xyz, 1.0);
188+
}
189+
else
44190
{
45-
vec2 newp=p + time*0.001;
46-
newp.x+=0.6/float(i)*sin(float(i)*p.y+time/fluid_speed+0.3*float(i)) + 0.5; // + mouse.y/mouse_factor+mouse_offset;
47-
newp.y+=0.6/float(i)*sin(float(i)*p.x+time/fluid_speed+0.3*float(i+10)) - 0.5; // - mouse.x/mouse_factor+mouse_offset;
48-
p=newp;
191+
gl_FragColor =vec4(.0, .6, .0,1.);
49192
}
50-
vec3 col=vec3(color_intensity*sin(3.0*p.x)+color_intensity,color_intensity*sin(3.0*p.y)+color_intensity,color_intensity*sin(p.x+p.y)+color_intensity);
51-
gl_FragColor=vec4(col, 1);
52193
}
53194
else if(color_id == 1)
54195
gl_FragColor =vec4(.0, .5, .0,1.0);

hmiios2014/hmiios2014_en.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@
7272
<context>
7373
<name>TSDWindow</name>
7474
<message>
75-
<location filename="TSDWindow.cpp" line="718"/>
75+
<location filename="TSDWindow.cpp" line="720"/>
7676
<source>Angle: %1, Dist: %2</source>
7777
<translation type="unfinished">Angle: %1, Dist: %2</translation>
7878
</message>

hmiios2014/hmiios2014_zh.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@
7272
<context>
7373
<name>TSDWindow</name>
7474
<message>
75-
<location filename="TSDWindow.cpp" line="718"/>
75+
<location filename="TSDWindow.cpp" line="720"/>
7676
<source>Angle: %1, Dist: %2</source>
7777
<translation type="unfinished">角度: %1, 距离: %2</translation>
7878
</message>

hmiios2014/vshader.glsl

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,7 @@
11
attribute highp vec4 posAttr;
2-
//attribute lowp vec4 colAttr;
3-
//varying lowp vec4 col;
42
varying lowp vec4 pos;
5-
uniform lowp vec2 mouse;
6-
uniform lowp vec2 mouseDelta;
7-
uniform lowp vec2 resolution;
8-
uniform float time;
93
uniform highp mat4 matrix;
104
void main() {
11-
//col = colAttr;
125
pos = posAttr;
136
gl_Position = matrix * posAttr;
147
}

0 commit comments

Comments
 (0)