jMonkeyEngine 3.0 Beginner’s Guide
上QQ阅读APP看书,第一时间看更新

Time for action – initializing a scene step by step

If you no longer have the BasicGame template open, open it again in the jMonkeyEngine SDK. Remember that you can always recreate this project template using the New Project wizard in the File menu.

Let's look at the main class of the BasicGame project:

  1. Open the BasicGame project in the Projects window.
  2. Double-click on the mygame/Main.java file in the Source Packages folder. Main.java opens in the source editor.
  3. When you look at the Main.java template, you see a standard Java class that extends the com.jme3.app.SimpleApplication class. SimpleApplication is the base class that we use to write all our 3D games.

The basic code sample is short enough so that you can type it yourself—we just use the BasicGame template because it is quicker:

import com.jme3.app.SimpleApplication;
import com.jme3.material.Material;
import com.jme3.math.ColorRGBA;
import com.jme3.math.Vector3f;
import com.jme3.renderer.RenderManager;
import com.jme3.scene.Geometry;
import com.jme3.scene.shape.Box;

/** Basic jMonkeyEngine game template. */
public class Main extends SimpleApplication {

    @Override
    /** initialize the scene here */
    public void simpleInitApp() {
       // create a cube-shaped mesh
       Box b = new Box(Vector3f.ZERO, 1, 1, 1);    
       // create an object from the mesh
       Geometry geom = new Geometry("Box", b);     

       // create a simple blue material
       Material mat = new Material(assetManager,
               "Common/MatDefs/Misc/Unshaded.j3md");
       mat.setColor("Color", ColorRGBA.Blue);      
       // give the object the blue material
       geom.setMaterial(mat);
       // make the object appear in the scene
       rootNode.attachChild(geom);
    }

    @Override
    /** (optional) Interact with update loop here */
    public void simpleUpdate(float tpf) {}

    @Override
    /** (optional) Advanced renderer/frameBuffer modifications */
    public void simpleRender(RenderManager rm) {}

    /** Start the jMonkeyEngine application */
    public static void main(String[] args) {
       Main app = new Main();
       app.start();
    }
}
Tip

Downloading the example code

You can download the example code files for all Packt books you have purchased from your account at http://www.packtpub.com. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.

You can right-click on the file and click on Run, if you want to see the application again. The Main class generates a simple scene containing a blue cube. Let's take a closer look at the code template to learn how to structure a jMonkeyEngine application.

What just happened?

The scene displayed by our BasicGame template only contains a blue cube, but this is enough to introduce you to the basic process that all scenes have in common, whether it's a haunted dungeon or an alien jungle. The cube, like any other scene object, is initialized in the simpleInitApp() method. Let's walk through the simpleInitApp() method line by line to understand what is happening.

The com.jme3.scene.shape.Box class provides us with a quick and simple box shape for testing. The Box() constructor expects a location, and three values defining the size of the box—we create the shape in the center of the scene, which we express in Java as Vector3f.ZERO. We choose to extend the box's size by 1 in every direction, which results in a 2x2x2 cube. We refer to our Box()object with the variable b. The Java code that creates the box looks as follows:

Box b = new Box(Vector3f.ZERO, 1, 1, 1);

Internally, a Box() object contains numbers specifying the corner points of the cube. The corners describe the shape (mesh) of the object. But a cube is not just corners, it also has faces. The faces are the surfaces that make the cube visible in the first place. In 3D graphics, you must specify a shape and a surface for every object.

jMonkeyEngine has a data type that combines the shape and surface information of an object. This data type is called geometry (com.jme3.scene.Geometry), and we will be using it a lot. Let's create a geometry geom for our cube shape b, and label the geometry Box. In larger scenes, you can use these labels to access geometries.

In code, this step looks as follows:

Geometry geom = new Geometry("Box", b);

Our geometry now has a cube shape; next, we must add the surface information for the cube's faces. In 3D graphics terms, the surface of a geometry has a material (com.jme3.material.Material). In this example, we use a type of material called Unshaded.j3md. This default material definition is built into the engine, and we access it using the assetManager object. You create the material using the following line of code:

Material mat = new Material(assetManager,
    "Common/MatDefs/Misc/Unshaded.j3md");

Every material has parameters that specify how the surface looks. Materials can be very intricate—you can make them look like wood, metal, rock, grass, skin, plastic, water, glass, and so on. The unshaded material that we use here is simple in comparison; for now, we only choose to set this material's Color parameter to Blue. To do that, we use the Blue constant from the com.jme3.math.ColorRGBA class:

mat.setColor("Color", ColorRGBA.Blue);

Now we have a geometry object geom, which contains the cube's shape, and a material object mat containing a plain blue material for the cube's surface. The next step is to combine the two in order to create a blue cube:

geom.setMaterial(mat);

The blue cube is ready, but its geometry only exists in memory. To actually make it visible, you have to attach the object to the scene. Remember that we made this class extend SimpleApplication? We did that so we would inherit a special object—the rootNode. SimpleApplication is preconfigured to draw all objects that are attached to the rootNode; that's just what we need! We add our blue cube geom to the 3D scene using the following command:

rootNode.attachChild(geom);

This short simpleInitApp() method demonstrates the typical initialization process. For each object that you add to the scene, remember to perform the following steps:

  1. Create a shape.
  2. Create a geometry from the shape.
  3. Create a material.
  4. Apply the material to the geometry.
  5. Attach the geometry to the rootNode.