An image file printed on a shape is called a texture. I used the 256 × 256 WorldTile.png from Google Maps.

Source code in GL.zip

  1. main.mm
  2. texture.cpp
  3. Class GLAppDelegate
  4. Class GLViewController
  5. Class EAGLView
  6. GLTools.cpp
  7. WorldTile.tga

tga files

WorldTile.png WorldTile.tga

For the time being, our textures will have to be tga files. To convert other formats to tga, see the free, on-line converters at Go 2 Convert, Convert Hub, etc.

Tga files come in two flavors, with and without RLE compression. The program file will tell you if your tga file has RLE. The following file does not have RLE.

file WorldTile.tga
WorldTile.tga: Targa image data - RGB 256 x 256
For the time being, we will have to use the type without compression. This C program removes the compression from a tga file, creating a new file named tgatest.tga.

The function SetupRC in texture.cpp calls LoadTGATexture, which calls gltReadTGABits in GLTools.cpp, which opens the tga file by calling the C function fopen. To get fopen to work, the main function had to call gltSetWorkingDirectory in GLTools.cpp. Since gltSetWorkingDirectory is a C++ function, I had to change the name of main.m to main.mm.

The width and height in pixels of the tga file can’t be bigger than GL_MAX_TEXTURE_SIZE in gl.h, currently 3379. This excludes the class photo.

The triangle batch

Our shape is the simplest possible GLTriangleBatch, consisting of one triangle. The batch is declared at the top of texture.cpp and created in Setup. The fussy trigonometry makes the triangle equilateral.

A vector is an arrow that indicates a direction. A normal vector is an arrow that is perpendicular to something. Normal means perpendicular. The triangle is flat because the normal vectors that extend from each vertex are parallel.

The triangle does not cover the whole earth. It covers only the triangular area determined by the three vertices in the texture coördinates. These coördinates believe that the origin of the texture is at the lower left corner. The x coördinates increase rightwards from 0 to 1. The x coördinates increase upwards from 0 to 1.

Things to try

  1. Print Texas on the triangle. In the SetupRC function in texture.cpp, change the texture coördinates array to the folloowing.
    	M3DVector2f textureCoordinates[] = {
    		{0.20f, 0.72f},
    		{0.15f, 0.52f},
    		{0.35f, 0.52f}
  2. In texture.cpp, comment out the triangle and comment in the sphere (in three places). Roll the world with your finger.

  3. Is it hard to roll the Earth in the desired direction? Let the axis of rotation be a line that is perpendicular to the direction of the swipe, and also perpendicular to the line from the observer to the center of the Earth. Create the axis by taking the cross product of these two vectors.

  4. Wierd planet: use a torus instead of a sphere.

  5. Research question. How could we use a png file as a texture?

  6. Research question. WorldTile.png was never intended to be a texture printed on a sphere. I picked it only because Google Maps is the iconic image of our era. Find an image file of the whole earth that would print without the horizontal distortion. Can you find a “blue marble” with clouds?

  7. Write an app that gets the current date and time from class NSDate and displays the Earth with the correct hemisphere illiminated. The default point of view should be over New York City (41° N 74° W) or over the subsolar point.

  8. Print a Campbell’s Soup can label on a cylinder.