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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  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. import com.sun.j3d.utils.picking.*;
  32. import java.awt.event.*;
  33. /**
  34. * This class is responsible for displaying the 3D View of our Cube.
  35. *
  36. * @author Thomas Buck
  37. * @author Max Nuding
  38. * @author Felix Bäder
  39. * @version 1.0
  40. */
  41. public class Led3D extends MouseAdapter {
  42. private Canvas3D canvas = null;
  43. private PickCanvas pickCanvas = null;
  44. private SimpleUniverse universe = null;
  45. private BranchGroup group = null;
  46. private Transform3D trans3D = null;
  47. private TransformGroup transGroup = null;
  48. private Matrix4d mat = null;
  49. private Matrix4d fullScreenMat = null;
  50. private Frame parentFrame = null;
  51. private Sphere[][][] leds = new Sphere[8][8][8];
  52. private static ColoringAttributes redColor = new ColoringAttributes(
  53. new Color3f(1.0f, 0.0f, 0.0f), ColoringAttributes.FASTEST);
  54. private static ColoringAttributes whiteColor = new ColoringAttributes(
  55. new Color3f(0.2f, 0.2f, 0.2f), ColoringAttributes.FASTEST);
  56. private static Material whiteMat = new Material(new Color3f(0.2f, 0.2f,
  57. 0.2f), new Color3f(0.0f, 0.0f, 0.0f),
  58. new Color3f(0.2f, 0.2f, 0.2f), new Color3f(0.2f, 0.2f, 0.2f), 64.0f);
  59. private static Material redMat = new Material(
  60. new Color3f(1.0f, 0.0f, 0.0f), new Color3f(1.0f, 0.0f, 0.0f),
  61. new Color3f(1.0f, 0.0f, 0.0f), new Color3f(1.0f, 0.0f, 0.0f), 64.0f);
  62. private Background background;
  63. /**
  64. * @param canv The Canvas3D we render our cube in
  65. */
  66. public Led3D(Canvas3D canv, Frame f) {
  67. canvas = canv;
  68. parentFrame = f;
  69. group = new BranchGroup();
  70. // Position viewer, so we are looking at object
  71. trans3D = new Transform3D();
  72. mat = new Matrix4d();
  73. mat.setRow(0, 0.7597, -0.0204, 0.64926, 0.56);
  74. mat.setRow(1, -0.08, -0.995, 0.061, 0.02);
  75. mat.setRow(2, 0.64473, -0.09786, -0.758, -14.68);
  76. mat.setRow(3, 0.0, 0.0, 0.0, 1.0);
  77. fullScreenMat = new Matrix4d();
  78. fullScreenMat.setRow(0, 0.7597, -0.0204, 0.64926, 0.56);
  79. fullScreenMat.setRow(1, -0.08, -0.995, 0.061, 0.02);
  80. fullScreenMat.setRow(2, 0.64473, -0.09786, -0.758, -14.68);
  81. fullScreenMat.setRow(3, 0.0, 0.0, 0.0, 1.0);
  82. trans3D.set(mat);
  83. transGroup = new TransformGroup(trans3D);
  84. transGroup.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
  85. transGroup.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
  86. ViewingPlatform viewingPlatform = new ViewingPlatform();
  87. Viewer viewer = new Viewer(canvas);
  88. universe = new SimpleUniverse(viewingPlatform, viewer);
  89. BoundingBox boundBox = new BoundingBox(new Point3d(-5.0, -5.0, -5.0),
  90. new Point3d(13.0, 13.0, 13.0));
  91. // roration with left mouse button
  92. MouseRotate behaviour = new MouseRotate(transGroup);
  93. behaviour.setSchedulingBounds(boundBox);
  94. transGroup.addChild(behaviour);
  95. // zoom with middle mouse button
  96. MouseZoom beh2 = new MouseZoom(transGroup);
  97. beh2.setSchedulingBounds(boundBox);
  98. transGroup.addChild(beh2);
  99. // move with right mouse button
  100. MouseTranslate beh3 = new MouseTranslate(transGroup);
  101. beh3.setSchedulingBounds(boundBox);
  102. transGroup.addChild(beh3);
  103. group.setCapability(BranchGroup.ALLOW_CHILDREN_READ);
  104. group.setCapability(BranchGroup.ALLOW_CHILDREN_WRITE);
  105. group.setCapability(BranchGroup.ALLOW_DETACH);
  106. background = createBackground();
  107. group.addChild(background);
  108. // Add all our led sphares to the universe
  109. for (int x = 0; x < 8; x++) {
  110. for (int y = 0; y < 8; y++) {
  111. for (int z = 0; z < 8; z++) {
  112. Appearance a = new Appearance();
  113. a.setMaterial(whiteMat);
  114. a.setColoringAttributes(whiteColor);
  115. leds[x][y][z] = new Sphere(0.08f,
  116. Sphere.ENABLE_APPEARANCE_MODIFY, a);
  117. TransformGroup tg = new TransformGroup();
  118. Transform3D transform = new Transform3D();
  119. Vector3f vector = new Vector3f(x - 4, y - 4, z - 4);
  120. transform.setTranslation(vector);
  121. tg.setTransform(transform);
  122. tg.addChild(leds[x][y][z]);
  123. transGroup.addChild(tg);
  124. drawLedFeetVertical((double) x - 4, y - 3.5, (double) z-4, 0.9f, 0.01f);
  125. if (x < 7)
  126. drawLedFeetHorizontal(x - 3.5, (double) y - 4, (double) z - 4, 0.9f, 0.01f, 0);
  127. }
  128. }
  129. // 8 times, use x as y
  130. drawLedFeetHorizontal(-3.5, (double) x-4, 3.5, 7.0f, 0.02f, 90);
  131. drawLedFeetHorizontal(2.5, (double) x-4, 3.5, 7.0f, 0.02f, 90);
  132. drawLedFeetHorizontal(-1.5, (double) x-4, 3.5, 7.0f, 0.02f, 90);
  133. }
  134. // Add an ambient light
  135. Color3f light2Color = new Color3f(1.0f, 1.0f, 1.0f);
  136. AmbientLight light2 = new AmbientLight(light2Color);
  137. BoundingSphere bounds = new BoundingSphere(new Point3d(0.0, 0.0, 0.0),
  138. 100.0);
  139. light2.setInfluencingBounds(bounds);
  140. light2.setEnable(true);
  141. transGroup.addChild(light2);
  142. group.addChild(transGroup);
  143. universe.addBranchGraph(group); // Add group to universe
  144. // Mouse-Selectable objects
  145. pickCanvas = new PickCanvas(canvas, group);
  146. pickCanvas.setMode(PickCanvas.BOUNDS);
  147. canvas.addMouseListener(this);
  148. }
  149. /**
  150. * Listen for mouse events so the user can click on LEDs.
  151. * @param e MouseEvent generated by the user
  152. */
  153. public void mouseClicked(MouseEvent e) {
  154. pickCanvas.setShapeLocation(e);
  155. PickResult result = pickCanvas.pickClosest();
  156. if (result != null) {
  157. // User clicked near something
  158. Primitive p = (Primitive)result.getNode(PickResult.PRIMITIVE);
  159. if (p != null) {
  160. // p is now a Primitive that the user clicked
  161. if (p.getClass().getName().equals("com.sun.j3d.utils.geometry.Sphere")) {
  162. // p is a Cylinder. Our LEDs are Spheres, so p.equals(led[x][y][z]) does not find anything...
  163. for (int x = 0; x < 8; x++) {
  164. for (int y = 0; y < 8; y++) {
  165. for (int z = 0; z < 8; z++) {
  166. if (p.equals(leds[x][y][z])) {
  167. // Clicked led found!
  168. System.out.println("Clicked LED found: " + x + " " + y + " " + z);
  169. parentFrame.toggleLED(x, y, z);
  170. break;
  171. } else if ((x == 7) && (y == 7) && (z == 7)) {
  172. System.out.println("Clicked, but LED not found!");
  173. }
  174. }
  175. }
  176. }
  177. } else {
  178. System.out.println("Clicked, but not a sphere. Clicked object: " + p.getClass().getName());
  179. }
  180. }
  181. } else {
  182. System.out.println("Clicked, but hit nothing");
  183. }
  184. }
  185. private void drawLedFeetVertical(double x, double y, double z,
  186. float length, float rad) {
  187. // draw Feet going down
  188. Appearance feetApp = new Appearance();
  189. feetApp.setMaterial(whiteMat);
  190. feetApp.setColoringAttributes(whiteColor);
  191. Cylinder c = new Cylinder(rad, length, feetApp);
  192. TransformGroup tg = new TransformGroup();
  193. Transform3D transform = new Transform3D();
  194. Vector3d vector = new Vector3d(x, y, z);
  195. transform.setTranslation(vector);
  196. tg.setTransform(transform);
  197. tg.addChild(c);
  198. transGroup.addChild(tg);
  199. }
  200. private void drawLedFeetHorizontal(double x, double y, double z,
  201. float length, float rad, int deg) {
  202. // draw Feet going down
  203. Appearance feetApp = new Appearance();
  204. feetApp.setMaterial(whiteMat);
  205. feetApp.setColoringAttributes(whiteColor);
  206. Cylinder c = new Cylinder(rad, length, feetApp);
  207. TransformGroup tg = new TransformGroup();
  208. Transform3D transform = new Transform3D();
  209. Vector3d vector = new Vector3d(x, y, z);
  210. transform.rotZ(Math.toRadians(90));
  211. if (deg != 0)
  212. transform.rotX(Math.toRadians(deg));
  213. transform.setTranslation(vector);
  214. tg.setTransform(transform);
  215. tg.addChild(c);
  216. transGroup.addChild(tg);
  217. }
  218. /**
  219. * Rotate the cube back to its initial position.
  220. */
  221. public void resetView() {
  222. Matrix4d mat = new Matrix4d();
  223. mat.setRow(0, 0.744, 0.0237, -0.66756, -0.34);
  224. mat.setRow(1, 0.136, -0.9837, 0.117, 3.24);
  225. mat.setRow(2, -0.6536, -0.1785, -0.735, -8.32);
  226. mat.setRow(3, 0.0, 0.0, 0.0, 1.0);
  227. trans3D.set(mat);
  228. transGroup.setTransform(trans3D);
  229. }
  230. /**
  231. * Prints the translation matrix that is changed by moving/rotating the 3D
  232. * Cube with your mouse.
  233. */
  234. public void printTranslationData() {
  235. Matrix4d mat = new Matrix4d();
  236. Transform3D t = new Transform3D();
  237. transGroup.getTransform(t);
  238. t.get(mat);
  239. String s = mat.toString();
  240. System.out.println(s.replaceAll(", ", "\t"));
  241. }
  242. /**
  243. * Sets the data that is displayed by the LEDs
  244. *
  245. * @param data 64 byte array with the data (8 bits/LEDs per byte)
  246. */
  247. public void setData(short[] data) {
  248. for (int y = 0; y < 8; y++) {
  249. for (int z = 0; z < 8; z++) {
  250. for (int x = 0; x < 8; x++) {
  251. Appearance a = new Appearance();
  252. if ((data[y + (z * 8)] & (1 << x)) != 0) {
  253. // Activate led
  254. a.setColoringAttributes(redColor);
  255. a.setMaterial(redMat);
  256. } else {
  257. // Deactivate led
  258. a.setColoringAttributes(whiteColor);
  259. a.setMaterial(whiteMat);
  260. }
  261. leds[x][y][z].setAppearance(a);
  262. }
  263. }
  264. }
  265. }
  266. // create nice background
  267. private Background createBackground() {
  268. Background bg = new Background(0.0f, 0.0f, 1.0f);
  269. int radius = canvas.getWidth();
  270. bg.setApplicationBounds(new BoundingSphere(new Point3d(0.0, 0.0, 0.0), radius));
  271. return bg;
  272. }
  273. /**
  274. * Create new background to reflect changed Canvas size.
  275. */
  276. private void toggleFullscreen() {
  277. group.detach();
  278. if(group.indexOfChild(background) != -1){
  279. group.removeChild(background);
  280. }
  281. background = createBackground();
  282. group.addChild(background);
  283. universe.addBranchGraph(group);
  284. }
  285. /**
  286. * Create new background and adjust view.
  287. */
  288. public void enterFullscreen() {
  289. toggleFullscreen();
  290. trans3D.set(fullScreenMat);
  291. transGroup.setTransform(trans3D);
  292. }
  293. /**
  294. * Create new background and adjust view.
  295. */
  296. public void leaveFullscreen() {
  297. toggleFullscreen();
  298. trans3D.set(mat);
  299. transGroup.setTransform(trans3D);
  300. }
  301. }