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

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