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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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. /**
  32. * This class is responsible for displaying the 3D View of our Cube.
  33. *
  34. * @author Thomas Buck
  35. * @author Max Nuding
  36. * @author Felix Bäder
  37. * @version 1.0
  38. */
  39. public class Led3D {
  40. private Canvas3D canvas = null;
  41. private SimpleUniverse universe = null;
  42. private BranchGroup group = null;
  43. private Transform3D trans3D = null;
  44. private TransformGroup transGroup = null;
  45. private Matrix4d mat = null;
  46. private Matrix4d fullScreenMat = null;
  47. private Sphere[][][] leds = new Sphere[8][8][8];
  48. private static ColoringAttributes redColor = new ColoringAttributes(
  49. new Color3f(1.0f, 0.0f, 0.0f), ColoringAttributes.FASTEST);
  50. private static ColoringAttributes whiteColor = new ColoringAttributes(
  51. new Color3f(0.2f, 0.2f, 0.2f), ColoringAttributes.FASTEST);
  52. private static Material whiteMat = new Material(new Color3f(0.2f, 0.2f,
  53. 0.2f), new Color3f(0.0f, 0.0f, 0.0f),
  54. new Color3f(0.2f, 0.2f, 0.2f), new Color3f(0.2f, 0.2f, 0.2f), 64.0f);
  55. private static Material redMat = new Material(
  56. new Color3f(1.0f, 0.0f, 0.0f), new Color3f(1.0f, 0.0f, 0.0f),
  57. new Color3f(1.0f, 0.0f, 0.0f), new Color3f(1.0f, 0.0f, 0.0f), 64.0f);
  58. private Background background;
  59. /**
  60. * @param canv The Canvas3D we render our cube in
  61. */
  62. public Led3D(Canvas3D canv) {
  63. canvas = canv;
  64. group = new BranchGroup();
  65. // Position viewer, so we are looking at object
  66. trans3D = new Transform3D();
  67. mat = new Matrix4d();
  68. mat.setRow(0, 0.744, 0.0237, -0.66756, -0.34);
  69. mat.setRow(1, 0.136, -0.9837, 0.117, 3.24);
  70. mat.setRow(2, -0.6536, -0.1785, -0.735, -8.32);
  71. mat.setRow(3, 0.0, 0.0, 0.0, 1.0);
  72. fullScreenMat = new Matrix4d();
  73. fullScreenMat.setRow(0, 0.744, 0.0237, -0.66756, -0.34);
  74. fullScreenMat.setRow(1, 0.136, -0.9837, 0.117, 3.24);
  75. fullScreenMat.setRow(2, -0.6536, -0.1785, -0.735, -18.0);
  76. fullScreenMat.setRow(3, 0.0, 0.0, 0.0, 1.0);
  77. trans3D.set(mat);
  78. transGroup = new TransformGroup(trans3D);
  79. transGroup.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
  80. transGroup.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
  81. ViewingPlatform viewingPlatform = new ViewingPlatform();
  82. Viewer viewer = new Viewer(canvas);
  83. universe = new SimpleUniverse(viewingPlatform, viewer);
  84. BoundingBox boundBox = new BoundingBox(new Point3d(-5.0, -5.0, -5.0),
  85. new Point3d(13.0, 13.0, 13.0));
  86. // roration with left mouse button
  87. MouseRotate behaviour = new MouseRotate(transGroup);
  88. behaviour.setSchedulingBounds(boundBox);
  89. transGroup.addChild(behaviour);
  90. // zoom with middle mouse button
  91. MouseZoom beh2 = new MouseZoom(transGroup);
  92. beh2.setSchedulingBounds(boundBox);
  93. transGroup.addChild(beh2);
  94. // move with right mouse button
  95. MouseTranslate beh3 = new MouseTranslate(transGroup);
  96. beh3.setSchedulingBounds(boundBox);
  97. transGroup.addChild(beh3);
  98. group.setCapability(BranchGroup.ALLOW_CHILDREN_READ);
  99. group.setCapability(BranchGroup.ALLOW_CHILDREN_WRITE);
  100. group.setCapability(BranchGroup.ALLOW_DETACH);
  101. background = createBackground();
  102. group.addChild(background);
  103. // Add all our led sphares to the universe
  104. for (int x = 0; x < 8; x++) {
  105. for (int y = 0; y < 8; y++) {
  106. for (int z = 0; z < 8; z++) {
  107. Appearance a = new Appearance();
  108. a.setMaterial(whiteMat);
  109. a.setColoringAttributes(whiteColor);
  110. leds[x][y][z] = new Sphere(0.08f,
  111. Sphere.ENABLE_APPEARANCE_MODIFY, a);
  112. TransformGroup tg = new TransformGroup();
  113. Transform3D transform = new Transform3D();
  114. Vector3f vector = new Vector3f(x, y, z);
  115. transform.setTranslation(vector);
  116. tg.setTransform(transform);
  117. tg.addChild(leds[x][y][z]);
  118. transGroup.addChild(tg);
  119. drawLedFeetVertical((double) x, y + 0.5, (double) z, 0.9f,
  120. 0.01f);
  121. if (x < 7)
  122. drawLedFeetHorizontal(x + 0.5, (double) y, (double) z,
  123. 0.9f, 0.01f, 0);
  124. }
  125. }
  126. // 8 times, use x as y
  127. drawLedFeetHorizontal(0.5, (double) x, 3.5, 7.0f, 0.02f, 90);
  128. drawLedFeetHorizontal(6.5, (double) x, 3.5, 7.0f, 0.02f, 90);
  129. drawLedFeetHorizontal(3.5, (double) x, 3.5, 7.0f, 0.02f, 90);
  130. }
  131. // Add an ambient light
  132. Color3f light2Color = new Color3f(1.0f, 1.0f, 1.0f);
  133. AmbientLight light2 = new AmbientLight(light2Color);
  134. BoundingSphere bounds = new BoundingSphere(new Point3d(0.0, 0.0, 0.0),
  135. 100.0);
  136. light2.setInfluencingBounds(bounds);
  137. light2.setEnable(true);
  138. transGroup.addChild(light2);
  139. group.addChild(transGroup);
  140. universe.addBranchGraph(group); // Add group to universe
  141. }
  142. private void drawLedFeetVertical(double x, double y, double z,
  143. float length, float rad) {
  144. // draw Feet going down
  145. Appearance feetApp = new Appearance();
  146. feetApp.setMaterial(whiteMat);
  147. feetApp.setColoringAttributes(whiteColor);
  148. Cylinder c = new Cylinder(rad, length, feetApp);
  149. TransformGroup tg = new TransformGroup();
  150. Transform3D transform = new Transform3D();
  151. Vector3d vector = new Vector3d(x, y, z);
  152. transform.setTranslation(vector);
  153. tg.setTransform(transform);
  154. tg.addChild(c);
  155. transGroup.addChild(tg);
  156. }
  157. private void drawLedFeetHorizontal(double x, double y, double z,
  158. float length, float rad, int deg) {
  159. // draw Feet going down
  160. Appearance feetApp = new Appearance();
  161. feetApp.setMaterial(whiteMat);
  162. feetApp.setColoringAttributes(whiteColor);
  163. Cylinder c = new Cylinder(rad, length, feetApp);
  164. TransformGroup tg = new TransformGroup();
  165. Transform3D transform = new Transform3D();
  166. Vector3d vector = new Vector3d(x, y, z);
  167. transform.rotZ(Math.toRadians(90));
  168. if (deg != 0)
  169. transform.rotX(Math.toRadians(deg));
  170. transform.setTranslation(vector);
  171. tg.setTransform(transform);
  172. tg.addChild(c);
  173. transGroup.addChild(tg);
  174. }
  175. /**
  176. * Rotate the cube back to its initial position.
  177. */
  178. public void resetView() {
  179. Matrix4d mat = new Matrix4d();
  180. mat.setRow(0, 0.744, 0.0237, -0.66756, -0.34);
  181. mat.setRow(1, 0.136, -0.9837, 0.117, 3.24);
  182. mat.setRow(2, -0.6536, -0.1785, -0.735, -8.32);
  183. mat.setRow(3, 0.0, 0.0, 0.0, 1.0);
  184. trans3D.set(mat);
  185. transGroup.setTransform(trans3D);
  186. }
  187. /**
  188. * Prints the translation matrix that is changed by moving/rotating the 3D
  189. * Cube with your mouse.
  190. */
  191. public void printTranslationData() {
  192. Matrix4d mat = new Matrix4d();
  193. Transform3D t = new Transform3D();
  194. transGroup.getTransform(t);
  195. t.get(mat);
  196. String s = mat.toString();
  197. System.out.println(s.replaceAll(", ", "\t"));
  198. }
  199. /**
  200. * Sets the data that is displayed by the LEDs
  201. *
  202. * @param data 64 byte array with the data (8 bits/LEDs per byte)
  203. */
  204. public void setData(short[] data) {
  205. for (int y = 0; y < 8; y++) {
  206. for (int z = 0; z < 8; z++) {
  207. for (int x = 0; x < 8; x++) {
  208. Appearance a = new Appearance();
  209. if ((data[y + (z * 8)] & (1 << x)) != 0) {
  210. // Activate led
  211. a.setColoringAttributes(redColor);
  212. a.setMaterial(redMat);
  213. } else {
  214. // Deactivate led
  215. a.setColoringAttributes(whiteColor);
  216. a.setMaterial(whiteMat);
  217. }
  218. leds[x][y][z].setAppearance(a);
  219. }
  220. }
  221. }
  222. }
  223. // create nice background
  224. private Background createBackground() {
  225. Background bg = new Background(0.0f, 0.0f, 1.0f);
  226. int radius = canvas.getWidth();
  227. bg.setApplicationBounds(new BoundingSphere(new Point3d(0.0, 0.0, 0.0), radius));
  228. return bg;
  229. }
  230. /**
  231. * Create new background to reflect changed Canvas size.
  232. */
  233. private void toggleFullscreen() {
  234. group.detach();
  235. if(group.indexOfChild(background) != -1){
  236. group.removeChild(background);
  237. }
  238. background = createBackground();
  239. group.addChild(background);
  240. universe.addBranchGraph(group);
  241. }
  242. /**
  243. * Create new background and adjust view.
  244. */
  245. public void enterFullscreen() {
  246. toggleFullscreen();
  247. trans3D.set(fullScreenMat);
  248. transGroup.setTransform(trans3D);
  249. }
  250. /**
  251. * Create new background and adjust view.
  252. */
  253. public void leaveFullscreen() {
  254. toggleFullscreen();
  255. trans3D.set(mat);
  256. transGroup.setTransform(trans3D);
  257. }
  258. }