Simple single-color 8x8x8 LED Cube with AVRs
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  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 BranchGroup group2 = null;
  47. private Transform3D trans3D = null;
  48. private TransformGroup transGroup = null;
  49. private TransformGroup feetGroup = null;
  50. private Matrix4d mat = null;
  51. private Matrix4d fullScreenMat = null;
  52. private Frame parentFrame = null;
  53. private boolean inFullscreen = false;
  54. private Sphere[][][] leds = new Sphere[8][8][8];
  55. private static ColoringAttributes redColor = new ColoringAttributes(
  56. new Color3f(1.0f, 0.0f, 0.0f), ColoringAttributes.FASTEST);
  57. private static ColoringAttributes whiteColor = new ColoringAttributes(
  58. new Color3f(0.2f, 0.2f, 0.2f), ColoringAttributes.FASTEST);
  59. private static Material whiteMat = new Material(new Color3f(0.2f, 0.2f,
  60. 0.2f), new Color3f(0.0f, 0.0f, 0.0f),
  61. new Color3f(0.2f, 0.2f, 0.2f), new Color3f(0.2f, 0.2f, 0.2f), 64.0f);
  62. private static Material redMat = new Material(
  63. new Color3f(1.0f, 0.0f, 0.0f), new Color3f(1.0f, 0.0f, 0.0f),
  64. new Color3f(1.0f, 0.0f, 0.0f), new Color3f(1.0f, 0.0f, 0.0f), 64.0f);
  65. private Background background;
  66. /**
  67. * @param canv The Canvas3D we render our cube in
  68. */
  69. public Led3D(Canvas3D canv, Frame f) {
  70. canvas = canv;
  71. parentFrame = f;
  72. group = new BranchGroup();
  73. group2 = new BranchGroup();
  74. // Position viewer, so we are looking at object
  75. trans3D = new Transform3D();
  76. mat = new Matrix4d();
  77. mat.setRow(0, 0.7597, -0.0204, 0.64926, 0.56);
  78. mat.setRow(1, -0.08, -0.995, 0.061, 0.02);
  79. mat.setRow(2, 0.64473, -0.09786, -0.758, -14.68);
  80. mat.setRow(3, 0.0, 0.0, 0.0, 1.0);
  81. fullScreenMat = new Matrix4d();
  82. fullScreenMat.setRow(0, 0.7597, -0.0204, 0.64926, 0.68);
  83. fullScreenMat.setRow(1, -0.08, -0.995, 0.061, 0.7);
  84. fullScreenMat.setRow(2, 0.64473, -0.09786, -0.758, -22.88);
  85. fullScreenMat.setRow(3, 0.0, 0.0, 0.0, 1.0);
  86. trans3D.set(mat);
  87. transGroup = new TransformGroup(trans3D);
  88. transGroup.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
  89. transGroup.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
  90. feetGroup = new TransformGroup(trans3D);
  91. feetGroup.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
  92. feetGroup.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
  93. ViewingPlatform viewingPlatform = new ViewingPlatform();
  94. Viewer viewer = new Viewer(canvas);
  95. universe = new SimpleUniverse(viewingPlatform, viewer);
  96. BoundingBox boundBox = new BoundingBox(new Point3d(-5.0, -5.0, -5.0),
  97. new Point3d(13.0, 13.0, 13.0));
  98. // roration with left mouse button
  99. MouseRotate behaviour = new MouseRotate(transGroup);
  100. behaviour.setSchedulingBounds(boundBox);
  101. transGroup.addChild(behaviour);
  102. MouseRotate feetBehaviour = new MouseRotate(feetGroup);
  103. feetBehaviour.setSchedulingBounds(boundBox);
  104. feetGroup.addChild(feetBehaviour);
  105. // zoom with middle mouse button
  106. MouseZoom beh2 = new MouseZoom(transGroup);
  107. beh2.setSchedulingBounds(boundBox);
  108. transGroup.addChild(beh2);
  109. MouseZoom feetBeh2 = new MouseZoom(feetGroup);
  110. feetBeh2.setSchedulingBounds(boundBox);
  111. feetGroup.addChild(feetBeh2);
  112. // move with right mouse button
  113. MouseTranslate beh3 = new MouseTranslate(transGroup);
  114. beh3.setSchedulingBounds(boundBox);
  115. transGroup.addChild(beh3);
  116. MouseTranslate feetBeh3 = new MouseTranslate(feetGroup);
  117. feetBeh3.setSchedulingBounds(boundBox);
  118. feetGroup.addChild(feetBeh3);
  119. group.setCapability(BranchGroup.ALLOW_CHILDREN_READ);
  120. group.setCapability(BranchGroup.ALLOW_CHILDREN_WRITE);
  121. group.setCapability(BranchGroup.ALLOW_DETACH);
  122. group2.setCapability(BranchGroup.ALLOW_CHILDREN_READ);
  123. group2.setCapability(BranchGroup.ALLOW_CHILDREN_WRITE);
  124. group2.setCapability(BranchGroup.ALLOW_DETACH);
  125. background = createBackground();
  126. group.addChild(background);
  127. // Add all our led sphares to the universe
  128. for (int x = 0; x < 8; x++) {
  129. for (int y = 0; y < 8; y++) {
  130. for (int z = 0; z < 8; z++) {
  131. Appearance a = new Appearance();
  132. a.setMaterial(whiteMat);
  133. a.setColoringAttributes(whiteColor);
  134. leds[x][y][z] = new Sphere(0.08f,
  135. Sphere.ENABLE_APPEARANCE_MODIFY, a);
  136. TransformGroup tg = new TransformGroup();
  137. Transform3D transform = new Transform3D();
  138. Vector3f vector = new Vector3f(x - 3.5f, y -3.5f, z-3.5f);
  139. transform.setTranslation(vector);
  140. tg.setTransform(transform);
  141. tg.addChild(leds[x][y][z]);
  142. transGroup.addChild(tg);
  143. drawLedFeetVertical((double) x - 3.5, y - 3, (double) z-3.5, 0.9f, 0.01f);
  144. if (x < 7)
  145. drawLedFeetHorizontal(x - 3, (double) y - 3.5, (double) z - 3.5, 0.9f, 0.01f, 0);
  146. }
  147. }
  148. // 8 times, use x as y
  149. for(int i = 0; i > -8; i--){
  150. drawLedFeetHorizontal(i+3.5, (double) x-3.5, 0, 7.0f, 0.02f, 90);
  151. }
  152. }
  153. drawLedFeetVertical(0, 0, 0, 10, 0.01f); //
  154. drawLedFeetHorizontal(0, 0, 0, 10, 0.01f, 0); // x, y, and z axis
  155. Appearance c = new Appearance();
  156. c.setMaterial(redMat);
  157. c.setColoringAttributes(redColor);
  158. Sphere center = new Sphere(0.04f, Sphere.ENABLE_APPEARANCE_MODIFY, c);
  159. TransformGroup tg = new TransformGroup();
  160. Transform3D transform = new Transform3D();
  161. Vector3f vector = new Vector3f(0, 0, 0);
  162. transform.setTranslation(vector);
  163. tg.setTransform(transform);
  164. tg.addChild(center);
  165. feetGroup.addChild(tg);
  166. // Add an ambient light
  167. Color3f light2Color = new Color3f(1.0f, 1.0f, 1.0f);
  168. AmbientLight light2 = new AmbientLight(light2Color);
  169. BoundingSphere bounds = new BoundingSphere(new Point3d(0.0, 0.0, 0.0),
  170. 100.0);
  171. light2.setInfluencingBounds(bounds);
  172. light2.setEnable(true);
  173. transGroup.addChild(light2);
  174. group.addChild(transGroup);
  175. group2.addChild(feetGroup);
  176. universe.addBranchGraph(group); // Add group to universe
  177. universe.addBranchGraph(group2);
  178. // Mouse-Selectable objects
  179. pickCanvas = new PickCanvas(canvas, group);
  180. pickCanvas.setMode(PickCanvas.BOUNDS);
  181. canvas.addMouseListener(this);
  182. }
  183. /**
  184. * Listen for mouse events so the user can click on LEDs.
  185. * @param e MouseEvent generated by the user
  186. */
  187. public void mouseClicked(MouseEvent e) {
  188. pickCanvas.setShapeLocation(e);
  189. PickResult result = pickCanvas.pickClosest();
  190. if (result != null) {
  191. // User clicked near something
  192. Primitive p = (Primitive)result.getNode(PickResult.PRIMITIVE);
  193. if (p != null) {
  194. // p is now a Primitive that the user clicked
  195. if (p.getClass().getName().equals("com.sun.j3d.utils.geometry.Sphere")) {
  196. // p is a Cylinder. Our LEDs are Spheres, so p.equals(led[x][y][z]) does not find anything...
  197. for (int x = 0; x < 8; x++) {
  198. for (int y = 0; y < 8; y++) {
  199. for (int z = 0; z < 8; z++) {
  200. if (p.equals(leds[x][y][z])) {
  201. // Clicked led found!
  202. System.out.println("Clicked LED found: " + x + " " + y + " " + z);
  203. parentFrame.toggleLED(x, y, z);
  204. x = 8;
  205. y = 8;
  206. z = 8;
  207. }
  208. }
  209. }
  210. }
  211. } else {
  212. System.out.println("Clicked, but not a sphere. Clicked object: " + p.getClass().getName());
  213. if(p.getClass().getName().equals("com.sun.j3d.utils.geometry.Cylinder")){
  214. }
  215. }
  216. }
  217. } else {
  218. System.out.println("Clicked, but hit nothing");
  219. }
  220. }
  221. private void drawLedFeetVertical(double x, double y, double z,
  222. float length, float rad) {
  223. // draw Feet going down
  224. Appearance feetApp = new Appearance();
  225. feetApp.setMaterial(whiteMat);
  226. feetApp.setColoringAttributes(whiteColor);
  227. Cylinder c = new Cylinder(rad, length, feetApp);
  228. TransformGroup tg = new TransformGroup();
  229. Transform3D transform = new Transform3D();
  230. Vector3d vector = new Vector3d(x, y, z);
  231. transform.setTranslation(vector);
  232. tg.setTransform(transform);
  233. tg.addChild(c);
  234. feetGroup.addChild(tg);
  235. }
  236. private void drawLedFeetHorizontal(double x, double y, double z,
  237. float length, float rad, int deg) {
  238. // draw Feet going down
  239. Appearance feetApp = new Appearance();
  240. feetApp.setMaterial(whiteMat);
  241. feetApp.setColoringAttributes(whiteColor);
  242. Cylinder c = new Cylinder(rad, length, feetApp);
  243. TransformGroup tg = new TransformGroup();
  244. Transform3D transform = new Transform3D();
  245. Vector3d vector = new Vector3d(x, y, z);
  246. transform.rotZ(Math.toRadians(90));
  247. if (deg != 0)
  248. transform.rotX(Math.toRadians(deg));
  249. transform.setTranslation(vector);
  250. tg.setTransform(transform);
  251. tg.addChild(c);
  252. feetGroup.addChild(tg);
  253. }
  254. /**
  255. * Rotate the cube back to its initial position.
  256. */
  257. public void resetView() {
  258. Matrix4d mat = new Matrix4d();
  259. if(inFullscreen){
  260. mat.setRow(0, 0.7597, -0.0204, 0.64926, 0.68);
  261. mat.setRow(1, -0.08, -0.995, 0.061, 0.7);
  262. mat.setRow(2, 0.64473, -0.09786, -0.758, -22.88);
  263. mat.setRow(3, 0.0, 0.0, 0.0, 1.0);
  264. } else {
  265. mat.setRow(0, 0.7597, -0.0204, 0.64926, 0.56);
  266. mat.setRow(1, -0.08, -0.995, 0.061, 0.02);
  267. mat.setRow(2, 0.64473, -0.09786, -0.758, -14.68);
  268. mat.setRow(3, 0.0, 0.0, 0.0, 1.0);
  269. }
  270. trans3D.set(mat);
  271. transGroup.setTransform(trans3D);
  272. feetGroup.setTransform(trans3D);
  273. }
  274. /**
  275. * Prints the translation matrix that is changed by moving/rotating the 3D
  276. * Cube with your mouse.
  277. */
  278. public void printTranslationData() {
  279. Matrix4d mat = new Matrix4d();
  280. Transform3D t = new Transform3D();
  281. transGroup.getTransform(t);
  282. t.get(mat);
  283. String s = mat.toString();
  284. System.out.println(s.replaceAll(", ", "\t"));
  285. }
  286. /**
  287. * Sets the data that is displayed by the LEDs
  288. *
  289. * @param data 64 byte array with the data (8 bits/LEDs per byte)
  290. */
  291. public void setData(short[] data) {
  292. for (int y = 0; y < 8; y++) {
  293. for (int z = 0; z < 8; z++) {
  294. for (int x = 0; x < 8; x++) {
  295. Appearance a = new Appearance();
  296. if ((data[y + (z * 8)] & (1 << x)) != 0) {
  297. // Activate led
  298. a.setColoringAttributes(redColor);
  299. a.setMaterial(redMat);
  300. } else {
  301. // Deactivate led
  302. a.setColoringAttributes(whiteColor);
  303. a.setMaterial(whiteMat);
  304. }
  305. leds[x][y][z].setAppearance(a);
  306. }
  307. }
  308. }
  309. }
  310. // create nice background
  311. private Background createBackground() {
  312. Background bg = new Background(0.0f, 0.0f, 1.0f);
  313. int radius = canvas.getWidth();
  314. bg.setApplicationBounds(new BoundingSphere(new Point3d(0.0, 0.0, 0.0), radius));
  315. return bg;
  316. }
  317. /**
  318. * Create new background to reflect changed Canvas size.
  319. */
  320. private void toggleFullscreen() {
  321. group.detach();
  322. if(group.indexOfChild(background) != -1){
  323. group.removeChild(background);
  324. }
  325. background = createBackground();
  326. group.addChild(background);
  327. universe.addBranchGraph(group);
  328. inFullscreen = !inFullscreen;
  329. }
  330. /**
  331. * Create new background and adjust view.
  332. */
  333. public void enterFullscreen() {
  334. toggleFullscreen();
  335. trans3D.set(fullScreenMat);
  336. transGroup.setTransform(trans3D);
  337. feetGroup.setTransform(trans3D);
  338. resetView(); //This is important. For some reason some legs are missing when entering fullscreen mode. Calling this function solves the problem.
  339. }
  340. /**
  341. * Create new background and adjust view.
  342. */
  343. public void leaveFullscreen() {
  344. toggleFullscreen();
  345. trans3D.set(mat);
  346. transGroup.setTransform(trans3D);
  347. feetGroup.setTransform(trans3D);
  348. }
  349. }