This repository was archived by the owner on Feb 6, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathArty.java
403 lines (350 loc) · 13.1 KB
/
Arty.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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
import gmaths.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import com.jogamp.opengl.*;
import com.jogamp.opengl.awt.GLCanvas;
import com.jogamp.opengl.util.FPSAnimator;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
/**
* Arty.java
* Creates a frame containing a JOGL-powered canvas and controls for the scene
*
* @author Will Garside // worgarside@gmail.com
* @version 1.0 2017-12-06
*/
public class Arty extends JFrame implements ActionListener {
private static final int WIDTH = 1080;
private static final int HEIGHT = 920;
private static final Dimension dimension = new Dimension(WIDTH, HEIGHT);
private GLCanvas canvas;
private Arty_GLEventListener glEventListener;
private final FPSAnimator animator;
private Camera camera;
private static final String CSV_DELIM = ",";
private static final String LIGHT_DATA_FILE = "lightData.csv";
private static final String KEYFRAME_DATA_FILE = "keyframes.csv";
static ArrayList<float[]> lightData = new ArrayList<float[]>();
static ArrayList<Keyframe> keyframes = new ArrayList<Keyframe>();
static int lightCount = 0;
static Keyframe neutralKeyframe;
private static JPanel panel = new JPanel();
private static RotatedIcon clockRot;
public static boolean night = false;
/**
* Initialises the Arty frame
*
* @param args
*/
public static void main(String[] args) {
readLightData();
readKeyframeData();
Arty arty = new Arty("COM3503 - Robot Hand");
arty.getContentPane().setPreferredSize(dimension);
arty.pack();
arty.setVisible(true);
}
/**
* Constructor for Arty frame, sets up the JOGL canvas and the control panel
*
* @param frameTitle - title of frame
*/
public Arty(String frameTitle) {
super(frameTitle);
GLCapabilities glcapabilities = new GLCapabilities(GLProfile.get(GLProfile.GL3));
// Use MSAA for cleaner model
glcapabilities.setSampleBuffers(true);
glcapabilities.setNumSamples(4);
canvas = new GLCanvas(glcapabilities);
camera = new Camera(new Vec3(-4f, 12f, 24f), new Vec3(0f, 8f, 0f), Camera.DEFAULT_UP);
glEventListener = new Arty_GLEventListener(camera);
canvas.addGLEventListener(glEventListener);
canvas.addMouseMotionListener(new MyMouseInput(camera));
canvas.addKeyListener(new MyKeyboardInput(camera));
getContentPane().add(canvas, BorderLayout.CENTER);
panel.setPreferredSize(new Dimension(WIDTH, 100));
panel.setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
createClockFace(panel, gbc);
createControls(panel, gbc);
this.add(panel, BorderLayout.SOUTH);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
animator.stop();
remove(canvas);
dispose();
System.exit(0);
}
});
animator = new FPSAnimator(canvas, 60);
animator.start();
}
/**
* Listener for Arm Bearing slider to set Robot Hand bearing value
*/
ChangeListener sliderListener = new ChangeListener() {
public void stateChanged(ChangeEvent event) {
JSlider sliderArmBearing = (JSlider) event.getSource();
glEventListener.rotArmToAngle(sliderArmBearing.getValue());
}
};
/**
* Function called on button press to run function in the scene
*
* @param e - the event taking place
*/
public void actionPerformed(ActionEvent e) {
for (int i = 0; i < keyframes.size(); i++) {
if (e.getActionCommand().equalsIgnoreCase(keyframes.get(i).getName())) {
glEventListener.setHandKeyframe(i);
}
}
switch (e.getActionCommand().toLowerCase()) {
case "lamps":
glEventListener.toggleLamps();
break;
case "world light":
glEventListener.toggleWorldLight();
break;
case "keyframe sequence":
glEventListener.toggleKeyframeSequence();
break;
case "all animations":
glEventListener.toggleGlobalAnims();
break;
case "day/night cycle":
glEventListener.toggleDayNight();
break;
case "exit":
System.exit(0);
break;
}
}
/**
* Reads the data provided in lightData.csv to add to set up the lights and their properties
*/
private static void readLightData() {
BufferedReader br = null;
try{
br = new BufferedReader(new FileReader(LIGHT_DATA_FILE));
String line = "";
br.readLine();
while ((line = br.readLine()) != null) {
String[] dataString = line.split(CSV_DELIM);
if(dataString.length > 0 ) {
float[] dataFloats = new float[dataString.length];
for (int i = 0; i < dataString.length; i++) {
dataFloats[i] = Float.parseFloat(dataString[i]);
}
lightData.add(dataFloats);
lightCount ++;
}
}
} catch(Exception ee) {
ee.printStackTrace();
} finally {
try {
br.close();
} catch(IOException ie) {
System.out.println("Error occured while closing the BufferedReader");
ie.printStackTrace();
}
}
}
/**
* Reads the data provided in keyframes.csv to add to an ArrayList of keyframes for the RobotHand
*/
private static void readKeyframeData() {
BufferedReader br = null;
try{
br = new BufferedReader(new FileReader(KEYFRAME_DATA_FILE));
String line = "";
br.readLine();
while ((line = br.readLine()) != null) {
String[] dataString = line.split(CSV_DELIM);
if(dataString.length > 0 ) {
String keyframeName = dataString[0];
String[] prmAnglesSection = Arrays.copyOfRange(dataString, 1, 16);
String[] secAnglesSection = Arrays.copyOfRange(dataString, 16, 21);
int[][] prmAngles = new int[RobotHand.DIGIT_COUNT][RobotHand.PHALANGE_COUNT];
int[] secAngles = new int[RobotHand.DIGIT_COUNT];
int i = 0;
for (int j = 0; j < RobotHand.DIGIT_COUNT; j++) {
for (int k = 0; k < RobotHand.PHALANGE_COUNT; k++) {
prmAngles[j][k] = Integer.parseInt(prmAnglesSection[i++]);
}
}
for (int j = 0; j < secAnglesSection.length; j++) {
secAngles[j] = Integer.parseInt(secAnglesSection[j]);
}
keyframes.add(new Keyframe(keyframeName, prmAngles, secAngles));
if (keyframeName.equalsIgnoreCase("neutral")) {
neutralKeyframe = new Keyframe(keyframeName, prmAngles, secAngles);
}
}
}
} catch(Exception ee) {
ee.printStackTrace();
} finally {
try {
br.close();
} catch(IOException ie) {
System.out.println("Error occured while closing the BufferedReader");
ie.printStackTrace();
}
}
}
/**
* Creates a clockFace to represent the Day/Night cycle used in the scene
*
* @param panel - the panel that the clockFace is added to
* @param gbc - GridBagConstraints used in the panel
*/
private static void createClockFace(JPanel panel, GridBagConstraints gbc) {
JPanel clockPanel = new JPanel();
LayoutManager overlay = new OverlayLayout(clockPanel);
clockPanel.setLayout(overlay);
clockPanel.setPreferredSize(new Dimension(90, 90));
JLabel clockFace = new JLabel(new ImageIcon("textures/clockFace.png"));
clockFace.setLayout(new BorderLayout());
clockPanel.add(clockFace);
ImageIcon clockIcon = new ImageIcon("textures/pointer.png");
clockRot = new RotatedIcon(clockIcon, 0);
JLabel clockLabel = new JLabel("", clockRot, JLabel.CENTER);
clockFace.add(clockLabel);
clockPanel.add(clockFace, BorderLayout.CENTER );
gbc.gridx = 0;
gbc.gridy = 0;
gbc.gridheight = 2;
panel.add(clockPanel, gbc);
}
/**
* Creates the controls for the scene and adds them to a JPanel in the frame
*
* @param panel - the panel that the controls are added to
* @param gbc - GridBagConstraints used in the panel
*/
private void createControls(JPanel panel, GridBagConstraints gbc) {
// Create buttons for each keyframe defined in KEYFRAME_DATA_FILE
JButton btn = new JButton();
JLabel posLabel = new JLabel("Positions");
JLabel armLabel = new JLabel("Arm Bearing");
JLabel toggleLabel = new JLabel("Toggle Controls");
posLabel.setHorizontalAlignment(SwingConstants.CENTER);
armLabel.setHorizontalAlignment(SwingConstants.CENTER);
toggleLabel.setHorizontalAlignment(SwingConstants.CENTER);
JPanel posBtnPanel = new JPanel();
for (int i = 0; i < keyframes.size(); i++) {
btn = new JButton(keyframes.get(i).getName());
btn.addActionListener(this);
posBtnPanel.add(btn);
}
// Controls for rotating the entire model
JSlider armAngleSlider = new JSlider(JSlider.HORIZONTAL, 0, 720, 360);
armAngleSlider.setPreferredSize(new Dimension(125, 25));
armAngleSlider.addChangeListener(sliderListener);
JPanel sliderPanel = new JPanel();
sliderPanel.add(armAngleSlider);
JPanel toggleBtnPanel = new JPanel();
btn = new JButton("Lamps");
btn.addActionListener(this);
toggleBtnPanel.add(btn);
btn = new JButton("World Light");
btn.addActionListener(this);
toggleBtnPanel.add(btn);
btn = new JButton("Keyframe Sequence");
btn.addActionListener(this);
toggleBtnPanel.add(btn);
btn = new JButton("All Animations");
btn.addActionListener(this);
toggleBtnPanel.add(btn);
btn = new JButton("Day/Night Cycle");
btn.addActionListener(this);
toggleBtnPanel.add(btn);
gbc.anchor = GridBagConstraints.NORTH;
gbc.gridx = 1;
gbc.gridy = 0;
panel.add(posLabel, gbc);
gbc.gridx = 2;
gbc.gridy = 0;
panel.add(armLabel, gbc);
gbc.gridx = 3;
gbc.gridy = 0;
panel.add(toggleLabel, gbc);
gbc.gridx = 1;
gbc.gridy = 1;
panel.add(posBtnPanel, gbc);
gbc.gridx = 2;
gbc.gridy = 1;
panel.add(sliderPanel, gbc);
gbc.gridx = 3;
gbc.gridy = 1;
panel.add(toggleBtnPanel, gbc);
btn = new JButton("Exit");
btn.addActionListener(this);
panel.add(btn);
}
/**
* Updates the Day/Night clock
*
* @param seconds - current seconds value displayed on the clock
*/
public static void updateClock(int seconds) {
clockRot.setDegrees(seconds*6);
panel.repaint();
}
}
/**
* Allows use of keybaord to move through scene
*
* @author Dr. Steve Maddock
*/
class MyKeyboardInput extends KeyAdapter {
private Camera camera;
public MyKeyboardInput(Camera camera) {
this.camera = camera;
}
public void keyPressed(KeyEvent e) {
Camera.Movement m = Camera.Movement.NO_MOVEMENT;
switch (e.getKeyCode()) {
case KeyEvent.VK_LEFT: m = Camera.Movement.LEFT; break;
case KeyEvent.VK_RIGHT: m = Camera.Movement.RIGHT; break;
case KeyEvent.VK_UP: m = Camera.Movement.UP; break;
case KeyEvent.VK_DOWN: m = Camera.Movement.DOWN; break;
case KeyEvent.VK_A: m = Camera.Movement.FORWARD; break;
case KeyEvent.VK_Z: m = Camera.Movement.BACK; break;
case KeyEvent.VK_ESCAPE: System.exit(0); break;
}
camera.keyboardInput(m);
}
}
/**
* Allows use of mouse to control camera
*
* @author Dr. Steve Maddock
*/
class MyMouseInput extends MouseMotionAdapter {
private Point lastpoint;
private Camera camera;
public MyMouseInput(Camera camera) {
this.camera = camera;
}
public void mouseDragged(MouseEvent e) {
Point ms = e.getPoint();
float sensitivity = 0.001f;
float dx=(float) (ms.x-lastpoint.x)*sensitivity;
float dy=(float) (ms.y-lastpoint.y)*sensitivity;
if (e.getModifiers()==MouseEvent.BUTTON1_MASK) {
camera.updateYawPitch(dx, -dy);
}
lastpoint = ms;
}
public void mouseMoved(MouseEvent e) {
lastpoint = e.getPoint();
}
}