TestConeVSFrustum.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. /*
  2. * Copyright (c) 2009-2012 jMonkeyEngine
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are
  7. * met:
  8. *
  9. * * Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. *
  12. * * Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. *
  16. * * Neither the name of 'jMonkeyEngine' nor the names of its contributors
  17. * may be used to endorse or promote products derived from this software
  18. * without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
  22. * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  23. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  24. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  25. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  26. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  27. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  28. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  29. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  30. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. */
  32. package jme3test.light;
  33. import com.jme3.app.ChaseCameraAppState;
  34. import com.jme3.app.SimpleApplication;
  35. import com.jme3.input.KeyInput;
  36. import com.jme3.input.MouseInput;
  37. import com.jme3.input.controls.ActionListener;
  38. import com.jme3.input.controls.AnalogListener;
  39. import com.jme3.input.controls.KeyTrigger;
  40. import com.jme3.input.controls.MouseAxisTrigger;
  41. import com.jme3.input.controls.MouseButtonTrigger;
  42. import com.jme3.light.AmbientLight;
  43. import com.jme3.light.DirectionalLight;
  44. import com.jme3.light.SpotLight;
  45. import com.jme3.material.Material;
  46. import com.jme3.math.*;
  47. import com.jme3.renderer.Camera;
  48. import com.jme3.scene.Geometry;
  49. import com.jme3.scene.LightNode;
  50. import com.jme3.scene.Node;
  51. import com.jme3.scene.Spatial;
  52. import com.jme3.scene.debug.Grid;
  53. import com.jme3.scene.debug.WireFrustum;
  54. import com.jme3.scene.shape.Box;
  55. import com.jme3.scene.shape.Cylinder;
  56. import com.jme3.shadow.ShadowUtil;
  57. import com.jme3.texture.Texture;
  58. import com.jme3.util.TempVars;
  59. public class TestConeVSFrustum extends SimpleApplication {
  60. public static void main(String[] args) {
  61. TestConeVSFrustum app = new TestConeVSFrustum();
  62. app.start();
  63. }
  64. @Override
  65. public void simpleInitApp() {
  66. viewPort.setBackgroundColor(ColorRGBA.DarkGray);
  67. frustumCam = cam.clone();
  68. frustumCam.setFrustumFar(25);
  69. Vector3f[] points = new Vector3f[8];
  70. for (int i = 0; i < 8; i++) {
  71. points[i] = new Vector3f();
  72. }
  73. ShadowUtil.updateFrustumPoints2(frustumCam, points);
  74. WireFrustum frustumShape = new WireFrustum(points);
  75. Geometry frustum = new Geometry("frustum", frustumShape);
  76. frustum.setMaterial(new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md"));
  77. rootNode.attachChild(frustum);
  78. rootNode.addLight(new DirectionalLight());
  79. AmbientLight al = new AmbientLight();
  80. al.setColor(ColorRGBA.White.mult(0.2f));
  81. rootNode.addLight(al);
  82. Grid grid = new Grid(50, 50, 5);
  83. Geometry gridGeom = new Geometry("grid", grid);
  84. gridGeom.setMaterial(new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md"));
  85. gridGeom.getMaterial().setColor("Color", ColorRGBA.Gray);
  86. rootNode.attachChild(gridGeom);
  87. gridGeom.setLocalTranslation(-125, -25, -125);
  88. // flyCam.setMoveSpeed(30);
  89. // flyCam.setDragToRotate(true);
  90. // cam.setLocation(new Vector3f(56.182674f, 19.037334f, 7.093905f));
  91. // cam.setRotation(new Quaternion(0.0816657f, -0.82228005f, 0.12213967f, 0.5497892f));
  92. spotLight = new SpotLight();
  93. spotLight.setSpotRange(25);
  94. spotLight.setSpotOuterAngle(10 * FastMath.DEG_TO_RAD);
  95. float radius = FastMath.tan(spotLight.getSpotOuterAngle()) * spotLight.getSpotRange();
  96. Cylinder cylinder = new Cylinder(5, 16, 0.01f, radius, spotLight.getSpotRange(), true, false);
  97. geom = new Geometry("light", cylinder);
  98. geom.setMaterial(new Material(assetManager, "Common/MatDefs/Light/Lighting.j3md"));
  99. geom.getMaterial().setColor("Diffuse", ColorRGBA.White);
  100. geom.getMaterial().setColor("Ambient", ColorRGBA.DarkGray);
  101. geom.getMaterial().setBoolean("UseMaterialColors", true);
  102. final LightNode ln = new LightNode("lb", spotLight);
  103. ln.attachChild(geom);
  104. geom.setLocalTranslation(0, -spotLight.getSpotRange() / 2f, 0);
  105. geom.rotate(-FastMath.HALF_PI, 0, 0);
  106. rootNode.attachChild(ln);
  107. // ln.rotate(FastMath.QUARTER_PI, 0, 0);
  108. // ln.setLocalTranslation(0, 0, -16);
  109. inputManager.addMapping("click", new MouseButtonTrigger(MouseInput.BUTTON_RIGHT));
  110. inputManager.addMapping("shift", new KeyTrigger(KeyInput.KEY_LSHIFT), new KeyTrigger(KeyInput.KEY_RSHIFT));
  111. inputManager.addMapping("middleClick", new MouseButtonTrigger(MouseInput.BUTTON_MIDDLE));
  112. inputManager.addMapping("up", new MouseAxisTrigger(MouseInput.AXIS_Y, false));
  113. inputManager.addMapping("down", new MouseAxisTrigger(MouseInput.AXIS_Y, true));
  114. inputManager.addMapping("left", new MouseAxisTrigger(MouseInput.AXIS_X, true));
  115. inputManager.addMapping("right", new MouseAxisTrigger(MouseInput.AXIS_X, false));
  116. final Node camTarget = new Node("CamTarget");
  117. rootNode.attachChild(camTarget);
  118. ChaseCameraAppState chaser = new ChaseCameraAppState();
  119. chaser.setTarget(camTarget);
  120. chaser.setMaxDistance(150);
  121. chaser.setDefaultDistance(70);
  122. chaser.setDefaultHorizontalRotation(FastMath.HALF_PI);
  123. chaser.setMinVerticalRotation(-FastMath.PI);
  124. chaser.setMaxVerticalRotation(FastMath.PI * 2);
  125. chaser.setToggleRotationTrigger(new MouseButtonTrigger(MouseInput.BUTTON_LEFT));
  126. stateManager.attach(chaser);
  127. flyCam.setEnabled(false);
  128. inputManager.addListener(new AnalogListener() {
  129. public void onAnalog(String name, float value, float tpf) {
  130. Spatial s = null;
  131. float mult = 1;
  132. if (moving) {
  133. s = ln;
  134. }
  135. if (panning) {
  136. s = camTarget;
  137. mult = -1;
  138. }
  139. if ((moving || panning) && s!=null) {
  140. if (shift) {
  141. if (name.equals("left")) {
  142. tmp.set(cam.getDirection());
  143. s.rotate(tmpQuat.fromAngleAxis(value, tmp));
  144. }
  145. if (name.equals("right")) {
  146. tmp.set(cam.getDirection());
  147. s.rotate(tmpQuat.fromAngleAxis(-value, tmp));
  148. }
  149. } else {
  150. value *= MOVE_SPEED * mult;
  151. if (name.equals("up")) {
  152. tmp.set(cam.getUp()).multLocal(value);
  153. s.move(tmp);
  154. }
  155. if (name.equals("down")) {
  156. tmp.set(cam.getUp()).multLocal(-value);
  157. s.move(tmp);
  158. }
  159. if (name.equals("left")) {
  160. tmp.set(cam.getLeft()).multLocal(value);
  161. s.move(tmp);
  162. }
  163. if (name.equals("right")) {
  164. tmp.set(cam.getLeft()).multLocal(-value);
  165. s.move(tmp);
  166. }
  167. }
  168. }
  169. }
  170. }, "up", "down", "left", "right");
  171. inputManager.addListener(new ActionListener() {
  172. public void onAction(String name, boolean isPressed, float tpf) {
  173. if (name.equals("click")) {
  174. if (isPressed) {
  175. moving = true;
  176. } else {
  177. moving = false;
  178. }
  179. }
  180. if (name.equals("middleClick")) {
  181. if (isPressed) {
  182. panning = true;
  183. } else {
  184. panning = false;
  185. }
  186. }
  187. if (name.equals("shift")) {
  188. if (isPressed) {
  189. shift = true;
  190. } else {
  191. shift = false;
  192. }
  193. }
  194. }
  195. }, "click", "middleClick", "shift");
  196. /**
  197. * An unshaded textured cube. // * Uses texture from jme3-test-data
  198. * library!
  199. */
  200. Box boxMesh = new Box(1f, 1f, 1f);
  201. boxGeo = new Geometry("A Textured Box", boxMesh);
  202. Material boxMat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
  203. Texture monkeyTex = assetManager.loadTexture("Interface/Logo/Monkey.jpg");
  204. boxMat.setTexture("ColorMap", monkeyTex);
  205. boxGeo.setMaterial(boxMat);
  206. // rootNode.attachChild(boxGeo);
  207. //
  208. //boxGeo2 = boxGeo.clone();
  209. //rootNode.attachChild(boxGeo2);
  210. System.err.println("light " + spotLight.getPosition());
  211. }
  212. Geometry boxGeo, boxGeo2;
  213. private final static float MOVE_SPEED = 60;
  214. Vector3f tmp = new Vector3f();
  215. Quaternion tmpQuat = new Quaternion();
  216. boolean moving, shift;
  217. boolean panning;
  218. Geometry geom;
  219. SpotLight spotLight;
  220. Camera frustumCam;
  221. @Override
  222. public void simpleUpdate(float tpf) {
  223. TempVars vars = TempVars.get();
  224. boolean intersect = spotLight.intersectsFrustum(frustumCam, vars);
  225. if (intersect) {
  226. geom.getMaterial().setColor("Diffuse", ColorRGBA.Green);
  227. } else {
  228. geom.getMaterial().setColor("Diffuse", ColorRGBA.White);
  229. }
  230. Vector3f farPoint = vars.vect1.set(spotLight.getPosition()).addLocal(vars.vect2.set(spotLight.getDirection()).multLocal(spotLight.getSpotRange()));
  231. //computing the radius of the base disc
  232. float farRadius = (spotLight.getSpotRange() / FastMath.cos(spotLight.getSpotOuterAngle())) * FastMath.sin(spotLight.getSpotOuterAngle());
  233. //computing the projection direction : perpendicular to the light direction and coplanar with the direction vector and the normal vector
  234. Vector3f perpDirection = vars.vect2.set(spotLight.getDirection()).crossLocal(frustumCam.getWorldPlane(3).getNormal()).normalizeLocal().crossLocal(spotLight.getDirection());
  235. //projecting the far point on the base disc perimeter
  236. Vector3f projectedPoint = vars.vect3.set(farPoint).addLocal(perpDirection.multLocal(farRadius));
  237. vars.release();
  238. // boxGeo.setLocalTranslation(spotLight.getPosition());
  239. // boxGeo.setLocalTranslation(projectedPoint);
  240. }
  241. }