In the previous article, we saw how to create a rectangular mesh using one long triangle strip. I wrote that such a mesh can be used to wrap around a sphere and now I will show how to do it:

AnXcode project for the iPhone is provided with full src code, which you can use in your own project. The core code is written in plain C, so the project should be very easy to port over to GLUT or any other framework.
Note: for this project we made the choice to have X-axis run from left to right, Z-axis from the back of the screen towards the front, and our Y-axis to run from the bottom of the screen towards the top. Our sphere’s radius will be defined as span.
We start by wrapping the 2D mesh into a 3D tube:

We do this by choosing two opposite edges and stitching them together by using the same vertex values. We generate point values P(x,y, z), where y = [-span, ..., 0, ..., span] by using Eq. 1a as shown in Fig. 1, except that our points are P(x,z), not P(x,y):

At this point if we were to set r=0 for the top and bottom rows, and r=span for the rest of the rows then using Eq. 1.a we’d generate a solid cylinder:

We want to create a sphere, however, so we have a little bit more work to do. Our task is to find radius for each row (for the cylinder above, we in fact got 3 rows right already: the top, bottom and the middle row). We can find the radius values using Eq. 1b from Fig. 1. where our r=span, y is generated by some function with range of [-span, ..., 0, ..., span].
A first effort to generate the y would be to use a simple linear function f(x)=y. If we do that, we will create this interesting sphere:

We generated a sphere with bold spots at top and bottom. It lacks detail at the north and south zenith, and instead allocates more triangles to the rest of the rows, which might be exactly what we want, if we wanted to use it for sky view, for example.
To generate “perfect” sphere, however, all we need is to do is to use the circle equation again. Conceptually, we want to use Eq. 1a again, but this time we are only interested in generating y values for half of the circle, so all we need is just one part of the equation, ie: x= r * sin(angle) where r=span, angle is generated by linear function with domain [-span, ..., 0, ..., span] and range [0, 180]
Once we calculate the x, we use it as our y back in Eq. 1b. This time we get our final sphere:

The code with all the pieces put together:
void meshWrapAroundSphere(Mesh *mesh)
{
GLuint vertColumns = mesh->nrOfVerticiesInRow;
GLuint vertRows = mesh->nrOfVerticiesInColumn;
GLfloat heightStep = (2.0f / (vertRows-1));
GLfloat height = -1.0f;
GLfloat angleStep = DEGREES_TO_RADIANS(360.0f / (GLfloat)(vertColumns-1));
for (int r=0; r<vertRows; r++)
{
GLfloat heightScaled = mesh->span * sinf(DEGREES_TO_RADIANS(height*90.0f));
GLfloat radius = 0.0f;
if ((r > 0) && (r < (vertRows-1)))
{
radius = sqrtf((mesh->span*mesh->span)-(heightScaled*heightScaled));
}
GLfloat angle = angleStep;
for (int c=0; c<(vertColumns-1); c++)
{
MeshVertex *vertex = &mesh->verticies[r*vertColumns + c];
vertex->y = heightScaled;
vertex->x = (radius * cosf(angle));
vertex->z = (radius * sinf(angle));
if (c == 0)
{
MeshVertex *vertex2 = &mesh->verticies[r*vertColumns + (vertColumns-1)];
vertex2->x = vertex->x;
vertex2->y = vertex->y;
vertex2->z = vertex->z;
}
angle += angleStep;
}
height += heightStep;
}
}
![]()
While researching this topic I found the following page useful: http://local.wasp.uwa.edu.au/~pbourke/miscellaneous/sphere_cylinder/



