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.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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(1.0f, 1.0f, 1.0f), ColoringAttributes.FASTEST);
  46. private static Material whiteMat = new Material(new Color3f(1.0f, 1.0f, 1.0f), new Color3f(1.0f, 1.0f, 1.0f), new Color3f(1.0f, 1.0f, 1.0f), new Color3f(1.0f, 1.0f, 1.0f), 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. private Point3d eye = new Point3d(3.5, 3.5, -13.0);
  49. private Point3d look = new Point3d(3.5, 3.5, 0.0);
  50. private Vector3d lookVect = new Vector3d(1.0, 1.0, 1.0);
  51. /**
  52. * @param canv The Canvas3D we render our cube in
  53. */
  54. public Led3D(Canvas3D canv) {
  55. canvas = canv;
  56. group = new BranchGroup();
  57. // Position viewer, so we are looking at object
  58. trans3D = new Transform3D();
  59. trans3D.lookAt(eye, look, lookVect);
  60. trans3D.invert();
  61. transGroup = new TransformGroup(trans3D);
  62. transGroup = new TransformGroup();
  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.getViewingPlatform().getViewPlatformTransform().setTransform(trans3D);
  73. // universe.getViewingPlatform().setNominalViewingTransform();
  74. universe.addBranchGraph(group); // Add group to universe
  75. BoundingBox boundBox = new BoundingBox(new Point3d(-5.0, -5.0, -5.0), new Point3d(13.0, 13.0, 13.0));
  76. // roration with left mouse button
  77. MouseRotate behaviour = new MouseRotate(transGroup);
  78. BranchGroup inBetween = new BranchGroup();
  79. inBetween.setCapability(BranchGroup.ALLOW_CHILDREN_EXTEND);
  80. inBetween.setCapability(BranchGroup.ALLOW_CHILDREN_WRITE);
  81. inBetween.setCapability(BranchGroup.ALLOW_CHILDREN_READ);
  82. inBetween.addChild(behaviour);
  83. transGroup.addChild(inBetween);
  84. behaviour.setSchedulingBounds(boundBox);
  85. // zoom with middle mouse button
  86. MouseZoom beh2 = new MouseZoom(transGroup);
  87. BranchGroup brM2 = new BranchGroup();
  88. brM2.setCapability(BranchGroup.ALLOW_CHILDREN_EXTEND);
  89. brM2.setCapability(BranchGroup.ALLOW_CHILDREN_WRITE);
  90. brM2.setCapability(BranchGroup.ALLOW_CHILDREN_READ);
  91. brM2.addChild(beh2);
  92. inBetween.addChild(brM2);
  93. beh2.setSchedulingBounds(boundBox);
  94. // move with right mouse button
  95. MouseTranslate beh3 = new MouseTranslate(transGroup);
  96. BranchGroup brM3 = new BranchGroup();
  97. brM3.setCapability(BranchGroup.ALLOW_CHILDREN_EXTEND);
  98. brM3.setCapability(BranchGroup.ALLOW_CHILDREN_WRITE);
  99. brM3.setCapability(BranchGroup.ALLOW_CHILDREN_READ);
  100. brM3.addChild(beh3);
  101. inBetween.addChild(brM3);
  102. beh3.setSchedulingBounds(boundBox);
  103. // Add all our led sphares to the universe
  104. for (int x = 0; x < 8; x++) {
  105. for (int y = 0; y < 8; y++) {
  106. for (int z = 0; z < 8; z++) {
  107. leds[x][y][z] = new Sphere(0.05f);
  108. Appearance a = new Appearance();
  109. a.setMaterial(whiteMat);
  110. a.setColoringAttributes(whiteColor);
  111. leds[x][y][z].setAppearance(a);
  112. leds[x][y][z].getShape().setCapability(Shape3D.ALLOW_APPEARANCE_WRITE);
  113. TransformGroup tg = new TransformGroup();
  114. tg.setCapability(TransformGroup.ALLOW_CHILDREN_EXTEND);
  115. tg.setCapability(TransformGroup.ALLOW_CHILDREN_WRITE);
  116. tg.setCapability(TransformGroup.ALLOW_CHILDREN_READ);
  117. Transform3D transform = new Transform3D();
  118. Vector3f vector = new Vector3f(x, y, z);
  119. transform.setTranslation(vector);
  120. tg.setTransform(transform);
  121. tg.addChild(leds[x][y][z]);
  122. BranchGroup allTheseGroupsScareMe = new BranchGroup();
  123. allTheseGroupsScareMe.setCapability(BranchGroup.ALLOW_CHILDREN_EXTEND);
  124. allTheseGroupsScareMe.setCapability(BranchGroup.ALLOW_CHILDREN_READ);
  125. allTheseGroupsScareMe.setCapability(BranchGroup.ALLOW_CHILDREN_WRITE);
  126. allTheseGroupsScareMe.addChild(tg);
  127. inBetween.addChild(allTheseGroupsScareMe);
  128. }
  129. }
  130. }
  131. // Add an ambient light
  132. Color3f light2Color = new Color3f(1.0f, 1.0f, 1.0f);
  133. AmbientLight light2 = new AmbientLight(light2Color);
  134. BoundingSphere bounds = new BoundingSphere(new Point3d(0.0, 0.0, 0.0), 100.0);
  135. light2.setInfluencingBounds(bounds);
  136. BranchGroup fffuuuuu = new BranchGroup();
  137. light2.setEnable(true);
  138. fffuuuuu.addChild(light2);
  139. inBetween.addChild(fffuuuuu);
  140. }
  141. /**
  142. * Prints the translation matrix that is changed by moving/rotating the 3D Cube with your mouse.
  143. */
  144. public void printTranslationData() {
  145. Matrix4d mat = new Matrix4d();
  146. Transform3D t = new Transform3D();
  147. transGroup.getTransform(t);
  148. t.get(mat);
  149. String s = mat.toString();
  150. System.out.println(s.replaceAll(", ", "\t"));
  151. }
  152. /**
  153. * Sets the data that is displayed by the LEDs
  154. * @param data 64 byte array with the data (8 bits/LEDs per byte)
  155. */
  156. public void setData(short[] data) {
  157. for (int y = 0; y < 8; y++) {
  158. for (int z = 0; z < 8; z++) {
  159. for (int x = 0; x < 8; x++) {
  160. Appearance a = new Appearance();
  161. if ((data[y + (z * 8)] & (1 << x)) != 0) {
  162. // Activate led
  163. a.setColoringAttributes(redColor);
  164. a.setMaterial(redMat);
  165. } else {
  166. // Deactivate led
  167. a.setColoringAttributes(whiteColor);
  168. a.setMaterial(whiteMat);
  169. }
  170. leds[x][y][z].setAppearance(a);
  171. }
  172. }
  173. }
  174. }
  175. }