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

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