//
//  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)
{

    rollVal = 0.0;
    pitchVal = 0.0;
    yawVal = 0.0;

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

void Leap6DBox::setValue(int index, int value){
    if (index == 0){
        
    }
}

void Leap6DBox::setValueAndScale(int which, int val){
    
    
    if(which == 0)      xVal =  (val - minVal)/(maxVal - minVal);
    if(which == 1)      yVal =  (val - minVal)/(maxVal - minVal);
    if(which == 2)      zVal =  (val - minVal)/(maxVal - minVal);
    // angles
    
    if(which == 3)      rollVal =  57.3*  (2.0*(val - minVal)/(maxVal - minVal) - 1.0);
    if(which == 4)      pitchVal = 57.3*  (2.0*(val - minVal)/(maxVal - minVal) - 1.0);
    if(which == 5)      yawVal =   57.3*  (2.0*(val - minVal)/(maxVal - minVal) - 1.0);
}

void Leap6DBox::makeTexFace(ofPoint LT, ofPoint RT, ofPoint  RB, ofPoint LB){
    
    ofPoint texCoordLT = ofPoint(0, 0.5);
    ofPoint texCoordLB = ofPoint(0, 0);
    ofPoint texCoordRT = ofPoint(0.5, 0.5);
    ofPoint texCoordRB = ofPoint(0.5, 0);
    
    handMesh.addVertex(LT);
    handMesh.addTexCoord(texCoordLT);
    handMesh.addVertex(LB);
    handMesh.addTexCoord(texCoordLB);
    handMesh.addVertex(RB);
    handMesh.addTexCoord(texCoordRB);

    
    
    handMesh.addVertex(LT);
    handMesh.addTexCoord(texCoordLT);

    handMesh.addVertex(RB);
    handMesh.addTexCoord(texCoordRB);
    handMesh.addVertex(RT);
    handMesh.addTexCoord(texCoordRT);
}
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);q
    ofPoint rbf = ofPoint(hw, -hh, hl);
    ofPoint lbf = ofPoint(-hw, -hh, hl);
    ofPoint ltf = ofPoint(-hw, hh, hl);
 
    int imageWidth = 259;
    int imageHeight = 254;

    // back
    makeTexFace(ltr, rtr, rbr, lbr);
    
    // left
    makeTexFace(ltr,ltf, lbf, lbr);
    
    // front
    makeTexFace(ltf, rtf, rbf, lbf);
    
    // right
    makeTexFace(rtf,rtr, rbr, rbf);
    
    //top
    makeTexFace(ltr, rtr, rtf, ltf);
    
    // bottom
    makeTexFace(lbf, rbf, rbr, lbr);
    
    setUpThumb(ltf, lbf);
  
    handMesh.setupIndicesAuto();
    setNormals(handMesh);
}
void Leap6DBox::setUpThumb(ofPoint baseTR, ofPoint baseBR){
    // 3 triangles

    ofPoint baseTL = baseTR;
    ofPoint baseBL = baseBR;
    baseTL.z -= 0.5*hl;
    baseBL.z -= 0.5*hl;
    ofPoint tip = baseBL;
    tip.x -= hw;
    tip.y += hh;
    
    
    ofPoint texCoordLT = ofPoint(0, 0.5);
    ofPoint texCoordLB = ofPoint(0, 0);
    ofPoint texCoordRT = ofPoint(0.5, 0.5);
    ofPoint texCoordRB = ofPoint(0.5, 0);
    
    // front
    handMesh.addVertex(baseTR);
    handMesh.addTexCoord(texCoordLT);
    handMesh.addVertex(tip);
    handMesh.addTexCoord(texCoordRB);
    handMesh.addVertex(baseBR);
    handMesh.addTexCoord(texCoordLB);

    
    // top

    handMesh.addVertex(baseTL);
    handMesh.addTexCoord(texCoordLB);
    handMesh.addVertex(tip);
    handMesh.addTexCoord(texCoordRB);
    handMesh.addVertex(baseTR);
    handMesh.addTexCoord(texCoordLT);

    // back
    handMesh.addVertex(baseTL);
    handMesh.addTexCoord(texCoordLT);
    handMesh.addVertex(baseBL);
    handMesh.addTexCoord(texCoordLB);
    handMesh.addVertex(tip);
    handMesh.addTexCoord(texCoordRB);


    // left
    handMesh.addVertex(baseBL);
    handMesh.addTexCoord(texCoordLT);
    handMesh.addVertex(baseBR);
    handMesh.addTexCoord(texCoordLB);
    handMesh.addVertex(tip);
    handMesh.addTexCoord(texCoordRB);


    

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

void Leap6DBox::drawIndicatorBlob(float x, float y, float z, ofColor c){
    draw6DOFIndicatorBlob(x, y, z, c, rollVal, pitchVal, yawVal);
}

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(rollVal, fwd, pitchVal, right, yawVal, up);
	m.translate(x,y,z);
    
    ofPushMatrix();
    ofMultMatrix(m);
    ofMultMatrix(rot);
    
    ofSetColor(255,255,255);
    // render
    (*texImg).bind();
    
    handMesh.draw();
    (*texImg).unbind();
    ofPopMatrix();
    
}
