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.

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