00001 #ifndef _VCG_GL_GEOMETRY_
00002 #define _VCG_GL_GEOMETRY_
00003
00004
00005
00006
00007
00008
00009
00010
00011 #include<stdlib.h>
00012 #include<math.h>
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 static void fghCircleTable(double **sint,double **cost,const int n)
00025 {
00026 int i;
00027
00028
00029
00030 const int size = abs(n);
00031
00032
00033
00034 const double angle = 2*M_PI/(double)( ( n == 0 ) ? 1 : n );
00035
00036
00037
00038 *sint = (double *) calloc(sizeof(double), size+1);
00039 *cost = (double *) calloc(sizeof(double), size+1);
00040
00041
00042
00043 if (!(*sint) || !(*cost))
00044 {
00045 free(*sint);
00046 free(*cost);
00047 abort();
00048 }
00049
00050
00051
00052 (*sint)[0] = 0.0;
00053 (*cost)[0] = 1.0;
00054
00055 for (i=1; i<size; i++)
00056 {
00057 (*sint)[i] = sin(angle*i);
00058 (*cost)[i] = cos(angle*i);
00059 }
00060
00061
00062
00063 (*sint)[size] = (*sint)[0];
00064 (*cost)[size] = (*cost)[0];
00065 }
00066
00067
00068
00069
00070 static void glutSolidSphere(GLdouble radius, GLint slices, GLint stacks)
00071 {
00072 int i,j;
00073
00074
00075
00076 double z0,z1;
00077 double r0,r1;
00078
00079
00080
00081 double *sint1,*cost1;
00082 double *sint2,*cost2;
00083
00084
00085
00086 fghCircleTable(&sint1,&cost1,-slices);
00087 fghCircleTable(&sint2,&cost2,stacks*2);
00088
00089
00090
00091 z0 = 1.0;
00092 z1 = cost2[(stacks>0)?1:0];
00093 r0 = 0.0;
00094 r1 = sint2[(stacks>0)?1:0];
00095
00096 glBegin(GL_TRIANGLE_FAN);
00097
00098 glNormal3d(0,0,1);
00099 glVertex3d(0,0,radius);
00100
00101 for (j=slices; j>=0; j--)
00102 {
00103 glNormal3d(cost1[j]*r1, sint1[j]*r1, z1 );
00104 glVertex3d(cost1[j]*r1*radius, sint1[j]*r1*radius, z1*radius);
00105 }
00106
00107 glEnd();
00108
00109
00110
00111 for( i=1; i<stacks-1; i++ )
00112 {
00113 z0 = z1; z1 = cost2[i+1];
00114 r0 = r1; r1 = sint2[i+1];
00115
00116 glBegin(GL_QUAD_STRIP);
00117
00118 for(j=0; j<=slices; j++)
00119 {
00120 glNormal3d(cost1[j]*r1, sint1[j]*r1, z1 );
00121 glVertex3d(cost1[j]*r1*radius, sint1[j]*r1*radius, z1*radius);
00122 glNormal3d(cost1[j]*r0, sint1[j]*r0, z0 );
00123 glVertex3d(cost1[j]*r0*radius, sint1[j]*r0*radius, z0*radius);
00124 }
00125
00126 glEnd();
00127 }
00128
00129
00130
00131 z0 = z1;
00132 r0 = r1;
00133
00134 glBegin(GL_TRIANGLE_FAN);
00135
00136 glNormal3d(0,0,-1);
00137 glVertex3d(0,0,-radius);
00138
00139 for (j=0; j<=slices; j++)
00140 {
00141 glNormal3d(cost1[j]*r0, sint1[j]*r0, z0 );
00142 glVertex3d(cost1[j]*r0*radius, sint1[j]*r0*radius, z0*radius);
00143 }
00144
00145 glEnd();
00146
00147
00148
00149 free(sint1);
00150 free(cost1);
00151 free(sint2);
00152 free(cost2);
00153 }
00154
00155
00156
00157
00158 static void glutWireSphere(GLdouble radius, GLint slices, GLint stacks)
00159 {
00160 int i,j;
00161
00162
00163
00164 double r;
00165 double x,y,z;
00166
00167
00168
00169 double *sint1,*cost1;
00170 double *sint2,*cost2;
00171
00172
00173
00174 fghCircleTable(&sint1,&cost1,-slices );
00175 fghCircleTable(&sint2,&cost2, stacks*2);
00176
00177
00178
00179 for (i=1; i<stacks; i++)
00180 {
00181 z = cost2[i];
00182 r = sint2[i];
00183
00184 glBegin(GL_LINE_LOOP);
00185
00186 for(j=0; j<=slices; j++)
00187 {
00188 x = cost1[j];
00189 y = sint1[j];
00190
00191 glNormal3d(x,y,z);
00192 glVertex3d(x*r*radius,y*r*radius,z*radius);
00193 }
00194
00195 glEnd();
00196 }
00197
00198
00199
00200 for (i=0; i<slices; i++)
00201 {
00202 glBegin(GL_LINE_STRIP);
00203
00204 for(j=0; j<=stacks; j++)
00205 {
00206 x = cost1[i]*sint2[j];
00207 y = sint1[i]*sint2[j];
00208 z = cost2[j];
00209
00210 glNormal3d(x,y,z);
00211 glVertex3d(x*radius,y*radius,z*radius);
00212 }
00213
00214 glEnd();
00215 }
00216
00217
00218
00219 free(sint1);
00220 free(cost1);
00221 free(sint2);
00222 free(cost2);
00223 }
00224
00225 #endif