Simple single-color 8x8x8 LED Cube with AVRs
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

Led3D.java 7.6KB

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