Mercurial > hg > tweakathon2ios
diff UI code/3DboxGL.mm @ 0:a223551fdc1f
First commit - copy from tweakathlon.
author | Robert Tubb <rt300@eecs.qmul.ac.uk> |
---|---|
date | Fri, 10 Oct 2014 11:46:42 +0100 |
parents | |
children | 8eb530e0601b |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/UI code/3DboxGL.mm Fri Oct 10 11:46:42 2014 +0100 @@ -0,0 +1,157 @@ +// +// 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 = 0; + + // 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(); +} + + +//-------------------------------------------------------------- + +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 +