Advertisement
Guest User

Untitled

a guest
Jul 30th, 2014
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.48 KB | None | 0 0
  1. public class WorldTerrain extends TerrainQuad {
  2.  
  3. public static final String NAME = "terrain";
  4. public static final int PATCH_SIZE = 9;
  5. public static final int TOTAL_SIZE = 33;
  6. private static Vector3f spawnLocation = new Vector3f(13, 3, 16);
  7. private Material mat_terrain;
  8.  
  9. private void loadText(AssetManager assetManager) {
  10. /**
  11. * 1. Create terrain material and load four textures into it.
  12. */
  13. mat_terrain = new Material(assetManager,
  14. "Common/MatDefs/Terrain/Terrain.j3md");
  15.  
  16. /**
  17. * 1.1) Add ALPHA map (for red-blue-green coded splat textures)
  18. */
  19. mat_terrain.setTexture("Alpha", assetManager.loadTexture(
  20. "Textures/Terrain/splat/alphamap.png"));
  21.  
  22. /**
  23. * 1.2) Add GRASS texture into the red layer (Tex1).
  24. */
  25. Texture grass = assetManager.loadTexture(
  26. "Textures/Terrain/splat/grass.jpg");
  27. grass.setWrap(WrapMode.Repeat);
  28. mat_terrain.setTexture("Tex1", grass);
  29. mat_terrain.setFloat("Tex1Scale", 64f);
  30.  
  31. /**
  32. * 1.3) Add DIRT texture into the green layer (Tex2)
  33. */
  34. Texture dirt = assetManager.loadTexture(
  35. "Textures/GrassRocky.png");
  36. dirt.setWrap(WrapMode.Repeat);
  37. mat_terrain.setTexture("Tex2", dirt);
  38. mat_terrain.setFloat("Tex2Scale", 32f);
  39.  
  40. /**
  41. * 1.4) Add ROAD texture into the blue layer (Tex3)
  42. */
  43. Texture rock = assetManager.loadTexture(
  44. "Textures/Terrain/splat/road.jpg");
  45. rock.setWrap(WrapMode.Repeat);
  46. mat_terrain.setTexture("Tex3", rock);
  47. mat_terrain.setFloat("Tex3Scale", 128f);
  48. }
  49.  
  50. private static float[] generateHeightMap(AssetManager assetManager) {
  51. AbstractHeightMap heightmap = null;
  52. try {
  53. heightmap = new HillHeightMap(1024, 100, 2, 10, 1);
  54. } catch (Exception ex) {
  55. Logger.getLogger(Interface.class.getName()).log(Level.SEVERE, null, ex);
  56. }
  57. return heightmap.getHeightMap();
  58. }
  59.  
  60. public static Vector3f getSpawnLocation() {
  61. return spawnLocation;
  62. }
  63.  
  64. public WorldTerrain(Interface app) {
  65. super(NAME, PATCH_SIZE, TOTAL_SIZE, generateHeightMap(app.getAssetManager()));
  66. loadText(app.getAssetManager());
  67. setMaterial(mat_terrain);
  68.  
  69. setModelBound(new BoundingBox());
  70. setLocalTranslation(32f, 0f, 32f);
  71.  
  72. List<Camera> cameras = new ArrayList<Camera>();
  73. cameras.add(app.getCamera());
  74.  
  75. RigidBodyControl landscape = new RigidBodyControl(0.0f);
  76. addControl(landscape);
  77.  
  78. public class Interface extends SimpleApplication {
  79.  
  80. private TerrainQuad terrain;
  81. Material mat_terrain;
  82.  
  83. public static void main(String[] args) {
  84. Interface app = new Interface();
  85. app.start();
  86. }
  87.  
  88. @Override
  89. public void simpleInitApp() {
  90. flyCam.setMoveSpeed(50);
  91.  
  92. terrain = new WorldTerrain(this);
  93. rootNode.attachChild(terrain);
  94. }
  95. int x=0;
  96. @Override
  97. public void simpleUpdate(float tpf) {
  98.  
  99. for(int i=0;i<1000;i++){
  100. terrain.setHeight(new Vector2f(x,i), (float) (10*Math.random()));
  101. }
  102. x++;
  103. }
  104. private Ray getMouseRay() {
  105. Vector2f mouseCoords = new Vector2f(inputManager.getCursorPosition());
  106. Vector3f origin = cam.getWorldCoordinates(mouseCoords, 0).normalizeLocal();
  107. Ray mouseRay = new Ray(origin, cam.getWorldCoordinates(mouseCoords, 1).subtractLocal(origin).normalizeLocal());
  108. return mouseRay;
  109. }
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement