Project

General

Profile

« Previous | Next » 

Revision 0318e7e3

Added by Leszek Koltunski almost 8 years ago

Important bugfix in the vertex shader: there was division by 0 in case Center point was on the edge of the Object, resulting in some vertices being shot into outer space.

View differences:

src/main/res/raw/main_vertex_shader.glsl
17 17
// along with Distorted.  If not, see <http://www.gnu.org/licenses/>.                       //
18 18
//////////////////////////////////////////////////////////////////////////////////////////////
19 19

  
20
uniform vec3 u_bmpD;                      // half of object width x half of object height X half the depth; 
20
uniform vec3 u_objD;                      // half of object width x half of object height X half the depth;
21 21
                                          // point (0,0,0) is the center of the object
22 22

  
23 23
uniform float u_Depth;                    // max absolute value of v.z ; beyond that the vertex would be culled by the near or far planes.
......
90 90
//
91 91
//    If we are dragging the top edge:    
92 92
//
93
//    Now point (x,u_bmpD.x) on the top edge will move by vector (X(t),Y(t)) where those functions are given by (*) and
94
//    t =  x < dSx ? (u_bmpD.x+x)/(u_bmpD.x+dSx) : (u_bmpD.x-x)/(u_bmpD.x-dSx)   
95
//    Any point (x,y) will move by vector (a*X(t),a*Y(t)) where a is (y+u_bmpD.y)/(2*u_bmpD.y)
93
//    Now point (x,u_objD.x) on the top edge will move by vector (X(t),Y(t)) where those functions are given by (*) and
94
//    t =  x < dSx ? (u_objD.x+x)/(u_objD.x+dSx) : (u_objD.x-x)/(u_objD.x-dSx)
95
//    Any point (x,y) will move by vector (a*X(t),a*Y(t)) where a is (y+u_objD.y)/(2*u_objD.y)
96 96
  
97 97
void deform(in int effect, inout vec4 v)
98 98
  {
......
100 100
  vec2 w = vUniforms[effect].xy;    // w = vec(MM')
101 101
  vec2 vert_vec, horz_vec; 
102 102
  vec2 signXY = sign(p-v.xy);  
103
  vec2 time = (u_bmpD.xy+signXY*v.xy)/(u_bmpD.xy+signXY*p);
104
  vec2 factorV = vec2(0.5,0.5) + sign(p)*v.xy/(4.0*u_bmpD.xy);
105
  vec2 factorD = (u_bmpD.xy-signXY*p)/(2.0*u_bmpD.xy);
103
  vec2 time = (u_objD.xy+signXY*v.xy)/(u_objD.xy+signXY*p);
104
  vec2 factorV = vec2(0.5,0.5) + sign(p)*v.xy/(4.0*u_objD.xy);
105
  vec2 factorD = (u_objD.xy-signXY*p)/(2.0*u_objD.xy);
106 106
  vec2 vert_d = factorD.x*w;
107 107
  vec2 horz_d = factorD.y*w;
108
  vec2 corr = 0.33 / ( 1.0 + (4.0*u_bmpD.x*u_bmpD.x)/dot(w,w) ) * (p+w+signXY*u_bmpD.xy); // .x = the vector tangent to X(t) at Fl = 0.3*vec(LM')  (or vec(RM') if signXY.x=-1). 
109
                                                                                       // .y = the vector tangent to X(t) at Fb = 0.3*vec(BM')  (or vec(TM') if signXY.y=-1)
110
                                                                                       // the scalar: make the length of the speed vectors at Fl and Fr be 0 when force vector 'w' is zero 
108
  vec2 corr = 0.33 / ( 1.0 + (4.0*u_objD.x*u_objD.x)/dot(w,w) ) * (p+w+signXY*u_objD.xy); // .x = the vector tangent to X(t) at Fl = 0.3*vec(LM')  (or vec(RM') if signXY.x=-1).
109
                                                                                          // .y = the vector tangent to X(t) at Fb = 0.3*vec(BM')  (or vec(TM') if signXY.y=-1)
110
                                                                                          // the scalar: make the length of the speed vectors at Fl and Fr be 0 when force vector 'w' is zero
111 111
  vert_vec.x = ( w.x-vert_d.x-corr.x )*time.x*time.x + corr.x*time.x + vert_d.x;
112 112
  horz_vec.y = (-w.y+horz_d.y+corr.y )*time.y*time.y - corr.y*time.y - horz_d.y;
113 113
  vert_vec.y = (-3.0*vert_d.y+2.0*w.y )*time.x*time.x*time.x + (-3.0*w.y+5.0*vert_d.y )*time.x*time.x - vert_d.y*time.x - vert_d.y;
......
133 133
//
134 134
// the trick below is the if-less version of the
135 135
// 
136
// t = dx<0.0 ? (u_bmpD.x-v.x) / (u_bmpD.x-ux) : (u_bmpD.x+v.x) / (u_bmpD.x+ux);
137
// h = dy<0.0 ? (u_bmpD.y-v.y) / (u_bmpD.y-uy) : (u_bmpD.y+v.y) / (u_bmpD.y+uy);
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 138
// d = min(t,h);      
139 139
//
140
// float d = min(-ps.x/(sign(ps.x)*u_bmpD.x+p.x),-ps.y/(sign(ps.y)*u_bmpD.y+p.y))+1.0;    
141

  
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
//
142 145
//////////////////////////////////////////////////////////////////////////////////////////////
143 146
// return degree of the point as defined by the bitmap rectangle
144 147
   
145 148
float degree_bitmap(in vec2 S, in vec2 PS)
146 149
  {
147
  return min(-PS.x/(sign(PS.x)*u_bmpD.x+S.x),-PS.y/(sign(PS.y)*u_bmpD.y+S.y))+1.0;    
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));
148 154
  }
149 155

  
150 156
//////////////////////////////////////////////////////////////////////////////////////////////
......
168 174
  {
169 175
  vec2 PO  = PS + region.xy;
170 176
  float D = region.z*region.z-dot(PO,PO);      // D = |OX|^2 - |PO|^2
171
  float E = min(-PS.x/(sign(PS.x)*u_bmpD.x+S.x),-PS.y/(sign(PS.y)*u_bmpD.y+S.y))+1.0;    
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));
172 180
  float ps_sq = dot(PS,PS);
173 181
  float DOT  = dot(PS,PO)/ps_sq;
174 182
  
......
311 319
  		  
312 320
void main()                                                 	
313 321
  {              
314
  vec4 v = vec4( 2.0*u_bmpD*a_Position,1.0 );
322
  vec4 v = vec4( 2.0*u_objD*a_Position,1.0 );
315 323
  vec4 n = vec4(a_Normal,0.0);
316 324

  
317 325
#if NUM_VERTEX>0

Also available in: Unified diff