Simple single-color 8x8x8 LED Cube with AVRs
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

Led3D.java 6.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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. Appearance a = new Appearance();
  106. a.setMaterial(whiteMat);
  107. a.setColoringAttributes(whiteColor);
  108. leds[x][y][z] = new Sphere(0.05f, Sphere.ENABLE_APPEARANCE_MODIFY, a);
  109. TransformGroup tg = new TransformGroup();
  110. tg.setCapability(TransformGroup.ALLOW_CHILDREN_EXTEND);
  111. tg.setCapability(TransformGroup.ALLOW_CHILDREN_WRITE);
  112. tg.setCapability(TransformGroup.ALLOW_CHILDREN_READ);
  113. Transform3D transform = new Transform3D();
  114. Vector3f vector = new Vector3f(x, y, z);
  115. transform.setTranslation(vector);
  116. tg.setTransform(transform);
  117. tg.addChild(leds[x][y][z]);
  118. BranchGroup allTheseGroupsScareMe = new BranchGroup();
  119. allTheseGroupsScareMe.setCapability(BranchGroup.ALLOW_CHILDREN_EXTEND);
  120. allTheseGroupsScareMe.setCapability(BranchGroup.ALLOW_CHILDREN_READ);
  121. allTheseGroupsScareMe.setCapability(BranchGroup.ALLOW_CHILDREN_WRITE);
  122. allTheseGroupsScareMe.addChild(tg);
  123. inBetween.addChild(allTheseGroupsScareMe);
  124. }
  125. }
  126. }
  127. // Add an ambient light
  128. Color3f light2Color = new Color3f(1.0f, 1.0f, 1.0f);
  129. AmbientLight light2 = new AmbientLight(light2Color);
  130. BoundingSphere bounds = new BoundingSphere(new Point3d(0.0, 0.0, 0.0), 100.0);
  131. light2.setInfluencingBounds(bounds);
  132. BranchGroup fffuuuuu = new BranchGroup();
  133. light2.setEnable(true);
  134. fffuuuuu.addChild(light2);
  135. inBetween.addChild(fffuuuuu);
  136. }
  137. /**
  138. * Prints the translation matrix that is changed by moving/rotating the 3D Cube with your mouse.
  139. */
  140. public void printTranslationData() {
  141. Matrix4d mat = new Matrix4d();
  142. Transform3D t = new Transform3D();
  143. transGroup.getTransform(t);
  144. t.get(mat);
  145. String s = mat.toString();
  146. System.out.println(s.replaceAll(", ", "\t"));
  147. }
  148. /**
  149. * Sets the data that is displayed by the LEDs
  150. * @param data 64 byte array with the data (8 bits/LEDs per byte)
  151. */
  152. public void setData(short[] data) {
  153. for (int y = 0; y < 8; y++) {
  154. for (int z = 0; z < 8; z++) {
  155. for (int x = 0; x < 8; x++) {
  156. Appearance a = new Appearance();
  157. if ((data[y + (z * 8)] & (1 << x)) != 0) {
  158. // Activate led
  159. a.setColoringAttributes(redColor);
  160. a.setMaterial(redMat);
  161. } else {
  162. // Deactivate led
  163. a.setColoringAttributes(whiteColor);
  164. a.setMaterial(whiteMat);
  165. }
  166. leds[x][y][z].setAppearance(a);
  167. }
  168. }
  169. }
  170. }
  171. }