]> git.mxchange.org Git - simgear.git/blob - simgear/sky/sphere.cxx
Added top level ephemeris class.
[simgear.git] / simgear / sky / sphere.cxx
1 // sphere.cxx -- build an ssg sphere object
2 //
3 // Pulled straight out of MesaGLU/quadratic.c
4 //
5 // Original gluSphere code is Copyright (C) 1999-2000  Brian Paul and
6 // licensed under the GPL
7 //
8 // This program is free software; you can redistribute it and/or
9 // modify it under the terms of the GNU General Public License as
10 // published by the Free Software Foundation; either version 2 of the
11 // License, or (at your option) any later version.
12 //
13 // This program is distributed in the hope that it will be useful, but
14 // WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16 // General Public License for more details.
17 //
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the Free Software
20 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 //
22 // $Id$
23
24
25 #include <plib/ssg.h>
26
27
28 // return a sphere object as an ssgBranch
29 ssgBranch *ssgMakeSphere( ssgSimpleState *state, double radius, int slices,
30                           int stacks ) {
31     float rho, drho, theta, dtheta;
32     float x, y, z;
33     float s, t, ds, dt;
34     int i, j, imin, imax;
35     float nsign = 1.0;
36     ssgBranch *sphere = new ssgBranch;
37     sgVec2 vec2;
38     sgVec3 vec3;
39
40     drho = M_PI / (float) stacks;
41     dtheta = 2.0 * M_PI / (float) slices;
42
43     /* texturing: s goes from 0.0/0.25/0.5/0.75/1.0 at +y/+x/-y/-x/+y
44        axis t goes from -1.0/+1.0 at z = -radius/+radius (linear along
45        longitudes) cannot use triangle fan on texturing (s coord. at
46        top/bottom tip varies) */
47
48     ds = 1.0 / slices;
49     dt = 1.0 / stacks;
50     t = 1.0;  /* because loop now runs from 0 */
51     imin = 0;
52     imax = stacks;
53
54     /* build slices as quad strips */
55     for ( i = imin; i < imax; i++ ) {
56         ssgVertexArray   *vl = new ssgVertexArray();
57         ssgNormalArray   *nl = new ssgNormalArray();
58         ssgTexCoordArray *tl = new ssgTexCoordArray();
59
60         rho = i * drho;
61         s = 0.0;
62         for ( j = 0; j <= slices; j++ ) {
63             theta = (j == slices) ? 0.0 : j * dtheta;
64             x = -sin(theta) * sin(rho);
65             y = cos(theta) * sin(rho);
66             z = nsign * cos(rho);
67
68             // glNormal3f( x*nsign, y*nsign, z*nsign );
69             sgSetVec3( vec3, x*nsign, y*nsign, z*nsign );
70             nl->add( vec3 );
71
72             // glTexCoord2f(s,t);
73             sgSetVec2( vec2, 2, t );
74             tl->add( vec2 );
75
76             // glVertex3f( x*radius, y*radius, z*radius );
77             sgSetVec3( vec3, x*radius, y*radius, z*radius );
78             vl->add( vec3 );
79
80             x = -sin(theta) * sin(rho+drho);
81             y = cos(theta) * sin(rho+drho);
82             z = nsign * cos(rho+drho);
83
84             // glNormal3f( x*nsign, y*nsign, z*nsign );
85             sgSetVec3( vec3, x*nsign, y*nsign, z*nsign );
86             nl->add( vec3 );
87
88             // glTexCoord2f(s,t-dt);
89             sgSetVec2( vec2, s, t-dt );
90             tl->add( vec2 );
91             s += ds;
92
93             // glVertex3f( x*radius, y*radius, z*radius );
94             sgSetVec3( vec3, x*radius, y*radius, z*radius );
95             vl->add( vec3 );
96         }
97
98         ssgLeaf *slice = 
99             new ssgVtxTable ( GL_TRIANGLE_STRIP, vl, nl, tl, NULL );
100         slice->setState( state );
101
102         sphere->addKid( slice );
103
104         t -= dt;
105     }
106
107     return sphere;
108 }