| 1 | #include <stdlib.h> | 
|---|
| 2 | #include <math.h> | 
|---|
| 3 | #include <GL/gl.h> | 
|---|
| 4 | #include "glu.h" | 
|---|
| 5 |  | 
|---|
| 6 |  | 
|---|
| 7 | void drawTorus(float rc, int numc, float rt, int numt) | 
|---|
| 8 | { | 
|---|
| 9 |     int i, j, k; | 
|---|
| 10 |     double s, t; | 
|---|
| 11 |     double x, y, z; | 
|---|
| 12 |     double pi, twopi; | 
|---|
| 13 |  | 
|---|
| 14 |     pi = 3.14159265358979323846; | 
|---|
| 15 |     twopi = 2 * pi; | 
|---|
| 16 |   | 
|---|
| 17 |     for (i = 0; i < numc; i++) { | 
|---|
| 18 |         glBegin(GL_QUAD_STRIP); | 
|---|
| 19 |         for (j = 0; j <= numt; j++) { | 
|---|
| 20 |             for (k = 1; k >= 0; k--) { | 
|---|
| 21 |                 s = (i + k) % numc + 0.5; | 
|---|
| 22 |                 t = j % numt; | 
|---|
| 23 |  | 
|---|
| 24 |                 x = cos(t*twopi/numt) * cos(s*twopi/numc); | 
|---|
| 25 |                 y = sin(t*twopi/numt) * cos(s*twopi/numc); | 
|---|
| 26 |                 z = sin(s*twopi/numc); | 
|---|
| 27 |                 glNormal3f(x, y, z); | 
|---|
| 28 |  | 
|---|
| 29 |                 x = (rt + rc * cos(s*twopi/numc)) * cos(t*twopi/numt); | 
|---|
| 30 |                 y = (rt + rc * cos(s*twopi/numc)) * sin(t*twopi/numt); | 
|---|
| 31 |                 z = rc * sin(s*twopi/numc); | 
|---|
| 32 |                 glVertex3f(x, y, z); | 
|---|
| 33 |             } | 
|---|
| 34 |         } | 
|---|
| 35 |         glEnd(); | 
|---|
| 36 |     } | 
|---|
| 37 | } | 
|---|
| 38 |  | 
|---|
| 39 | static void normal3f( GLfloat x, GLfloat y, GLfloat z ) | 
|---|
| 40 | { | 
|---|
| 41 |    GLdouble mag; | 
|---|
| 42 |  | 
|---|
| 43 |    mag = sqrt( x*x + y*y + z*z ); | 
|---|
| 44 |    if (mag>0.00001F) { | 
|---|
| 45 |       x /= mag; | 
|---|
| 46 |       y /= mag; | 
|---|
| 47 |       z /= mag; | 
|---|
| 48 |    } | 
|---|
| 49 |    glNormal3f( x, y, z ); | 
|---|
| 50 | } | 
|---|
| 51 |  | 
|---|
| 52 | void gluPerspective( GLdouble fovy, GLdouble aspect, | 
|---|
| 53 |                      GLdouble zNear, GLdouble zFar ) | 
|---|
| 54 | { | 
|---|
| 55 |    GLdouble xmin, xmax, ymin, ymax; | 
|---|
| 56 |  | 
|---|
| 57 |    ymax = zNear * tan( fovy * M_PI / 360.0 ); | 
|---|
| 58 |    ymin = -ymax; | 
|---|
| 59 |  | 
|---|
| 60 |    xmin = ymin * aspect; | 
|---|
| 61 |    xmax = ymax * aspect; | 
|---|
| 62 |  | 
|---|
| 63 |    glFrustum( xmin, xmax, ymin, ymax, zNear, zFar ); | 
|---|
| 64 | } | 
|---|
| 65 |  | 
|---|
| 66 | GLUquadricObj *gluNewQuadric(void) | 
|---|
| 67 | { | 
|---|
| 68 |   return NULL; | 
|---|
| 69 | } | 
|---|
| 70 |  | 
|---|
| 71 | void gluQuadricDrawStyle(GLUquadricObj *obj, int style) | 
|---|
| 72 | { | 
|---|
| 73 | } | 
|---|
| 74 |  | 
|---|
| 75 | void gluCylinder( GLUquadricObj *qobj, | 
|---|
| 76 |                   GLdouble baseRadius, GLdouble topRadius, GLdouble height, | 
|---|
| 77 |                   GLint slices, GLint stacks ) | 
|---|
| 78 | { | 
|---|
| 79 |    GLdouble da, r, dr, dz; | 
|---|
| 80 |    GLfloat z, nz, nsign; | 
|---|
| 81 |    GLint i, j; | 
|---|
| 82 |    GLfloat du = 1.0 / slices; | 
|---|
| 83 |    GLfloat dv = 1.0 / stacks; | 
|---|
| 84 |    GLfloat tcx = 0.0, tcy = 0.0; | 
|---|
| 85 |     | 
|---|
| 86 |    nsign = 1.0; | 
|---|
| 87 |  | 
|---|
| 88 |    da = 2.0*M_PI / slices; | 
|---|
| 89 |    dr = (topRadius-baseRadius) / stacks; | 
|---|
| 90 |    dz = height / stacks; | 
|---|
| 91 |    nz = (baseRadius-topRadius) / height;  /* Z component of normal vectors */ | 
|---|
| 92 |  | 
|---|
| 93 |    for (i=0;i<slices;i++) { | 
|---|
| 94 |          GLfloat x1 = -sin(i*da); | 
|---|
| 95 |          GLfloat y1 = cos(i*da); | 
|---|
| 96 |          GLfloat x2 = -sin((i+1)*da); | 
|---|
| 97 |          GLfloat y2 = cos((i+1)*da); | 
|---|
| 98 |          z = 0.0; | 
|---|
| 99 |          r = baseRadius; | 
|---|
| 100 |          tcy = 0.0; | 
|---|
| 101 |          glBegin( GL_QUAD_STRIP ); | 
|---|
| 102 |          for (j=0;j<=stacks;j++) { | 
|---|
| 103 |             if (nsign==1.0) { | 
|---|
| 104 |                normal3f( x1*nsign, y1*nsign, nz*nsign ); | 
|---|
| 105 |                glTexCoord2f(tcx, tcy); | 
|---|
| 106 |                glVertex3f( x1*r, y1*r, z ); | 
|---|
| 107 |                normal3f( x2*nsign, y2*nsign, nz*nsign ); | 
|---|
| 108 |                glTexCoord2f(tcx+du, tcy); | 
|---|
| 109 |                glVertex3f( x2*r, y2*r, z ); | 
|---|
| 110 |             } | 
|---|
| 111 |             else { | 
|---|
| 112 |                normal3f( x2*nsign, y2*nsign, nz*nsign ); | 
|---|
| 113 |                glTexCoord2f(tcx, tcy); | 
|---|
| 114 |                glVertex3f( x2*r, y2*r, z ); | 
|---|
| 115 |                normal3f( x1*nsign, y1*nsign, nz*nsign ); | 
|---|
| 116 |                glTexCoord2f(tcx+du, tcy); | 
|---|
| 117 |                glVertex3f( x1*r, y1*r, z ); | 
|---|
| 118 |             } | 
|---|
| 119 |             z += dz; | 
|---|
| 120 |             r += dr; | 
|---|
| 121 |             tcy += dv; | 
|---|
| 122 |          } | 
|---|
| 123 |          glEnd(); | 
|---|
| 124 |          tcx += du; | 
|---|
| 125 |       } | 
|---|
| 126 | } | 
|---|
| 127 |  | 
|---|
| 128 | /* Disk (adapted from Mesa) */ | 
|---|
| 129 |  | 
|---|
| 130 | void gluDisk( GLUquadricObj *qobj, | 
|---|
| 131 |               GLdouble innerRadius, GLdouble outerRadius, | 
|---|
| 132 |               GLint slices, GLint loops ) | 
|---|
| 133 | { | 
|---|
| 134 |    GLdouble a, da; | 
|---|
| 135 |    GLfloat dr; | 
|---|
| 136 |    GLfloat r1, r2, dtc; | 
|---|
| 137 |    GLint s, l; | 
|---|
| 138 |    GLfloat sa,ca; | 
|---|
| 139 |  | 
|---|
| 140 |    /* Normal vectors */ | 
|---|
| 141 |          glNormal3f( 0.0, 0.0, +1.0 ); | 
|---|
| 142 |  | 
|---|
| 143 |    da = 2.0*M_PI / slices; | 
|---|
| 144 |    dr = (outerRadius-innerRadius) / (GLfloat) loops; | 
|---|
| 145 |  | 
|---|
| 146 |    /* texture of a gluDisk is a cut out of the texture unit square */ | 
|---|
| 147 |    /* x, y in [-outerRadius, +outerRadius]; s, t in [0, 1] (linear mapping) */ | 
|---|
| 148 |    dtc = 2.0f * outerRadius; | 
|---|
| 149 |  | 
|---|
| 150 |    r1 = innerRadius; | 
|---|
| 151 |    for (l=0;l<loops;l++) { | 
|---|
| 152 |             r2 = r1 + dr; | 
|---|
| 153 |                glBegin( GL_QUAD_STRIP ); | 
|---|
| 154 |                for (s=0;s<=slices;s++) { | 
|---|
| 155 |                   if (s==slices) a = 0.0; | 
|---|
| 156 |                   else  a = s * da; | 
|---|
| 157 |                   sa = sin(a); ca = cos(a); | 
|---|
| 158 |                   glTexCoord2f(0.5+sa*r2/dtc,0.5+ca*r2/dtc); | 
|---|
| 159 |                   glVertex2f( r2*sa, r2*ca ); | 
|---|
| 160 |                   glTexCoord2f(0.5+sa*r1/dtc,0.5+ca*r1/dtc); | 
|---|
| 161 |                   glVertex2f( r1*sa, r1*ca ); | 
|---|
| 162 |                } | 
|---|
| 163 |                glEnd(); | 
|---|
| 164 |             r1 = r2; | 
|---|
| 165 |          } | 
|---|
| 166 |  | 
|---|
| 167 | } | 
|---|
| 168 |  | 
|---|
| 169 | /* | 
|---|
| 170 |  * Sphère (adapted from Mesa) | 
|---|
| 171 |  */ | 
|---|
| 172 |  | 
|---|
| 173 | void gluSphere(GLUquadricObj *qobj, | 
|---|
| 174 |                float radius,int slices,int stacks) | 
|---|
| 175 | { | 
|---|
| 176 |    float rho, drho, theta, dtheta; | 
|---|
| 177 |    float x, y, z; | 
|---|
| 178 |    float s, t, ds, dt; | 
|---|
| 179 |    int i, j, imin, imax; | 
|---|
| 180 |    int normals; | 
|---|
| 181 |    float nsign; | 
|---|
| 182 |    | 
|---|
| 183 |    normals=1; | 
|---|
| 184 |    nsign=1; | 
|---|
| 185 |  | 
|---|
| 186 |    drho = M_PI / (float) stacks; | 
|---|
| 187 |    dtheta = 2.0 * M_PI / (float) slices; | 
|---|
| 188 |  | 
|---|
| 189 |   /* draw +Z end as a triangle fan */ | 
|---|
| 190 |   glBegin( GL_TRIANGLE_FAN ); | 
|---|
| 191 |   glNormal3f( 0.0, 0.0, 1.0 ); | 
|---|
| 192 |   glTexCoord2f(0.5,0.0); | 
|---|
| 193 |   glVertex3f( 0.0, 0.0, nsign * radius ); | 
|---|
| 194 |   for (j=0;j<=slices;j++) { | 
|---|
| 195 |          theta = (j==slices) ? 0.0 : j * dtheta; | 
|---|
| 196 |          x = -sin(theta) * sin(drho); | 
|---|
| 197 |          y = cos(theta) * sin(drho); | 
|---|
| 198 |          z = nsign * cos(drho); | 
|---|
| 199 |          if (normals)  glNormal3f( x*nsign, y*nsign, z*nsign ); | 
|---|
| 200 |          glVertex3f( x*radius, y*radius, z*radius ); | 
|---|
| 201 |       } | 
|---|
| 202 |    glEnd(); | 
|---|
| 203 |  | 
|---|
| 204 |  | 
|---|
| 205 |       ds = 1.0 / slices; | 
|---|
| 206 |       dt = 1.0 / stacks; | 
|---|
| 207 |       t = 1.0;  /* because loop now runs from 0 */ | 
|---|
| 208 |       if (1) { | 
|---|
| 209 |         imin = 0; | 
|---|
| 210 |         imax = stacks; | 
|---|
| 211 |       } | 
|---|
| 212 |       else { | 
|---|
| 213 |         imin = 1; | 
|---|
| 214 |         imax = stacks-1; | 
|---|
| 215 |       } | 
|---|
| 216 |  | 
|---|
| 217 |       /* draw intermediate stacks as quad strips */ | 
|---|
| 218 |       for (i=imin;i<imax;i++) { | 
|---|
| 219 |          rho = i * drho; | 
|---|
| 220 |          glBegin( GL_QUAD_STRIP ); | 
|---|
| 221 |          s = 0.0; | 
|---|
| 222 |          for (j=0;j<=slices;j++) { | 
|---|
| 223 |             theta = (j==slices) ? 0.0 : j * dtheta; | 
|---|
| 224 |             x = -sin(theta) * sin(rho); | 
|---|
| 225 |             y = cos(theta) * sin(rho); | 
|---|
| 226 |             z = nsign * cos(rho); | 
|---|
| 227 |             if (normals)  glNormal3f( x*nsign, y*nsign, z*nsign ); | 
|---|
| 228 |             glTexCoord2f(s,1-t); | 
|---|
| 229 |             glVertex3f( x*radius, y*radius, z*radius ); | 
|---|
| 230 |             x = -sin(theta) * sin(rho+drho); | 
|---|
| 231 |             y = cos(theta) * sin(rho+drho); | 
|---|
| 232 |             z = nsign * cos(rho+drho); | 
|---|
| 233 |             if (normals)  glNormal3f( x*nsign, y*nsign, z*nsign ); | 
|---|
| 234 |             glTexCoord2f(s,1-(t-dt)); | 
|---|
| 235 |             s += ds; | 
|---|
| 236 |             glVertex3f( x*radius, y*radius, z*radius ); | 
|---|
| 237 |          } | 
|---|
| 238 |          glEnd(); | 
|---|
| 239 |          t -= dt; | 
|---|
| 240 |       } | 
|---|
| 241 |  | 
|---|
| 242 | /* draw -Z end as a triangle fan */ | 
|---|
| 243 |     glBegin( GL_TRIANGLE_FAN ); | 
|---|
| 244 |     glNormal3f( 0.0, 0.0, -1.0 ); | 
|---|
| 245 |       glTexCoord2f(0.5,1.0); | 
|---|
| 246 |       glVertex3f( 0.0, 0.0, -radius*nsign ); | 
|---|
| 247 |       rho = M_PI - drho; | 
|---|
| 248 |       s = 1.0; | 
|---|
| 249 |       t = dt; | 
|---|
| 250 |       for (j=slices;j>=0;j--) { | 
|---|
| 251 |          theta = (j==slices) ? 0.0 : j * dtheta; | 
|---|
| 252 |          x = -sin(theta) * sin(rho); | 
|---|
| 253 |          y = cos(theta) * sin(rho); | 
|---|
| 254 |          z = nsign * cos(rho); | 
|---|
| 255 |          if (normals)  glNormal3f( x*nsign, y*nsign, z*nsign ); | 
|---|
| 256 |          glTexCoord2f(s,1-t); | 
|---|
| 257 |          s -= ds; | 
|---|
| 258 |          glVertex3f( x*radius, y*radius, z*radius ); | 
|---|
| 259 |       } | 
|---|
| 260 |       glEnd(); | 
|---|
| 261 | } | 
|---|