Now with new hope some will be proud
This is no hoax, no one pushed out
Receive a reprieve and be a pioneer
Break new ground of a new frontier
New ideas will surely get by
No deed, or dividend. Some may ask "Why?"
You'll find the solution, the answers in the sky
-Dave Mustaine
A hundred times every day I remind myself that my
inner and outer life are based on the labors of
other men, living and dead, and that I must exert
myself in order to give in the same measure as I
have received and am still receiving...
-Albert Einstein
The worthwhile problems are the ones you can really solve or help solve,
the ones you can really contribute something to. ...
No problem is too small or too trivial if we can really do something about it.
-Richard Feynman
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.
What we are is the sum of 1000 lives
What we know is almost nothing at all
But we are what we are until the day we die
Together we will find the strength to go on
-Rise against (intentionally missquoted and
taken out of context)
We need to be the change we wish to see in the world.
-Mahatma Gandhi
Seven social sins:
politics without principles,
wealth without work,
pleasure without conscience,
knowledge without character,
commerce without morality,
science without humanity,
and worship without sacrifice.
-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.
A loud "Fuck yeah!"
-From my heart (with James Hetfield's voice)
Contact me at:
Virtual reality - Lesson 3
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 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(; 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.

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