Defining the universe
6.1 Locales and HiResCoord
6.2 View, ViewPlatform, and Locale 6.3 SimpleUniverse
6.4 Background geometry 6.5 Using multiple views 6.6 Summary
One of the fundamental choices you will have to make in your choice of VirtualUniverse configuration is whether to use the SimpleUniverse utility classes or to rely upon the lower level VirtualUniverse classes. By the end of this chapter you should understand the elements of the Java 3D scenegraph rendering model.
Essential elements of the Java 3D scenegraph are covered:
Defining regions within your universe using Locales
•
Attaching the View model to the VirtualUniverse through the ViewPlatform
•
Using multiple Views for rendering
•
SimpleUniverse utility classes
•
Creating Views, Geometry, and PlatformGeometry for Avatars
•
6.1 Locales and HiResCoord
The VirtualUniverse class contains the virtual world that an application developer populates with Geometry, Behaviors, Lights, and so forth. The VirtualUniverse consists of a collection of Locales. A Locale defines a geographical area within the VirtualUniverse and is anchored at a given 3D coordinate. A Locale uses 256−bit coordinates to specify the anchored x, y, and z position. The 256−bit coordinates are stored in a HiResCoord object and allow a Locale to be positioned within a virtual space the size of the known (physical) universe yet also maintain a precision of a Planck length (smaller than the size of a proton).
Most applications do not require more than one Locale; hence the SimpleUniverse class creates a VirtualUniverse instance with a single Locale. SimpleUniverse is derived from
VirtualUniverse and is covered in detail in chapter 3. The default constructor for a Locale positions the Locale at the origin of the VirtualUniverse. Within a Locale, doubles or floats are used to specify the positions of objects. If a double is large enough to represent the positions of all the objects within your scene accurately, then a single Locale should be sufficient.
However, imagine a scenario: your application is to model parts of the galaxy. The model is to contain the planets orbiting the Sun. On the Earth, the model contains a geometry object a few meters across to represent a house. All the objects in the model are to be created to scale.
How would one go about building such a model for the viewer to be able to switch between three different viewing modes?
Galaxy: See the Sun with the planets rotating about it.
•
Earth: See the planet Earth as it spins about its axis.
•
House: View the house on earth.
•
Locales were designed to handle applications such as that just described. The HiResCoordTest example implements the application shown in figure 6.1. Creating multiple Locales is a fairly lengthy process, so the whole example cannot be included here.
Figure 6.1 Views of the three Locales defined in HiResCoordTest.java
The first problem encountered is how to specify the location of a Locale.
From HiResCoordTest.java
protected Locale createLocaleEarth( VirtualUniverse u ) {
int[] xPos = { 0, 0, 0, 0, 0, 0, 0, 0 };
int[] yPos = { 0, 0, 0, 0, 0, 0, 0, 0 };
int[] zPos = { 0, 0, 0, 1, 0, 0, 0, 0 };
HiResCoord hiResCoord = new HiResCoord( xPos, yPos, zPos );
return new Locale( u, hiResCoord );
}
A HiResCoord is created using three 8−element integer arrays. An int is 32 bits, so 8 * 32 = 256 bits.
NOTE The integer at index 0 contains the most significant bit, while the integer at index 7 contains the least significant bit. The decimal point is defined to lie between indexes 3 and 4, that is,
0x0 0x0 0x0 0x0 . 0x0 0x0 0x0 0x0
The 8−element integer array to specify a coordinate can be considered an 8−digit number in base 232. The numbers that can be expressed by such an entity are mind−boggling, but table 6.1 (from the API specification) can be used to get you into the right ballpark for the quantity you are trying to
express.
Table 6.1 Physical dimensions expressed as a power of 2
2n Meters Units
87.29 Universe (20 billion light−years)
69.68 Galaxy (100,000 light−years)
For example, to specify a distance in light−years (9,460 billion kilometers), find from table 6.1 that 253.07 is equal to 9,460 billion kilometers. That is, a 1 at the 53rd bit position will be approximately one light−year.
When mapped into the integer array, the 53rd bit is located within the integer at index 3 – 53/32 = 2. It is the 21st bit (53 – 32) within the third integer of the array. The number 221 equals 0x200000, so setting the integer at index 2 of the integer array to 0x200000 will create a HiResCoord instance that stores approximately a light−year.
The integer at index 3 is defined to store meters, so simply setting the integer at index 3 will define a
HiResCoord in meters. Conversely, the 20th bit to the right of the decimal point position is approximately a micron.
A useful exercise would be to develop a Java class that returned a HiResCoord for quantities expressed in the various units shown in table 6.1.
//creates a Locale that is positioned (232 + 5) meters //away from the origin in the +Z direction.
int[] xPos = { 0, 0, 0, 0, 0, 0, 0, 0 };
int[] yPos = { 0, 0, 0, 0, 0, 0, 0, 0 };
int[] zPos = { 0, 0, 1, 5, 0, 0, 0, 0 };
HiResCoord hiResCoord = new HiResCoord( xPos, yPos, zPos );
Once a Locale has been created and positioned, it can be populated with geometry by attaching a BranchGroup containing scenegraph elements:
//create the Universe
m_Universe = new VirtualUniverse();
//create the position for the Locale int[] xPos = { 0, 0, 0, 0, 0, 0, 0, 0 };
int[] yPos = { 0, 0, 0, 0, 0, 0, 0, 0 };
int[] zPos = { 0, 0, 1, 5, 0, 0, 0, 0 };
HiResCoord hiResCoord = new HiResCoord( xPos, yPos, zPos );
//create the Locale and attach to the VirtualUniverse Locale locale = new Locale( u, hiResCoord );
//create the BranchGroup containing the Geometry for the scene
BranchGroup sceneBranchGroup = createSceneBranchGroup();
sceneBranchGroup.compile();
//add the scene BranchGroup to the Locale locale.addBranchGraph( sceneBranchGroup );
Note that this code will not yet make the geometry visible until a View and a ViewPlatform are created.
This is the subject of the next section.