Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

Untitled

By: a guest on Feb 6th, 2013  |  syntax: OpenGL Shading  |  size: 3.81 KB  |  views: 69  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. in vec2 texCoord;
  2.  
  3. out vec2 newPosition;
  4.  
  5. uniform int quads_x;
  6. uniform int quads_y;
  7.  
  8. uniform GridMatrix{
  9.    vec2 grid[400];
  10. } g;
  11.  
  12. float distToLine(vec2 pt1, vec2 pt2, vec2 testPt){
  13.         vec2 lineDir = pt2 - pt1;
  14.         vec2 perpDir = vec2(lineDir.y, -lineDir.x);
  15.         vec2 dirToPt1 = pt1 - testPt;
  16.         return abs(dot(normalize(perpDir), dirToPt1));
  17. }
  18.  
  19. bool pointInTriangle(vec2 A, vec2 B, vec2 C, vec2 X){
  20.         float alpha, beta;
  21.  
  22.         // Calculate barycentric coordinates
  23.         float det = ((B.y-C.y)*(A.x-C.x)+(C.x-B.x)*(A.y-C.y));
  24.         alpha = ((B.y-C.y)*(X.x-C.x)+(C.x-B.x)*(X.y-C.y))/det;
  25.         beta  = ((C.y-A.y)*(X.x-C.x)+(A.x-C.x)*(X.y-C.y))/det;
  26.  
  27.         if(alpha+beta >= 0.0 && alpha+beta <= 1.0 && alpha >= 0.0 && alpha <= 1.0 && beta >= 0.0 && beta <= 1.0)
  28.                 return true;
  29.         return false;
  30. }
  31.  
  32.  
  33. vec2 bilinearInterpolation(vec2 point, vec2 local_bl, vec2 local_br, vec2 local_tl, vec2 local_tr, bool vert_parallel, bool hori_parallel){
  34.     float s, t;
  35.  
  36.     bool vert_parallel = cross(vec3((local_tl-local_bl), 0), vec3(local_tr-local_br, 0)).z == 0.0;
  37.     bool hori_parallel = cross(vec3((local_tl-local_tr), 0), vec3(local_bl-local_br, 0)).z == 0.0;
  38.        
  39.     vec3 A = vec3(local_bl.x, local_bl.y, 0);
  40.     vec3 B = vec3(local_br.x, local_br.y, 0);
  41.     vec3 C = vec3(local_tr.x, local_tr.y, 0);
  42.     vec3 D = vec3(local_tl.x, local_tl.y, 0);
  43.     vec3 M = vec3(point, 0);
  44.  
  45.     if(!vert_parallel && !hori_parallel){
  46.         float a = (cross(C,D)+cross(D,B)+cross(A,C)+cross(B,A)).z;
  47.         float b = (cross(M,C)+cross(D,M)+cross(B,M)+cross(M,A)+cross(C,A)+cross(B,D)+2.0*cross(A,B)).z;
  48.         float c = (cross(M,B)+cross(A,M)+cross(B,A)).z;
  49.  
  50.         float t1 = (-b+sqrt(pow(b,2.0)-4.0*a*c))/(2.0*a);
  51.         float t2 = (-b-sqrt(pow(b,2.0)-4.0*a*c))/(2.0*a);
  52.  
  53.         t = t1;
  54.         if(t1 < 0.0 || t1 > 1.0)
  55.             if(abs(t1) > abs(t2))      
  56.                 t = t2;
  57.  
  58.         s = (M.x-A.x-t *(D.x-A.x))/(B.x-A.x+t*(C.x-D.x-B.x+A.x));              
  59.         }
  60.         else if(vert_parallel && !hori_parallel){ // vertical edges parallel
  61.             float dist1 = distToLine(local_tl, local_bl, point);
  62.             float dist2 = distToLine(local_tr, local_br, point);
  63.             s = dist1/(dist1+dist2);           
  64.             t = (M.y-A.y-s *(B.y-A.y))/(D.y-A.y+s*(C.y-D.y-B.y+A.y));
  65.         }
  66.         else if(!vert_parallel && hori_parallel){// horizontal edges parallel
  67.             float dist1 = distToLine(local_bl, local_br, point);
  68.             float dist2 = distToLine(local_tl, local_tr, point);
  69.             t = dist1/(dist1+dist2);
  70.  
  71.             s = (M.x-A.x-t *(D.x-A.x))/(B.x-A.x+t*(C.x-D.x-B.x+A.x));
  72.         }
  73.         else{
  74.             float dist1 = distToLine(local_bl, local_br, point);
  75.             float dist2 = distToLine(local_tl, local_tr, point);
  76.             t = dist1/(dist1+dist2);
  77.  
  78.             dist1 = distToLine(local_bl, local_tl, point);
  79.             dist2 = distToLine(local_br, local_tr, point);
  80.             s = dist1/(dist1+dist2);
  81.     }
  82.     return vec2(s, t);
  83. }
  84.  
  85. vec2 staticWarp(vec2 point){
  86.     int x = 0;
  87.     int y = 0;
  88.     while(y < quads_y){
  89.         int curr_y = y*(quads_x+1);
  90.         int curr_y_plus1 = (y+1)*(quads_x+1);
  91.         vec2 bl = g.grid[curr_y_plus1+x]; // bottom left
  92.         vec2 br = g.grid[curr_y_plus1+x+1]; // bottom right
  93.         vec2 tl = g.grid[curr_y+x]; // top left
  94.         vec2 tr = g.grid[curr_y+x+1]; // top right
  95.         if(pointInTriangle(bl, br, tl, point) || pointInTriangle(tl, tr, br, point)){
  96.              point = bilinearInterpolation(point, bl, br, tl, tr);
  97.              point = vec2((x + point.x)/quads_x, (quads_y - y - 1 + point.y)/quads_y);
  98.              return point;
  99.         }
  100.         x = x + 1;
  101.         if ( x ==  quads_x){
  102.             x = 0;
  103.             y = y + 1;
  104.         }
  105.     }
  106.     return vec2(-1.0, -1.0);
  107. }
  108.  
  109.  
  110. void main()
  111. {
  112.     vec2 point = texCoord;
  113.    
  114.     point = staticWarp(point);
  115.    
  116.     newPosition = point;
  117. }