Simple single-color 8x8x8 LED Cube with AVRs
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

Led3D.java 7.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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. if (x < 7)
  101. drawLedFeetHorizontal(x + 0.5, (double)y, (double)z, 0.9f, 0.01f, 0);
  102. }
  103. }
  104. // 8 times, use x as y
  105. drawLedFeetHorizontal(0.5, (double)x, 3.5, 7.0f, 0.02f, 90);
  106. drawLedFeetHorizontal(6.5, (double)x, 3.5, 7.0f, 0.02f, 90);
  107. drawLedFeetHorizontal(3.5, (double)x, 3.5, 7.0f, 0.02f, 90);
  108. }
  109. // Add an ambient light
  110. Color3f light2Color = new Color3f(1.0f, 1.0f, 1.0f);
  111. AmbientLight light2 = new AmbientLight(light2Color);
  112. BoundingSphere bounds = new BoundingSphere(new Point3d(0.0, 0.0, 0.0), 100.0);
  113. light2.setInfluencingBounds(bounds);
  114. light2.setEnable(true);
  115. transGroup.addChild(light2);
  116. group.addChild(transGroup);
  117. universe.addBranchGraph(group); // Add group to universe
  118. }
  119. private void drawLedFeetVertical(double x, double y, double z, float length, float rad) {
  120. // draw Feet going down
  121. Appearance feetApp = new Appearance();
  122. feetApp.setMaterial(whiteMat);
  123. feetApp.setColoringAttributes(whiteColor);
  124. Cylinder c = new Cylinder(rad, length, feetApp);
  125. TransformGroup tg = new TransformGroup();
  126. Transform3D transform = new Transform3D();
  127. Vector3d vector = new Vector3d(x, y, z);
  128. transform.setTranslation(vector);
  129. tg.setTransform(transform);
  130. tg.addChild(c);
  131. transGroup.addChild(tg);
  132. }
  133. private void drawLedFeetHorizontal(double x, double y, double z, float length, float rad, int deg) {
  134. // draw Feet going down
  135. Appearance feetApp = new Appearance();
  136. feetApp.setMaterial(whiteMat);
  137. feetApp.setColoringAttributes(whiteColor);
  138. Cylinder c = new Cylinder(rad, length, feetApp);
  139. TransformGroup tg = new TransformGroup();
  140. Transform3D transform = new Transform3D();
  141. Vector3d vector = new Vector3d(x, y, z);
  142. transform.rotZ(Math.toRadians(90));
  143. if (deg != 0)
  144. transform.rotX(Math.toRadians(deg));
  145. transform.setTranslation(vector);
  146. tg.setTransform(transform);
  147. tg.addChild(c);
  148. transGroup.addChild(tg);
  149. }
  150. /**
  151. * Rotate the cube back to its initial position.
  152. */
  153. public void resetView() {
  154. Matrix4d mat = new Matrix4d();
  155. mat.setRow(0, 0.744, 0.0237, -0.66756, -0.34);
  156. mat.setRow(1, 0.136, -0.9837, 0.117, 3.24);
  157. mat.setRow(2, -0.6536, -0.1785, -0.735, -8.32);
  158. mat.setRow(3, 0.0, 0.0, 0.0, 1.0);
  159. trans3D.set(mat);
  160. transGroup.setTransform(trans3D);
  161. }
  162. /**
  163. * Prints the translation matrix that is changed by moving/rotating the 3D Cube with your mouse.
  164. */
  165. public void printTranslationData() {
  166. Matrix4d mat = new Matrix4d();
  167. Transform3D t = new Transform3D();
  168. transGroup.getTransform(t);
  169. t.get(mat);
  170. String s = mat.toString();
  171. System.out.println(s.replaceAll(", ", "\t"));
  172. }
  173. /**
  174. * Sets the data that is displayed by the LEDs
  175. * @param data 64 byte array with the data (8 bits/LEDs per byte)
  176. */
  177. public void setData(short[] data) {
  178. for (int y = 0; y < 8; y++) {
  179. for (int z = 0; z < 8; z++) {
  180. for (int x = 0; x < 8; x++) {
  181. Appearance a = new Appearance();
  182. if ((data[y + (z * 8)] & (1 << x)) != 0) {
  183. // Activate led
  184. a.setColoringAttributes(redColor);
  185. a.setMaterial(redMat);
  186. } else {
  187. // Deactivate led
  188. a.setColoringAttributes(whiteColor);
  189. a.setMaterial(whiteMat);
  190. }
  191. leds[x][y][z].setAppearance(a);
  192. }
  193. }
  194. }
  195. }
  196. // create nice background
  197. private Background createBackground() {
  198. ImageComponent2D image = new TextureLoader(getClass().getResource("bg.png"), null).getImage();
  199. Background bg = new Background(image);
  200. bg.setApplicationBounds(new BoundingSphere(new Point3d(0.0, 0.0, 0.0), 100.0));
  201. return bg;
  202. }
  203. }