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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  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.744, 0.0237, -0.66756, -0.34);
  74. mat.setRow(1, 0.136, -0.9837, 0.117, 3.24);
  75. mat.setRow(2, -0.6536, -0.1785, -0.735, -8.32);
  76. mat.setRow(3, 0.0, 0.0, 0.0, 1.0);
  77. fullScreenMat = new Matrix4d();
  78. fullScreenMat.setRow(0, 0.744, 0.0237, -0.66756, -0.34);
  79. fullScreenMat.setRow(1, 0.136, -0.9837, 0.117, 3.24);
  80. fullScreenMat.setRow(2, -0.6536, -0.1785, -0.735, -18.0);
  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. transGroup.setCapability(Node.ALLOW_BOUNDS_READ);
  87. transGroup.setCapability(Node.ALLOW_AUTO_COMPUTE_BOUNDS_READ);
  88. ViewingPlatform viewingPlatform = new ViewingPlatform();
  89. Viewer viewer = new Viewer(canvas);
  90. universe = new SimpleUniverse(viewingPlatform, viewer);
  91. BoundingBox boundBox = new BoundingBox(new Point3d(-5.0, -5.0, -5.0),
  92. new Point3d(13.0, 13.0, 13.0));
  93. // roration with left mouse button
  94. MouseRotate behaviour = new MouseRotate(transGroup);
  95. behaviour.setSchedulingBounds(boundBox);
  96. transGroup.addChild(behaviour);
  97. // zoom with middle mouse button
  98. MouseZoom beh2 = new MouseZoom(transGroup);
  99. beh2.setSchedulingBounds(boundBox);
  100. transGroup.addChild(beh2);
  101. // move with right mouse button
  102. MouseTranslate beh3 = new MouseTranslate(transGroup);
  103. beh3.setSchedulingBounds(boundBox);
  104. transGroup.addChild(beh3);
  105. group.setCapability(BranchGroup.ALLOW_CHILDREN_READ);
  106. group.setCapability(BranchGroup.ALLOW_CHILDREN_WRITE);
  107. group.setCapability(BranchGroup.ALLOW_DETACH);
  108. group.setCapability(Node.ALLOW_BOUNDS_READ);
  109. group.setCapability(Node.ALLOW_AUTO_COMPUTE_BOUNDS_READ);
  110. background = createBackground();
  111. group.addChild(background);
  112. // Add all our led sphares to the universe
  113. for (int x = 0; x < 8; x++) {
  114. for (int y = 0; y < 8; y++) {
  115. for (int z = 0; z < 8; z++) {
  116. Appearance a = new Appearance();
  117. a.setMaterial(whiteMat);
  118. a.setColoringAttributes(whiteColor);
  119. leds[x][y][z] = new Sphere(0.08f,
  120. Sphere.ENABLE_APPEARANCE_MODIFY, a);
  121. TransformGroup tg = new TransformGroup();
  122. Transform3D transform = new Transform3D();
  123. Vector3f vector = new Vector3f(x, y, z);
  124. transform.setTranslation(vector);
  125. tg.setTransform(transform);
  126. tg.setCapability(Node.ALLOW_BOUNDS_READ);
  127. tg.setCapability(Node.ALLOW_AUTO_COMPUTE_BOUNDS_READ);
  128. tg.addChild(leds[x][y][z]);
  129. transGroup.addChild(tg);
  130. drawLedFeetVertical((double) x, y + 0.5, (double) z, 0.9f, 0.01f);
  131. if (x < 7)
  132. drawLedFeetHorizontal(x + 0.5, (double) y, (double) z, 0.9f, 0.01f, 0);
  133. }
  134. }
  135. // 8 times, use x as y
  136. drawLedFeetHorizontal(0.5, (double) x, 3.5, 7.0f, 0.02f, 90);
  137. drawLedFeetHorizontal(6.5, (double) x, 3.5, 7.0f, 0.02f, 90);
  138. drawLedFeetHorizontal(3.5, (double) x, 3.5, 7.0f, 0.02f, 90);
  139. }
  140. // Add an ambient light
  141. Color3f light2Color = new Color3f(1.0f, 1.0f, 1.0f);
  142. AmbientLight light2 = new AmbientLight(light2Color);
  143. BoundingSphere bounds = new BoundingSphere(new Point3d(0.0, 0.0, 0.0),
  144. 100.0);
  145. light2.setInfluencingBounds(bounds);
  146. light2.setEnable(true);
  147. transGroup.addChild(light2);
  148. group.addChild(transGroup);
  149. universe.addBranchGraph(group); // Add group to universe
  150. // Mouse-Selectable objects
  151. pickCanvas = new PickCanvas(canvas, group);
  152. pickCanvas.setMode(PickCanvas.BOUNDS);
  153. canvas.addMouseListener(this);
  154. }
  155. /**
  156. * Listen for mouse events so the user can click on LEDs.
  157. * @param e MouseEvent generated by the user
  158. */
  159. public void mouseClicked(MouseEvent e) {
  160. pickCanvas.setShapeLocation(e);
  161. PickResult result = pickCanvas.pickClosest();
  162. if (result != null) {
  163. // User clicked near something
  164. Primitive p = (Primitive)result.getNode(PickResult.PRIMITIVE);
  165. Shape3D s = (Shape3D)result.getNode(PickResult.SHAPE3D);
  166. Cylinder c = null;
  167. if (p != null) {
  168. if (p.getClass().getName().equals("com.sun.j3d.utils.geometry.Cylinder")) {
  169. c = (Cylinder)p;
  170. }
  171. } else if (s != null) {
  172. if (s.getClass().getName().equals("com.sun.j3d.utils.geometry.Cylinder")) {
  173. c = (Cylinder)p;
  174. }
  175. }
  176. if (c != null) {
  177. /* ---> */ Bounds b = c.getBounds();
  178. // CapabilityNotSetException: no capability to read user bounds
  179. for (int x = 0; x < 8; x++) {
  180. for (int y = 0; y < 8; y++) {
  181. for (int z = 0; z < 8; z++) {
  182. if (b.equals(leds[x][y][z].getBounds())) {
  183. // Clicked led found!
  184. parentFrame.toggleLED(x, y, z);
  185. }
  186. }
  187. }
  188. }
  189. }
  190. }
  191. }
  192. private void drawLedFeetVertical(double x, double y, double z,
  193. float length, float rad) {
  194. // draw Feet going down
  195. Appearance feetApp = new Appearance();
  196. feetApp.setMaterial(whiteMat);
  197. feetApp.setColoringAttributes(whiteColor);
  198. Cylinder c = new Cylinder(rad, length, feetApp);
  199. TransformGroup tg = new TransformGroup();
  200. Transform3D transform = new Transform3D();
  201. Vector3d vector = new Vector3d(x, y, z);
  202. transform.setTranslation(vector);
  203. tg.setTransform(transform);
  204. tg.addChild(c);
  205. transGroup.addChild(tg);
  206. }
  207. private void drawLedFeetHorizontal(double x, double y, double z,
  208. float length, float rad, int deg) {
  209. // draw Feet going down
  210. Appearance feetApp = new Appearance();
  211. feetApp.setMaterial(whiteMat);
  212. feetApp.setColoringAttributes(whiteColor);
  213. Cylinder c = new Cylinder(rad, length, feetApp);
  214. TransformGroup tg = new TransformGroup();
  215. Transform3D transform = new Transform3D();
  216. Vector3d vector = new Vector3d(x, y, z);
  217. transform.rotZ(Math.toRadians(90));
  218. if (deg != 0)
  219. transform.rotX(Math.toRadians(deg));
  220. transform.setTranslation(vector);
  221. tg.setTransform(transform);
  222. tg.addChild(c);
  223. transGroup.addChild(tg);
  224. }
  225. /**
  226. * Rotate the cube back to its initial position.
  227. */
  228. public void resetView() {
  229. Matrix4d mat = new Matrix4d();
  230. mat.setRow(0, 0.744, 0.0237, -0.66756, -0.34);
  231. mat.setRow(1, 0.136, -0.9837, 0.117, 3.24);
  232. mat.setRow(2, -0.6536, -0.1785, -0.735, -8.32);
  233. mat.setRow(3, 0.0, 0.0, 0.0, 1.0);
  234. trans3D.set(mat);
  235. transGroup.setTransform(trans3D);
  236. }
  237. /**
  238. * Prints the translation matrix that is changed by moving/rotating the 3D
  239. * Cube with your mouse.
  240. */
  241. public void printTranslationData() {
  242. Matrix4d mat = new Matrix4d();
  243. Transform3D t = new Transform3D();
  244. transGroup.getTransform(t);
  245. t.get(mat);
  246. String s = mat.toString();
  247. System.out.println(s.replaceAll(", ", "\t"));
  248. }
  249. /**
  250. * Sets the data that is displayed by the LEDs
  251. *
  252. * @param data 64 byte array with the data (8 bits/LEDs per byte)
  253. */
  254. public void setData(short[] data) {
  255. for (int y = 0; y < 8; y++) {
  256. for (int z = 0; z < 8; z++) {
  257. for (int x = 0; x < 8; x++) {
  258. Appearance a = new Appearance();
  259. if ((data[y + (z * 8)] & (1 << x)) != 0) {
  260. // Activate led
  261. a.setColoringAttributes(redColor);
  262. a.setMaterial(redMat);
  263. } else {
  264. // Deactivate led
  265. a.setColoringAttributes(whiteColor);
  266. a.setMaterial(whiteMat);
  267. }
  268. leds[x][y][z].setAppearance(a);
  269. }
  270. }
  271. }
  272. }
  273. // create nice background
  274. private Background createBackground() {
  275. Background bg = new Background(0.0f, 0.0f, 1.0f);
  276. int radius = canvas.getWidth();
  277. bg.setApplicationBounds(new BoundingSphere(new Point3d(0.0, 0.0, 0.0), radius));
  278. return bg;
  279. }
  280. /**
  281. * Create new background to reflect changed Canvas size.
  282. */
  283. private void toggleFullscreen() {
  284. group.detach();
  285. if(group.indexOfChild(background) != -1){
  286. group.removeChild(background);
  287. }
  288. background = createBackground();
  289. group.addChild(background);
  290. universe.addBranchGraph(group);
  291. }
  292. /**
  293. * Create new background and adjust view.
  294. */
  295. public void enterFullscreen() {
  296. toggleFullscreen();
  297. trans3D.set(fullScreenMat);
  298. transGroup.setTransform(trans3D);
  299. }
  300. /**
  301. * Create new background and adjust view.
  302. */
  303. public void leaveFullscreen() {
  304. toggleFullscreen();
  305. trans3D.set(mat);
  306. transGroup.setTransform(trans3D);
  307. }
  308. }