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 8.3KB

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