Advertisement
Guest User

Untitled

a guest
May 16th, 2013
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #version 110
  2. #define CUBE 1.0/3.0
  3.  
  4. varying vec3 p;  // Candidate point
  5. varying vec3 p0; // Control point
  6. varying vec3 p1; // Control point
  7. varying vec3 p2; // Control point
  8.  
  9. float sum(vec3 v) {
  10.   return v.x + v.y + v.z;
  11. }
  12.  
  13. // The point on the bezier curve at t where 0 >= t >= 1
  14. vec3 curve(float t) {
  15.   return (pow(1.0-t, 2.0)*p0) + ((2.0*(1.0-t)*t)*p1) + (t*t*p2);
  16. }
  17.  
  18. void main(void) {
  19.   // These are vectors of coefficients for the cubic equation resulting from
  20.   // (1) Q'(t) `dot` (P - Q(t)) = 0
  21.   // First I expanded (1) into (2) (Q'.x*P.x - Q'.x*Q.x) + (Q'.y*P.y - Q'.y*Q.y) + (Q'.z*P.z - Q'.z*Q.z)
  22.   // Each term of (2) is a cubic equation (3) at^3 + bt^2 + ct + d = 0
  23.   // These four vectors are each meant to represent the terms in (2) for a given coefficient in (3)
  24.   vec3 av = (4.0*p1*p1) - (2.0*p1*p0) - (4.0*p2*p1) + (2.0*p0*p2) - (4.0*p0*p1) + (2.0*p0*p0) + (4.0*p1*p1) - (2.0*p0*p1);
  25.   vec3 bv = (-4.0*p1*p1) + (4.0*p1*p0) + (4.0*p2*p1) - (4.0*p2*p0) + (4.0*p0*p1) - (4.0*p0*p0) - (4.0*p1*p1) + (4.0*p0*p1) + (4.0*p0*p1) - (2.0*p0*p0) + (2.0*p1*p0);
  26.   vec3 cv = (-2.0*p1*p0) + (2.0*p2*p0) + (2.0*p0*p0) - (2.0*p0*p1) - (4.0*p0*p1) + (4.0*p0*p0) + (4.0*p1*p1) - (4.0*p1*p0) - (2.0*p*p1) + (2.0*p*p2) + (2.0*p*p0) - (2.0*p*p1);
  27.   vec3 dv = (-2.0*p0*p0) + (2.0*p1*p0) - (2.0*p*p0) + (2.0*p*p1);
  28.  
  29.   // Here I just apply the additions from (2)
  30.   float a = sum(av);
  31.   float b = sum(bv);
  32.   float c = sum(cv);
  33.   float d = sum(dv);
  34.  
  35.   // From here on out I'm trying to find the roots of the cubic equation with coefficients a, b, c, d
  36.   // I'm using trusty Wikipedia and Cardano's method: http://en.wikipedia.org/wiki/Cubic_equation#Cardano.27s_method
  37.   // Because there is already a variable named p, I've used s where p would be used on in the Wikipedia entry
  38.   float s = ((3.0*a*c) - (b*b)) / (3.0*a*a);
  39.   float q = ((2.0*b*b*b) - (9.0*a*b*c) + (27.0*a*a*d)) / (27.0*a*a*a);
  40.  
  41.   float q2   = (-q) / 2.0;
  42.   float qs4  = (q*q) / 4.0;
  43.   float pc27 = (s*s*s) / 27.0;
  44.  
  45.   float x = sqrt(qs4 + pc27);
  46.  
  47.   float y1= q2 + x;
  48.   float z1= q2 - x;
  49.  
  50.   float y2= q2 + x;
  51.   float z2= q2 + x;
  52.  
  53.   float y3= q2 - x;
  54.   float z3= q2 - x;
  55.  
  56.   float u1 = pow(y1, (1.0/3.0));
  57.   float v1 = pow(z1, (1.0/3.0));
  58.  
  59.   float u2 = pow(y2, (1.0/3.0));
  60.   float v2 = pow(z2, (1.0/3.0));
  61.  
  62.   float u3 = pow(y3, (1.0/3.0));
  63.   float v3 = pow(z3, (1.0/3.0));
  64.  
  65.   float t1 = u1 + v1;
  66.   float t2 = u2 + v2;
  67.   float t3 = u3 + v3;
  68.  
  69.   if(t1 >= 0.0 && t1 <= 1.0 && length(p - curve(t1)) < 0.1)
  70.     gl_FragColor = vec4(0.0, 0.0, 0.0, 1.0);
  71.   else if(t2 >= 0.0 && t2 <= 1.0 && length(p - curve(t2)) < 0.1)
  72.     gl_FragColor = vec4(0.0, 0.0, 0.0, 1.0);
  73.   else if(t3 >= 0.0 && t3 <= 1.0 && length(p - curve(t3)) < 0.1)
  74.     gl_FragColor = vec4(0.0, 0.0, 0.0, 1.0);
  75.   else
  76.     discard;
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement