-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNode.java
367 lines (301 loc) · 9.11 KB
/
Node.java
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
import java.util.ArrayList;
import javax.media.opengl.GL;
/**
* Basic node for a JOGL 3D Application
* @author Andrew
*/
public class Node
{
/** Name of this node */
private String nodeName;
/** Parent node (if null then this is rootNode - or not attached to scene */
protected Node parentNode;
/** A list of all the children nodes below this node */
private ArrayList<Node> childrenNodes;
/* Transformations */
private final float[] scale;
private final float[] translation;
private final float[] rotation;
/** Global Transformation Matrix */
private final float[] m;
/** Temp matrix to use to calculate transformation m */
private final float[] temp;
/** True when transformation matrix is needed to be recalculated */
private boolean recalculateTransformMatrix;
/**
* Creates a new node
* @param nodeName the name of this node
*/
public Node(String nodeName)
{
this.nodeName = nodeName;
parentNode = null;
childrenNodes = new ArrayList<Node>();
scale = new float[3];
translation = new float[3];
rotation = new float[4];
resetLocalTransformations();
m = new float[16];
temp = new float[16];
recalculateTransformMatrix = true;
}
/**
* Sets all translations back to original
*/
protected void resetLocalTransformations()
{
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
*/
protected void setRecalculateTransformMatrix()
{
this.recalculateTransformMatrix = true;
for(Node c : 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.
*/
public boolean addChild(Node child)
{
boolean success = 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;
}
return success;
}
/**
* Removes a child from this node
* @param child the child to remove from this node
* @return true if successfully removed - false if not.
*/
public boolean removeChild(Node child)
{
return childrenNodes.remove(child);
}
/**
* Sets the scale of this node
* @param x x scale
* @param y y scale
* @param z z scale
*/
public void setScale(float x, float y, float z)
{
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
*/
public void setScale(float xyz)
{
setScale(xyz, xyz, xyz);
}
/**
* Sets the translation of this node
* @param x x translation
* @param y y translation
* @param z z translation
*/
public void setTranslation(float x, float y, float z)
{
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
*/
public void setTranslation(float x, float y)
{
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
*/
public void setRotation(float degrees, float x, float y, float z)
{
rotation[0] = degrees;
rotation[1] = x;
rotation[2] = y;
rotation[3] = z;
setRecalculateTransformMatrix();
}
/**
* Sets the identify matrix
* @param matrix the matrix to reset
*/
private static void identityMatrix(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)
*/
private static void multiplyMatrix(float[] a, float[] b)
{
for (int col = 0; col < 4; col++)
{
float p0 = b[col * 4];
float p1 = b[col * 4 + 1];
float p2 = b[col * 4 + 2];
float p3 = b[col * 4 + 3];
for (int row = 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
*/
public float[] 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);
float cosAngle = (float) Math.cos(Math.toRadians(rotation[0]));
float sinAngle = (float) Math.sin(Math.toRadians(rotation[0]));
float oneMinusCos = 1.0f - cosAngle;
float ux = rotation[1]; float uy = rotation[2]; float uz = 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;
}
return m;
}
/**
* Initilisation method.
* Also inits children nodes
*/
public void init(GL gl)
{
//inits position
getNodeGlobalTransform();
for(Node n : childrenNodes)
n.init(gl);
}
/**
* Updates this node.
* Also updates children nodes
*/
public void update()
{
//this node does nothing so update children nodes
for(Node n : childrenNodes)
n.update();
}
/**
* Function to run before drawing
* @param gl
*/
protected void preDraw(GL gl)
{
gl.glPushMatrix();
//push transformation on stack
gl.glMultMatrixf(getNodeGlobalTransform(), 0);
}
/**
* Function to run after drawing
* @param gl
*/
protected void postDraw(GL gl)
{
gl.glPopMatrix();
}
/**
* Draws the node.
* Also draws children nodes
*/
public void draw(GL gl)
{
preDraw(gl);
//this node does nothing so draw children nodes
for(Node n : 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
*/
public int getShaderProgram()
{
if(parentNode != null)
return parentNode.getShaderProgram();
//zero is default shader
return 0;
}
/**
* toString method
* @return String representation of this
*/
@Override
public String toString()
{
return "Node: " + nodeName + " with (" + childrenNodes.size() + ") children";
}
}