/*
	This file is part of "Interface Fractures III - Silicon".
	Copyright (c) 2015 Luka Prinčič, All rights reserved.
	This program is free software distributed under
	GNU General Public Licence. See COPYING for more info.
	- - - - - - - - - - - - - - - - - - - - - - - - - - - -

  drawCurvesfly.pde
    
                                                                             */


void drawFlylines( boolean render, // should we render or not?
                   int curvesNum,
                   int pixBank, int pixId,
                   float speed, float direction, float sensitivity,
                   float brightDirection,
                   float rotation, float brightRotation,
                   int curveHue, int curveSatur, int curveBright,
                   int curveAlpha, float curveBrightAlpha,
                   int lineLength, float brightLength, int strokeWeight,
                   int pixHue, int pixSatur, int pixBright, int pixAlpha
                   )
{
  if (render) {

    colorMode(HSB, 127, 127, 127, 127);
		blendMode(BLEND); // reset blend mode just in case
    textureMode(IMAGE);

    // curvesNum = int(map(float(curvesNum), 0, 127, 0, 7000));
    speed = speed * 0.002;
    brightDirection = map(brightDirection, 0, 127, -5, 5);
    brightRotation = map(brightRotation, 0, 127, 0, 7);
    curveBrightAlpha = map(curveBrightAlpha, 0, 127, 0, 1);

    pixBank = min(pixBank, imgPool.length - 1);
    pixId = min(pixId, imgPool[pixBank].length - 1);

    
    // main loop!
    for (int i = 0; i  < curvesNum; i++) { // for each curve

      // is this the time of creation? 
      if (curveX[i] == 0.0) {
        if (curveY[i] == 0.0) {
          curveX[i] = random(width);
          curveY[i] = random(height);
        }
      }
      
      color curveC = imgPool[pixBank][pixId] // load only full-size HD images!
        .get(int(curveX[i]), int(curveY[i])); // get color of the pixel
      float curvePixBrightness = pow(brightness(curveC), 1.8) / 127.0;
      // maximum: pow(127, 1.8) / 127 = 48.19954
      // if sensitivity is 0 then speedPixBrightness is a constant 48.2 / 2
      // speed * (curvePixBrightness * sensitivity) + ((1-sensitivity) * 25))

      float curvDirection = radians(map(direction, 0, 127, 0, 360) + (curvePixBrightness * brightDirection));
      float cosdir = cos(curvDirection);
      float sindir = sin(curvDirection);
      
      float speedPixBrightness = speed * ((curvePixBrightness * sensitivity) + ((1-sensitivity) * 25));

      float lineLengthBri = pow(lineLength * 0.1, 2) * ( (curvePixBrightness * brightLength ) + ((1 - brightLength)*1 ) );
      //println(curvePixBrightness);

      curveX[i] = curveX[i] + speedPixBrightness * cosdir;
      curveY[i] = curveY[i] + speedPixBrightness * sindir;

      
      if (curveX[i] > width-1) { // if off-sceen on X-axis
        curveX[i] = 1; // wrap back
        curveY[i] = random(height-1); } // randomize Y-axis position
      else if (curveX[i] < 1) {
        curveX[i] = width-1;
        curveY[i] = random(height-1); // randomize Y-axis position
      }

      if (curveY[i] > height-1) { // if off-sceen on X-axis
        curveY[i] = 1; // wrap back
        curveX[i] = random(width-1); } // randomize Y-axis position
      else if (curveY[i] < 0-1) {
        curveY[i] = height-1;
        curveX[i] = random(width-1);  // randomize Y-axis position
      }

      float curveRotation = radians(map(rotation, 0, 127, 0, 180) + (brightRotation * curvePixBrightness) );  


      // float curveBrightAlphaFact = (brightness(curveC) / 127.0) * curveBrightAlpha;
      float curveAlphaDest = curveAlpha * (((brightness(curveC) / 127.0)
            * curveBrightAlpha)
           + (1 - curveBrightAlpha));
      
      pushMatrix();
      stroke(color(curveHue, curveSatur, curveBright, curveAlphaDest));
      strokeWeight(strokeWeight);
      translate(curveX[i], curveY[i]);
      rotate(curveRotation);
      line (0, lineLengthBri, 0, -lineLengthBri);
      popMatrix();

    }

    noStroke();
    beginShape();
    texture(imgPool[pixBank][pixId]);
    tint(pixHue, pixSatur, pixBright, pixAlpha);
    vertex(0, 0, 0, 0);
    vertex(width, 0, width, 0);
    vertex(width, height, width, height);
    vertex(0, height, 0, height);
    endShape();


  } 
} 

