Simple single-color 8x8x8 LED Cube with AVRs
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

Led3D.java 12KB

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