1 /* sphere, Copyright (c) 1998 David Konerding <dek@cgl.ucsf.edu>
2 * Utility function to create a unit sphere in GL.
4 * Permission to use, copy, modify, distribute, and sell this software and its
5 * documentation for any purpose is hereby granted without fee, provided that
6 * the above copyright notice appear in all copies and that both that
7 * copyright notice and this permission notice appear in supporting
8 * documentation. No representations are made about the suitability of this
9 * software for any purpose. It is provided "as is" without express or
12 * 8-Oct-98: dek Released initial version of "glplanet"
13 * 21-Mar-01: jwz@jwz.org Broke sphere routine out into its own file.
22 /* Function for determining points on the surface of the sphere */
24 parametric_sphere (float theta, float rho, GLfloat *vector)
26 vector[0] = -sin(theta) * sin(rho);
27 vector[1] = cos(theta) * sin(rho);
33 unit_sphere (int stacks, int slices, Bool wire)
42 glShadeModel(GL_SMOOTH);
44 /* Generate a sphere with quadrilaterals.
45 * Quad vertices are determined using a parametric sphere function.
46 * For fun, you could generate practically any parameteric surface and
47 * map an image onto it.
50 dtheta = 2.0 * M_PI / slices;
55 glBegin (wire ? GL_LINE_LOOP : GL_QUADS);
58 for (i=0; i < stacks; i++) {
61 for (j=0; j < slices; j++) {
65 parametric_sphere (theta, rho, vector);
67 parametric_sphere (theta, rho, vector);
68 glVertex3f (vector[0], vector[1], vector[2]);
70 glTexCoord2f (s,t+dt);
71 parametric_sphere (theta, rho+drho, vector);
73 parametric_sphere (theta, rho+drho, vector);
74 glVertex3f (vector[0], vector[1], vector[2]);
76 glTexCoord2f (s+ds,t+dt);
77 parametric_sphere (theta + dtheta, rho+drho, vector);
79 parametric_sphere (theta + dtheta, rho+drho, vector);
80 glVertex3f (vector[0], vector[1], vector[2]);
82 glTexCoord2f (s+ds, t);
83 parametric_sphere (theta + dtheta, rho, vector);
85 parametric_sphere (theta + dtheta, rho, vector);
86 glVertex3f (vector[0], vector[1], vector[2]);