]> git.hungrycats.org Git - xscreensaver/blob - hacks/glx/sphere.c
From https://www.jwz.org/xscreensaver/xscreensaver-6.09.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 "screenhackI.h"
21 #include "sphere.h"
22
23 #include <math.h>
24 #include <stdlib.h>
25
26 typedef struct { GLfloat x, y, z; } XYZ;
27
28 static int
29 unit_sphere_1 (int stacks, int slices, int wire_p, int half_p)
30 {
31   int polys = 0;
32   int i,j;
33   double theta1, theta2, theta3;
34   XYZ p, n;
35   XYZ la = { 0, -1, 0 }, lb = { 0, -1, 0 };
36   XYZ c = {0, 0, 0};  /* center */
37   double r = 1.0;     /* radius */
38   int stacks2 = stacks * 2;
39   int end = (half_p ? stacks/2 : stacks);
40
41   int mode = (wire_p ? GL_LINE_STRIP : GL_TRIANGLE_STRIP);
42
43   int arraysize, out;
44   struct { XYZ p; XYZ n; GLfloat s, t; } *array;
45
46   if (r < 0)
47     r = -r;
48   if (slices < 0)
49     slices = -slices;
50
51   arraysize = (stacks+1) * (slices+1) * (wire_p ? 4 : 2);
52   array = (void *) calloc (arraysize, sizeof(*array));
53   if (! array) abort();
54   out = 0;
55
56   if (slices < 4 || stacks < 2 || r <= 0)
57     {
58       mode = GL_POINTS;
59       array[out++].p = c;
60       goto END;
61     }
62
63   for (j = 0; j < end; j++)
64     {
65       theta1 = j       * (M_PI+M_PI) / stacks2 - M_PI_2;
66       theta2 = (j + 1) * (M_PI+M_PI) / stacks2 - M_PI_2;
67
68       for (i = slices; i >= 0; i--)
69         {
70           theta3 = i * (M_PI+M_PI) / slices;
71
72           if (wire_p)
73             {
74               array[out++].p = lb;                              /* vertex */
75               array[out++].p = la;                              /* vertex */
76             }
77
78           n.x = cos (theta2) * cos(theta3);
79           n.y = sin (theta2);
80           n.z = cos (theta2) * sin(theta3);
81           p.x = c.x + r * n.x;
82           p.y = c.y + r * n.y;
83           p.z = c.z + r * n.z;
84
85           array[out].p = p;                                     /* vertex */
86           array[out].n = n;                                     /* normal */
87           array[out].s = i       / (GLfloat) slices;            /* texture */
88           array[out].t = 2*(j+1) / (GLfloat) stacks2;
89           out++;
90
91           if (wire_p) la = p;
92
93           n.x = cos(theta1) * cos(theta3);
94           n.y = sin(theta1);
95           n.z = cos(theta1) * sin(theta3);
96           p.x = c.x + r * n.x;
97           p.y = c.y + r * n.y;
98           p.z = c.z + r * n.z;
99
100           array[out].p = p;                                     /* vertex */
101           array[out].n = n;                                     /* normal */
102           array[out].s = i   / (GLfloat) slices;                /* texture */
103           array[out].t = 2*j / (GLfloat) stacks2;
104           out++;
105
106           if (out >= arraysize) abort();
107
108           if (wire_p) lb = p;
109           polys++;
110         }
111     }
112
113  END:
114
115   glEnableClientState (GL_VERTEX_ARRAY);
116   glEnableClientState (GL_NORMAL_ARRAY);
117   glEnableClientState (GL_TEXTURE_COORD_ARRAY);
118
119   glVertexPointer   (3, GL_FLOAT, sizeof(*array), &array[0].p);
120   glNormalPointer   (   GL_FLOAT, sizeof(*array), &array[0].n);
121   glTexCoordPointer (2, GL_FLOAT, sizeof(*array), &array[0].s);
122
123   glDrawArrays (mode, 0, out);
124
125   free (array);
126
127   return polys;
128 }
129
130
131 int
132 unit_sphere (int stacks, int slices, int wire_p)
133 {
134   return unit_sphere_1 (stacks, slices, wire_p, 0);
135 }
136
137 int
138 unit_dome (int stacks, int slices, int wire_p)
139 {
140   return unit_sphere_1 (stacks, slices, wire_p, 1);
141 }