1 /* sphere, Copyright (c) 2002, 2008 Paul Bourke <pbourke@swin.edu.au>
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.
14 * 28-Feb-02: jwz@jwz.org New implementation from Paul Bourke:
15 * http://astronomy.swin.edu.au/~pbourke/opengl/sphere/
26 # include <OpenGL/gl.h>
33 typedef struct { GLfloat x, y, z; } XYZ;
36 unit_sphere (int stacks, int slices, int wire_p)
40 double theta1, theta2, theta3;
42 XYZ la = { 0, 0, 0 }, lb = { 0, 0, 0 };
43 XYZ c = {0, 0, 0}; /* center */
44 double r = 1.0; /* radius */
45 int stacks2 = stacks * 2;
52 if (slices < 4 || stacks < 2 || r <= 0)
55 glVertex3f (c.x, c.y, c.z);
62 for (j = 0; j < stacks; j++)
64 theta1 = j * (M_PI+M_PI) / stacks2 - M_PI_2;
65 theta2 = (j + 1) * (M_PI+M_PI) / stacks2 - M_PI_2;
67 glBegin (wire_p ? GL_LINE_LOOP : GL_TRIANGLE_STRIP);
68 for (i = 0; i <= slices; i++)
70 theta3 = i * (M_PI+M_PI) / slices;
74 glVertex3f (lb.x, lb.y, lb.z);
75 glVertex3f (la.x, la.y, la.z);
78 e.x = cos (theta2) * cos(theta3);
80 e.z = cos (theta2) * sin(theta3);
85 glNormal3f (e.x, e.y, e.z);
86 glTexCoord2f (i / (double)slices,
87 2*(j+1) / (double)stacks2);
88 glVertex3f (p.x, p.y, p.z);
91 e.x = cos(theta1) * cos(theta3);
93 e.z = cos(theta1) * sin(theta3);
98 glNormal3f (e.x, e.y, e.z);
99 glTexCoord2f (i / (double)slices,
100 2*j / (double)stacks2);
101 glVertex3f (p.x, p.y, p.z);