TestSpotLightTerrain.java 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. /*
  2. * Copyright (c) 2009-2010 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.SimpleApplication;
  34. import com.jme3.bounding.BoundingBox;
  35. import com.jme3.font.BitmapText;
  36. import com.jme3.light.AmbientLight;
  37. import com.jme3.light.PointLight;
  38. import com.jme3.light.SpotLight;
  39. import com.jme3.material.Material;
  40. import com.jme3.math.ColorRGBA;
  41. import com.jme3.math.FastMath;
  42. import com.jme3.math.Quaternion;
  43. import com.jme3.math.Vector3f;
  44. import com.jme3.scene.Geometry;
  45. import com.jme3.scene.Spatial;
  46. import com.jme3.terrain.geomipmap.TerrainLodControl;
  47. import com.jme3.terrain.geomipmap.TerrainQuad;
  48. import com.jme3.terrain.geomipmap.lodcalc.DistanceLodCalculator;
  49. import com.jme3.terrain.heightmap.AbstractHeightMap;
  50. import com.jme3.terrain.heightmap.ImageBasedHeightMap;
  51. import com.jme3.texture.Texture;
  52. import com.jme3.texture.Texture.WrapMode;
  53. import com.jme3.util.SkyFactory;
  54. /**
  55. * Uses the terrain's lighting texture with normal maps and lights.
  56. *
  57. * @author bowens
  58. */
  59. public class TestSpotLightTerrain extends SimpleApplication {
  60. private TerrainQuad terrain;
  61. Material matTerrain;
  62. Material matWire;
  63. boolean wireframe = false;
  64. boolean triPlanar = false;
  65. boolean wardiso = false;
  66. boolean minnaert = false;
  67. protected BitmapText hintText;
  68. PointLight pl;
  69. Geometry lightMdl;
  70. private float grassScale = 64;
  71. private float dirtScale = 16;
  72. private float rockScale = 128;
  73. SpotLight sl;
  74. public static void main(String[] args) {
  75. TestSpotLightTerrain app = new TestSpotLightTerrain();
  76. app.start();
  77. }
  78. @Override
  79. public void simpleInitApp() {
  80. makeTerrain();
  81. flyCam.setMoveSpeed(50);
  82. sl = new SpotLight();
  83. sl.setSpotRange(100);
  84. sl.setSpotOuterAngle(20 * FastMath.DEG_TO_RAD);
  85. sl.setSpotInnerAngle(15 * FastMath.DEG_TO_RAD);
  86. sl.setDirection(new Vector3f(-0.39820394f, -0.73094344f, 0.55421597f));
  87. sl.setPosition(new Vector3f(-64.61567f, -87.615425f, -202.41328f));
  88. rootNode.addLight(sl);
  89. AmbientLight ambLight = new AmbientLight();
  90. ambLight.setColor(new ColorRGBA(0.8f, 0.8f, 0.8f, 0.2f));
  91. rootNode.addLight(ambLight);
  92. cam.setLocation(new Vector3f(-41.219646f, -84.8363f, -171.67267f));
  93. cam.setRotation(new Quaternion(-0.04562731f, 0.89917684f, -0.09668826f, -0.4243236f));
  94. sl.setDirection(cam.getDirection());
  95. sl.setPosition(cam.getLocation());
  96. }
  97. @Override
  98. public void simpleUpdate(float tpf) {
  99. super.simpleUpdate(tpf);
  100. sl.setDirection(cam.getDirection());
  101. sl.setPosition(cam.getLocation());
  102. }
  103. private void makeTerrain() {
  104. // TERRAIN TEXTURE material
  105. matTerrain = new Material(assetManager, "Common/MatDefs/Terrain/TerrainLighting.j3md");
  106. matTerrain.setBoolean("useTriPlanarMapping", false);
  107. matTerrain.setBoolean("WardIso", true);
  108. // ALPHA map (for splat textures)
  109. matTerrain.setTexture("AlphaMap", assetManager.loadTexture("Textures/Terrain/splat/alpha1.png"));
  110. matTerrain.setTexture("AlphaMap_1", assetManager.loadTexture("Textures/Terrain/splat/alpha2.png"));
  111. // HEIGHTMAP image (for the terrain heightmap)
  112. Texture heightMapImage = assetManager.loadTexture("Textures/Terrain/splat/mountains512.png");
  113. // GRASS texture
  114. Texture grass = assetManager.loadTexture("Textures/Terrain/splat/grass.jpg");
  115. grass.setWrap(WrapMode.Repeat);
  116. matTerrain.setTexture("DiffuseMap", grass);
  117. matTerrain.setFloat("DiffuseMap_0_scale", grassScale);
  118. // DIRT texture
  119. Texture dirt = assetManager.loadTexture("Textures/Terrain/splat/dirt.jpg");
  120. dirt.setWrap(WrapMode.Repeat);
  121. matTerrain.setTexture("DiffuseMap_1", dirt);
  122. matTerrain.setFloat("DiffuseMap_1_scale", dirtScale);
  123. // ROCK texture
  124. Texture rock = assetManager.loadTexture("Textures/Terrain/splat/road.jpg");
  125. rock.setWrap(WrapMode.Repeat);
  126. matTerrain.setTexture("DiffuseMap_2", rock);
  127. matTerrain.setFloat("DiffuseMap_2_scale", rockScale);
  128. // BRICK texture
  129. Texture brick = assetManager.loadTexture("Textures/Terrain/BrickWall/BrickWall.jpg");
  130. brick.setWrap(WrapMode.Repeat);
  131. matTerrain.setTexture("DiffuseMap_3", brick);
  132. matTerrain.setFloat("DiffuseMap_3_scale", rockScale);
  133. // RIVER ROCK texture
  134. Texture riverRock = assetManager.loadTexture("Textures/Terrain/Pond/Pond.jpg");
  135. riverRock.setWrap(WrapMode.Repeat);
  136. matTerrain.setTexture("DiffuseMap_4", riverRock);
  137. matTerrain.setFloat("DiffuseMap_4_scale", rockScale);
  138. Texture normalMap0 = assetManager.loadTexture("Textures/Terrain/splat/grass_normal.jpg");
  139. normalMap0.setWrap(WrapMode.Repeat);
  140. Texture normalMap1 = assetManager.loadTexture("Textures/Terrain/splat/dirt_normal.png");
  141. normalMap1.setWrap(WrapMode.Repeat);
  142. Texture normalMap2 = assetManager.loadTexture("Textures/Terrain/splat/road_normal.png");
  143. normalMap2.setWrap(WrapMode.Repeat);
  144. matTerrain.setTexture("NormalMap", normalMap0);
  145. matTerrain.setTexture("NormalMap_1", normalMap2);
  146. matTerrain.setTexture("NormalMap_2", normalMap2);
  147. matTerrain.setTexture("NormalMap_4", normalMap2);
  148. // WIREFRAME material
  149. matWire = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
  150. matWire.getAdditionalRenderState().setWireframe(true);
  151. matWire.setColor("Color", ColorRGBA.Green);
  152. createSky();
  153. // CREATE HEIGHTMAP
  154. AbstractHeightMap heightmap = null;
  155. try {
  156. //heightmap = new HillHeightMap(1025, 1000, 50, 100, (byte) 3);
  157. heightmap = new ImageBasedHeightMap(heightMapImage.getImage(), 1f);
  158. heightmap.load();
  159. } catch (Exception e) {
  160. e.printStackTrace();
  161. }
  162. terrain = new TerrainQuad("terrain", 65, 513, heightmap.getHeightMap());//, new LodPerspectiveCalculatorFactory(getCamera(), 4)); // add this in to see it use entropy for LOD calculations
  163. TerrainLodControl control = new TerrainLodControl(terrain, getCamera());
  164. control.setLodCalculator( new DistanceLodCalculator(65, 2.7f) );
  165. terrain.addControl(control);
  166. terrain.setMaterial(matTerrain);
  167. terrain.setModelBound(new BoundingBox());
  168. terrain.updateModelBound();
  169. terrain.setLocalTranslation(0, -100, 0);
  170. terrain.setLocalScale(1f, 1f, 1f);
  171. rootNode.attachChild(terrain);
  172. }
  173. private void createSky() {
  174. Texture west = assetManager.loadTexture("Textures/Sky/Lagoon/lagoon_west.jpg");
  175. Texture east = assetManager.loadTexture("Textures/Sky/Lagoon/lagoon_east.jpg");
  176. Texture north = assetManager.loadTexture("Textures/Sky/Lagoon/lagoon_north.jpg");
  177. Texture south = assetManager.loadTexture("Textures/Sky/Lagoon/lagoon_south.jpg");
  178. Texture up = assetManager.loadTexture("Textures/Sky/Lagoon/lagoon_up.jpg");
  179. Texture down = assetManager.loadTexture("Textures/Sky/Lagoon/lagoon_down.jpg");
  180. Spatial sky = SkyFactory.createSky(assetManager, west, east, north, south, up, down);
  181. rootNode.attachChild(sky);
  182. }
  183. }