Project

General

Profile

« Previous | Next » 

Revision 341c803d

Added by Leszek Koltunski over 8 years ago

Progress with Vertex3D

View differences:

src/main/res/raw/main_fragment_shader.glsl
36 36
const vec3 LUMI = vec3( 0.2125, 0.7154, 0.0721 );                                        
37 37
 
38 38
//////////////////////////////////////////////////////////////////////////////////////////////
39
// macroblocks
39
// MACROBLOCK EFFECT
40 40

  
41 41
void macroblock(float degree, int effect, inout vec2 tex)
42 42
  {
......
46 46
  }
47 47

  
48 48
//////////////////////////////////////////////////////////////////////////////////////////////
49
// change the RGB values
49
// CHROMA EFFECT
50 50

  
51 51
void chroma(float degree, int effect, inout vec4 color)
52 52
  {
......
54 54
  }
55 55

  
56 56
//////////////////////////////////////////////////////////////////////////////////////////////
57
// change the transparency level
57
// ALPHA EFFECT (change transparency level)
58 58

  
59 59
void alpha(float degree, int effect, inout vec4 color)
60 60
  {
......
62 62
  }
63 63

  
64 64
//////////////////////////////////////////////////////////////////////////////////////////////
65
// change the brightness level
65
// BRIGHTNESS EFFECT
66 66

  
67 67
void brightness(float degree, int effect, inout vec4 color)
68 68
  {
......
70 70
  }
71 71
  
72 72
//////////////////////////////////////////////////////////////////////////////////////////////
73
// change the contrast level
73
// CONTRAST EFFECT
74 74

  
75 75
void contrast(float degree, int effect, inout vec4 color)
76 76
  {
......
78 78
  }
79 79

  
80 80
//////////////////////////////////////////////////////////////////////////////////////////////
81
// change the saturation level
81
// SATURATION EFFECT
82 82

  
83 83
void saturation(float degree, int effect, inout vec4 color)
84 84
  {
......
102 102
    {
103 103
    diff = (v_Position.xy - fUniforms[2*i+1].xy)/fUniforms[2*i+1].zw;
104 104
    pointDegree = max(0.0,1.0-dot(diff,diff));
105
  
106
    //switch(fType[i])  // future version of GLSL
107
    //  {
108
    //  case MACROBLOCK        : macroblock(sign(pointDegree),2*i,tex); break;
109
    //  case CHROMA            : chroma    (sign(pointDegree),2*i,col); break;
110
    //  case SMOOTH_CHROMA     : chroma    (     pointDegree ,2*i,col); break;
111
    //  case ALPHA             : alpha     (sign(pointDegree),2*i,col); break;
112
    //  case SMOOTH_ALPHA      : alpha     (     pointDegree ,2*i,col); break;
113
    //  case BRIGHTNESS        : brightness(sign(pointDegree),2*i,col); break;
114
    //  case SMOOTH_BRIGHTNESS : brightness(     pointDegree ,2*i,col); break;
115
    //  case CONTRAST          : contrast  (sign(pointDegree),2*i,col); break;
116
    //  case SMOOTH_CONTRAST   : contrast  (     pointDegree ,2*i,col); break;
117
    //  case SATURATION        : saturation(sign(pointDegree),2*i,col); break;
118
    //  case SMOOTH_SATURATION : saturation(     pointDegree ,2*i,col); break;
119
    //  }
120
    
105

  
121 106
         if( fType[i]==MACROBLOCK        ) macroblock(sign(pointDegree),2*i,tex);
122 107
    else if( fType[i]==CHROMA            ) chroma    (sign(pointDegree),2*i,col);
123 108
    else if( fType[i]==SMOOTH_CHROMA     ) chroma    (     pointDegree ,2*i,col);
......
133 118
#endif
134 119
 
135 120
  gl_FragColor = (col * 0.5 * (v_Normal.z+1.0) * texture2D(u_Texture, tex));
136
  }
121
  }
src/main/res/raw/main_vertex_shader.glsl
48 48
#endif
49 49

  
50 50
#if NUM_VERTEX>0
51

  
52
//////////////////////////////////////////////////////////////////////////////////////////////
53
// HELPER FUNCTIONS
54
//////////////////////////////////////////////////////////////////////////////////////////////
55
// Let (v.x,v.y) be point P (the current vertex).
56
// Let vPoint[effect].xy be point S (the center of effect)
57
// Let vPoint[effect].xy + vRegion[effect].xy be point O (the center of the Region circle)
58
// Let X be the point where the halfline SP meets a) if region is non-null, the region circle b) otherwise, the edge of the bitmap.
59
//
60
// If P is inside the Region, this function returns |PX|/||SX|, aka the 'degree' of point P. Otherwise, it returns 0.
61
//
62
// We compute the point where half-line from S to P intersects the edge of the bitmap. If that's inside the circle, end. If not, we solve the
63
// the triangle with vertices at O, P and the point of intersection with the circle we are looking for X.
64
// We know the lengths |PO|, |OX| and the angle OPX , because cos(OPX) = cos(180-OPS) = -cos(OPS) = -PS*PO/(|PS|*|PO|)
65
// then from the law of cosines PX^2 + PO^2 - 2*PX*PO*cos(OPX) = OX^2 so
66
// PX = -a + sqrt(a^2 + OX^2 - PO^2) where a = PS*PO/|PS| but we are really looking for d = |PX|/(|PX|+|PS|) = 1/(1+ (|PS|/|PX|) ) and
67
// |PX|/|PS| = -b + sqrt(b^2 + (OX^2-PO^2)/PS^2) where b=PS*PO/|PS|^2 which can be computed with only one sqrt.
68
//
69
// the trick below is the if-less version of the
70
//
71
// t = dx<0.0 ? (u_objD.x-v.x) / (u_objD.x-ux) : (u_objD.x+v.x) / (u_objD.x+ux);
72
// h = dy<0.0 ? (u_objD.y-v.y) / (u_objD.y-uy) : (u_objD.y+v.y) / (u_objD.y+uy);
73
// d = min(t,h);
74
//
75
// float d = min(-ps.x/(sign(ps.x)*u_objD.x+p.x),-ps.y/(sign(ps.y)*u_objD.y+p.y))+1.0;
76
//
77
// We still have to avoid division by 0 when p.x = +- u_objD.x or p.y = +- u_objD.y (i.e on the edge of the Object)
78
// We do that by first multiplying the above 'float d' with sign(denominator1*denominator2)^2.
79
//
80
//////////////////////////////////////////////////////////////////////////////////////////////
81
// return degree of the point as defined by the bitmap rectangle
82

  
83
float degree_bitmap(in vec2 S, in vec2 PS)
84
  {
85
  vec2 A = sign(PS)*u_objD.xy + S;
86
  float B = sign(A.x*A.y);
87

  
88
  return B*B*(1.0 + min(-PS.x/A.x,-PS.y/A.y));
89
  }
90

  
91
//////////////////////////////////////////////////////////////////////////////////////////////
92
// return degree of the point as defined by the Region
93
// Currently only supports circles; .xy = vector from center of effect to the center of the circle, .z = radius
94

  
95
float degree_region(in vec3 region, in vec2 PS)
96
  {
97
  vec2 PO  = PS + region.xy;
98
  float D = region.z*region.z-dot(PO,PO);      // D = |OX|^2 - |PO|^2
99
  float ps_sq = dot(PS,PS);
100
  float DOT  = dot(PS,PO)/ps_sq;
101

  
102
  return max(sign(D),0.0) / (1.0 + 1.0/(sqrt(DOT*DOT+D/ps_sq)-DOT));  // if D<=0 (i.e p is outside the Region) return 0.
103
  }
104

  
105
//////////////////////////////////////////////////////////////////////////////////////////////
106
// return min(degree_bitmap,degree_region). Just like degree_region, currently only supports circles.
107

  
108
float degree(in vec3 region, in vec2 S, in vec2 PS)
109
  {
110
  vec2 PO  = PS + region.xy;
111
  float D = region.z*region.z-dot(PO,PO);      // D = |OX|^2 - |PO|^2
112
  vec2 A = sign(PS)*u_objD.xy + S;
113
  float B = sign(A.x*A.y);
114
  float E = B*B*(1.0 + min(-PS.x/A.x,-PS.y/A.y));
115
  float ps_sq = dot(PS,PS);
116
  float DOT  = dot(PS,PO)/ps_sq;
117

  
118
  return max(sign(D),0.0) * min(1.0/(1.0 + 1.0/(sqrt(DOT*DOT+D/ps_sq)-DOT)),E);  // if D<=0 (i.e p is outside the Region) return 0.
119
  }
120

  
121
//////////////////////////////////////////////////////////////////////////////////////////////
122
// Clamp v.z to (-u_Depth,u_Depth) with the following function:
123
// define h to be, say, 0.7; let H=u_Depth
124
//      if v.z < -hH then v.z = (-(1-h)^2 * H^2)/(v.z+(2h-1)H) -H   (function satisfying f(-hH)=-hH, f'(-hH)=1, lim f(x) = -H)
125
// else if v.z >  hH then v.z = (-(1-h)^2 * H^2)/(v.z-(2h-1)H) +H   (function satisfying f(+hH)=+hH, f'(+hH)=1, lim f(x) = +H)
126
// else v.z = v.z
127

  
128
void restrict(inout float v)
129
  {
130
  const float h = 0.7;
131
  float signV = 2.0*max(0.0,sign(v))-1.0;
132
  float c = ((1.0-h)*(h-1.0)*u_Depth*u_Depth)/(v-signV*(2.0*h-1.0)*u_Depth) +signV*u_Depth;
133
  float b = max(0.0,sign(abs(v)-h*u_Depth));
134

  
135
  v = b*c+(1.0-b)*v; // Avoid branching: if abs(v)>h*u_Depth, then v=c; otherwise v=v.
136
  }
137

  
51 138
//////////////////////////////////////////////////////////////////////////////////////////////
52
// Deform the whole shape of the bitmap by force V 
139
// DEFORM EFFECT
140
//
141
// Deform the whole shape of the bitmap by force V
53 142
// 
54 143
// If the point of application (Sx,Sy) is on the edge of the bitmap, then:
55 144
// a) ignore Vz
......
117 206
  }
118 207

  
119 208
//////////////////////////////////////////////////////////////////////////////////////////////
120
// Let (v.x,v.y) be point P (the current vertex). 
121
// Let vPoint[effect].xy be point S (the center of effect)
122
// Let vPoint[effect].xy + vRegion[effect].xy be point O (the center of the Region circle)
123
// Let X be the point where the halfline SP meets a) if region is non-null, the region circle b) otherwise, the edge of the bitmap. 
124
//
125
// If P is inside the Region, this function returns |PX|/||SX|, aka the 'degree' of point P. Otherwise, it returns 0. 
126
//
127
// We compute the point where half-line from S to P intersects the edge of the bitmap. If that's inside the circle, end. If not, we solve the 
128
// the triangle with vertices at O, P and the point of intersection with the circle we are looking for X.
129
// We know the lengths |PO|, |OX| and the angle OPX , because cos(OPX) = cos(180-OPS) = -cos(OPS) = -PS*PO/(|PS|*|PO|)
130
// then from the law of cosines PX^2 + PO^2 - 2*PX*PO*cos(OPX) = OX^2 so 
131
// PX = -a + sqrt(a^2 + OX^2 - PO^2) where a = PS*PO/|PS| but we are really looking for d = |PX|/(|PX|+|PS|) = 1/(1+ (|PS|/|PX|) ) and
132
// |PX|/|PS| = -b + sqrt(b^2 + (OX^2-PO^2)/PS^2) where b=PS*PO/|PS|^2 which can be computed with only one sqrt.
133
//
134
// the trick below is the if-less version of the
135
// 
136
// t = dx<0.0 ? (u_objD.x-v.x) / (u_objD.x-ux) : (u_objD.x+v.x) / (u_objD.x+ux);
137
// h = dy<0.0 ? (u_objD.y-v.y) / (u_objD.y-uy) : (u_objD.y+v.y) / (u_objD.y+uy);
138
// d = min(t,h);      
139
//
140
// float d = min(-ps.x/(sign(ps.x)*u_objD.x+p.x),-ps.y/(sign(ps.y)*u_objD.y+p.y))+1.0;
141
//
142
// We still have to avoid division by 0 when p.x = +- u_objD.x or p.y = +- u_objD.y (i.e on the edge of the Object)
143
// We do that by first multiplying the above 'float d' with sign(denominator1*denominator2)^2.
144
//
145
//////////////////////////////////////////////////////////////////////////////////////////////
146
// return degree of the point as defined by the bitmap rectangle
147
   
148
float degree_bitmap(in vec2 S, in vec2 PS)
149
  {
150
  vec2 A = sign(PS)*u_objD.xy + S;
151
  float B = sign(A.x*A.y);
152

  
153
  return B*B*(1.0 + min(-PS.x/A.x,-PS.y/A.y));
154
  }
155

  
156
//////////////////////////////////////////////////////////////////////////////////////////////
157
// return degree of the point as defined by the Region
158
// Currently only supports circles; .xy = vector from center of effect to the center of the circle, .z = radius
159
      
160
float degree_region(in vec3 region, in vec2 PS)
161
  {
162
  vec2 PO  = PS + region.xy;
163
  float D = region.z*region.z-dot(PO,PO);      // D = |OX|^2 - |PO|^2
164
  float ps_sq = dot(PS,PS);
165
  float DOT  = dot(PS,PO)/ps_sq;
166
  
167
  return max(sign(D),0.0) / (1.0 + 1.0/(sqrt(DOT*DOT+D/ps_sq)-DOT));  // if D<=0 (i.e p is outside the Region) return 0.
168
  }
169
   
170
//////////////////////////////////////////////////////////////////////////////////////////////
171
// return min(degree_bitmap,degree_region). Just like degree_region, currently only supports circles.
172
    
173
float degree(in vec3 region, in vec2 S, in vec2 PS)
174
  {
175
  vec2 PO  = PS + region.xy;
176
  float D = region.z*region.z-dot(PO,PO);      // D = |OX|^2 - |PO|^2
177
  vec2 A = sign(PS)*u_objD.xy + S;
178
  float B = sign(A.x*A.y);
179
  float E = B*B*(1.0 + min(-PS.x/A.x,-PS.y/A.y));
180
  float ps_sq = dot(PS,PS);
181
  float DOT  = dot(PS,PO)/ps_sq;
182
  
183
  return max(sign(D),0.0) * min(1.0/(1.0 + 1.0/(sqrt(DOT*DOT+D/ps_sq)-DOT)),E);  // if D<=0 (i.e p is outside the Region) return 0.
184
  }
185

  
186
//////////////////////////////////////////////////////////////////////////////////////////////
187
// Distort effect
209
// DISTORT EFFECT
188 210
//
189 211
// Point (Px,Py) gets moved by vector (Wx,Wy,Wz) where Wx/Wy = Vx/Vy i.e. Wx=aVx and Wy=aVy where 
190 212
// a=Py/Sy (N --> when (Px,Py) is above (Sx,Sy)) or a=Px/Sx (W) or a=(w-Px)/(w-Sx) (E) or a=(h-Py)/(h-Sy) (S) 
......
260 282
  }
261 283
 
262 284
//////////////////////////////////////////////////////////////////////////////////////////////
263
// sink effect
285
// SINK EFFECT
286
//
264 287
// Pull P=(v.x,v.y) towards S=vPoint[effect] with P' = P + (1-h)d(S-P)
265 288
// when h>1 we are pushing points away from S: P' = P + (1/h-1)d(S-P)
266 289
 
......
275 298
  }
276 299

  
277 300
//////////////////////////////////////////////////////////////////////////////////////////////
278
// Swirl 
301
// SWIRL EFFECT
279 302
//
280 303
// Let d be the degree of the current vertex V with respect to center of the effect S and Region vRegion.
281 304
// This effect rotates the current vertex V by vInterpolated.x radians clockwise around the circle dilated 
......
297 320
  P.xy += min(d1_circle,d1_bitmap)*(PS - PS2/(1.0-d2));        // if d2=1 (i.e P=S) we should have P unchanged. How to do it?
298 321
  }
299 322

  
300
//////////////////////////////////////////////////////////////////////////////////////////////
301
// Clamp v.z to (-u_Depth,u_Depth) with the following function:
302
// define h to be, say, 0.7; let H=u_Depth
303
//      if v.z < -hH then v.z = (-(1-h)^2 * H^2)/(v.z+(2h-1)H) -H   (function satisfying f(-hH)=-hH, f'(-hH)=1, lim f(x) = -H)
304
// else if v.z >  hH then v.z = (-(1-h)^2 * H^2)/(v.z-(2h-1)H) +H   (function satisfying f(+hH)=+hH, f'(+hH)=1, lim f(x) = +H)
305
// else v.z = v.z  
306
	
307
void restrict(inout float v)
308
  {
309
  const float h = 0.7;
310
  float signV = 2.0*max(0.0,sign(v))-1.0;
311
  float c = ((1.0-h)*(h-1.0)*u_Depth*u_Depth)/(v-signV*(2.0*h-1.0)*u_Depth) +signV*u_Depth;
312
  float b = max(0.0,sign(abs(v)-h*u_Depth));
313
  
314
  v = b*c+(1.0-b)*v; // Avoid branching: if abs(v)>h*u_Depth, then v=c; otherwise v=v.
315
  }                
316 323
#endif
317 324

  
318 325
//////////////////////////////////////////////////////////////////////////////////////////////
......
325 332
#if NUM_VERTEX>0
326 333
  for(int i=0; i<vNumEffects; i++)
327 334
    {
328
    //switch(vType[i])
329
    //  {
330
    //  case DISTORT: distort(3*i,v,n); break;
331
    //  case DEFORM : deform(3*i,v)   ; break;
332
    //  case SINK   : sink(3*i,v)     ; break;
333
    //  case SWIRL  : swirl(3*i,v)    ; break;
334
    //  }
335
        
336 335
         if( vType[i]==DISTORT) distort(3*i,v,n);
337
    else if( vType[i]==DEFORM ) deform(3*i,v);
338
    else if( vType[i]==SINK   ) sink(3*i,v);
339
    else if( vType[i]==SWIRL  ) swirl(3*i,v);
336
    else if( vType[i]==DEFORM ) deform (3*i,v);
337
    else if( vType[i]==SINK   ) sink   (3*i,v);
338
    else if( vType[i]==SWIRL  ) swirl  (3*i,v);
340 339
    }
341 340
 
342 341
  restrict(v.z);  

Also available in: Unified diff