From http://www.jwz.org/xscreensaver/xscreensaver-5.30.tar.gz
[xscreensaver] / hacks / glx / sphere.c
1 /* sphere, Copyright (c) 2002 Paul Bourke <pbourke@swin.edu.au>,
2  *         Copyright (c) 2010-2014 Jamie Zawinski <jwz@jwz.org>
3  * Utility function to create a unit sphere in GL.
4  *
5  * Permission to use, copy, modify, distribute, and sell this software and its
6  * documentation for any purpose is hereby granted without fee, provided that
7  * the above copyright notice appear in all copies and that both that
8  * copyright notice and this permission notice appear in supporting
9  * documentation.  No representations are made about the suitability of this
10  * software for any purpose.  It is provided "as is" without express or 
11  * implied warranty.
12  *
13  *  8-Oct-98: dek          Released initial version of "glplanet"
14  * 21-Mar-01: jwz@jwz.org  Broke sphere routine out into its own file.
15  * 28-Feb-02: jwz@jwz.org  New implementation from Paul Bourke:
16  *                         http://astronomy.swin.edu.au/~pbourke/opengl/sphere/
17  * 21-Aug-10  jwz@jwz.org  Converted to use glDrawArrays, for OpenGL ES.
18  */
19
20 #include <math.h>
21 #include <stdlib.h>
22
23 #ifdef HAVE_CONFIG_H
24 # include "config.h"
25 #endif
26
27 #ifndef HAVE_COCOA
28 # include <GL/gl.h>
29 #endif
30
31 #ifdef HAVE_JWZGLES
32 # include "jwzgles.h"
33 #endif /* HAVE_JWZGLES */
34
35 #include "sphere.h"
36
37 typedef struct { GLfloat x, y, z; } XYZ;
38
39 static int
40 unit_sphere_1 (int stacks, int slices, int wire_p, int half_p)
41 {
42   int polys = 0;
43   int i,j;
44   double theta1, theta2, theta3;
45   XYZ p, n;
46   XYZ la = { 0, -1, 0 }, lb = { 0, -1, 0 };
47   XYZ c = {0, 0, 0};  /* center */
48   double r = 1.0;     /* radius */
49   int stacks2 = stacks * 2;
50   int end = (half_p ? stacks/2 : stacks);
51
52   int mode = (wire_p ? GL_LINE_STRIP : GL_TRIANGLE_STRIP);
53
54   int arraysize, out;
55   struct { XYZ p; XYZ n; GLfloat s, t; } *array;
56
57   if (r < 0)
58     r = -r;
59   if (slices < 0)
60     slices = -slices;
61
62   arraysize = (stacks+1) * (slices+1) * (wire_p ? 4 : 2);
63   array = (void *) calloc (arraysize, sizeof(*array));
64   if (! array) abort();
65   out = 0;
66
67   if (slices < 4 || stacks < 2 || r <= 0)
68     {
69       mode = GL_POINTS;
70       array[out++].p = c;
71       goto END;
72     }
73
74   for (j = 0; j < end; j++)
75     {
76       theta1 = j       * (M_PI+M_PI) / stacks2 - M_PI_2;
77       theta2 = (j + 1) * (M_PI+M_PI) / stacks2 - M_PI_2;
78
79       for (i = slices; i >= 0; i--)
80         {
81           theta3 = i * (M_PI+M_PI) / slices;
82
83           if (wire_p)
84             {
85               array[out++].p = lb;                              /* vertex */
86               array[out++].p = la;                              /* vertex */
87             }
88
89           n.x = cos (theta2) * cos(theta3);
90           n.y = sin (theta2);
91           n.z = cos (theta2) * sin(theta3);
92           p.x = c.x + r * n.x;
93           p.y = c.y + r * n.y;
94           p.z = c.z + r * n.z;
95
96           array[out].p = p;                                     /* vertex */
97           array[out].n = n;                                     /* normal */
98           array[out].s = i       / (GLfloat) slices;            /* texture */
99           array[out].t = 2*(j+1) / (GLfloat) stacks2;
100           out++;
101
102           if (wire_p) la = p;
103
104           n.x = cos(theta1) * cos(theta3);
105           n.y = sin(theta1);
106           n.z = cos(theta1) * sin(theta3);
107           p.x = c.x + r * n.x;
108           p.y = c.y + r * n.y;
109           p.z = c.z + r * n.z;
110
111           array[out].p = p;                                     /* vertex */
112           array[out].n = n;                                     /* normal */
113           array[out].s = i   / (GLfloat) slices;                /* texture */
114           array[out].t = 2*j / (GLfloat) stacks2;
115           out++;
116
117           if (out >= arraysize) abort();
118
119           if (wire_p) lb = p;
120           polys++;
121         }
122     }
123
124  END:
125
126   glEnableClientState (GL_VERTEX_ARRAY);
127   glEnableClientState (GL_NORMAL_ARRAY);
128   glEnableClientState (GL_TEXTURE_COORD_ARRAY);
129
130   glVertexPointer   (3, GL_FLOAT, sizeof(*array), &array[0].p);
131   glNormalPointer   (   GL_FLOAT, sizeof(*array), &array[0].n);
132   glTexCoordPointer (2, GL_FLOAT, sizeof(*array), &array[0].s);
133
134   glDrawArrays (mode, 0, out);
135
136   free (array);
137
138   return polys;
139 }
140
141
142 int
143 unit_sphere (int stacks, int slices, int wire_p)
144 {
145   return unit_sphere_1 (stacks, slices, wire_p, 0);
146 }
147
148 int
149 unit_dome (int stacks, int slices, int wire_p)
150 {
151   return unit_sphere_1 (stacks, slices, wire_p, 1);
152 }