/*
	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.
	- - - - - - - - - - - - - - - - - - - - - - - - - - - -

  drawImageBlender.pde
    
                                                                             */


void drawImageBlender(boolean render, // on off
                      int pixBank, int pixId,
                      //                      PImage iblImg1,
                      int iblNum,
                      float iblX, float iblY, float iblWidth, float iblHeight,
                      float iblRot,
                      float iblSpeedX, float iblSpeedY,
                      float iblSpeedXfactor, float iblSpeedYfactor,
                      //float iblTexX, float iblTexY,  // using global array
                      float iblTexWidth, float iblTexHeight,
                      float iblTexSpeedX, float iblTexSpeedY,
                      float iblTexSpeedXfactor, float iblTexSpeedYfactor,
                      int iblH, int iblS, int iblB, int iblA,
                      int iblBflicker,
                      float iblItX, float iblItY,
                      float iblItTexX, float iblItTexY, float iblItRot
                      )

{ 
  if (render) {


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

    //iblRot = iblRot * 0.1;
    
    pushMatrix();

    translate(width/2, height/2); // center coordinate system
    
    // autonomous movement
    iblSpeedX = 0.1;
    iblSpeedY = 0.1;
    iblSpeedX *= iblSpeedXfactor;
    iblSpeedY *= iblSpeedYfactor;
    iblX += iblSpeedX;													
    iblY += iblSpeedY;

    if (iblX > width) { iblX = -width; }
    if (iblX < -width) { iblX = width; }
    if (iblY > height) { iblY = -height; }
    if (iblY < -height) { iblY = height; }
    
    iblTexSpeedX = 0.1;
    iblTexSpeedY = 0.1;
    iblTexSpeedX *= iblTexSpeedXfactor;
    iblTexSpeedY *= iblTexSpeedYfactor;
    iblTexXY[0] += iblTexSpeedX;
    iblTexXY[1] += iblTexSpeedY;

    int iblBflicked;
    if (frameCount % flickCount == 1) { iblBflicked = iblB - iblBflicker; }
    else { iblBflicked = iblB; }

    colorMode(HSB, 127);

    for (int i=0; i < iblNum; i++) {
      
      // draw in an independent coordinate system
      pushMatrix();
    
      // XY = translate
      translate(iblX + i*iblItX, iblY + i*iblItY);
      rotate(iblRot + (i*iblItRot));
      textureWrap(REPEAT);
      // draw the quad
      beginShape();
      tint(iblH, iblS, iblBflicked, iblA);
      //tint(200,255,255);
      //      texture(iblImg1);
      textureMode(IMAGE);
      texture(imgPool[pixBank][pixId]);
               
      vertex(-iblWidth, -iblHeight, // X & Y of the vertex
             width/2 - iblWidth + iblX + iblTexXY[0] + (i*iblItTexX) - iblTexWidth, // X of the texture
             height/2 - iblHeight + iblY + iblTexXY[1] + (i*iblItTexY) - iblTexHeight); // Y of the texture
      vertex(iblWidth, -iblHeight,
             width/2 + iblWidth + iblX + iblTexXY[0] + (i*iblItTexX) + iblTexWidth,
             height/2 - iblHeight + iblY + iblTexXY[1] + (i*iblItTexY) - iblTexHeight); 
      vertex(iblWidth, iblHeight,
             width/2 + iblWidth + iblX + iblTexXY[0] + (i*iblItTexX) + iblTexWidth,
             height/2 + iblHeight + iblY + iblTexXY[1] + (i*iblItTexY) + iblTexHeight); 
      vertex(-iblWidth, iblHeight,
             width/2 - iblWidth + iblX + iblTexXY[0] + (i*iblItTexX) - iblTexWidth,
             height/2 + iblHeight + iblY + iblTexXY[1] + (i*iblItTexY) + iblTexHeight);
      endShape();

      popMatrix(); // end independent coordinate system

    }

    popMatrix();
    
    /* //** debug ****************************
       fill(0,125);
       rect(0, 0, 200, 30);

       fill(255);
       text(iblTexSpeedXfactor, 5, 30);

       //text(iblSpeedXfactor, 20, 60);
       */

  }
}
