need code for texturing the earth image on a sphere

I have the texture map file for the earth and I want to read it in and texture map a sphere. Does anyone have code to do this? I have looked for examples but I could not find it.


Draw a parametric sphere, where you iterate longitude and latitude. Once you can draw a sphere like this you just specify appropriate s & t texture coordinates for each vertex where s maps 0.0 to 1.0 in longitude 0 360 and t maps 0.0 to 1 in latitude -90 to +90.

It’s trivial if you figure out how to draw a sphere. and that’s just a case of sin & cos multipliers on a unit vector.

// epsilon must be significantly smaller than itteration increment
#define epsilon .0001
float long;
float div = 20.0;
float s, t;
float x, x, y;

for(long = 0.0f, t = 0.0; long < M_PI*2.0 - epsilon; long += M_PI*2.0/div, t+=1.0/div)
  for(lat = -M_PI/2.0; lat<=M_PI/2.0+epsilon; lat+=M_PI/div, s+= 1.0/div)
  // rotate unit vector (1.0, 0.0, 0.0) by latitude about z
  // rotate result by longitude about y & store in x, y, z

  glTexCoord2f(s, t);
  glVertex3f(x, y, z);

  // rotate unit vector (1.0, 0.0, 0.0) by latitude+M_PI/div about z
  // rotate result by longitude+M_PI*2.0/div about y & store in x, y, z

  glTexCoord2f(s+1.0/div, t+1.0/div);
  glVertex3f(x, y, z);

That should about do it, you can figure out the rotations yourself. It will generate a degenerate vert at the start and end of each strip, you can fix that easily, by hardcoding the boundary conditions and lopping the start & end of the lat itteration.

Then there’s the problem of the texture singularity at the poles, increase div to reduce the issue.

or you could use a glu quadric, check it out