Sunday, April 27, 2014

Creating 3d video games 6 - Loading the model into Jmonkey

In this tutorial we load the model we created into the Jmonkey engine and view it. Open Jmonkey and under file select new project. Under categories select JME3 and under projects select Basic Game then click next. On the next tab name your project Cow Invasion the click the finish button.

Jmonkey will create a project containing the assets folders for us. Next we want to load the model into our project. Click the green button with the white arrow to load a model.


In the dialogue select open model then browse to the folder containing the model. Jmonkey can load blender , .obj and 3ds files. You can download plug-ins to make more formats available. Select next then on the following screen you will see a model preview window and the status showing what parts of the model loaded successfully. Click next and you will be asked if you want to load the original files in the folder. I decline this option as I only want to use the .j3o format.

In the project panel on the left side expand out the project assets folder. Then expand models. You will see a new folder added containing your model. Notice the model is automatically converted and given the .j30 format. Now we want to add the model into our code. Expand the source package folder then expand the my game folder. Double click the Main java file. Now let's edit the code.

We want to add in the following imports for ambient light , color and spatial.

import com.jme3.app.SimpleApplication;
import com.jme3.light.AmbientLight;
import com.jme3.light.DirectionalLight;
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.Spatial;
import com.jme3.scene.shape.Box;

Now navigate the file and find the simpleinitapp method. Delete the contents within and add the following code.

    Spatial Farm = assetManager.loadModel("Models/Farm/Farm.j3o");
        Farm.scale(0.05f, 0.05f, 0.05f);
    
        rootNode.attachChild(Farm);
   
        AmbientLight sun = new AmbientLight();
        sun.setColor(ColorRGBA.White);
        rootNode.addLight(sun);

The first thing we do is create a spatial. Spatials are our 3d objects and contain data about our models. We use the asset manager to find the file. By default asset manage starts in our project asset folder. We navigate from there down into the models folder then into the farm folder. Next we call the scale method to scale down the farm. By default the farm is very large so we scale it on the x,y,z(Width,Height,Depth) by .05 this will shrink the farm down.

Next we add the model to our scene using the rootNode.attachChild method. We pass the spatial as the only parameter. We will come back to nodes later but for now you only need to know that the root node contains all of the objects and data in our scene.

Next create an ambient light the use the setColor method to set it's color to White. An ambient light scatters light evenly on all objects in our scene. With out lighting we would not be able to see any objects in our scene. Finally we add the light to the root node with the addLight method. Passing the light as the only parameter.



Once you are done save your project and press F6 or the green triangle to run the project. You can use the W,A,S,D keys to move and the drag the mouse to rotate the camera.

In our next tutorial we will add some more movement and collision detection to the scene.

Creating 3d video games 5 - Completing th farm model.

In the previous tutorial we made the landscape for our farm. Now we want to add in the barns and hay bales to complete the model. Here is a link to the completed model http://sourceforge.net/projects/jmonkeytutorial/files/latest/download?source=directory



I used a prefabricated barn model to save time. To the load the barn into the scene we use the plug-ins import 3ds. The dialogue will ask you to browse to the folder containing the file. You want to leave the other settings at default the select ok. Once the barn is loaded you notice it is a little small. Use the object mode scale to resize the barn to roughly 1/10 the size of the farm. Then press Ctrl + C to copy the barn then press Ctrl+V to paste the copy. Rename them to Barn1 and Barn2 respectively. Use the move and rotate functions under object mode to position to the layout we did in tutorial 2.



Next we create 3 square bales of hay. Select cube under the item creation panel and change default height, width and depth to 56. Using the top or front view make the cube one grid wide. In the materials panel select the hay material we added previously then assign it to the cube. Make two more copies of the cube. Rename each cube to bale1,2, and 3. Now use the move tool to reposition them according to our layout. It doesn't have to be exact just close.

The finished model should look something like this.


In the next tutorial we will load the model into Jmonkey and view the scene.