| 1 |
#ifndef _VCG_GL_GEOMETRY_ |
| 2 |
#define _VCG_GL_GEOMETRY_ |
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
#include<stdlib.h> |
| 12 |
#include<math.h> |
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
static void fghCircleTable(double **sint,double **cost,const int n) |
| 25 |
{ |
| 26 |
int i; |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
const int size = abs(n); |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
const double angle = 2*M_PI/(double)( ( n == 0 ) ? 1 : n ); |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
*sint = (double *) calloc(sizeof(double), size+1); |
| 39 |
*cost = (double *) calloc(sizeof(double), size+1); |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
if (!(*sint) || !(*cost)) |
| 44 |
{ |
| 45 |
free(*sint); |
| 46 |
free(*cost); |
| 47 |
abort(); |
| 48 |
} |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
(*sint)[0] = 0.0; |
| 53 |
(*cost)[0] = 1.0; |
| 54 |
|
| 55 |
for (i=1; i<size; i++) |
| 56 |
{ |
| 57 |
(*sint)[i] = sin(angle*i); |
| 58 |
(*cost)[i] = cos(angle*i); |
| 59 |
} |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
(*sint)[size] = (*sint)[0]; |
| 64 |
(*cost)[size] = (*cost)[0]; |
| 65 |
} |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
static void glutSolidSphere(GLdouble radius, GLint slices, GLint stacks) |
| 71 |
{ |
| 72 |
int i,j; |
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
double z0,z1; |
| 77 |
double r0,r1; |
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
double *sint1,*cost1; |
| 82 |
double *sint2,*cost2; |
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
fghCircleTable(&sint1,&cost1,-slices); |
| 87 |
fghCircleTable(&sint2,&cost2,stacks*2); |
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
z0 = 1.0; |
| 92 |
z1 = cost2[(stacks>0)?1:0]; |
| 93 |
r0 = 0.0; |
| 94 |
r1 = sint2[(stacks>0)?1:0]; |
| 95 |
|
| 96 |
glBegin(GL_TRIANGLE_FAN); |
| 97 |
|
| 98 |
glNormal3d(0,0,1); |
| 99 |
glVertex3d(0,0,radius); |
| 100 |
|
| 101 |
for (j=slices; j>=0; j--) |
| 102 |
{ |
| 103 |
glNormal3d(cost1[j]*r1, sint1[j]*r1, z1 ); |
| 104 |
glVertex3d(cost1[j]*r1*radius, sint1[j]*r1*radius, z1*radius); |
| 105 |
} |
| 106 |
|
| 107 |
glEnd(); |
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
for( i=1; i<stacks-1; i++ ) |
| 112 |
{ |
| 113 |
z0 = z1; z1 = cost2[i+1]; |
| 114 |
r0 = r1; r1 = sint2[i+1]; |
| 115 |
|
| 116 |
glBegin(GL_QUAD_STRIP); |
| 117 |
|
| 118 |
for(j=0; j<=slices; j++) |
| 119 |
{ |
| 120 |
glNormal3d(cost1[j]*r1, sint1[j]*r1, z1 ); |
| 121 |
glVertex3d(cost1[j]*r1*radius, sint1[j]*r1*radius, z1*radius); |
| 122 |
glNormal3d(cost1[j]*r0, sint1[j]*r0, z0 ); |
| 123 |
glVertex3d(cost1[j]*r0*radius, sint1[j]*r0*radius, z0*radius); |
| 124 |
} |
| 125 |
|
| 126 |
glEnd(); |
| 127 |
} |
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
z0 = z1; |
| 132 |
r0 = r1; |
| 133 |
|
| 134 |
glBegin(GL_TRIANGLE_FAN); |
| 135 |
|
| 136 |
glNormal3d(0,0,-1); |
| 137 |
glVertex3d(0,0,-radius); |
| 138 |
|
| 139 |
for (j=0; j<=slices; j++) |
| 140 |
{ |
| 141 |
glNormal3d(cost1[j]*r0, sint1[j]*r0, z0 ); |
| 142 |
glVertex3d(cost1[j]*r0*radius, sint1[j]*r0*radius, z0*radius); |
| 143 |
} |
| 144 |
|
| 145 |
glEnd(); |
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
free(sint1); |
| 150 |
free(cost1); |
| 151 |
free(sint2); |
| 152 |
free(cost2); |
| 153 |
} |
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
static void glutWireSphere(GLdouble radius, GLint slices, GLint stacks) |
| 159 |
{ |
| 160 |
int i,j; |
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
double r; |
| 165 |
double x,y,z; |
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
double *sint1,*cost1; |
| 170 |
double *sint2,*cost2; |
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
fghCircleTable(&sint1,&cost1,-slices ); |
| 175 |
fghCircleTable(&sint2,&cost2, stacks*2); |
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
for (i=1; i<stacks; i++) |
| 180 |
{ |
| 181 |
z = cost2[i]; |
| 182 |
r = sint2[i]; |
| 183 |
|
| 184 |
glBegin(GL_LINE_LOOP); |
| 185 |
|
| 186 |
for(j=0; j<=slices; j++) |
| 187 |
{ |
| 188 |
x = cost1[j]; |
| 189 |
y = sint1[j]; |
| 190 |
|
| 191 |
glNormal3d(x,y,z); |
| 192 |
glVertex3d(x*r*radius,y*r*radius,z*radius); |
| 193 |
} |
| 194 |
|
| 195 |
glEnd(); |
| 196 |
} |
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
for (i=0; i<slices; i++) |
| 201 |
{ |
| 202 |
glBegin(GL_LINE_STRIP); |
| 203 |
|
| 204 |
for(j=0; j<=stacks; j++) |
| 205 |
{ |
| 206 |
x = cost1[i]*sint2[j]; |
| 207 |
y = sint1[i]*sint2[j]; |
| 208 |
z = cost2[j]; |
| 209 |
|
| 210 |
glNormal3d(x,y,z); |
| 211 |
glVertex3d(x*radius,y*radius,z*radius); |
| 212 |
} |
| 213 |
|
| 214 |
glEnd(); |
| 215 |
} |
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
free(sint1); |
| 220 |
free(cost1); |
| 221 |
free(sint2); |
| 222 |
free(cost2); |
| 223 |
} |
| 224 |
|
| 225 |
#endif |