All pastes #735773 Raw Edit

Untitled

public text v1 · immutable
·published 2007-10-13 22:56 UTC
rendered paste body
// Hmm, svg perhaps?

uniform sampler2D image;
varying vec2  TexCoord0;

vec4 strokeColour  = vec4(1.0, 0.0, 1.0, 0.5);
vec4 fillColour    = vec4(1.0, 1.0, 1.0, 1.0);
vec4 current       = vec4(1.0, 1.0, 1.0, 0.0);
float lineWidth    = 0.01;

uniform vec2 pee0, pee1, pee2;




// a clipping rectangle. 2 positions follow
#define CLIPRECT  1.0
// start a new path, value is length in segments. position follows
#define STARTPATH 2.0
// set the line width, value is width
#define LINEWIDTH 3.0

// set the line colour, colour value follows
#define LINECOL 4.0

// a straight line segment
#define SEGMENT_LINE  1.0
// a quadratic curve segment. relative end and 
#define SEGMENT_QUADRATIC 2.0
// an eliptical line segment
#define SEGMENT_ELIPTIC 3.0









// colours in textures are 0-1 in r, g and b components
// ieee floats hold 24 bits of data, so we'll just use the rgb part to hold a 24-bit int
// then divide by its max value to get 
float col2float(const vec4 col)
{
   float ret = 0;
   return ret;
}

//#define blend(c1, c2) ( vec4( (c1.rgb * (1.0-c2.a)) + (c2.rgb * c2.a)) + vec4(0.0, 0.0, 0.0, c1.a + c2.a))

vec4 blend(const vec4 c1, const vec4 c2)
{
   vec4 ret;
   ret.rgb = c1.rgb*(1.0-c2.a) + (c2.rgb * c2.a);
   ret.a = c1.a + c2.a;
   
   return ret;
}
bool clipped(const vec2 low, const vec2 high, const vec2 point)
{
   return    (point.x > low.x && point.x < high.x)
          && (point.y > low.y && point.y < high.y);
}

float Line_OrientationOfPoint(const vec2 start, const vec2 end, const vec2 point) 
{
   return   (end.x-start.x)  *(point.y-start.y)
          - (point.x-start.x)*(end.y-start.y);
}

vec2 Line_ClosestPointTo(const vec2 start, const vec2 end, const vec2 point)
{ 
   const vec2 c = point - start;
   vec2 v = end - start;
   const float d = length(v);
   v /= d;
   const float t = dot(v,c);
   
   vec2 r;
   
   if (t < 0.0)
      r = start;
   else if (t > d) 
      r = end;
   else
   {
      v *= t;
      r = start + v;
   }
   return r;
}

/*
   cubic bezier curve
   
   t = 0 to 1.0
   t1 = 1-t;
   
   // (1-t)^3*p0 + 3t(1-t)^2*p1 + 3t^2(1-t)p2 + t^3p3
   
   res.x =   t1*t1*t1*pos0.x 
           + 3.0*t*t1*t1*pos1.x 
           + 3.0*t*t*t1*off0.x
           + t*t*t*off1.x;
           
   res.y =   t1*t1*t1*pos0.y 
           + 3.0*t*t1*t1*pos1.y 
           + 3.0*t*t*t1*off0.y
           + t*t*t*off1.y;
   
   
   // distance to quadratic
   
   

*/

float Line_Draw(const vec2 start, const vec2 end)
{
   //bool clip = clipped( start - vec2(lineWidth), TexCoord0 + vec2(lineWidth), TexCoord0);
   float ret = Line_OrientationOfPoint(start, end, TexCoord0);
   
   //if (!clip)
   {
      vec2 v = Line_ClosestPointTo( start, end, TexCoord0 );
      
      if ( distance(v, TexCoord0) < lineWidth)
      {
         ret = 0.0;
         current = blend(current, strokeColour);
      }
   }
   
   return ret;
}

uniform float bleh;

void main (void)
{
    vec4 col = texture2D(image, TexCoord0).rgba;
    if (col.a == 0.0)
      discard;
      
    vec4 result = vec4(1.0, 1.0, 1.0, 0.0);
    
/*
    for (int n=0; n<30; ++n)
    {
       float v = Line_Draw( vec2( 0.0 + (n/30.0), 0.5 ), 
                            vec2( 0.2 + (n/30.0) , 0.2 ));
    }
*/
    
    
      
    vec2 p0 = pee0; //vec2(0.2, 0.5);
    vec2 p1 = pee1;//vec2(0.5, 0.6);
    vec2 p2 = pee2; // vec2(0.5, 0.3);
    
    /*
    float d0 = distance(TexCoord0, p2);
    float d1 = d0 / distance(p0, p2);
    float d2 = d0 / distance(p1, p2);
    
    if (// abs(1.0 - (d1 + d2)) 
    distance(mix(mix(p0, p2, d1), mix(p0, p1, d2), bleh), TexCoord0) < lineWidth )
       current = vec4(1,1,1,1);
       
    */
    /*
    for (int n=0; n<50; ++n)
    {
       float r0 = atan(abs((p0 - p2).x / (p0 - p2).y));
       float r1 = atan((p1 - p2).x / (p1 - p2).y);
       float range = abs(max(r0, r1) - min(r0, r1));
       
       float r2 = atan((TexCoord0 - p2).x / (TexCoord0 - p2).y);
       
       float pos = clamp(r2, r0, r1);
       
       if (pos < 1.0)
          current = vec4(pos,pos,1,1); 
    }
    
    
    float d1 = dot( normalize(p2-p1), normalize(p2-p0));
    float d2 = dot( normalize(p2-p1), normalize(p2-TexCoord0));
    float range = d1;
    float pos = d2/d1;
    
    if (pos > 1.0)
       current = vec4(pos, 1, 1, 1);
    
    if (distance(TexCoord0, p1) < 0.01)
       current = vec4(1,0,0,1); 
    if (distance(TexCoord0, p0) < 0.01)
       current = vec4(0,1,0,1); 
    if (distance(TexCoord0, p2) < 0.03)
       current = vec4(0,0,1,1);
    //   }
    */
    gl_FragColor = blend(col,current);
    return;
}