PDA

View Full Version : Sphere With Lines



Stan Swank II
01-30-2006, 07:45 AM
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?????

Elefagente Secreto
01-30-2006, 09:37 AM
Originally posted by Stan Swank II:
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?????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();
}
}

Stan Swank II
01-30-2006, 11:51 AM
This code does not work.. I put it in a program and there was no output

Stan Swank II
01-30-2006, 06:44 PM
Sorry I lied I could use Quads If needed.

dorbie
02-01-2006, 12:19 PM
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.

dorbie
02-01-2006, 12:32 PM
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.