#include #include #include #include #define PI 3.141592654 #define TWOPI 6.283185308 void Cartesian2spheric( double x, double y, double z, double radius, double *s, double *t) { *t = acos( z /radius ) / PI; if (y >= 0 ) { *s = acos( x / (radius * sin( PI * (*t) ) ) ) / TWOPI; } else { *s = ( PI + acos( x / ( radius * sin( PI * (*t) ) ) ) ) / TWOPI; } } void Spheric2cartesian( double rayon, double latitude, double longitude, double *x, double *y, double *z ) { *x = rayon * cos( latitude ) * cos( longitude ); *y = rayon * cos( latitude ) * sin ( longitude ); *z = rayon * sin( latitude ); } void DrawSphere( double r, int n ) { int i, j; double phi1, phi2, theta; double s, t; double ex, ey, ez; if (r < 0) r = -r; if (n < 0) n = -n; if (n < 4 || r <= 0) { glBegin(GL_POINTS); glVertex3f(0, 0, 0); glEnd(); return; } for ( j = 0 ; j < n ; j++ ) { phi1 = j * TWOPI / n; phi2 = (j + 1) * TWOPI / n; //next phi glBegin(GL_QUAD_STRIP); for ( i = 0 ; i <= n ; i++ ) { theta = i * PI / n; ex = sin ( theta ) * cos ( phi2 ); ey = sin ( theta ) * sin ( phi2 ); ez = cos ( theta ); glNormal3f(ex,ey,ez); s = phi2 / TWOPI; // column t = 1.0f - theta / PI; // row glTexCoord2f(s, t); glVertex3f( r * ex , r * ey , r * ez ); ex = sin ( theta ) * cos ( phi1 ); ey = sin ( theta ) * sin ( phi1 ); ez = cos ( theta ); glNormal3f(ex,ey,ez); s = phi1 / TWOPI; // column t = 1.0f - theta / PI; // row glTexCoord2f(s, t); glVertex3f( r * ex , r * ey , r * ez ); } glEnd(); } }