浏览代码

Fixed levs, added axis and center point

hutattedonmyarm 12 年前
父节点
当前提交
48631b5348
共有 1 个文件被更改,包括 360 次插入0 次删除
  1. 360
    0
      CubeControl/Led3D.java

+ 360
- 0
CubeControl/Led3D.java 查看文件

@@ -1,3 +1,4 @@
1
+<<<<<<< HEAD
1 2
 /*
2 3
  * Led3D.java
3 4
  *
@@ -337,3 +338,362 @@ public class Led3D extends MouseAdapter {
337 338
 		transGroup.setTransform(trans3D);
338 339
 	}
339 340
 }
341
+=======
342
+/*
343
+ * Led3D.java
344
+ *
345
+ *
346
+ * Copyright 2011 Thomas Buck <xythobuz@me.com>
347
+ * Copyright 2011 Max Nuding <max.nuding@gmail.com>
348
+ * Copyright 2011 Felix Bäder <baeder.felix@gmail.com>
349
+ *
350
+ * This file is part of LED-Cube.
351
+ *
352
+ * LED-Cube is free software: you can redistribute it and/or modify
353
+ * it under the terms of the GNU General Public License as published by
354
+ * the Free Software Foundation, either version 3 of the License, or
355
+ * (at your option) any later version.
356
+ *
357
+ * LED-Cube is distributed in the hope that it will be useful,
358
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
359
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
360
+ * GNU General Public License for more details.
361
+ *
362
+ * You should have received a copy of the GNU General Public License
363
+ * along with LED-Cube. If not, see <http://www.gnu.org/licenses/>.
364
+ */
365
+import com.sun.j3d.utils.universe.*;
366
+import com.sun.j3d.utils.geometry.*;
367
+import javax.media.j3d.*;
368
+import javax.vecmath.*;
369
+import com.sun.j3d.utils.behaviors.mouse.*;
370
+import com.sun.j3d.utils.image.TextureLoader;
371
+import java.awt.Toolkit;
372
+import com.sun.j3d.utils.picking.*;
373
+import java.awt.event.*;
374
+
375
+/**
376
+ * This class is responsible for displaying the 3D View of our Cube.
377
+ * 
378
+ * @author Thomas Buck
379
+ * @author Max Nuding
380
+ * @author Felix Bäder
381
+ * @version 1.0
382
+ */
383
+
384
+public class Led3D extends MouseAdapter {
385
+	private Canvas3D canvas = null;
386
+	private PickCanvas pickCanvas = null;
387
+	private SimpleUniverse universe = null;
388
+	private BranchGroup group = null;
389
+	private Transform3D trans3D = null;
390
+	private TransformGroup transGroup = null;
391
+	private Matrix4d mat = null;
392
+	private Matrix4d fullScreenMat = null;
393
+	private Frame parentFrame = null;
394
+
395
+	private Sphere[][][] leds = new Sphere[8][8][8];
396
+	private static ColoringAttributes redColor = new ColoringAttributes(
397
+			new Color3f(1.0f, 0.0f, 0.0f), ColoringAttributes.FASTEST);
398
+	private static ColoringAttributes whiteColor = new ColoringAttributes(
399
+			new Color3f(0.2f, 0.2f, 0.2f), ColoringAttributes.FASTEST);
400
+	private static Material whiteMat = new Material(new Color3f(0.2f, 0.2f,
401
+			0.2f), new Color3f(0.0f, 0.0f, 0.0f),
402
+			new Color3f(0.2f, 0.2f, 0.2f), new Color3f(0.2f, 0.2f, 0.2f), 64.0f);
403
+	private static Material redMat = new Material(
404
+			new Color3f(1.0f, 0.0f, 0.0f), new Color3f(1.0f, 0.0f, 0.0f),
405
+			new Color3f(1.0f, 0.0f, 0.0f), new Color3f(1.0f, 0.0f, 0.0f), 64.0f);
406
+	private Background background;
407
+
408
+	/**
409
+	 * @param canv The Canvas3D we render our cube in
410
+	 */
411
+	public Led3D(Canvas3D canv, Frame f) {
412
+
413
+		canvas = canv;
414
+		parentFrame = f;
415
+		group = new BranchGroup();
416
+		// Position viewer, so we are looking at object
417
+		trans3D = new Transform3D();
418
+		mat = new Matrix4d();
419
+		mat.setRow(0, 0.7597, -0.0204, 0.64926, 0.56);
420
+		mat.setRow(1, -0.08, -0.995, 0.061, 0.02);
421
+		mat.setRow(2, 0.64473, -0.09786, -0.758, -14.68);
422
+		mat.setRow(3, 0.0, 0.0, 0.0, 1.0);
423
+
424
+		fullScreenMat = new Matrix4d();
425
+		fullScreenMat.setRow(0, 0.7597, -0.0204, 0.64926, 0.56);
426
+		fullScreenMat.setRow(1, -0.08, -0.995, 0.061, 0.02);
427
+		fullScreenMat.setRow(2, 0.64473, -0.09786, -0.758, -14.68);
428
+		fullScreenMat.setRow(3, 0.0, 0.0, 0.0, 1.0);
429
+
430
+
431
+		trans3D.set(mat);
432
+
433
+		transGroup = new TransformGroup(trans3D);
434
+		transGroup.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
435
+		transGroup.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
436
+		ViewingPlatform viewingPlatform = new ViewingPlatform();
437
+		Viewer viewer = new Viewer(canvas);
438
+		universe = new SimpleUniverse(viewingPlatform, viewer);
439
+
440
+		BoundingBox boundBox = new BoundingBox(new Point3d(-5.0, -5.0, -5.0),
441
+				new Point3d(13.0, 13.0, 13.0));
442
+		// roration with left mouse button
443
+		MouseRotate behaviour = new MouseRotate(transGroup);
444
+		behaviour.setSchedulingBounds(boundBox);
445
+		transGroup.addChild(behaviour);
446
+
447
+		// zoom with middle mouse button
448
+		MouseZoom beh2 = new MouseZoom(transGroup);
449
+		beh2.setSchedulingBounds(boundBox);
450
+		transGroup.addChild(beh2);
451
+
452
+		// move with right mouse button
453
+		MouseTranslate beh3 = new MouseTranslate(transGroup);
454
+		beh3.setSchedulingBounds(boundBox);
455
+		transGroup.addChild(beh3);
456
+
457
+		
458
+		group.setCapability(BranchGroup.ALLOW_CHILDREN_READ);
459
+		group.setCapability(BranchGroup.ALLOW_CHILDREN_WRITE);
460
+		group.setCapability(BranchGroup.ALLOW_DETACH);
461
+		background = createBackground();
462
+		group.addChild(background);
463
+
464
+		// Add all our led sphares to the universe
465
+		for (int x = 0; x < 8; x++) {
466
+			for (int y = 0; y < 8; y++) {
467
+				for (int z = 0; z < 8; z++) {
468
+					Appearance a = new Appearance();
469
+					a.setMaterial(whiteMat);
470
+					a.setColoringAttributes(whiteColor);
471
+
472
+					leds[x][y][z] = new Sphere(0.08f,
473
+							Sphere.ENABLE_APPEARANCE_MODIFY, a);
474
+
475
+					TransformGroup tg = new TransformGroup();
476
+					Transform3D transform = new Transform3D();
477
+					Vector3f vector = new Vector3f(x - 3.5f, y -3.5f, z-3.5f);
478
+					transform.setTranslation(vector);
479
+					tg.setTransform(transform);
480
+					tg.addChild(leds[x][y][z]);
481
+					transGroup.addChild(tg);
482
+
483
+					drawLedFeetVertical((double) x - 3.5, y - 3, (double) z-3.5, 0.9f, 0.01f);
484
+					if (x < 7)
485
+						drawLedFeetHorizontal(x - 3, (double) y - 3.5, (double) z - 3.5, 0.9f, 0.01f, 0);
486
+				}
487
+			}
488
+			// 8 times, use x as y
489
+			for(int i = 0; i > -8; i--){
490
+				drawLedFeetHorizontal(i+3.5, (double) x-3.5, 0, 7.0f, 0.02f, 90);
491
+			}
492
+		}
493
+
494
+		drawLedFeetVertical(0, 0, 0, 10, 0.01f); //
495
+		drawLedFeetHorizontal(0, 0, 0, 10, 0.01f, 0); // x, y, and z axis
496
+
497
+		Appearance c = new Appearance();
498
+		c.setMaterial(redMat);
499
+		c.setColoringAttributes(redColor);
500
+
501
+
502
+		Sphere center = new Sphere(0.04f, Sphere.ENABLE_APPEARANCE_MODIFY, c);
503
+		TransformGroup tg = new TransformGroup();
504
+		Transform3D transform = new Transform3D();
505
+		Vector3f vector = new Vector3f(0, 0, 0);
506
+		transform.setTranslation(vector);
507
+		tg.setTransform(transform);
508
+		tg.addChild(center);
509
+		transGroup.addChild(tg);	
510
+
511
+
512
+		// Add an ambient light
513
+		Color3f light2Color = new Color3f(1.0f, 1.0f, 1.0f);
514
+		AmbientLight light2 = new AmbientLight(light2Color);
515
+		BoundingSphere bounds = new BoundingSphere(new Point3d(0.0, 0.0, 0.0),
516
+				100.0);
517
+		light2.setInfluencingBounds(bounds);
518
+		light2.setEnable(true);
519
+		transGroup.addChild(light2);
520
+		group.addChild(transGroup);
521
+		universe.addBranchGraph(group); // Add group to universe
522
+
523
+		// Mouse-Selectable objects
524
+		pickCanvas = new PickCanvas(canvas, group);
525
+		pickCanvas.setMode(PickCanvas.BOUNDS);
526
+		canvas.addMouseListener(this);
527
+	}
528
+
529
+	/**
530
+	 * Listen for mouse events so the user can click on LEDs.
531
+	 * @param e MouseEvent generated by the user
532
+	 */
533
+	public void mouseClicked(MouseEvent e) {
534
+		pickCanvas.setShapeLocation(e);
535
+		PickResult result = pickCanvas.pickClosest();
536
+		if (result != null) {
537
+			// User clicked near something
538
+			Primitive p = (Primitive)result.getNode(PickResult.PRIMITIVE);
539
+			if (p != null) {
540
+				// p is now a Primitive that the user clicked
541
+				if (p.getClass().getName().equals("com.sun.j3d.utils.geometry.Sphere")) {
542
+					// p is a Cylinder. Our LEDs are Spheres, so p.equals(led[x][y][z]) does not find anything...
543
+					for (int x = 0; x < 8; x++) {
544
+						for (int y = 0; y < 8; y++) {
545
+							for (int z = 0; z < 8; z++) {
546
+								if (p.equals(leds[x][y][z])) {
547
+									// Clicked led found!
548
+									System.out.println("Clicked LED found: " + x + " " + y + " " + z);
549
+									parentFrame.toggleLED(x, y, z);
550
+
551
+									x = 8;
552
+									y = 8;
553
+									z = 8;
554
+								}
555
+							}
556
+						}
557
+					}
558
+				} else {
559
+					System.out.println("Clicked, but not a sphere. Clicked object: " + p.getClass().getName());
560
+					if(p.getClass().getName().equals("com.sun.j3d.utils.geometry.Cylinder")){
561
+
562
+					}
563
+				}
564
+			}
565
+		} else {
566
+			System.out.println("Clicked, but hit nothing");
567
+		}
568
+	}
569
+
570
+	private void drawLedFeetVertical(double x, double y, double z,
571
+			float length, float rad) {
572
+		// draw Feet going down
573
+		Appearance feetApp = new Appearance();
574
+		feetApp.setMaterial(whiteMat);
575
+		feetApp.setColoringAttributes(whiteColor);
576
+		Cylinder c = new Cylinder(rad, length, feetApp);
577
+
578
+		TransformGroup tg = new TransformGroup();
579
+		Transform3D transform = new Transform3D();
580
+		Vector3d vector = new Vector3d(x, y, z);
581
+		transform.setTranslation(vector);
582
+		tg.setTransform(transform);
583
+		tg.addChild(c);
584
+
585
+		transGroup.addChild(tg);
586
+	}
587
+
588
+	private void drawLedFeetHorizontal(double x, double y, double z,
589
+			float length, float rad, int deg) {
590
+		// draw Feet going down
591
+		Appearance feetApp = new Appearance();
592
+		feetApp.setMaterial(whiteMat);
593
+		feetApp.setColoringAttributes(whiteColor);
594
+		Cylinder c = new Cylinder(rad, length, feetApp);
595
+
596
+		TransformGroup tg = new TransformGroup();
597
+		Transform3D transform = new Transform3D();
598
+		Vector3d vector = new Vector3d(x, y, z);
599
+		transform.rotZ(Math.toRadians(90));
600
+		if (deg != 0)
601
+			transform.rotX(Math.toRadians(deg));
602
+		transform.setTranslation(vector);
603
+		tg.setTransform(transform);
604
+		tg.addChild(c);
605
+
606
+		transGroup.addChild(tg);
607
+	}
608
+
609
+	/**
610
+	 * Rotate the cube back to its initial position.
611
+	 */
612
+	public void resetView() {
613
+		Matrix4d mat = new Matrix4d();
614
+		mat.setRow(0, 0.744, 0.0237, -0.66756, -0.34);
615
+		mat.setRow(1, 0.136, -0.9837, 0.117, 3.24);
616
+		mat.setRow(2, -0.6536, -0.1785, -0.735, -8.32);
617
+		mat.setRow(3, 0.0, 0.0, 0.0, 1.0);
618
+		trans3D.set(mat);
619
+		transGroup.setTransform(trans3D);
620
+	}
621
+
622
+	/**
623
+	 * Prints the translation matrix that is changed by moving/rotating the 3D
624
+	 * Cube with your mouse.
625
+	 */
626
+	public void printTranslationData() {
627
+		Matrix4d mat = new Matrix4d();
628
+		Transform3D t = new Transform3D();
629
+		transGroup.getTransform(t);
630
+		t.get(mat);
631
+		String s = mat.toString();
632
+		System.out.println(s.replaceAll(", ", "\t"));
633
+	}
634
+
635
+	/**
636
+	 * Sets the data that is displayed by the LEDs
637
+	 * 
638
+	 * @param data 64 byte array with the data (8 bits/LEDs per byte)
639
+	 */
640
+	public void setData(short[] data) {
641
+		for (int y = 0; y < 8; y++) {
642
+			for (int z = 0; z < 8; z++) {
643
+				for (int x = 0; x < 8; x++) {
644
+					Appearance a = new Appearance();
645
+					if ((data[y + (z * 8)] & (1 << x)) != 0) {
646
+						// Activate led
647
+						a.setColoringAttributes(redColor);
648
+						a.setMaterial(redMat);
649
+					} else {
650
+						// Deactivate led
651
+						a.setColoringAttributes(whiteColor);
652
+						a.setMaterial(whiteMat);
653
+					}
654
+					leds[x][y][z].setAppearance(a);
655
+				}
656
+			}
657
+		}
658
+	}
659
+
660
+	// create nice background
661
+	private Background createBackground() {
662
+		Background bg = new Background(0.0f, 0.0f, 1.0f);
663
+		int radius = canvas.getWidth();
664
+		bg.setApplicationBounds(new BoundingSphere(new Point3d(0.0, 0.0, 0.0), radius));
665
+		return bg;
666
+	}
667
+
668
+	/**
669
+	 * Create new background to reflect changed Canvas size.
670
+	 */
671
+	private void toggleFullscreen() {
672
+		group.detach();
673
+		if(group.indexOfChild(background) != -1){
674
+			group.removeChild(background);
675
+		}
676
+		background = createBackground();
677
+		group.addChild(background);
678
+		universe.addBranchGraph(group);
679
+	}
680
+	
681
+	/**
682
+	 * Create new background and adjust view.
683
+	 */
684
+	public void enterFullscreen() {
685
+		toggleFullscreen();
686
+		trans3D.set(fullScreenMat);
687
+		transGroup.setTransform(trans3D);
688
+	}
689
+	
690
+	/**
691
+	 * Create new background and adjust view.
692
+	 */
693
+	public void leaveFullscreen() {
694
+		toggleFullscreen();
695
+		trans3D.set(mat);
696
+		transGroup.setTransform(trans3D);
697
+	}
698
+}
699
+>>>>>>> Fixed levs, added axis and center point

正在加载...
取消
保存