//
//  3DboxGL.cpp
//  tweakathlon
//
//  Created by Robert Tubb on 17/04/2014.
//
//

#include "3DboxGL.h"


Leap3DBoxGL::Leap3DBoxGL(float ax,
                         float ay,
                         float awidth,
                         float aheight,
                         float azx,
                         float azy,
                         const UIProps& props) :
Leap3DBox(ax,ay,awidth, aheight, azx, azy, props)
{
    indicatorColor = ofColor(123, 123, 220);
    // how much to rotate the box
    angleX = -25; // elevation
    angleY = 0; // yaw
    
    depth = width; // its a cube
    
    // where is the camera
    camTrans = 250;
    
    // cube is centred on 0,0,0
    float I = width/2;
    float O = -width/2;
    
    // left face x=O
    // back face z=O
    // top face y=O
    
    // positions of vertices
    ofPoint ltf = ofPoint( O,O,I );
    ofPoint lbf = ofPoint( O,I,I );
    ofPoint rtf = ofPoint( I,O,I );
    ofPoint rbf = ofPoint( I,I,I );
    
    ofPoint ltr = ofPoint( O,O,O );
    ofPoint lbr = ofPoint( O,I,O );
    ofPoint rtr = ofPoint( I,O,O );
    ofPoint rbr = ofPoint( I,I,O );
    
    // now build faces
    // rear
    makeTexFace(&boxMesh,  rtr, ltr, lbr, rbr);
    
    
//    boxMesh.addVertex(ltr);
//    boxMesh.addVertex(rtr);
//    boxMesh.addVertex(rbr);
//    
//    boxMesh.addVertex(ltr);
//    boxMesh.addVertex(rbr);
//    boxMesh.addVertex(lbr);
    
    // left
    makeTexFace(&boxMesh,   ltr, ltf, lbf,lbr);
    
//    boxMesh.addVertex(ltf);
//    boxMesh.addVertex(ltr);
//    boxMesh.addVertex(lbr);
//    
//    boxMesh.addVertex(ltf);
//    boxMesh.addVertex(lbr);
//    boxMesh.addVertex(lbf);
    
    // bottom
    makeTexFace(&boxMesh,   lbr, lbf, rbf,rbr);
    
//    boxMesh.addVertex(lbf);
//    boxMesh.addVertex(lbr);
//    boxMesh.addVertex(rbr);
//    
//    boxMesh.addVertex(lbf);
//    boxMesh.addVertex(rbr);
//    boxMesh.addVertex(rbf);

    
    boxMesh.setupIndicesAuto();
    setNormals(boxMesh);
}


//--------------------------------------------------------------

void setNormals( ofMesh &mesh )

{
    //The number of the vertices
    int nV = mesh.getNumVertices();
    
    //The number of the triangles
    int nT = mesh.getNumIndices() / 3;
    
    vector<ofPoint> norm( nV );
    
    //Array for the normals
    //Scan all the triangles. For each triangle add its
    //normal to norm's vectors of triangle's vertices
    
    for (int t=0; t<nT; t++) {
        //Get indices of the triangle t
        int i1 = mesh.getIndex( 3 * t );
        int i2 = mesh.getIndex( 3 * t + 1 );
        int i3 = mesh.getIndex( 3 * t + 2 );
        
        //Get vertices of the triangle
        const ofPoint &v1 = mesh.getVertex( i1 );
        const ofPoint &v2 = mesh.getVertex( i2 );
        const ofPoint &v3 = mesh.getVertex( i3 );
        
        //Compute the triangle's normal
        ofPoint dir = ( (v2 - v1).crossed( v3 - v1 ) ).normalized();
        
        //Accumulate it to norm array for i1, i2, i3
        norm[ i1 ] += dir;
        norm[ i2 ] += dir;
        norm[ i3 ] += dir;
    }
    //Normalize the normal's length
    for (int i=0; i<nV; i++)
    {
        norm[i].normalize();
        
    }
    //Set the normals to mesh
    mesh.clearNormals();
    mesh.addNormals( norm );
}

// algs

