- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNode.java
More file actions
Latest commit
367 lines (301 loc) · 9.11 KB
/
Copy pathNode.java
File metadata and controls
367 lines (301 loc) · 9.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
importjava.util.ArrayList;
importjavax.media.opengl.GL;
/**
* Basic node for a JOGL 3D Application
* @author Andrew
*/
publicclassNode
{
/** Name of this node */
privateStringnodeName;
/** Parent node (if null then this is rootNode - or not attached to scene */
protectedNodeparentNode;
/** A list of all the children nodes below this node */
privateArrayList<Node> childrenNodes;
/* Transformations */
privatefinalfloat[] scale;
privatefinalfloat[] translation;
privatefinalfloat[] rotation;
/** Global Transformation Matrix */
privatefinalfloat[] m;
/** Temp matrix to use to calculate transformation m */
privatefinalfloat[] temp;
/** True when transformation matrix is needed to be recalculated */
privatebooleanrecalculateTransformMatrix;
/**
* Creates a new node
* @param nodeName the name of this node
*/
publicNode(StringnodeName)
{
this.nodeName = nodeName;
parentNode = null;
childrenNodes = newArrayList<Node>();
scale = newfloat[3];
translation = newfloat[3];
rotation = newfloat[4];
resetLocalTransformations();
m = newfloat[16];
temp = newfloat[16];
recalculateTransformMatrix = true;
}
/**
* Sets all translations back to original
*/
protectedvoidresetLocalTransformations()
{
scale[0] = 1.0f; scale[1] = 1.0f; scale[2] = 1.0f;
translation[0] = 0.0f; translation[1] = 0.0f; translation[2] = 0.0f;
rotation[0] = 0.0f; rotation[1] = 0.0f; rotation[2] = 1.0f; rotation[3] = 0.0f;
setRecalculateTransformMatrix();
}
/**
* Sets the transformation matrix to be recalculated in this
* node and children nodes
*/
protectedvoidsetRecalculateTransformMatrix()
{
this.recalculateTransformMatrix = true;
for(Nodec : childrenNodes)
c.setRecalculateTransformMatrix();
}
/**
* Adds a child to this node
* (Removes it from any other node it was attached to
* @param child the child to attach
* @return true if successfully added - false if not.
*/
publicbooleanaddChild(Nodechild)
{
booleansuccess = false;
//if it has a parent already - remove it from the parent
if(child.parentNode != null)
{
child.parentNode.removeChild(child);
}
//now add it to this child
if(childrenNodes.add(child))
{
child.parentNode = this;
//recalculate because changed parent
child.setRecalculateTransformMatrix();
success = true;
}
returnsuccess;
}
/**
* Removes a child from this node
* @param child the child to remove from this node
* @return true if successfully removed - false if not.
*/
publicbooleanremoveChild(Nodechild)
{
returnchildrenNodes.remove(child);
}
/**
* Sets the scale of this node
* @param x x scale
* @param y y scale
* @param z z scale
*/
publicvoidsetScale(floatx, floaty, floatz)
{
scale[0] = x;
scale[1] = y;
scale[2] = z;
setRecalculateTransformMatrix();
}
/**
* Sets the scale of all 3 vertices
* @param xyz the x, y and z scale
*/
publicvoidsetScale(floatxyz)
{
setScale(xyz, xyz, xyz);
}
/**
* Sets the translation of this node
* @param x x translation
* @param y y translation
* @param z z translation
*/
publicvoidsetTranslation(floatx, floaty, floatz)
{
translation[0] = x;
translation[1] = y;
translation[2] = z;
setRecalculateTransformMatrix();
}
/**
* Sets the translation of x and y, and sets z to 0.0
* @param x x translation
* @param y y translation
*/
publicvoidsetTranslation(floatx, floaty)
{
setTranslation(x, y, 0.0f);
}
/**
* Sets the rotation of this node
* @param degrees how many degrees to rotate this node
* @param x amount of rotation around x axis
* @param y amount of rotation around y axis
* @param z amount of rotation around z axis
*/
publicvoidsetRotation(floatdegrees, floatx, floaty, floatz)
{
rotation[0] = degrees;
rotation[1] = x;
rotation[2] = y;
rotation[3] = z;
setRecalculateTransformMatrix();
}
/**
* Sets the identify matrix
* @param matrix the matrix to reset
*/
privatestaticvoididentityMatrix(float[] matrix)
{
matrix[0] = 1.0f; matrix[4] = 0.0f; matrix[8] = 0.0f; matrix[12] = 0.0f;
matrix[1] = 0.0f; matrix[5] = 1.0f; matrix[9] = 0.0f; matrix[13] = 0.0f;
matrix[2] = 0.0f; matrix[6] = 0.0f; matrix[10] = 1.0f; matrix[14] = 0.0f;
matrix[3] = 0.0f; matrix[7] = 0.0f; matrix[11] = 0.0f; matrix[15] = 1.0f;
}
/**
* Multiplies two matrices and puts result in b
* @param a first matrix
* @param b second matrix (and output)
*/
privatestaticvoidmultiplyMatrix(float[] a, float[] b)
{
for (intcol = 0; col < 4; col++)
{
floatp0 = b[col * 4];
floatp1 = b[col * 4 + 1];
floatp2 = b[col * 4 + 2];
floatp3 = b[col * 4 + 3];
for (introw = 0; row < 4; row++)
{
b[col * 4 + row] = a[row] * p0 + a[row + 4] * p1 + a[row + 8] * p2 + a[row + 12] * p3;
}
}
}
/**
* Calculates the node global transformation matrix
* @return this node global transformation matrix
*/
publicfloat[] getNodeGlobalTransform()
{
if(recalculateTransformMatrix)
{
identityMatrix(m);
identityMatrix(temp);
//scale
temp[0] = scale[0];
temp[5] = scale[1];
temp[10] = scale[2];
multiplyMatrix(temp, m);
//rotation
identityMatrix(temp);
floatcosAngle = (float) Math.cos(Math.toRadians(rotation[0]));
floatsinAngle = (float) Math.sin(Math.toRadians(rotation[0]));
floatoneMinusCos = 1.0f - cosAngle;
floatux = rotation[1]; floatuy = rotation[2]; floatuz = rotation[3];
temp[0] = oneMinusCos*ux*ux + cosAngle;
temp[1] = oneMinusCos*ux*uy + sinAngle*uz;
temp[2] = oneMinusCos*ux*uz - sinAngle*uy;
temp[4] = oneMinusCos*ux*uy - sinAngle*uz;
temp[5] = oneMinusCos*uy*uy + cosAngle;
temp[6] = oneMinusCos*uy*uz + sinAngle*ux;
temp[8] = oneMinusCos*ux*uz + sinAngle*uy;
temp[9] = oneMinusCos*uy*uz - sinAngle*ux;
temp[10]= oneMinusCos*uz*uz + cosAngle;
multiplyMatrix(temp, m);
//translation
identityMatrix(temp);
temp[12] = translation[0];
temp[13] = translation[1];
temp[14] = translation[2];
multiplyMatrix(temp, m);
//applies parent transform also (if it has a parent)
if (parentNode != null) //then get the parent node global transform
multiplyMatrix(parentNode.getNodeGlobalTransform(), m);
//finished recalculating
recalculateTransformMatrix = false;
}
returnm;
}
/**
* Initilisation method.
* Also inits children nodes
*/
publicvoidinit(GLgl)
{
//inits position
getNodeGlobalTransform();
for(Noden : childrenNodes)
n.init(gl);
}
/**
* Updates this node.
* Also updates children nodes
*/
publicvoidupdate()
{
//this node does nothing so update children nodes
for(Noden : childrenNodes)
n.update();
}
/**
* Function to run before drawing
* @param gl
*/
protectedvoidpreDraw(GLgl)
{
gl.glPushMatrix();
//push transformation on stack
gl.glMultMatrixf(getNodeGlobalTransform(), 0);
}
/**
* Function to run after drawing
* @param gl
*/
protectedvoidpostDraw(GLgl)
{
gl.glPopMatrix();
}
/**
* Draws the node.
* Also draws children nodes
*/
publicvoiddraw(GLgl)
{
preDraw(gl);
//this node does nothing so draw children nodes
for(Noden : childrenNodes)
n.draw(gl);
postDraw(gl);
}
/**
* Gets the shader program of this node.
* If this is not a shader node then it will get the parent shader program.
* Default is zero.
* @return The shader program ID
*/
publicintgetShaderProgram()
{
if(parentNode != null)
returnparentNode.getShaderProgram();
//zero is default shader
return0;
}
/**
* toString method
* @return String representation of this
*/
@Override
publicStringtoString()
{
return"Node: " + nodeName + " with (" + childrenNodes.size() + ") children";
}
}