This tutorial is based upon the tutorial written by Hideya Kawahara.
| Aim: | To demonstrate more complex manipulations and user interaction with a 3D application within the lg3d framework. We will create a sphere with the Earth texture and a small handle to move it. |
|---|---|
| Requirements: | This tutorial builds upon Tutorial 1 and Tutorial 2. To use this tutorial you will need a functional installation of the of lg3d. The stable version is the best version to begin with. This version is located at the lg3d-core web site. Follow the "Getting started with Project Looking Glass" link for installation instructions. Note: You do not need to run the lg3d-session version. |
| Additionally, you need to have the Java 1.5 SDK. This is available at the Sun Java web site. | |
| Steps: | |
| Step 1: | Initialize the 3D application |
| Just like in Tutorial 1 the first step is to create a Frame3D. | |
|
|
In this tutorial we will be demonstration two behaviors, RotateAction and ResilientRotateAction. These are conflicting actions (you cannot have an object rotate and rotate resiliently) therefore we must create a container to house the sphere and then we can apply the ResilientRotateAction to the container itself. |
|
|
|
| Step 2: | Create a textured sphere |
| The SimpleAppearance class provides a useful constructor that takes the location of a texture to load. | |
|
|
| Now we can create the sphere using this textured appearance. Be sure to specify Primitive.GENERATE_TEXTURE_COORDS so that the texture coordinates will be generated along with the position coordinates. | |
|
|
| Now the sphere can be added to a Component3D, set it's position, and set the cursor for it. Note we can set a cursor for each Component3D.
|
|
|
|
| Step 3: | Setup some simple behaviors for the earth |
|
A number of actions are provided by the API. I suggest having a look through the org.jdesktop.lg3d.utils.action, org.jdesktop.lg3d.utils.actionadapter and org.jdesktop.lg3d.utils.eventadapter packages. For this tutorial we will be implementing a |
|
|
|
| Step 4: | Add a more complex action adapter |
| To demonstrate a more complex example of an action adapter we will add a handle to the earth to aid in dragging the earth about. | |
|
|
Now we need to allow the handle to be grabbed to drag the sphere about. We use the ComponentMover to enable this action. Note this time we use two arguments - the first one is the item that can be grabbed, and the second is the item moved by the one that we drag. Since we move the entire application, we specify the frame3d object.
|
|
|
|
We can now add the handle to the Container3D |
|
|
|
| Moving the earth about should not be an easy task! Therefore we shall add some visual effects to show the struggle. The following achieves the wiggle motion when the application is dragged. | |
|
|
| First we set the rotation axis for the rotation movement, and use ResilientRotateAction to actually wiggle the application. The Float2Splitter, FloatDiffer and FloatScaler convert and adjust the x and y values generated by MouseDraggedEventAdapter into a float that can be used for the rotation action. | |
| Step 5: | Initialize the container |
| Finally, we can add the frame to the container and initialize it to make it visible, as described in Tutorial 1 step 4. Note, since the sphere has different dimension and has been rotated and scaled, a large hint size should be given to the SceneManager to avoid conflicts with other 3D applications. | |
|
|
| Step 6: | Compile the code |
|
We have finished writing the code. The full version includes the required import statements, and various constructor and main methods. Download Tutorial3.java. Additionally you need the earth texture image. Download earth.jpg - note this needs to be in the same directory as the application is run from (the same directory as the class file if you follow these instructions). Now we need to compile the source file. The tutorial requires the lg3d core library and Java 1.5 to compile. The core library can be found under the lib directory of the lg3d distribution (replace $LG3DHOME with the path to the lg3d distribution).
|
|
|
|
| Step 7: | Run our application |
| To run the tutorial, simply execute the runtutorial script with the name of the class file (without the .class extension) | |
|
|
| The lg3d desktop should be displayed with the earth textured sphere centered as you see below. The handle should be visible and can be grabbed allowing movement of the sphere. | |
![]() |
|