{"id":2375,"date":"2019-11-18T23:35:13","date_gmt":"2019-11-18T23:35:13","guid":{"rendered":"http:\/\/www.andygibson.net\/blog\/?p=2375"},"modified":"2019-11-19T10:23:19","modified_gmt":"2019-11-19T10:23:19","slug":"procedural-terrain-with-java-part-1","status":"publish","type":"post","link":"https:\/\/www.andygibson.net\/blog\/programming\/procedural-terrain-with-java-part-1\/","title":{"rendered":"Procedural Terrain With Java &#8211; part 1"},"content":{"rendered":"\n<p>Every year, around Christmas time I always get a hankering to write some 3D terrain rendering code. Not so much the actual rendering engine, that part has already been done with 3D APIs such as OpenGL and Direct X. Its more about rendering a 3D terrain generated by code, more specifically pseudo-random numbers, enabling a continuous never ending terrain. <\/p>\n\n\n\n<!--more-->\n\n\n\n<p>This year I&#8217;m getting a head start, and to celebrate that, I&#8217;m going to blog it as I go.  I&#8217;m going to be using Java with JMonkeyEngine APIs on top of the LWJGL OpenGL libs. I quite like JMonkeyEngine as it puts more control into your code giving with the power of the Java language. Being Java based means it is flexible enough to use some good design patterns and code structure without too much rigidity and I can unit test my logic too!<\/p>\n\n\n\n<p>So, with that, I&#8217;m going to create a Maven project which will pull in the JMonkeyEngine libs and create a very simple, cube lit by a sun. I&#8217;m going to be using Java 13 and the latest JMonkeyEngine version (3.2.4 -stable). If you&#8217;ve looked at JME before, this probably isn&#8217;t going to be anything earth shattering, but serves as a solid ground for part II.<\/p>\n\n\n\n<p>Start by creating a new simple Maven project and add the following content in the <code>pom.xml<\/code> file.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: xml; title: ; notranslate\" title=\"\">\n\t&lt;properties&gt;\n\t\t&lt;project.build.sourceEncoding&gt;UTF-8&lt;\/project.build.sourceEncoding&gt;\n\t\t&lt;maven.compiler.source&gt;13&lt;\/maven.compiler.source&gt;\n\t\t&lt;maven.compiler.target&gt;13&lt;\/maven.compiler.target&gt;\n\t\t&lt;jme-version&gt;3.2.4-stable&lt;\/jme-version&gt;\n\t&lt;\/properties&gt;\n\n\t&lt;repositories&gt;\n\t\t&lt;repository&gt;\n\t\t\t&lt;id&gt;JCenter&lt;\/id&gt;\n\t\t\t&lt;url&gt;https:\/\/jcenter.bintray.com\/&lt;\/url&gt;\n\t\t&lt;\/repository&gt;\n\t&lt;\/repositories&gt;\n\n\t&lt;dependencies&gt;\n\t\t&lt;dependency&gt;\n\t\t\t&lt;groupId&gt;org.jmonkeyengine&lt;\/groupId&gt;\n\t\t\t&lt;artifactId&gt;jme3-core&lt;\/artifactId&gt;\n\t\t\t&lt;version&gt;${jme-version}&lt;\/version&gt;\n\t\t&lt;\/dependency&gt;\n\n\t\t&lt;dependency&gt;\n\t\t\t&lt;groupId&gt;org.jmonkeyengine&lt;\/groupId&gt;\n\t\t\t&lt;artifactId&gt;jme3-terrain&lt;\/artifactId&gt;\n\t\t\t&lt;version&gt;${jme-version}&lt;\/version&gt;\n\t\t&lt;\/dependency&gt;\n\n\t\t&lt;dependency&gt;\n\t\t\t&lt;groupId&gt;org.jmonkeyengine&lt;\/groupId&gt;\n\t\t\t&lt;artifactId&gt;jme3-plugins&lt;\/artifactId&gt;\n\t\t\t&lt;version&gt;${jme-version}&lt;\/version&gt;\n\t\t&lt;\/dependency&gt;\n\n\t\t&lt;dependency&gt;\n\t\t\t&lt;groupId&gt;org.jmonkeyengine&lt;\/groupId&gt;\n\t\t\t&lt;artifactId&gt;jme3-lwjgl&lt;\/artifactId&gt;\n\t\t\t&lt;version&gt;${jme-version}&lt;\/version&gt;\n\t\t&lt;\/dependency&gt;\n\t&lt;\/dependencies&gt;\n\n\t&lt;build&gt;\n\t\t&lt;plugins&gt;\n\t\t\t&lt;plugin&gt;\n\t\t\t\t&lt;groupId&gt;org.apache.maven.plugins&lt;\/groupId&gt;\n\t\t\t\t&lt;artifactId&gt;maven-compiler-plugin&lt;\/artifactId&gt;\n\t\t\t\t&lt;version&gt;3.8.0&lt;\/version&gt;\n\t\t\t\t&lt;configuration&gt;\n\t\t\t\t\t&lt;source&gt;${maven.compiler.source}&lt;\/source&gt;\n\t\t\t\t\t&lt;target&gt;${maven.compiler.target}&lt;\/target&gt;\n\t\t\t\t&lt;\/configuration&gt;\n\t\t\t&lt;\/plugin&gt;\n\t\t&lt;\/plugins&gt;\n\t&lt;\/build&gt;\n&lt;\/project&gt;\n<\/pre><\/div>\n\n\n<p>Now at some point, you will have to download the JMonkeyEngine SDK to get the binary libraries to use with JME. For now, we just need one, which is <code>liblwjgl64.so<\/code>. Your filename may be different depending on the platform and 32\/64 bit.  This is the lib for supporting the LWJGL and for now, can go in the root of the project folder, next to the <code>pom.xml<\/code> file.<\/p>\n\n\n\n<p>Our next step it to write a piece of code which will get us displaying some graphics in a page. We&#8217;ll create a main class which inherited from the <code>SimpleApplication<\/code> class in the JME library.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: java; title: ; notranslate\" title=\"\">\nimport com.jme3.app.SimpleApplication;\n\npublic class Main extends SimpleApplication {\n\n  public static void main(final String&#x5B;] args) {\n    final Main app = new Main();\n    app.start();\n  }\n\n  @Override\n  public void simpleInitApp() {\n\n  }\n}\n<\/pre><\/div>\n\n\n<p>Run that and you should get the JMonkeyEngine splash screen where you can pick your settings, and upon clicking OK, you should see a big black screen with some debug information on it:<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img loading=\"lazy\" width=\"640\" height=\"510\" src=\"https:\/\/www.andygibson.net\/blog\/wp-content\/uploads\/2019\/11\/Screenshot-from-2019-11-18-22-48-41.png\" alt=\"\" class=\"wp-image-2378\" srcset=\"https:\/\/www.andygibson.net\/blog\/wp-content\/uploads\/2019\/11\/Screenshot-from-2019-11-18-22-48-41.png 640w, https:\/\/www.andygibson.net\/blog\/wp-content\/uploads\/2019\/11\/Screenshot-from-2019-11-18-22-48-41-300x239.png 300w\" sizes=\"(max-width: 640px) 100vw, 640px\" \/><figcaption>My new space game<\/figcaption><\/figure><\/div>\n\n\n\n<p>Not much is going on here, just some debug information including our FPS rate. Of course, we haven&#8217;t added anything to the display yet. So we need to add some  light and an object. We&#8217;ll also position the camera from where we are generating the image so we can get a good view. We&#8217;ll do this in the <code>simpleInitApp<\/code> method that is used to initialise the content.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: java; title: ; notranslate\" title=\"\">\n  @Override\n  public void simpleInitApp() {\n\n    \/\/ move up, to the side and look at the origin\n    getCamera().setLocation(new Vector3f(-4, 5, 10));\n    getCamera().lookAt(Vector3f.ZERO, Vector3f.UNIT_Y);\n\n    \/\/ lets put some light in\n    final DirectionalLight sun = new DirectionalLight(new Vector3f(0.2f, -0.6f, -0.5f).normalize());\n    sun.setColor(ColorRGBA.White);\n    rootNode.addLight(sun);\n\n    \/\/ create a simple box\n    final Box b = new Box(1, 1, 1);\n    final Geometry geom = new Geometry(&quot;Box&quot;, b);\n\n    \/\/ create a very plain lit material\n    final Material mat = new Material(assetManager, &quot;Common\/MatDefs\/Light\/Lighting.j3md&quot;);\n\n    \/\/ assign this material to the box geometry.\n    geom.setMaterial(mat);\n\n    \/\/ add the geometry to the scene and we are good to go\n    rootNode.attachChild(geom);\n  }\n\n<\/pre><\/div>\n\n\n<p>This article isn&#8217;t meant to be  primer on 3D graphics, or JMonkeyEngine in particular, but this is a simple application that will get you up and running with a simple lit scene in JME with Java.<\/p>\n\n\n\n<p>We move the camera and look at the box so we can see all 3 sides and get an idea of how they are shaded. The directional light has no position so its only influence on shading depends solely on the angle between the box face and the direction of the light that was specified in the constructor. The material we have used is a simple lit material using the default parameters. <\/p>\n\n\n\n<p>If you run this, you will see a simple scene with a lit box that you can use the mouse to look around with and the WASD keys to move around in. <\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img loading=\"lazy\" width=\"640\" height=\"510\" src=\"https:\/\/www.andygibson.net\/blog\/wp-content\/uploads\/2019\/11\/cube.png\" alt=\"\" class=\"wp-image-2381\" srcset=\"https:\/\/www.andygibson.net\/blog\/wp-content\/uploads\/2019\/11\/cube.png 640w, https:\/\/www.andygibson.net\/blog\/wp-content\/uploads\/2019\/11\/cube-300x239.png 300w\" sizes=\"(max-width: 640px) 100vw, 640px\" \/><figcaption>A Cube In Space<\/figcaption><\/figure><\/div>\n\n\n\n<p>This gets us up and running with JME. Next time we&#8217;ll look at creating a simple terrain using code, in particular the JME noise functions to create terrain.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Every year, around Christmas time I always get a hankering to write some 3D terrain rendering code. Not so much the actual rendering engine, that part has already been done with 3D APIs such as OpenGL and Direct X. Its more about rendering a 3D terrain generated by code, more specifically pseudo-random numbers, enabling a [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0},"categories":[80],"tags":[128,29,22,6,129,120],"_links":{"self":[{"href":"https:\/\/www.andygibson.net\/blog\/wp-json\/wp\/v2\/posts\/2375"}],"collection":[{"href":"https:\/\/www.andygibson.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.andygibson.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.andygibson.net\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.andygibson.net\/blog\/wp-json\/wp\/v2\/comments?post=2375"}],"version-history":[{"count":8,"href":"https:\/\/www.andygibson.net\/blog\/wp-json\/wp\/v2\/posts\/2375\/revisions"}],"predecessor-version":[{"id":2385,"href":"https:\/\/www.andygibson.net\/blog\/wp-json\/wp\/v2\/posts\/2375\/revisions\/2385"}],"wp:attachment":[{"href":"https:\/\/www.andygibson.net\/blog\/wp-json\/wp\/v2\/media?parent=2375"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.andygibson.net\/blog\/wp-json\/wp\/v2\/categories?post=2375"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.andygibson.net\/blog\/wp-json\/wp\/v2\/tags?post=2375"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}