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.
No comments:
Post a Comment