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

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