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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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. import java.awt.Color;
  30. import com.sun.j3d.utils.image.TextureLoader;
  31. /**
  32. * This class is responsible for displaying the 3D View of our Cube.
  33. * @author Thomas Buck
  34. * @author Max Nuding
  35. * @author Felix Bäder
  36. * @version 1.0
  37. */
  38. public class Led3D {
  39. private Canvas3D canvas = null;
  40. private SimpleUniverse universe = null;
  41. private BranchGroup group = null;
  42. private Transform3D trans3D = null;
  43. private BranchGroup inBetween = null;
  44. private TransformGroup transGroup = null;
  45. private Sphere[][][] leds = new Sphere[8][8][8];
  46. private static ColoringAttributes redColor = new ColoringAttributes(new Color3f(1.0f, 0.0f, 0.0f), ColoringAttributes.FASTEST);
  47. private static ColoringAttributes whiteColor = new ColoringAttributes(new Color3f(0.2f, 0.2f, 0.2f), ColoringAttributes.FASTEST);
  48. 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);
  49. 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);
  50. /**
  51. * @param canv The Canvas3D we render our cube in
  52. */
  53. public Led3D(Canvas3D canv) {
  54. canvas = canv;
  55. group = new BranchGroup();
  56. // Position viewer, so we are looking at object
  57. trans3D = new Transform3D();
  58. Matrix4d mat = new Matrix4d();
  59. mat.setRow(0, 0.744, 0.0237, -0.66756, -0.34);
  60. mat.setRow(1, 0.136, -0.9837, 0.117, 3.24);
  61. mat.setRow(2, -0.6536, -0.1785, -0.735, -8.32);
  62. mat.setRow(3, 0.0, 0.0, 0.0, 1.0);
  63. trans3D.set(mat);
  64. transGroup = new TransformGroup(trans3D);
  65. transGroup.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
  66. transGroup.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
  67. ViewingPlatform viewingPlatform = new ViewingPlatform();
  68. Viewer viewer = new Viewer(canvas);
  69. universe = new SimpleUniverse(viewingPlatform, viewer);
  70. BoundingBox boundBox = new BoundingBox(new Point3d(-5.0, -5.0, -5.0), new Point3d(13.0, 13.0, 13.0));
  71. // roration with left mouse button
  72. MouseRotate behaviour = new MouseRotate(transGroup);
  73. behaviour.setSchedulingBounds(boundBox);
  74. transGroup.addChild(behaviour);
  75. // zoom with middle mouse button
  76. MouseZoom beh2 = new MouseZoom(transGroup);
  77. beh2.setSchedulingBounds(boundBox);
  78. transGroup.addChild(beh2);
  79. // move with right mouse button
  80. MouseTranslate beh3 = new MouseTranslate(transGroup);
  81. beh3.setSchedulingBounds(boundBox);
  82. transGroup.addChild(beh3);
  83. group.addChild(createBackground());
  84. // Add all our led sphares to the universe
  85. for (int x = 0; x < 8; x++) {
  86. for (int y = 0; y < 8; y++) {
  87. for (int z = 0; z < 8; z++) {
  88. Appearance a = new Appearance();
  89. a.setMaterial(whiteMat);
  90. a.setColoringAttributes(whiteColor);
  91. leds[x][y][z] = new Sphere(0.08f, Sphere.ENABLE_APPEARANCE_MODIFY, a);
  92. TransformGroup tg = new TransformGroup();
  93. Transform3D transform = new Transform3D();
  94. Vector3f vector = new Vector3f(x, y, z);
  95. transform.setTranslation(vector);
  96. tg.setTransform(transform);
  97. tg.addChild(leds[x][y][z]);
  98. transGroup.addChild(tg);
  99. drawLedFeetVertical((double)x, y + 0.5, (double)z, 0.9f, 0.01f);
  100. drawLedFeetHorizontal(x + 0.5, (double)y, (double)z, 0.9f, 0.01f, 0);
  101. }
  102. }
  103. // 8 times, use x as y
  104. drawLedFeetHorizontal(0.5, (double)x, 3.5, 7.0f, 0.02f, 90);
  105. drawLedFeetHorizontal(6.5, (double)x, 3.5, 7.0f, 0.02f, 90);
  106. drawLedFeetHorizontal(3.5, (double)x, 3.5, 7.0f, 0.02f, 90);
  107. }
  108. // Add an ambient light
  109. Color3f light2Color = new Color3f(1.0f, 1.0f, 1.0f);
  110. AmbientLight light2 = new AmbientLight(light2Color);
  111. BoundingSphere bounds = new BoundingSphere(new Point3d(0.0, 0.0, 0.0), 100.0);
  112. light2.setInfluencingBounds(bounds);
  113. light2.setEnable(true);
  114. transGroup.addChild(light2);
  115. group.addChild(transGroup);
  116. universe.addBranchGraph(group); // Add group to universe
  117. }
  118. private void drawLedFeetVertical(double x, double y, double z, float length, float rad) {
  119. // draw Feet going down
  120. Appearance feetApp = new Appearance();
  121. feetApp.setMaterial(whiteMat);
  122. feetApp.setColoringAttributes(whiteColor);
  123. Cylinder c = new Cylinder(rad, length, feetApp);
  124. TransformGroup tg = new TransformGroup();
  125. Transform3D transform = new Transform3D();
  126. Vector3d vector = new Vector3d(x, y, z);
  127. transform.setTranslation(vector);
  128. tg.setTransform(transform);
  129. tg.addChild(c);
  130. transGroup.addChild(tg);
  131. }
  132. private void drawLedFeetHorizontal(double x, double y, double z, float length, float rad, int deg) {
  133. // draw Feet going down
  134. Appearance feetApp = new Appearance();
  135. feetApp.setMaterial(whiteMat);
  136. feetApp.setColoringAttributes(whiteColor);
  137. Cylinder c = new Cylinder(rad, length, feetApp);
  138. TransformGroup tg = new TransformGroup();
  139. Transform3D transform = new Transform3D();
  140. Vector3d vector = new Vector3d(x, y, z);
  141. transform.rotZ(Math.toRadians(90));
  142. if (deg != 0)
  143. transform.rotX(Math.toRadians(deg));
  144. transform.setTranslation(vector);
  145. tg.setTransform(transform);
  146. tg.addChild(c);
  147. transGroup.addChild(tg);
  148. }
  149. /**
  150. * Prints the translation matrix that is changed by moving/rotating the 3D Cube with your mouse.
  151. */
  152. public void printTranslationData() {
  153. Matrix4d mat = new Matrix4d();
  154. Transform3D t = new Transform3D();
  155. transGroup.getTransform(t);
  156. t.get(mat);
  157. String s = mat.toString();
  158. System.out.println(s.replaceAll(", ", "\t"));
  159. }
  160. /**
  161. * Sets the data that is displayed by the LEDs
  162. * @param data 64 byte array with the data (8 bits/LEDs per byte)
  163. */
  164. public void setData(short[] data) {
  165. for (int y = 0; y < 8; y++) {
  166. for (int z = 0; z < 8; z++) {
  167. for (int x = 0; x < 8; x++) {
  168. Appearance a = new Appearance();
  169. if ((data[y + (z * 8)] & (1 << x)) != 0) {
  170. // Activate led
  171. a.setColoringAttributes(redColor);
  172. a.setMaterial(redMat);
  173. } else {
  174. // Deactivate led
  175. a.setColoringAttributes(whiteColor);
  176. a.setMaterial(whiteMat);
  177. }
  178. leds[x][y][z].setAppearance(a);
  179. }
  180. }
  181. }
  182. }
  183. // create nice background
  184. private Background createBackground() {
  185. ImageComponent2D image = new TextureLoader(getClass().getResource("bg.png"), null).getImage();
  186. Background bg = new Background(image);
  187. bg.setApplicationBounds(new BoundingSphere(new Point3d(0.0, 0.0, 0.0), 100.0));
  188. return bg;
  189. }
  190. }