Welcome!

Java IoT Authors: Zakia Bouachraoui, Pat Romanski, Elizabeth White, Liz McMillan, Yeshim Deniz

Related Topics: Java IoT

Java IoT: Article

Bringing Mars Down to Earth with Java3D

Exploring Java3D

You've probably seen the breathtaking photographs of the surface of Mars from the rovers Spirit and Opportunity. You've also seen the amazing high-altitude photographs taken from satellites orbiting Mars. But let's face it, the rovers must land on flat, boring areas and the satellites are too high above the interesting places like canyons and craters. What is an Earth-bound Java programmer to do? Using the Java3D API and publicly available data, you can create pictures like Figure 1.

The Java3D API is robust enough to handle just about any 3D programming job. The complexity can also make a grown man cry. This article covers the Java3D basics with an emphasis on producing something from another world without the tears. We'll first go over the anatomy of a scene - how to add shapes to the scene, light it, and explore it by moving the view. In the end you'll have a way to explore any location on Mars from any angle moving through the landscape in real time.

What Is Java3D?
Java3D is a sophisticated programming interface intended for rendering three-dimensional scenes and sounds. The latest version is 1.3.1 and supports DirectX and OpenGL. There is substantial support for vector math, animation, lighting, shading, fog, stereo images, collision detection, texturing, loading three-dimensional models from other products, and interactions with the universe you create.

Your Java3D universe consists of a tree structure called a scene graph that contains the shapes, lights, and virtual cameras called views. Java3D automatically renders the scene, properly lighting the shapes based on the view, and changing the view based on user input. Java3D takes care of details like removing hidden surfaces and applying textures to surfaces. The scene graph is the center of your Java3D universe.

Creating a Scene
A VirtualUniverse has a scene graph created as a tree structure with a root of a Locale object (see Figure 2). All other nodes in the scene graph form a parent-child relationship with other nodes in the tree. A scene graph separates the content of the universe from the viewing parameters at the Locale root. The nodes of the content and view branches of the tree are grouped with a BranchGroup. A group node can have multiple children and one parent in the tree. Another example of a group node is a TransformGroup. As the name implies, the TransformGroup is for uniformly translating, scaling, and rotating child nodes. In our example, a TransformGroup is used on the view branch to allow the virtual camera to be moved in the scene.

Figure 2 shows additional objects that make up the view branch, which are beyond the scope of this article. Luckily, Java3D has a SimpleUniverse utility class so it's easy to create the universe and view branch. The SimpleUniverse object allows you to add your content branch and access the view transform group to change the view.

The content branch contains the shapes and lights in your scene. User interaction with the scene is accomplished by adding behaviors to the content branch. A behavior links keyboard, mouse, or temporal events with changes to the content or view. For example, the keyboard or mouse can be used to update the view transform, allowing the user to move the virtual camera through the scene. Time can be used to animate the movement or morphing of shapes in your scene. For our purposes we'll use a modified version of the Java3D KeyNavigator behavior to move over the surface of Mars. There are many other predefined behaviors in Java3D and you can create your own as well. The following code shows how to build the universe and scene.


				...
				1		canvas = new Canvas3D(config);
				2		universe = new SimpleUniverse(canvas);
				3		BranchGroup content=createSceneGraph();
				4		addBehaviors(content);
				5		addLights(content);
				6		content.compile();
				7		universe.addBranchGraph(content);
				     // The scene is ready to render

This code includes the steps taken in the Java3DApplication class included in the source code. (The source code for this article can be downloaded from www.sys-con.com/java/sourcec.cfm.) This class implements a common recipe for Java3D applications.

Shaping Up
Visual objects in Java3D are represented in the content branch by instances or subclasses of Shape3D. A shape maintains references to an appearance and geometry. Java3D considers the appearance and geometry to be node components. Node components are associated with tree nodes through a reference relationship as shown in Figure 2. Nodes in the scene graph determine what is rendered while node components determine how the nodes are rendered. Node components are not technically considered part of the scene graph because multiple shape nodes can share an appearance or geometry.

The geometry object determines the topology of the shape. There are several utility geometries available to create cubes, spheres, cylinders, and cones. These utility geometries are useful for learning, prototyping, or even assembling larger composite visual objects. More complex geometries can be created with subclasses of GeometryArray that organize arrays of triangles, lines, points, or quadrilaterals. Creating geometries with the GeometryArray subclasses is complex, but thankfully the GeometryInfo utility class drastically simplifies the process. We'll use the GeometryInfo class with a QuadArray to create the landscape geometry for Mars.

The appearance object is used to describe the color, material, polygon, texture, and transparency attributes of a shape (see Figure 3). A ColoringAttributes object defines the color of the shape in an unlit scene as well as the type of shading to use (i.e., flat or smooth). The Material object specifies the color of the entire shape in a lit scene as well as the shininess of the shape. The PolygonAttributes object is used to render the shape as points, a wire frame, or a solid, and determine which polygons are to be culled. The TextureAttributes object controls how textures are combined with the shape color if the shape is textured. Texturing is valuable when there is a need to present a higher level of detail than what is available with the geometry. As you might have guessed by now, the TransparencyAttributes object helps you render objects such as glass or water.

Now that you have your shape defined with a geometry and appearance, all you have left to do is add the shape to the content branch group. This is done by using the addChild() method on the branch group.

Shedding Light on Your Scene
Very simple scenes don't require lighting; however, if we are to render a realistic landscape, lighting is very important. Java3D comes through with support for ambient, directional, point, and spotlights. Ambient light has no location or direction but still illuminates the scene. Directional lights have no location but have a direction specified by a vector. Point lights provide attenuated light from a position and illuminate in all directions. Spotlights also provide attenuated light from a location as well as direction with added control of the spread angle and concentration. For all types of lights you can specify the color and the region of influence. A region of influence is the volume of space in the scene that the light affects. Adding lights to a scene is as easy as using the addChild() method on the content branch group.

A typical scene uses ambient light combined with two directional lights. Our Mars landscape uses ambient light and two spotlights.

Creating the Landscape of Mars
With these Java3D concepts under our belt, it's time to turn our attention to creating content. We need a way to create realistic geometry and appearance objects for a Mars landscape. This is where we can use publicly available data to do both. Several successful Mars missions have accumulated terabytes of photos and data. The Mars Global Surveyor (MGS) was one of those missions that carried several scientific instruments.

One of the instruments aboard the MGS was the Mars Orbiter Laser Altimeter (MOLA). If you've been to a home improvement store lately you'll conceptually understand how this instrument works. The latest home improvement gadget is a laser measuring tape. You point the gadget at a wall and it precisely measures the distance between you and the wall. The MOLA took over one billion measurements while the MGS circled the planet for 2.5 years. At the highest precision, those results were used to create a huge matrix of altitude measurements every 0.00781 degrees of longitude and latitude. This high precision data can detect objects as small as 460 meters, making it ideal for major terrain features, but you won't see the rock-level details seen in rover photographs. Today this data is used by NASA for mission planning and is available for the public to download as MOLA Mission Experiment Gridded Data Records (MEGDR). The MEGDR data is available at other resolutions as well, providing us with a wealth of geometry data.

The MOLA data can be used with a Java3D QuadArray object to create an accurate Mars geometry. The MOLA data is organized as a large two-dimensional matrix with the longitude and latitude serving as the dimensions. The value at a given longitude and latitude is the average altitude for that area. A QuadArray object can be used to draw an array of vertices as individual quadrilaterals using a group of four vertices to define a quadrilateral. We'll use four neighboring MOLA data elements as vertices to create a series of quadrilaterals across an area of interest. The geometry of the planet surface is created by mapping the longitude, latitude, and altitude of the MOLA data to three-dimensional points needed for the vertices of the QuadArray. The following code uses GeometryInfo to easily create a geometry object.


	...
				1	    GeometryInfo gi = new
				GeometryInfo(GeometryInfo.QUAD_ARRAY);
				2			...
				          // Map MOLA data to 3D points in
				          // Point3d[] coordinates variable
				3		 gi.setCoordinates(coordinates);
				4		 // Other texture or coloring tasks 
				5		NormalGenerator ng = new ormalGenerator();
				6	    ng.generateNormals(gi);
				7	    Geometry g = gi.getGeometryArray();

Once the MOLA data is converted into three-dimensional points, a NormalGenerator can be used to finish the geometry object. A normal is a unit vector that defines the orientation of a surface such as a quadrilateral or triangle. Java3D uses the normal of a surface for hidden surface removal and to render the lighting effects of visible surfaces. The NormalGenerator computes the normal vectors for the quadrilaterals in the QuadArray and stores the results in the GeometryInfo object. You may have noticed that we skipped over line 4. This is where some impressive effects can be implemented with Java3D: vertex coloring and texture mapping.

Vertex Coloring
The Jet Propulsion Lab (JPL) and the MOLA Science Team have published numerous color-coded topographical maps of Mars. Colors are used to signify altitude ranges with colors blending nicely from one altitude to the next. This technique can be replicated in Java3D by using the GeometryInfo and vertex coloring. Java3D uses the Material object of a shape to determine the color unless the geometry has colors assigned. Colors are assigned to each vertex by first determining the color (based on altitude in this case) for each vertex. The array of colors is then set on the GeometryInfo object by using the setColors() method. The JPL color scheme can be reproduced by using vertex coloring in combination with smooth shading to produce results similar to Figure 4. Other available data such as soil element composition can be used to overlay hematite deposits on your landscape. You could do this with a special vertex color at the location of the hematite deposits or create an image and overlay the image using texture mapping.

Texture Mapping
A photograph is the most striking and realistic overlay. There are literally thousands of pictures of the surface of Mars but we need undistorted pictures of the entire planet. Luckily, the United States Geographical Survey (USGS) Astrogeology Research Program has gone to the trouble of creating precisely what we need. They have taken Viking 2 era photographs and processed them to eliminate spherical distortions and aligned the resulting photograph mosaics with landmarks in the MOLA data. The result is called the Mars Global Digital Image Mosaic (MDIM), and includes 30 files covering the planet in a form we can use, except for the poles. The highest-resolution photographs are four times more detailed than the MOLA data, but still represent about a football field per pixel. Future releases of MDIM will include the poles and even more higher-resolution photographs.

The number of files in the MDIM poses a challenge to allow the rendering of any location. The MDIM is divided into regions based on the USGS Mars Chart (MC) series of printed maps. The MC regions form a staggered grid and the most useful, high-resolution images are far too large to create one large composite image. The solution is to use the imaging support provided by ImageIO, BufferedImage, and AffineTransform objects to create our own personal mosaic of the area to be rendered. Once this image is created it can be used with Java3D as a photographic overlay.

As Figure 1 shows, Java3D supports the draping of a photograph over the geometry through texture mapping. Java3D automatically renders the photograph as a texture, provided we tell Java3D how to map the pixels in the texture to the vertices in the geometry. Java3D uses a simple texture coordinate system that specifies the horizontal and vertical texture coordinates as values ranging between zero and one. Similar to how we demonstrated vertex colors, texture coordinates must be determined for each geometry vertex. An array of texture coordinates is then set on the GeometryInfo object by using the setTextureCoordinates() method. To finish the appearance of the landscape, the texture and how to combine it with the geometry color is set in the TextureAttributes object. There are many ways to combine the texture color with the geometry color, but we'll use the MODULATE option. This option takes the product of the texture and geometry colors to create the final color. Colors in Java3D use the RGB model with each color component ranging between zero and one. Because the Mars photographs are black and white, the resulting product is a very realistic coloring of the landscape.

Finishing Touches
The appearance of the landscape looks great, but environmental effects such as clouds and a sun will make it even better. Java3D supports enough tools to fake a great atmosphere including a fog shape that can be any color. Using a gray fog will give our Mars landscape more apparent depth. A black fog can be used to create an eerie fade to a dark unknown. The fog does not interact with lights in Java3D nor does the fog have volume, both of which would allow us to create clouds. An atmosphere with clouds takes a bit more creativity, but we have already explored everything you need to know to create a reasonable atmosphere.

The atmosphere in Figure 1 is accomplished by surrounding the scene with a huge textured sphere. The sphere is one of the geometries supplied by Java3D that can automatically create normal vectors and texture coordinates. Simply provide the color and texture in an appearance object and Java3D combines them to create a sky sprinkled with wispy clouds. The atmosphere comes alive when we light it up with a sun.

Our Mars landscape uses ambient light and two spotlights to create the sun in the dusty atmosphere. Here is where we can take advantage of a missing feature of Java3D. Because of the computational requirements, Java3D does not automatically render true shadows. Consequently, light passes through objects and fully lights the area where shadows would naturally occur. The sun can be implemented with a spotlight just outside of the atmosphere sphere. Java3D renders the concentrated light on the sphere as a circle and light passes through the sphere to light the planet surface below. A second spotlight with a wider spread is used to create an aura of the sun. The lighting effects of the spotlights on the atmosphere sphere give the impression of a sun shining down on the landscape.

Explore Away
Java3D is a powerful programming interface able to handle many 3D programming tasks. This article scratched the surface of what is possible, so while you explore the Mars surface think about using Java3D for GIS studies, medical imaging, 3D puzzle games, or educational programs. The sky is no longer the limit.

References

  • Java3D: http://java.sun.com/products/java-media/3D/index.jsp
  • Mars Global Surveyor Mission: http://marsprogram.jpl.nasa.gov/mgs/
  • Mars Orbiter Laser Altimeter: http://marsprogram.jpl.nasa.gov/mgs/sci/mola/mola.html
  • MOLA Science Investigation: http://ltpwww.gsfc.nasa.gov/tharsis/mola.html
  • MOLA MEGDR archive: http://pds-geosciences.wustl.edu/missions/mgs/megdr.html
  • Mars Digital Image Mosaic: http://astrogeology.usgs.gov/Projects/MDIM21/
  • Mars Locations: http://planetarynames.wr.usgs.gov/mare/mareTOC.html
  • Java3D Resources: www.j3d.org

    Acknowledgments

    • MDIM photographs are in the public domain and are courtesy of the USGS Astrogeology Research Program.
    • MOLA data courtesy of the PDS Geosciences Node of Washington University in Saint Louis.
    • Thanks to the MOLA Science team (Neuman) for their help in understanding the JPL topographical color scheme.

    Copyright 2004 © Mayo Foundation for Medical Education and Research

  • More Stories By Mike Jacobs

    Mike Jacobs is technology architect and Technology Fellow focused on using technology to improve health care.

    Comments (4)

    Share your thoughts on this story.

    Add your comment
    You must be signed in to add a comment. Sign-in | Register

    In accordance with our Comment Policy, we encourage comments that are on topic, relevant and to-the-point. We will remove comments that include profanity, personal attacks, racial slurs, threats of violence, or other inappropriate material that violates our Terms and Conditions, and will block users who make repeated violations. We ask all readers to expect diversity of opinion and to treat one another with dignity and respect.


    IoT & Smart Cities Stories
    Moroccanoil®, the global leader in oil-infused beauty, is thrilled to announce the NEW Moroccanoil Color Depositing Masks, a collection of dual-benefit hair masks that deposit pure pigments while providing the treatment benefits of a deep conditioning mask. The collection consists of seven curated shades for commitment-free, beautifully-colored hair that looks and feels healthy.
    The textured-hair category is inarguably the hottest in the haircare space today. This has been driven by the proliferation of founder brands started by curly and coily consumers and savvy consumers who increasingly want products specifically for their texture type. This trend is underscored by the latest insights from NaturallyCurly's 2018 TextureTrends report, released today. According to the 2018 TextureTrends Report, more than 80 percent of women with curly and coily hair say they purcha...
    The textured-hair category is inarguably the hottest in the haircare space today. This has been driven by the proliferation of founder brands started by curly and coily consumers and savvy consumers who increasingly want products specifically for their texture type. This trend is underscored by the latest insights from NaturallyCurly's 2018 TextureTrends report, released today. According to the 2018 TextureTrends Report, more than 80 percent of women with curly and coily hair say they purcha...
    We all love the many benefits of natural plant oils, used as a deap treatment before shampooing, at home or at the beach, but is there an all-in-one solution for everyday intensive nutrition and modern styling?I am passionate about the benefits of natural extracts with tried-and-tested results, which I have used to develop my own brand (lemon for its acid ph, wheat germ for its fortifying action…). I wanted a product which combined caring and styling effects, and which could be used after shampo...
    The platform combines the strengths of Singtel's extensive, intelligent network capabilities with Microsoft's cloud expertise to create a unique solution that sets new standards for IoT applications," said Mr Diomedes Kastanis, Head of IoT at Singtel. "Our solution provides speed, transparency and flexibility, paving the way for a more pervasive use of IoT to accelerate enterprises' digitalisation efforts. AI-powered intelligent connectivity over Microsoft Azure will be the fastest connected pat...
    There are many examples of disruption in consumer space – Uber disrupting the cab industry, Airbnb disrupting the hospitality industry and so on; but have you wondered who is disrupting support and operations? AISERA helps make businesses and customers successful by offering consumer-like user experience for support and operations. We have built the world’s first AI-driven IT / HR / Cloud / Customer Support and Operations solution.
    Codete accelerates their clients growth through technological expertise and experience. Codite team works with organizations to meet the challenges that digitalization presents. Their clients include digital start-ups as well as established enterprises in the IT industry. To stay competitive in a highly innovative IT industry, strong R&D departments and bold spin-off initiatives is a must. Codete Data Science and Software Architects teams help corporate clients to stay up to date with the mod...
    At CloudEXPO Silicon Valley, June 24-26, 2019, Digital Transformation (DX) is a major focus with expanded DevOpsSUMMIT and FinTechEXPO programs within the DXWorldEXPO agenda. Successful transformation requires a laser focus on being data-driven and on using all the tools available that enable transformation if they plan to survive over the long term. A total of 88% of Fortune 500 companies from a generation ago are now out of business. Only 12% still survive. Similar percentages are found throug...
    Druva is the global leader in Cloud Data Protection and Management, delivering the industry's first data management-as-a-service solution that aggregates data from endpoints, servers and cloud applications and leverages the public cloud to offer a single pane of glass to enable data protection, governance and intelligence-dramatically increasing the availability and visibility of business critical information, while reducing the risk, cost and complexity of managing and protecting it. Druva's...
    BMC has unmatched experience in IT management, supporting 92 of the Forbes Global 100, and earning recognition as an ITSM Gartner Magic Quadrant Leader for five years running. Our solutions offer speed, agility, and efficiency to tackle business challenges in the areas of service management, automation, operations, and the mainframe.