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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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 java.awt.Color;
  30. /**
  31. * This class is responsible for displaying the 3D View of our Cube.
  32. * @author Thomas Buck
  33. * @author Max Nuding
  34. * @author Felix Bäder
  35. * @version 1.0
  36. */
  37. public class Led3D {
  38. private Canvas3D canvas = null;
  39. private SimpleUniverse universe = null;
  40. private BranchGroup group = null;
  41. private Transform3D trans3D = null;
  42. private BranchGroup inBetween = null;
  43. private TransformGroup transGroup = null;
  44. private Sphere[][][] leds = new Sphere[8][8][8];
  45. private static ColoringAttributes redColor = new ColoringAttributes(new Color3f(1.0f, 0.0f, 0.0f), ColoringAttributes.FASTEST);
  46. private static ColoringAttributes whiteColor = new ColoringAttributes(new Color3f(0.2f, 0.2f, 0.2f), ColoringAttributes.FASTEST);
  47. private static Material whiteMat = new Material(new Color3f(0.2f, 0.2f, 0.2f), new Color3f(0.0f, 0.0f, 0.0f), new Color3f(0.2f, 0.2f, 0.2f), new Color3f(0.2f, 0.2f, 0.2f), 64.0f);
  48. private static Material redMat = new Material(new Color3f(1.0f, 0.0f, 0.0f), new Color3f(1.0f, 0.0f, 0.0f), new Color3f(1.0f, 0.0f, 0.0f), new Color3f(1.0f, 0.0f, 0.0f), 64.0f);
  49. /**
  50. * @param canv The Canvas3D we render our cube in
  51. */
  52. public Led3D(Canvas3D canv) {
  53. canvas = canv;
  54. group = new BranchGroup();
  55. // Position viewer, so we are looking at object
  56. trans3D = new Transform3D();
  57. Matrix4d mat = new Matrix4d();
  58. mat.setRow(0, 0.744, 0.0237, -0.66756, -0.34);
  59. mat.setRow(1, 0.136, -0.9837, 0.117, 3.24);
  60. mat.setRow(2, -0.6536, -0.1785, -0.735, -8.32);
  61. mat.setRow(3, 0.0, 0.0, 0.0, 1.0);
  62. trans3D.set(mat);
  63. transGroup = new TransformGroup(trans3D);
  64. ViewingPlatform viewingPlatform = new ViewingPlatform();
  65. Viewer viewer = new Viewer(canvas);
  66. universe = new SimpleUniverse(viewingPlatform, viewer);
  67. BoundingBox boundBox = new BoundingBox(new Point3d(-5.0, -5.0, -5.0), new Point3d(13.0, 13.0, 13.0));
  68. // roration with left mouse button
  69. MouseRotate behaviour = new MouseRotate(transGroup);
  70. behaviour.setSchedulingBounds(boundBox);
  71. transGroup.addChild(behaviour);
  72. // zoom with middle mouse button
  73. MouseZoom beh2 = new MouseZoom(transGroup);
  74. beh2.setSchedulingBounds(boundBox);
  75. transGroup.addChild(beh2);
  76. // move with right mouse button
  77. MouseTranslate beh3 = new MouseTranslate(transGroup);
  78. beh3.setSchedulingBounds(boundBox);
  79. transGroup.addChild(beh3);
  80. Background bg = new Background(0.0f, 0.0f, 0.42f);
  81. bg.setApplicationBounds(new BoundingSphere(new Point3d(0.0, 0.0, 0.0), 100.0));
  82. group.addChild(bg);
  83. // Add all our led sphares to the universe
  84. for (int x = 0; x < 8; x++) {
  85. for (int y = 0; y < 8; y++) {
  86. for (int z = 0; z < 8; z++) {
  87. Appearance a = new Appearance();
  88. a.setMaterial(whiteMat);
  89. a.setColoringAttributes(whiteColor);
  90. leds[x][y][z] = new Sphere(0.05f, Sphere.ENABLE_APPEARANCE_MODIFY, a);
  91. TransformGroup tg = new TransformGroup();
  92. Transform3D transform = new Transform3D();
  93. Vector3f vector = new Vector3f(x, y, z);
  94. transform.setTranslation(vector);
  95. tg.setTransform(transform);
  96. tg.addChild(leds[x][y][z]);
  97. transGroup.addChild(tg);
  98. }
  99. }
  100. }
  101. // Add an ambient light
  102. Color3f light2Color = new Color3f(1.0f, 1.0f, 1.0f);
  103. AmbientLight light2 = new AmbientLight(light2Color);
  104. BoundingSphere bounds = new BoundingSphere(new Point3d(0.0, 0.0, 0.0), 100.0);
  105. light2.setInfluencingBounds(bounds);
  106. light2.setEnable(true);
  107. transGroup.addChild(light2);
  108. group.addChild(transGroup);
  109. universe.addBranchGraph(group); // Add group to universe
  110. }
  111. /**
  112. * Prints the translation matrix that is changed by moving/rotating the 3D Cube with your mouse.
  113. */
  114. public void printTranslationData() {
  115. Matrix4d mat = new Matrix4d();
  116. Transform3D t = new Transform3D();
  117. transGroup.getTransform(t);
  118. t.get(mat);
  119. String s = mat.toString();
  120. System.out.println(s.replaceAll(", ", "\t"));
  121. }
  122. /**
  123. * Sets the data that is displayed by the LEDs
  124. * @param data 64 byte array with the data (8 bits/LEDs per byte)
  125. */
  126. public void setData(short[] data) {
  127. for (int y = 0; y < 8; y++) {
  128. for (int z = 0; z < 8; z++) {
  129. for (int x = 0; x < 8; x++) {
  130. Appearance a = new Appearance();
  131. if ((data[y + (z * 8)] & (1 << x)) != 0) {
  132. // Activate led
  133. a.setColoringAttributes(redColor);
  134. a.setMaterial(redMat);
  135. } else {
  136. // Deactivate led
  137. a.setColoringAttributes(whiteColor);
  138. a.setMaterial(whiteMat);
  139. }
  140. leds[x][y][z].setAppearance(a);
  141. }
  142. }
  143. }
  144. }
  145. }