I have only two rules which I regard as principles of conduct.
The first is: Have no rules.
The second is: Be independent of the opinion of others.
-Albert Einstein
In everything, do to others what you would want them to do to you.
-Jesus
We need to be the change we wish to see in the world.
-Mahatma Gandhi
If I have seen further it is only by standing on the shoulders of giants.
-Isaac Newton
It is not enough to have a good mind.
The main thing is to use it well.
-Descartes
In the preceeding two lessons we have achieved nothing remarkable that openGL tutorials not has done for many years already. This is the lesson where the real virtual reality part begins. We will here learn how to use an orientation handler to handle the camera direction, so that we can start looking in different directions in the scene. Hopefully the application produced in this lesson will thrill your imagination and make your heart beat a little faster.
For the complete VRFirst.java file produced in this lesson, click
here.
For the complete application produced in this lesson, click
here.
Creating a couple of objects to look at
The first thing we would like to do is to create a couple of objects to look at. To do this, replace the code in the onCreate-method from the last lesson with the following content
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Window win = getWindow();
win.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main);
virtualRealityView =
(VirtualRealityView)findViewById(R.id.virtualRealityView);
BasicScene basicScene = new BasicScene();
float coordinates[][] = {{0, 0, -10}, {0, 0, 10}, {0, -10, 0},
{0, 10, 0}, {-10, 0, 0}, {10, 0, 0},
{0, 10, 10}, {10, 5, 0}, {5, 5, 5},
{-10, -10, -10}};
for(int c = 0; c < coordinates.length; c++){
Block block = new Block(new MathVector(1, 1, 1));
block.setPosition(new MathVector(coordinates[c][0],
coordinates[c][1],
coordinates[c][2]));
basicScene.addRenderable(block);
}
virtualRealityView.setScene(basicScene);
}
There is nothing new to this code from that in the last lesson, except that we now add ten Blocks rather than one to the scene. The creation of the BasicScene has also been moved to appear before the creation of the blocks, so that we can add the Blocks one after one to the scene as they are created.
VROrientationHandler
The camera direction in the VirtualRealityView is handled by the implementation of VROrientationHandler that is registered to the VirtualRealityView with setOrientationHandler-method. It is possible to write a custom orientation handler by implementing the VROrientationHandler interface, but we will here use the prepacked LocalOrientationHandler, which acts as if there was a local coordinate system with the x-axis pointing to the east, the y-axis pointing to the north, and the z-axis pointing to the sky.
To create a LocalOrientationHandler, all we have to do is to import it with
import com.dafer45.virtualreality.LocalOrientationHandler;
create a LocalOrientationHandler reference in the VRFirst class
private LocalOrientationHandler localOrientationHandler;
and then create and add one to the VirtualRealityView. To do this, add the following two lines to the onCreate-method.
localOrientationHandler = new LocalOrientationHandler(this);
virtualRealityView.setOrientationHandler(localOrientationHandler);
The LocalOrientationHandler uses the magnetic field, and acceleration sensors to determine the orientation of the phone. Because it registers to recieve updates from these, it need to unregister from them when the application is destroyed. This is handled by the destroy-method which should be called from the activitys onDestroy-method. Therefore add the following code to VRFirst to complete this lesson.
@Override
public void onDestroy(){
localOrientationHandler.destroy();
super.onDestroy();
}
Continue with lesson 4