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

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