Simple single-color 8x8x8 LED Cube with AVRs
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

Led3D.java 11KB

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