# Sphere With Lines

Hello Everyone;

I am trying to write a program to draw a sphere
using lines. Eg find a point and draw to next point. I would have to use sphereical to cartesian coordinate math…I think. Can anyone help???

Originally posted by Stan Swank II:
[b]Hello Everyone;

I am trying to write a program to draw a sphere
using lines. Eg find a point and draw to next point. I would have to use sphereical to cartesian coordinate math…I think. Can anyone help???[/b]
I found this code… haven’t tried it myself but hope it helps. It’s from a demo in NVIDIA website if I remember correctly.

``````

#define DTOR            (M_PI/180.0)

static void
vert(float theta, float phi)
{
float r = 0.75f;
float x, y, z, nx, ny, nz;

nx = sin(DTOR * theta) * cos(DTOR * phi);
ny = sin(DTOR * phi);
nz = cos(DTOR * theta) * cos(DTOR * phi);
glNormal3f(nx, ny, nz);

x = r * sin(DTOR * theta) * cos(DTOR * phi);
y = r * sin(DTOR * phi);
z = -ZTRANS + r * cos(DTOR * theta) * cos(DTOR * phi);
glVertex4f(x, y, z, 1.0);
}

static void
DrawSphere(float del)
{
float phi, phi2, theta;

glColor4f(1.0, 1.0, 1.0, 1.0);
for (phi = -90.0f; phi < 90.0f; phi += del) {
glBegin(GL_TRIANGLE_STRIP);

phi2 = phi + del;

for (theta = -90.0f; theta <= 90.0f; theta += del) {
vert(theta, phi);
vert(theta, phi2);
}
glEnd();
}
}
``````

This code does not work… I put it in a program and there was no output

Sorry I lied I could use Quads If needed.

Make a cube centered around the origin, subdivide the cube faces. Normalize each vertex (as a vector from the origin).

The results is the normal and vertex of a unit radius sphere. Duplicate for separate normals and then scale the vertices to the desired sphere size and translate to the desired location.

Draw the data with vertices and normals.

This approach is better behaved than polar coordinates.

You could start with another platonic solid and you could subdivide and normalize recursively for more even spacing, but that’s a subtle refinement and a cube is easy.

P.S. if you want to subsequently texture the sphere an octahedron may be a better starting point, but subdivision would obviously have to be triangular.