TestSpotLightTerrain.java 8.7 KB

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