//
//  6Dbox.mm
//  riftathon
//
//  Created by Robert Tubb on 27/10/2014.
//
//

#include "6Dbox.h"
Leap6DBox::Leap6DBox(float ax,
                         float ay,
                         float awidth,
                         float aheight,
                         float azx,
                         float azy,
                         const UIProps& props) :
Leap3DBoxGL(ax,ay,awidth, aheight, azx, azy, props)
{

    roll = 0.0;
    pitch = 0.0;
    yaw = 0.0;
    
    // set up hand mesh
    // width 0.5
    // length 1
    // height 0.25
    hw = 0.5;
    hh = 0.25;
    hl = 1.0;
    scale = 0.1;
    
    string fname = ofFilePath::getAbsolutePath(ofToDataPath("perf.jpeg"));
    ofFile f = ofFile(fname);
    defaultImage.loadImage(fname);
    setTexture(&defaultImage);
    setUpHandMesh();
    
}

void Leap6DBox::setUpHandMesh(){
    ofPoint rtr = ofPoint(hw, hh, hl);
    ofPoint ltr = ofPoint(-hw, hh, hl);
    ofPoint rbr = ofPoint(hw, -hh, hl);
    ofPoint rtf = ofPoint(hw, hh, -hl);
    ofPoint lbr = ofPoint(-hw, -hh, hl);
    ofPoint rbf = ofPoint(hw, -hh, -hl);
    ofPoint lbf = ofPoint(-hw, -hh, -hl);
    ofPoint ltf = ofPoint(-hw, hh, -hl);
 
    int imageWidth = 259;
    int imageHeight = 254;
    
    ofPoint texCoordLT = ofPoint(0, 0);
    ofPoint texCoordLB = ofPoint(0, imageHeight);
    ofPoint texCoordRT = ofPoint(imageWidth, 0);
    ofPoint texCoordRB = ofPoint(imageWidth, imageHeight);
    
    
    // now build faces
    // rear
    // now build faces
    // rear
    handMesh.addVertex(ltr);
    handMesh.addTexCoord(texCoordLT);
    
    handMesh.addVertex(rtr);
    handMesh.addTexCoord(texCoordRT);
    handMesh.addVertex(rbr);
    handMesh.addTexCoord(texCoordRB);
    
    handMesh.addVertex(ltr);
    handMesh.addTexCoord(texCoordLT);
    handMesh.addVertex(rbr);
    handMesh.addTexCoord(texCoordRB);
    handMesh.addVertex(lbr);
    handMesh.addTexCoord(texCoordLB);
    
    // left
    handMesh.addVertex(ltf);
    handMesh.addVertex(ltr);
    handMesh.addVertex(lbr);
    
    handMesh.addVertex(ltf);
    handMesh.addVertex(lbr);
    handMesh.addVertex(lbf);
    
    // bottom
    handMesh.addVertex(lbf);
    handMesh.addVertex(lbr);
    handMesh.addVertex(rbr);
    
    handMesh.addVertex(lbf);
    handMesh.addVertex(rbr);
    handMesh.addVertex(rbf);

    // top
    handMesh.addVertex(ltf);
    handMesh.addVertex(ltr);
    handMesh.addVertex(rtf);
    
    handMesh.addVertex(ltr);
    handMesh.addVertex(rtr);
    handMesh.addVertex(rtf);
    
    // right
    handMesh.addVertex(rbf);
    handMesh.addVertex(rtf);
    handMesh.addVertex(rbr);
    
    handMesh.addVertex(rtf);
    handMesh.addVertex(rtr);
    handMesh.addVertex(rbr);
 
    // front
    handMesh.addVertex(ltf);
    handMesh.addTexCoord(texCoordLT);
    
    handMesh.addVertex(rtf);
    handMesh.addTexCoord(texCoordRT);
    handMesh.addVertex(rbf);
    handMesh.addTexCoord(texCoordRB);
    
    handMesh.addVertex(ltf);
    handMesh.addTexCoord(texCoordLT);
    handMesh.addVertex(rbf);
    handMesh.addTexCoord(texCoordRB);
    handMesh.addVertex(lbf);
    handMesh.addTexCoord(texCoordLB);
    // TODO now make triange thumb
    
    
    handMesh.setupIndicesAuto();
}

void Leap6DBox::setTexture(ofImage* img){
    texImg = img;
    
    
}

void Leap6DBox::drawIndicatorBlob(float x, float y, float z, ofColor c){
    draw6DOFIndicatorBlob(x, y, z, c, roll, pitch, yaw);
}

void Leap6DBox::draw6DOFIndicatorBlob(float x, float y, float z, ofColor c, float r, float p, float yaw){
    ofVec3f up = ofVec3f(0.0, 1.0, 0.0);
    ofVec3f fwd = ofVec3f(0.0, 0.0, 1.0);
    ofVec3f right = ofVec3f(1.0, 0.0, 0.0);
    
    static ofMatrix4x4 m;
    static ofMatrix4x4 rot;
	m.makeScaleMatrix(scale,scale,scale);
    rot.makeRotationMatrix(roll, fwd, pitch, right, yaw, up);
	m.translate(x,y,z);
    
    ofPushMatrix();
    ofMultMatrix(m);
    ofMultMatrix(rot);
    // render
    (*texImg).bind();
    boxMesh.draw();
    (*texImg).unbind();
    ofPopMatrix();
    
}
