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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  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. for(int i = 0; i > -8; i--){
  131. drawLedFeetHorizontal(i+3, (double) x-4, -0.5, 7.0f, 0.02f, 90);
  132. }
  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. x = 8;
  171. y = 8;
  172. z = 8;
  173. }
  174. }
  175. }
  176. }
  177. } else {
  178. System.out.println("Clicked, but not a sphere. Clicked object: " + p.getClass().getName());
  179. if(p.getClass().getName().equals("com.sun.j3d.utils.geometry.Cylinder")){
  180. }
  181. }
  182. }
  183. } else {
  184. System.out.println("Clicked, but hit nothing");
  185. }
  186. }
  187. private void drawLedFeetVertical(double x, double y, double z,
  188. float length, float rad) {
  189. // draw Feet going down
  190. Appearance feetApp = new Appearance();
  191. feetApp.setMaterial(whiteMat);
  192. feetApp.setColoringAttributes(whiteColor);
  193. Cylinder c = new Cylinder(rad, length, feetApp);
  194. TransformGroup tg = new TransformGroup();
  195. Transform3D transform = new Transform3D();
  196. Vector3d vector = new Vector3d(x, y, z);
  197. transform.setTranslation(vector);
  198. tg.setTransform(transform);
  199. tg.addChild(c);
  200. transGroup.addChild(tg);
  201. }
  202. private void drawLedFeetHorizontal(double x, double y, double z,
  203. float length, float rad, int deg) {
  204. // draw Feet going down
  205. Appearance feetApp = new Appearance();
  206. feetApp.setMaterial(whiteMat);
  207. feetApp.setColoringAttributes(whiteColor);
  208. Cylinder c = new Cylinder(rad, length, feetApp);
  209. TransformGroup tg = new TransformGroup();
  210. Transform3D transform = new Transform3D();
  211. Vector3d vector = new Vector3d(x, y, z);
  212. transform.rotZ(Math.toRadians(90));
  213. if (deg != 0)
  214. transform.rotX(Math.toRadians(deg));
  215. transform.setTranslation(vector);
  216. tg.setTransform(transform);
  217. tg.addChild(c);
  218. transGroup.addChild(tg);
  219. }
  220. /**
  221. * Rotate the cube back to its initial position.
  222. */
  223. public void resetView() {
  224. Matrix4d mat = new Matrix4d();
  225. mat.setRow(0, 0.744, 0.0237, -0.66756, -0.34);
  226. mat.setRow(1, 0.136, -0.9837, 0.117, 3.24);
  227. mat.setRow(2, -0.6536, -0.1785, -0.735, -8.32);
  228. mat.setRow(3, 0.0, 0.0, 0.0, 1.0);
  229. trans3D.set(mat);
  230. transGroup.setTransform(trans3D);
  231. }
  232. /**
  233. * Prints the translation matrix that is changed by moving/rotating the 3D
  234. * Cube with your mouse.
  235. */
  236. public void printTranslationData() {
  237. Matrix4d mat = new Matrix4d();
  238. Transform3D t = new Transform3D();
  239. transGroup.getTransform(t);
  240. t.get(mat);
  241. String s = mat.toString();
  242. System.out.println(s.replaceAll(", ", "\t"));
  243. }
  244. /**
  245. * Sets the data that is displayed by the LEDs
  246. *
  247. * @param data 64 byte array with the data (8 bits/LEDs per byte)
  248. */
  249. public void setData(short[] data) {
  250. for (int y = 0; y < 8; y++) {
  251. for (int z = 0; z < 8; z++) {
  252. for (int x = 0; x < 8; x++) {
  253. Appearance a = new Appearance();
  254. if ((data[y + (z * 8)] & (1 << x)) != 0) {
  255. // Activate led
  256. a.setColoringAttributes(redColor);
  257. a.setMaterial(redMat);
  258. } else {
  259. // Deactivate led
  260. a.setColoringAttributes(whiteColor);
  261. a.setMaterial(whiteMat);
  262. }
  263. leds[x][y][z].setAppearance(a);
  264. }
  265. }
  266. }
  267. }
  268. // create nice background
  269. private Background createBackground() {
  270. Background bg = new Background(0.0f, 0.0f, 1.0f);
  271. int radius = canvas.getWidth();
  272. bg.setApplicationBounds(new BoundingSphere(new Point3d(0.0, 0.0, 0.0), radius));
  273. return bg;
  274. }
  275. /**
  276. * Create new background to reflect changed Canvas size.
  277. */
  278. private void toggleFullscreen() {
  279. group.detach();
  280. if(group.indexOfChild(background) != -1){
  281. group.removeChild(background);
  282. }
  283. background = createBackground();
  284. group.addChild(background);
  285. universe.addBranchGraph(group);
  286. }
  287. /**
  288. * Create new background and adjust view.
  289. */
  290. public void enterFullscreen() {
  291. toggleFullscreen();
  292. trans3D.set(fullScreenMat);
  293. transGroup.setTransform(trans3D);
  294. }
  295. /**
  296. * Create new background and adjust view.
  297. */
  298. public void leaveFullscreen() {
  299. toggleFullscreen();
  300. trans3D.set(mat);
  301. transGroup.setTransform(trans3D);
  302. }
  303. }