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@31
|
23 angleX = -25; // elevation
|
rt300@32
|
24 angleY = 0; // yaw
|
rt300@0
|
25
|
rt300@0
|
26 depth = width; // its a cube
|
rt300@0
|
27
|
rt300@0
|
28 // where is the camera
|
rt300@26
|
29 camTrans = 250;
|
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@32
|
52 makeTexFace(&boxMesh, rtr, ltr, lbr, rbr);
|
rt300@0
|
53
|
rt300@0
|
54
|
rt300@0
|
55 // boxMesh.addVertex(ltr);
|
rt300@0
|
56 // boxMesh.addVertex(rtr);
|
rt300@0
|
57 // boxMesh.addVertex(rbr);
|
rt300@0
|
58 //
|
rt300@0
|
59 // boxMesh.addVertex(ltr);
|
rt300@0
|
60 // boxMesh.addVertex(rbr);
|
rt300@0
|
61 // boxMesh.addVertex(lbr);
|
rt300@0
|
62
|
rt300@32
|
63 // left
|
rt300@32
|
64 makeTexFace(&boxMesh, ltr, ltf, lbf,lbr);
|
rt300@32
|
65
|
rt300@32
|
66 // boxMesh.addVertex(ltf);
|
rt300@32
|
67 // boxMesh.addVertex(ltr);
|
rt300@32
|
68 // boxMesh.addVertex(lbr);
|
rt300@32
|
69 //
|
rt300@32
|
70 // boxMesh.addVertex(ltf);
|
rt300@32
|
71 // boxMesh.addVertex(lbr);
|
rt300@32
|
72 // boxMesh.addVertex(lbf);
|
rt300@32
|
73
|
rt300@32
|
74 // bottom
|
rt300@32
|
75 makeTexFace(&boxMesh, lbr, lbf, rbf,rbr);
|
rt300@32
|
76
|
rt300@32
|
77 // boxMesh.addVertex(lbf);
|
rt300@32
|
78 // boxMesh.addVertex(lbr);
|
rt300@32
|
79 // boxMesh.addVertex(rbr);
|
rt300@32
|
80 //
|
rt300@32
|
81 // boxMesh.addVertex(lbf);
|
rt300@32
|
82 // boxMesh.addVertex(rbr);
|
rt300@32
|
83 // boxMesh.addVertex(rbf);
|
rt300@32
|
84
|
rt300@32
|
85
|
rt300@0
|
86 boxMesh.setupIndicesAuto();
|
rt300@25
|
87 setNormals(boxMesh);
|
rt300@0
|
88 }
|
rt300@0
|
89
|
rt300@0
|
90
|
rt300@0
|
91 //--------------------------------------------------------------
|
rt300@0
|
92
|
rt300@0
|
93 void setNormals( ofMesh &mesh )
|
rt300@0
|
94
|
rt300@0
|
95 {
|
rt300@0
|
96 //The number of the vertices
|
rt300@0
|
97 int nV = mesh.getNumVertices();
|
rt300@0
|
98
|
rt300@0
|
99 //The number of the triangles
|
rt300@0
|
100 int nT = mesh.getNumIndices() / 3;
|
rt300@0
|
101
|
rt300@0
|
102 vector<ofPoint> norm( nV );
|
rt300@0
|
103
|
rt300@0
|
104 //Array for the normals
|
rt300@0
|
105 //Scan all the triangles. For each triangle add its
|
rt300@0
|
106 //normal to norm's vectors of triangle's vertices
|
rt300@0
|
107
|
rt300@0
|
108 for (int t=0; t<nT; t++) {
|
rt300@0
|
109 //Get indices of the triangle t
|
rt300@0
|
110 int i1 = mesh.getIndex( 3 * t );
|
rt300@0
|
111 int i2 = mesh.getIndex( 3 * t + 1 );
|
rt300@0
|
112 int i3 = mesh.getIndex( 3 * t + 2 );
|
rt300@0
|
113
|
rt300@0
|
114 //Get vertices of the triangle
|
rt300@0
|
115 const ofPoint &v1 = mesh.getVertex( i1 );
|
rt300@0
|
116 const ofPoint &v2 = mesh.getVertex( i2 );
|
rt300@0
|
117 const ofPoint &v3 = mesh.getVertex( i3 );
|
rt300@0
|
118
|
rt300@0
|
119 //Compute the triangle's normal
|
rt300@0
|
120 ofPoint dir = ( (v2 - v1).crossed( v3 - v1 ) ).normalized();
|
rt300@0
|
121
|
rt300@0
|
122 //Accumulate it to norm array for i1, i2, i3
|
rt300@0
|
123 norm[ i1 ] += dir;
|
rt300@0
|
124 norm[ i2 ] += dir;
|
rt300@0
|
125 norm[ i3 ] += dir;
|
rt300@0
|
126 }
|
rt300@0
|
127 //Normalize the normal's length
|
rt300@0
|
128 for (int i=0; i<nV; i++)
|
rt300@0
|
129 {
|
rt300@0
|
130 norm[i].normalize();
|
rt300@0
|
131
|
rt300@0
|
132 }
|
rt300@0
|
133 //Set the normals to mesh
|
rt300@0
|
134 mesh.clearNormals();
|
rt300@0
|
135 mesh.addNormals( norm );
|
rt300@0
|
136 }
|
rt300@0
|
137
|
rt300@0
|
138 // algs
|
rt300@0
|
139
|