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

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