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 previous lesson you learned how to setup a virtual reality project, but the result was nothing more than a black screen. In this lesson we will continue to build on the previous project and learn how to display a first object on the screen. While doing so we will learn about the MathVector, Renderable, Block, Scene, and BasicScene classes, which will be used over and over again in these lessons.
As this lesson proceeds we will add new lines of code to the VRFirst.java file, and these will not be added in the order they finally appear in that file, but rather as they become logical within the context of the description. The complete file as it should appear when this lesson is finished can therefore be downloaded from
here. If you at any moment get confused about where in the file a particular line of code should be added, you can can look it up from there.
For the complete application produced in this lesson, click
here. (Still no virtual reality, you have to wait until lesson 3.)
Obtaining the VirtualRealityView
The first thing we need to do in order to use our VirtualRealityView is to obtain it from the Activity. To do this, first import the VirtualRealityView in the VRFirst class file with the line
import com.dafer45.virtualreality.VirtualRealityView;
Then, in the VRFirst class, add a VirtualRealityView reference so that the VirtualRealityView object can be obtained from any of the VRFirst methods.
private VirtualRealityView virtualRealityView;
Finally, in the onCreate-method and after the setContentView command, add the following line to obtain the VirtualRealityView from the activity:
virtualRealityView =
(VirtualRealityView)findViewById(R.id.virtualRealityView);
MathVector
The virtual reality package comes equiped with a class that simplifies mathematical vector operations. This class is called MathVector and will be used already in this first lesson. If you are familiar with vector algebra, you can use the MathVector to obtain scalar products, cross products, norms, etc., and the available opperations will be described in a later appendix. However, at the moment the only thing you have to understand about vectors are that they are directed line segments that starts at the origo and ends at some coordinate. These vectors will here be used to describe the possition of our object, which in this case is a block, as well as to specify the "half diagonal" of this block.
To be able to use the MathVector class, import it with
import com.dafer45.utilities.MathVector;
Renderable
It is now time to create our object that we want to display. For an object to be renderable by the VirtualRalityView, it has to implement the Renderable interface (com.dafer45.virtualreality.renderable.Renderable). In a later lesson we will see how a custom Renderable can be implemented. But in this lesson we will for simplicity use the built in Renderable called Block. To use this Renderable we first import it with the line
import com.dafer45.virtualreality.renderable.predefined.Block;
Then, in the onCreate-method, add the following lines to create a Block with side length two and possitioned at the coordinate (0, 0, -10).
Block block = new Block(new MathVector(1, 1, 1));
block.setPosition(new MathVector(0, 0, -10));
Now a note on the use of MathVector in this code. In the second line the blocks position is set by calling the setPosition-method with a MathVector, and if you have at least a basic understanding of vectors and coordinate systems the line should be quite self-explaning. The first line does however need some clarification. The MathVector passed to the Block-constructor tells it to construct a Block with corners at the coordinates (±1, ±1, ±1), and the MathVector that is passed can be seen as a half diagonal to the block that points from its center to one of the corners.
If you are a little unfamiliar with vectors, I recommend that once you have completed the rest of this lesson and have a working application, that you play around with the values of these two vectors to get some intuition about their use. Start by modifying the position values, and once you understand them, continue by experimenting with the values in the Block-constructor.
Scene
All Renderable objects that are to be rendered has to be provided to the VirtualRealityView through a class that implements the Scene interface. A scene is responsible for keeping track of all objects and of handling dynamics such as moving Renderables. We will in a later lesson see how to implement a custom Scene, but in this lesson the predefined BasicScene fills our needs.
The BasicScene is a simple container for the Renderables which can be used for any static scene. In this case we do not mean totaly static, as it still is possible to move the viewpoint and change the viewing direction dynamically within the scene. The BasicScene will therefore serve our needs for several lessons to come as we start to learn how to look in different directions and even move in it.
To be able to use the BasicScene we import it with
import com.dafer45.virtualreality.BasicScene;
The next thing we do is to create a BasicScene, add our previously created Renderable to it, and finally set the Scene to be rendered by the VirtualRealityView. To do this, add the following lines to the onCreate-method
BasicScene basicScene = new BasicScene();
basicScene.addRenderable(block);
virtualRealityView.setScene(basicScene);
Lesson 2 is over, and a note on the coordinate system
The application is now ready to compile, and should now display a white block at the center of the screen. Before we proceede to orient the camera in different directions in the next lesson, it might however be of interest to know how the coordinate system is oriented as it still is "fixed" to the phone. Especially if you at this moment want to experiment a little with the MathVector parameters mentioned above. Actullay, everyone should experiment a little with those parameters at this moment. So I encourage you to do so even if you feel completely comfortable with vectors.
So how is the coordinate system oriented? Well, if you hold the phone in front of you, the x-axis points to the right, the y-axis to the top of the phone, and the z-axis is pointing stright at you. This is the resason that we place the block at the coordinate (0, 0, -10) as x=y=0 is in the middle of the screen, while z = -10 is at the opposite side of the screen as you are and therefore is visible as you look "through" the screen.
After having experimented a little with the vector, be sure to hurry on to the next lesson, that is where the real adventure begins!
Continue with lesson 3