Mapping an Image Object onto a Sphere
This example maps an image containing world elevation data onto the surface of a sphere and displays the result using the XOBJVIEW utility. This utility automatically creates the window object and the view object. Therefore, this example creates an object based on IDLgrModel that contains the sphere, the image and the image palette, as shown in the conceptual representation in the following figure.
Note
For an example that maps a satellite image onto Digital Elevation Model data, see Mapping an Image onto Elevation Data (Image Processing in IDL).
Complete the following steps for a detailed description of the process.
Example Code
See maponsphere_object.pro in the examples/doc/objects subdirectory of the IDL installation directory for code that duplicates this example. Run the example procedure by entering maponsphere_object at the IDL command prompt or view the file in an IDL Editor window by entering .EDIT maponsphere_object.pro.
- Select the world elevation image. Define the array, read in the data and close the file.
- Use the MESH_OBJ procedure to create a sphere onto which the image will be mapped. The following invocation of MESH_OBJ uses a value of 4, which represents a spherical mesh:
- Initialize the display objects. In this example, it is necessary to define a model object that will contain the sphere, the image and the color table palette. Using the syntax,
oNewObject= OBJ_NEW('Class_Name'), create the model, palette and image objects: - Create texture coordinates that define how the texture map is applied to the mesh. A texture coordinate is associated with each vertex in the mesh. The value of the texture coordinate at a vertex determines what part of the texture will be mapped to the mesh at that vertex. Texture coordinates run from 0.0 to 1.0 across a texture, so a texture coordinate of [0.5, 0.5] at a vertex specifies that the image pixel at the exact center of the image is mapped to the mesh at that vertex.
- Enter the following line to initialize a polygon object with the image and geometry data using the IDLgrPolygon::Init function. Set
SHADING = 1for gouraud (smoother) shading. Set theDATAkeyword equal to the sphere defined with the MESH_OBJ function. SetCOLORto draw a white sphere onto which the image will be mapped. SetTEXTURE_COORDequal to the texture coordinates created in the previous steps. Assign the image object to the polygon object using theTEXTURE_MAPkeyword and force bilinear interpolation: - Add the polygon containing the image and the palette to the model object:
- Rotate the model -90° along the x-axis and y-axis:
- Display the results using XOBJVIEW, an interactive utility allowing you to rotate and resize objects:
file = FILEPATH('worldelv.dat', $
SUBDIRECTORY = ['examples', 'data'])
image = READ_BINARY(file, DATA_DIMS = [360, 360])
MESH_OBJ, 4, vertices, polygons, REPLICATE(0.25, 101, 101)When the MESH_OBJ procedure completes, the vertices and polygons variables contain the mesh vertices and polygonal mesh connectivity information, respectively. Although our image is 360 by 360, we can texture map the image to a mesh that has fewer vertices. IDL interpolates the image data across the mesh, retaining all the image detail between polygon vertices. The number of mesh vertices determines how close to perfectly round the sphere will be. Fewer vertices produce a sphere with larger facets, while more vertices make a sphere with smaller facets and more closely approximates a perfect sphere. A large number of mesh vertices will increase the time required to draw the sphere. In this example, MESH_OBJ produces a 101 by 101 array of vertices that are located in a sphere shape with a radius of 0.25.
oModel = OBJ_NEW('IDLgrModel')
oPalette = OBJ_NEW('IDLgrPalette')
oPalette -> LOADCT, 33
oPalette -> SetRGB, 255, 255, 255, 255
oImage = OBJ_NEW('IDLgrImage', image, PALETTE = oPalette)
The previous lines initialize the oPalette object with the color table and then set the final index value of the red, green and blue bands to 255 (white) in order to use white (instead of black) to designate the highest areas of elevation. The palette object is created before the image object so that the palette can be applied when initializing the image object. For more information, see IDLgrModel::Init, IDLgrPalette::Init and IDLgrImage::Init.
In this example, we want to do a simple linear mapping of the texture around the sphere, so we create a convenience vector that describes the mapping in each of the texture's x- and y-directions, and then create these texture coordinates:
vector = FINDGEN(101)/100. texure_coordinates = FLTARR(2, 101, 101) texure_coordinates[0, *, *] = vector # REPLICATE(1., 101) texure_coordinates[1, *, *] = REPLICATE(1., 101) # vectorThe code above copies the convenience vector through the array in each direction.
oPolygons = OBJ_NEW('IDLgrPolygon', SHADING = 1, $
DATA = vertices, POLYGONS = polygons, $
COLOR = [255, 255, 255], $
TEXTURE_COORD = texure_coordinates, $
TEXTURE_MAP = oImage, /TEXTURE_INTERP)
Note
When mapping an image onto an IDLgrPolygon object, you must specify both TEXTURE_MAP and TEXTURE_COORD keywords.
After displaying the object, you can rotate the sphere by clicking in the display window and dragging your mouse. Select the magnify button and click near the middle of the sphere. Drag your mouse away from the center of the display to magnify the image or toward the center of the display to shrink the image. Select the left-most button on the XOBJVIEW toolbar to reset the display. The previous figure shows a rotated and magnified view of the world elevation object.

