/*

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

  drawPlates.pde
    
                                                                             */

void drawPlates( boolean render, // should we render or not?
            
            int imgAbank, int imgAid,
            int texAspeed, int texAdirection,
            int imgATopLeft, int imgATopRight,
            int imgABotRight, int imgABotLeft,
            int imgAalpha, 
            int imgAhue, int imgAbright,

            int imgBbank, int imgBid,
            int texBspeed, int texBdirection,
            int imgBTopLeft, int imgBTopRight,
            int imgBBotRight, int imgBBotLeft,
            int imgBalpha, 
            int imgBhue, int imgBbright,

            int imgCbank, int imgCid,
            int texCspeed, int texCdirection,
            int imgCTopLeft, int imgCTopRight,
            int imgCBotRight, int imgCBotLeft,
            int imgCalpha, 
            int imgChue, int imgCbright,

            int imgBblendMode, int imgCblendMode,
            int saturation, // common saturation for all
            int coordError // glitch texture coordinates - amount of error 0-127 - add random

            )
{
  if (render) {

		blendMode(BLEND); // reset blend mode just in case
    colorMode(HSB, 127, 127, 127, 127);    
    textureWrap(REPEAT);
    textureMode(NORMAL);
    imgAbank = min(imgAbank, imgPool.length - 1);
    imgBbank = min(imgBbank, imgPool.length - 1);
    imgCbank = min(imgCbank, imgPool.length - 1);
    imgAid = min(imgAid, imgPool[imgAbank].length - 1);
    imgBid = min(imgBid, imgPool[imgBbank].length - 1);
    imgCid = min(imgCid, imgPool[imgCbank].length - 1);
    

    float textureError = float(coordError) * (1.0/127.0);
    //    float textureError = 1.0/127.0;
    textureError = textureError * rands[coordError]; //* (5.0/250.0);
    //println(textureError);
    
    // imgA ----------------------------------------------
    // prepare coordinates of all 4 corners for midi input
    int imgATopLeftX, imgATopLeftY;
    if (imgATopLeft < 64) { 
      imgATopLeftX = 0;
      imgATopLeftY = height - int( (float(imgATopLeft) * 0.015625) * height ); }
    else {
      imgATopLeftX = int( ((float(imgATopLeft) - 64) * 0.015625) * width );
      imgATopLeftY = 0; }

    int imgATopRightX, imgATopRightY;
    if (imgATopRight < 64) {
      imgATopRightX = width;
      imgATopRightY = height - int( (float(imgATopRight) / 64) * height ); }
    else {
      imgATopRightX = int( width - (((float(imgATopRight) - 64) / 64) * width) );
      imgATopRightY = 0; }

    int imgABotRightX, imgABotRightY;
    if (imgABotRight < 64) {
      imgABotRightX = width;
      imgABotRightY = int( ((float(imgABotRight) / 64) * height) ); }
    else {
      imgABotRightX = int( width - (((float(imgABotRight) - 64) / 64) * width) );
      imgABotRightY = height; }

    int imgABotLeftX, imgABotLeftY;
    if (imgABotLeft < 64) {
      imgABotLeftX = 0;
      imgABotLeftY = int( (float(imgABotLeft) / 64) * height ); }
    else {
      imgABotLeftX = int( ((float(imgABotLeft) - 64) / 64) * width );
      imgABotLeftY = height; }

    // prepare coordinates of texture using speed and direction
    float imgATopLeftTexX, imgATopLeftTexY;
    float imgATopRightTexX, imgATopRightTexY;
    float imgABotLeftTexX, imgABotLeftTexY;
    float imgABotRightTexX, imgABotRightTexY;
    float imgATexSpeed = frameCount * 0.0001 * pow(texAspeed,1.5); // temp
    float imgATexDirectionRad = radians(float(texAdirection) * 2.83464566929);
    
    imgATopLeftTexX = 0 + imgATexSpeed * cos(imgATexDirectionRad + textureError); // needs to be tested!!
    imgATopLeftTexY = 0 + imgATexSpeed * sin(imgATexDirectionRad + textureError);
    imgATopRightTexX = 1 + imgATexSpeed * cos(imgATexDirectionRad);
    imgATopRightTexY = 0 + imgATexSpeed * sin(imgATexDirectionRad);
    imgABotRightTexX = 1 + imgATexSpeed * cos(imgATexDirectionRad);
    imgABotRightTexY = 1 + imgATexSpeed * sin(imgATexDirectionRad);
    imgABotLeftTexX = 0 + imgATexSpeed * cos(imgATexDirectionRad);
    imgABotLeftTexY = 1 + imgATexSpeed * sin(imgATexDirectionRad);

    // color
    tint(imgAhue, saturation, imgAbright, imgAalpha);
            
    beginShape();
    texture(imgPool[imgAbank][imgAid]);
    vertex(imgATopLeftX,  imgATopLeftY,  imgATopLeftTexX, imgATopLeftTexY);
    vertex(imgATopRightX,  imgATopRightY,  imgATopRightTexX, imgATopRightTexY);
    vertex(imgABotRightX,  imgABotRightY,  imgABotRightTexX, imgABotRightTexY);
    vertex(imgABotLeftX,  imgABotLeftY,  imgABotLeftTexX, imgABotLeftTexY);
    endShape();

    // imgB ----------------------------------------------
    // prepare coordinates of all 4 corners for midi input
    int imgBTopLeftX, imgBTopLeftY;
    if (imgBTopLeft < 64) { 
      imgBTopLeftX = 0;
      imgBTopLeftY = height - int( (float(imgBTopLeft) * 0.015625) * height ); 
        }
    else {
      imgBTopLeftX = int( ((float(imgBTopLeft) - 64) * 0.015625) * width );
      imgBTopLeftY = 0; }

    int imgBTopRightX, imgBTopRightY;
    if (imgBTopRight < 64) {
      imgBTopRightX = width;
      imgBTopRightY = height - int( (float(imgBTopRight) / 64) * height ); }
    else {
      imgBTopRightX = int( width - (((float(imgBTopRight) - 64) / 64) * width) );
      imgBTopRightY = 0; }

    int imgBBotRightX, imgBBotRightY;
    if (imgBBotRight < 64) {
      imgBBotRightX = width;
      imgBBotRightY = int( ((float(imgBBotRight) / 64) * height) ); }
    else {
      imgBBotRightX = int( width - (((float(imgBBotRight) - 64) / 64) * width) );
      imgBBotRightY = height; }

    int imgBBotLeftX, imgBBotLeftY;
    if (imgBBotLeft < 64) {
      imgBBotLeftX = 0;
      imgBBotLeftY = int( (float(imgBBotLeft) / 64) * height ); }
    else {
      imgBBotLeftX = int( ((float(imgBBotLeft) - 64) / 64) * width );
      imgBBotLeftY = height; }

    // prepare coordinates of texture using speed and direction
    float imgBTopLeftTexX, imgBTopLeftTexY;
    float imgBTopRightTexX, imgBTopRightTexY;
    float imgBBotLeftTexX, imgBBotLeftTexY;
    float imgBBotRightTexX, imgBBotRightTexY;
    float imgBTexSpeed = frameCount * 0.0001 * pow(texBspeed,1.5); // temp
    float imgBTexDirectionRad = radians(float(texBdirection) * 2.83464566929);
    
    imgBTopLeftTexX = 0 + imgBTexSpeed * cos(imgBTexDirectionRad);
    imgBTopLeftTexY = 0 + imgBTexSpeed * sin(imgBTexDirectionRad);
    imgBTopRightTexX = 1 + imgBTexSpeed * cos(imgBTexDirectionRad);
    imgBTopRightTexY = 0 + imgBTexSpeed * sin(imgBTexDirectionRad);
    imgBBotRightTexX = 1 + imgBTexSpeed * cos(imgBTexDirectionRad);
    imgBBotRightTexY = 1 + imgBTexSpeed * sin(imgBTexDirectionRad);
    imgBBotLeftTexX = 0 + imgBTexSpeed * cos(imgBTexDirectionRad);
    imgBBotLeftTexY = 1 + imgBTexSpeed * sin(imgBTexDirectionRad);

    // color
    tint(imgBhue, saturation, imgBbright, imgBalpha);
    switchBlendMode(imgBblendMode);
            
    beginShape();
    texture(imgPool[imgBbank][imgBid]);
    vertex(imgBTopLeftX,  imgBTopLeftY,  imgBTopLeftTexX, imgBTopLeftTexY);
    vertex(imgBTopRightX,  imgBTopRightY,  imgBTopRightTexX, imgBTopRightTexY);
    vertex(imgBBotRightX,  imgBBotRightY,  imgBBotRightTexX, imgBBotRightTexY);
    vertex(imgBBotLeftX,  imgBBotLeftY,  imgBBotLeftTexX, imgBBotLeftTexY);
    endShape();


    // imgC ----------------------------------------------
    // prepare coordinates of all 4 corners for midi input
    int imgCTopLeftX, imgCTopLeftY;
    if (imgCTopLeft < 64) { 
      imgCTopLeftX = 0;
      imgCTopLeftY = height - int( (float(imgCTopLeft) * 0.015625) * height ); 
        }
    else {
      imgCTopLeftX = int( ((float(imgCTopLeft) - 64) * 0.015625) * width );
      imgCTopLeftY = 0; }

    int imgCTopRightX, imgCTopRightY;
    if (imgCTopRight < 64) {
      imgCTopRightX = width;
      imgCTopRightY = height - int( (float(imgCTopRight) / 64) * height ); }
    else {
      imgCTopRightX = int( width - (((float(imgCTopRight) - 64) / 64) * width) );
      imgCTopRightY = 0; }

    int imgCBotRightX, imgCBotRightY;
    if (imgCBotRight < 64) {
      imgCBotRightX = width;
      imgCBotRightY = int( ((float(imgCBotRight) / 64) * height) ); }
    else {
      imgCBotRightX = int( width - (((float(imgCBotRight) - 64) / 64) * width) );
      imgCBotRightY = height; }

    int imgCBotLeftX, imgCBotLeftY;
    if (imgCBotLeft < 64) {
      imgCBotLeftX = 0;
      imgCBotLeftY = int( (float(imgCBotLeft) / 64) * height ); }
    else {
      imgCBotLeftX = int( ((float(imgCBotLeft) - 64) / 64) * width );
      imgCBotLeftY = height; }

    // prepare coordinates of texture using speed and direction
    float imgCTopLeftTexX, imgCTopLeftTexY;
    float imgCTopRightTexX, imgCTopRightTexY;
    float imgCBotLeftTexX, imgCBotLeftTexY;
    float imgCBotRightTexX, imgCBotRightTexY;
    float imgCTexSpeed = frameCount * 0.0001 * pow(texCspeed,1.5); // temp
    float imgCTexDirectionRad = radians(float(texCdirection) * 2.83464566929);
    
    imgCTopLeftTexX = 0 + imgCTexSpeed * cos(imgCTexDirectionRad);
    imgCTopLeftTexY = 0 + imgCTexSpeed * sin(imgCTexDirectionRad);
    imgCTopRightTexX = 1 + imgCTexSpeed * cos(imgCTexDirectionRad);
    imgCTopRightTexY = 0 + imgCTexSpeed * sin(imgCTexDirectionRad);
    imgCBotRightTexX = 1 + imgCTexSpeed * cos(imgCTexDirectionRad);
    imgCBotRightTexY = 1 + imgCTexSpeed * sin(imgCTexDirectionRad);
    imgCBotLeftTexX = 0 + imgCTexSpeed * cos(imgCTexDirectionRad);
    imgCBotLeftTexY = 1 + imgCTexSpeed * sin(imgCTexDirectionRad);

    // color
    tint(imgChue, saturation, imgCbright, imgCalpha);
    switchBlendMode(imgCblendMode);
    
    beginShape();
    texture(imgPool[imgCbank][imgCid]);
    vertex(imgCTopLeftX,  imgCTopLeftY,  imgCTopLeftTexX, imgCTopLeftTexY);
    vertex(imgCTopRightX,  imgCTopRightY,  imgCTopRightTexX, imgCTopRightTexY);
    vertex(imgCBotRightX,  imgCBotRightY,  imgCBotRightTexX, imgCBotRightTexY);
    vertex(imgCBotLeftX,  imgCBotLeftY,  imgCBotLeftTexX, imgCBotLeftTexY);
    endShape();

    blendMode(BLEND);
    
  }
}
