Simple single-color 8x8x8 LED Cube with AVRs
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Led3D.java 7.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. /*
  2. * Led3D.java
  3. *
  4. *
  5. * Copyright 2011 Thomas Buck <xythobuz@me.com>
  6. * Copyright 2011 Max Nuding <max.nuding@gmail.com>
  7. * Copyright 2011 Felix Bäder <baeder.felix@gmail.com>
  8. *
  9. * This file is part of LED-Cube.
  10. *
  11. * LED-Cube is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation, either version 3 of the License, or
  14. * (at your option) any later version.
  15. *
  16. * LED-Cube is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with LED-Cube. If not, see <http://www.gnu.org/licenses/>.
  23. */
  24. import com.sun.j3d.utils.universe.*;
  25. import com.sun.j3d.utils.geometry.*;
  26. import javax.media.j3d.*;
  27. import javax.vecmath.*;
  28. import com.sun.j3d.utils.behaviors.mouse.*;
  29. /**
  30. * This class is responsible for displaying the 3D View of our Cube.
  31. * @author Thomas Buck
  32. * @author Max Nuding
  33. * @author Felix Bäder
  34. * @version 1.0
  35. */
  36. public class Led3D {
  37. private Canvas3D canvas = null;
  38. private SimpleUniverse universe = null;
  39. private BranchGroup group = null;
  40. private Transform3D trans3D = null;
  41. private BranchGroup inBetween = null;
  42. private TransformGroup transGroup = null;
  43. private Sphere[][][] leds = new Sphere[8][8][8];
  44. private static ColoringAttributes redColor = new ColoringAttributes(new Color3f(1.0f, 0.0f, 0.0f), ColoringAttributes.FASTEST);
  45. private static ColoringAttributes whiteColor = new ColoringAttributes(new Color3f(0.2f, 0.2f, 0.2f), ColoringAttributes.FASTEST);
  46. private static Material whiteMat = new Material(new Color3f(0.2f, 0.2f, 0.2f), new Color3f(0.0f, 0.0f, 0.0f), new Color3f(0.2f, 0.2f, 0.2f), new Color3f(0.2f, 0.2f, 0.2f), 64.0f);
  47. private static Material redMat = new Material(new Color3f(1.0f, 0.0f, 0.0f), new Color3f(1.0f, 0.0f, 0.0f), new Color3f(1.0f, 0.0f, 0.0f), new Color3f(1.0f, 0.0f, 0.0f), 64.0f);
  48. /**
  49. * @param canv The Canvas3D we render our cube in
  50. */
  51. public Led3D(Canvas3D canv) {
  52. canvas = canv;
  53. group = new BranchGroup();
  54. // Position viewer, so we are looking at object
  55. trans3D = new Transform3D();
  56. Matrix4d mat = new Matrix4d();
  57. mat.setRow(0, 0.744, 0.0237, -0.66756, -0.34);
  58. mat.setRow(1, 0.136, -0.9837, 0.117, 3.24);
  59. mat.setRow(2, -0.6536, -0.1785, -0.735, -8.32);
  60. mat.setRow(3, 0.0, 0.0, 0.0, 1.0);
  61. trans3D.set(mat);
  62. transGroup = new TransformGroup(trans3D);
  63. ViewingPlatform viewingPlatform = new ViewingPlatform();
  64. transGroup.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
  65. transGroup.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
  66. transGroup.setCapability(TransformGroup.ALLOW_CHILDREN_EXTEND);
  67. transGroup.setCapability(TransformGroup.ALLOW_CHILDREN_WRITE);
  68. transGroup.setCapability(TransformGroup.ALLOW_CHILDREN_READ);
  69. Viewer viewer = new Viewer(canvas);
  70. universe = new SimpleUniverse(viewingPlatform, viewer);
  71. group.addChild(transGroup);
  72. universe.addBranchGraph(group); // Add group to universe
  73. BoundingBox boundBox = new BoundingBox(new Point3d(-5.0, -5.0, -5.0), new Point3d(13.0, 13.0, 13.0));
  74. // roration with left mouse button
  75. MouseRotate behaviour = new MouseRotate(transGroup);
  76. BranchGroup inBetween = new BranchGroup();
  77. inBetween.setCapability(BranchGroup.ALLOW_CHILDREN_EXTEND);
  78. inBetween.setCapability(BranchGroup.ALLOW_CHILDREN_WRITE);
  79. inBetween.setCapability(BranchGroup.ALLOW_CHILDREN_READ);
  80. inBetween.addChild(behaviour);
  81. transGroup.addChild(inBetween);
  82. behaviour.setSchedulingBounds(boundBox);
  83. // zoom with middle mouse button
  84. MouseZoom beh2 = new MouseZoom(transGroup);
  85. BranchGroup brM2 = new BranchGroup();
  86. brM2.setCapability(BranchGroup.ALLOW_CHILDREN_EXTEND);
  87. brM2.setCapability(BranchGroup.ALLOW_CHILDREN_WRITE);
  88. brM2.setCapability(BranchGroup.ALLOW_CHILDREN_READ);
  89. brM2.addChild(beh2);
  90. inBetween.addChild(brM2);
  91. beh2.setSchedulingBounds(boundBox);
  92. // move with right mouse button
  93. MouseTranslate beh3 = new MouseTranslate(transGroup);
  94. BranchGroup brM3 = new BranchGroup();
  95. brM3.setCapability(BranchGroup.ALLOW_CHILDREN_EXTEND);
  96. brM3.setCapability(BranchGroup.ALLOW_CHILDREN_WRITE);
  97. brM3.setCapability(BranchGroup.ALLOW_CHILDREN_READ);
  98. brM3.addChild(beh3);
  99. inBetween.addChild(brM3);
  100. beh3.setSchedulingBounds(boundBox);
  101. // Add all our led sphares to the universe
  102. for (int x = 0; x < 8; x++) {
  103. for (int y = 0; y < 8; y++) {
  104. for (int z = 0; z < 8; z++) {
  105. leds[x][y][z] = new Sphere(0.05f);
  106. Appearance a = new Appearance();
  107. a.setMaterial(whiteMat);
  108. a.setColoringAttributes(whiteColor);
  109. leds[x][y][z].setAppearance(a);
  110. leds[x][y][z].getShape().setCapability(Shape3D.ALLOW_APPEARANCE_WRITE);
  111. TransformGroup tg = new TransformGroup();
  112. tg.setCapability(TransformGroup.ALLOW_CHILDREN_EXTEND);
  113. tg.setCapability(TransformGroup.ALLOW_CHILDREN_WRITE);
  114. tg.setCapability(TransformGroup.ALLOW_CHILDREN_READ);
  115. Transform3D transform = new Transform3D();
  116. Vector3f vector = new Vector3f(x, y, z);
  117. transform.setTranslation(vector);
  118. tg.setTransform(transform);
  119. tg.addChild(leds[x][y][z]);
  120. BranchGroup allTheseGroupsScareMe = new BranchGroup();
  121. allTheseGroupsScareMe.setCapability(BranchGroup.ALLOW_CHILDREN_EXTEND);
  122. allTheseGroupsScareMe.setCapability(BranchGroup.ALLOW_CHILDREN_READ);
  123. allTheseGroupsScareMe.setCapability(BranchGroup.ALLOW_CHILDREN_WRITE);
  124. allTheseGroupsScareMe.addChild(tg);
  125. inBetween.addChild(allTheseGroupsScareMe);
  126. }
  127. }
  128. }
  129. // Add an ambient light
  130. Color3f light2Color = new Color3f(1.0f, 1.0f, 1.0f);
  131. AmbientLight light2 = new AmbientLight(light2Color);
  132. BoundingSphere bounds = new BoundingSphere(new Point3d(0.0, 0.0, 0.0), 100.0);
  133. light2.setInfluencingBounds(bounds);
  134. BranchGroup fffuuuuu = new BranchGroup();
  135. light2.setEnable(true);
  136. fffuuuuu.addChild(light2);
  137. inBetween.addChild(fffuuuuu);
  138. }
  139. /**
  140. * Prints the translation matrix that is changed by moving/rotating the 3D Cube with your mouse.
  141. */
  142. public void printTranslationData() {
  143. Matrix4d mat = new Matrix4d();
  144. Transform3D t = new Transform3D();
  145. transGroup.getTransform(t);
  146. t.get(mat);
  147. String s = mat.toString();
  148. System.out.println(s.replaceAll(", ", "\t"));
  149. }
  150. /**
  151. * Sets the data that is displayed by the LEDs
  152. * @param data 64 byte array with the data (8 bits/LEDs per byte)
  153. */
  154. public void setData(short[] data) {
  155. for (int y = 0; y < 8; y++) {
  156. for (int z = 0; z < 8; z++) {
  157. for (int x = 0; x < 8; x++) {
  158. Appearance a = new Appearance();
  159. if ((data[y + (z * 8)] & (1 << x)) != 0) {
  160. // Activate led
  161. a.setColoringAttributes(redColor);
  162. a.setMaterial(redMat);
  163. } else {
  164. // Deactivate led
  165. a.setColoringAttributes(whiteColor);
  166. a.setMaterial(whiteMat);
  167. }
  168. leds[x][y][z].setAppearance(a);
  169. }
  170. }
  171. }
  172. }
  173. }