view UI code/3DboxGL.mm @ 28:953db6518738

leap version more or less there, needs btter results feedback but thats detail. "no movement" bit is stupid cos peopel can move their hand. light flash not work.
author Robert Tubb <rt300@eecs.qmul.ac.uk>
date Thu, 30 Oct 2014 18:35:00 +0000
parents 8d7ae43b2edd
children a677c027e3a0
line wrap: on
line source
//
//  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;
    angleY = -24;
    
    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
    boxMesh.addVertex(ltr);
    boxMesh.addVertex(rtr);
    boxMesh.addVertex(rbr);
    
    boxMesh.addVertex(ltr);
    boxMesh.addVertex(rbr);
    boxMesh.addVertex(lbr);
    
    // left
    boxMesh.addVertex(ltf);
    boxMesh.addVertex(ltr);
    boxMesh.addVertex(lbr);
    
    boxMesh.addVertex(ltf);
    boxMesh.addVertex(lbr);
    boxMesh.addVertex(lbf);
    
    // bottom
    boxMesh.addVertex(lbf);
    boxMesh.addVertex(lbr);
    boxMesh.addVertex(rbr);
    
    boxMesh.addVertex(lbf);
    boxMesh.addVertex(rbr);
    boxMesh.addVertex(rbf);
    
    // top
//    boxMesh.addVertex(ltr);
//    boxMesh.addVertex(rtr);
//    boxMesh.addVertex(rtf);
//    
//    boxMesh.addVertex(ltr);
//    boxMesh.addVertex(rtf);
//    boxMesh.addVertex(ltf);
//    
//    // right
//    boxMesh.addVertex(rtf);
//    boxMesh.addVertex(rtr);
//    boxMesh.addVertex(rbr);
//    
//    boxMesh.addVertex(rtf);
//    boxMesh.addVertex(rbr);
//    boxMesh.addVertex(rbf);
    
    // front
//    boxMesh.addVertex(ltr);
//    boxMesh.addVertex(rtr);
//    boxMesh.addVertex(rbr);
//    
//    boxMesh.addVertex(ltr);
//    boxMesh.addVertex(rbr);
//    boxMesh.addVertex(lbr);
    
    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