rt300@0
|
1 //
|
rt300@0
|
2 // 3DboxGL.cpp
|
rt300@0
|
3 // tweakathlon
|
rt300@0
|
4 //
|
rt300@0
|
5 // Created by Robert Tubb on 17/04/2014.
|
rt300@0
|
6 //
|
rt300@0
|
7 //
|
rt300@0
|
8
|
rt300@0
|
9 #include "3DboxGL.h"
|
rt300@0
|
10
|
rt300@0
|
11
|
rt300@0
|
12 Leap3DBoxGL::Leap3DBoxGL(float ax,
|
rt300@0
|
13 float ay,
|
rt300@0
|
14 float awidth,
|
rt300@0
|
15 float aheight,
|
rt300@0
|
16 float azx,
|
rt300@0
|
17 float azy,
|
rt300@0
|
18 const UIProps& props) :
|
rt300@0
|
19 Leap3DBox(ax,ay,awidth, aheight, azx, azy, props)
|
rt300@0
|
20 {
|
rt300@0
|
21 indicatorColor = ofColor(123, 123, 220);
|
rt300@0
|
22 // how much to rotate the box
|
rt300@0
|
23 angleX = -25;
|
rt300@0
|
24 angleY = -24;
|
rt300@0
|
25
|
rt300@0
|
26 depth = width; // its a cube
|
rt300@0
|
27
|
rt300@0
|
28 // where is the camera
|
rt300@0
|
29 camTrans = 0;
|
rt300@0
|
30
|
rt300@0
|
31 // cube is centred on 0,0,0
|
rt300@0
|
32 float I = width/2;
|
rt300@0
|
33 float O = -width/2;
|
rt300@0
|
34
|
rt300@0
|
35 // left face x=O
|
rt300@0
|
36 // back face z=O
|
rt300@0
|
37 // top face y=O
|
rt300@0
|
38
|
rt300@0
|
39 // positions of vertices
|
rt300@0
|
40 ofPoint ltf = ofPoint( O,O,I );
|
rt300@0
|
41 ofPoint lbf = ofPoint( O,I,I );
|
rt300@0
|
42 ofPoint rtf = ofPoint( I,O,I );
|
rt300@0
|
43 ofPoint rbf = ofPoint( I,I,I );
|
rt300@0
|
44
|
rt300@0
|
45 ofPoint ltr = ofPoint( O,O,O );
|
rt300@0
|
46 ofPoint lbr = ofPoint( O,I,O );
|
rt300@0
|
47 ofPoint rtr = ofPoint( I,O,O );
|
rt300@0
|
48 ofPoint rbr = ofPoint( I,I,O );
|
rt300@0
|
49
|
rt300@0
|
50 // now build faces
|
rt300@0
|
51 // rear
|
rt300@0
|
52 boxMesh.addVertex(ltr);
|
rt300@0
|
53 boxMesh.addVertex(rtr);
|
rt300@0
|
54 boxMesh.addVertex(rbr);
|
rt300@0
|
55
|
rt300@0
|
56 boxMesh.addVertex(ltr);
|
rt300@0
|
57 boxMesh.addVertex(rbr);
|
rt300@0
|
58 boxMesh.addVertex(lbr);
|
rt300@0
|
59
|
rt300@0
|
60 // left
|
rt300@0
|
61 boxMesh.addVertex(ltf);
|
rt300@0
|
62 boxMesh.addVertex(ltr);
|
rt300@0
|
63 boxMesh.addVertex(lbr);
|
rt300@0
|
64
|
rt300@0
|
65 boxMesh.addVertex(ltf);
|
rt300@0
|
66 boxMesh.addVertex(lbr);
|
rt300@0
|
67 boxMesh.addVertex(lbf);
|
rt300@0
|
68
|
rt300@0
|
69 // bottom
|
rt300@0
|
70 boxMesh.addVertex(lbf);
|
rt300@0
|
71 boxMesh.addVertex(lbr);
|
rt300@0
|
72 boxMesh.addVertex(rbr);
|
rt300@0
|
73
|
rt300@0
|
74 boxMesh.addVertex(lbf);
|
rt300@0
|
75 boxMesh.addVertex(rbr);
|
rt300@0
|
76 boxMesh.addVertex(rbf);
|
rt300@0
|
77
|
rt300@0
|
78 // top
|
rt300@0
|
79 // boxMesh.addVertex(ltr);
|
rt300@0
|
80 // boxMesh.addVertex(rtr);
|
rt300@0
|
81 // boxMesh.addVertex(rtf);
|
rt300@0
|
82 //
|
rt300@0
|
83 // boxMesh.addVertex(ltr);
|
rt300@0
|
84 // boxMesh.addVertex(rtf);
|
rt300@0
|
85 // boxMesh.addVertex(ltf);
|
rt300@0
|
86 //
|
rt300@0
|
87 // // right
|
rt300@0
|
88 // boxMesh.addVertex(rtf);
|
rt300@0
|
89 // boxMesh.addVertex(rtr);
|
rt300@0
|
90 // boxMesh.addVertex(rbr);
|
rt300@0
|
91 //
|
rt300@0
|
92 // boxMesh.addVertex(rtf);
|
rt300@0
|
93 // boxMesh.addVertex(rbr);
|
rt300@0
|
94 // boxMesh.addVertex(rbf);
|
rt300@0
|
95
|
rt300@0
|
96 // front
|
rt300@0
|
97 // boxMesh.addVertex(ltr);
|
rt300@0
|
98 // boxMesh.addVertex(rtr);
|
rt300@0
|
99 // boxMesh.addVertex(rbr);
|
rt300@0
|
100 //
|
rt300@0
|
101 // boxMesh.addVertex(ltr);
|
rt300@0
|
102 // boxMesh.addVertex(rbr);
|
rt300@0
|
103 // boxMesh.addVertex(lbr);
|
rt300@0
|
104
|
rt300@0
|
105 boxMesh.setupIndicesAuto();
|
rt300@0
|
106 }
|
rt300@0
|
107
|
rt300@0
|
108
|
rt300@0
|
109 //--------------------------------------------------------------
|
rt300@0
|
110
|
rt300@0
|
111 void setNormals( ofMesh &mesh )
|
rt300@0
|
112
|
rt300@0
|
113 {
|
rt300@0
|
114 //The number of the vertices
|
rt300@0
|
115 int nV = mesh.getNumVertices();
|
rt300@0
|
116
|
rt300@0
|
117 //The number of the triangles
|
rt300@0
|
118 int nT = mesh.getNumIndices() / 3;
|
rt300@0
|
119
|
rt300@0
|
120 vector<ofPoint> norm( nV );
|
rt300@0
|
121
|
rt300@0
|
122 //Array for the normals
|
rt300@0
|
123 //Scan all the triangles. For each triangle add its
|
rt300@0
|
124 //normal to norm's vectors of triangle's vertices
|
rt300@0
|
125
|
rt300@0
|
126 for (int t=0; t<nT; t++) {
|
rt300@0
|
127 //Get indices of the triangle t
|
rt300@0
|
128 int i1 = mesh.getIndex( 3 * t );
|
rt300@0
|
129 int i2 = mesh.getIndex( 3 * t + 1 );
|
rt300@0
|
130 int i3 = mesh.getIndex( 3 * t + 2 );
|
rt300@0
|
131
|
rt300@0
|
132 //Get vertices of the triangle
|
rt300@0
|
133 const ofPoint &v1 = mesh.getVertex( i1 );
|
rt300@0
|
134 const ofPoint &v2 = mesh.getVertex( i2 );
|
rt300@0
|
135 const ofPoint &v3 = mesh.getVertex( i3 );
|
rt300@0
|
136
|
rt300@0
|
137 //Compute the triangle's normal
|
rt300@0
|
138 ofPoint dir = ( (v2 - v1).crossed( v3 - v1 ) ).normalized();
|
rt300@0
|
139
|
rt300@0
|
140 //Accumulate it to norm array for i1, i2, i3
|
rt300@0
|
141 norm[ i1 ] += dir;
|
rt300@0
|
142 norm[ i2 ] += dir;
|
rt300@0
|
143 norm[ i3 ] += dir;
|
rt300@0
|
144 }
|
rt300@0
|
145 //Normalize the normal's length
|
rt300@0
|
146 for (int i=0; i<nV; i++)
|
rt300@0
|
147 {
|
rt300@0
|
148 norm[i].normalize();
|
rt300@0
|
149
|
rt300@0
|
150 }
|
rt300@0
|
151 //Set the normals to mesh
|
rt300@0
|
152 mesh.clearNormals();
|
rt300@0
|
153 mesh.addNormals( norm );
|
rt300@0
|
154 }
|
rt300@0
|
155
|
rt300@0
|
156 // algs
|
rt300@0
|
157
|