]> git.mxchange.org Git - simgear.git/blob - simgear/sky/sphere.cxx
Converted to the LGPL licencing terms.
[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 library is free software; you can redistribute it and/or
9 // modify it under the terms of the GNU Library General Public
10 // License as published by the Free Software Foundation; either
11 // version 2 of the License, or (at your option) any later version.
12 //
13 // This library is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16 // Library General Public License for more details.
17 //
18 // You should have received a copy of the GNU Library General Public
19 // License along with this library; if not, write to the
20 // Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21 // Boston, MA  02111-1307, USA.
22 //
23 // $Id$
24
25
26 #ifdef HAVE_CONFIG_H
27 #  include <config.h>
28 #endif
29
30 #include <simgear/compiler.h>
31
32 #include STL_IOSTREAM
33
34 #include <plib/sg.h>
35 #include <plib/ssg.h>
36
37
38 // return a sphere object as an ssgBranch
39 ssgBranch *ssgMakeSphere( ssgSimpleState *state, ssgColourArray *cl,
40                           double radius, int slices, int stacks,
41                           ssgCallback predraw, ssgCallback postdraw )
42 {
43     float rho, drho, theta, dtheta;
44     float x, y, z;
45     float s, t, ds, dt;
46     int i, j, imin, imax;
47     float nsign = 1.0;
48     ssgBranch *sphere = new ssgBranch;
49     sgVec2 vec2;
50     sgVec3 vec3;
51
52     drho = SG_PI / (float) stacks;
53     dtheta = 2.0 * SG_PI / (float) slices;
54
55     /* texturing: s goes from 0.0/0.25/0.5/0.75/1.0 at +y/+x/-y/-x/+y
56        axis t goes from -1.0/+1.0 at z = -radius/+radius (linear along
57        longitudes) cannot use triangle fan on texturing (s coord. at
58        top/bottom tip varies) */
59
60     ds = 1.0 / slices;
61     dt = 1.0 / stacks;
62     t = 1.0;  /* because loop now runs from 0 */
63     imin = 0;
64     imax = stacks;
65
66     /* build slices as quad strips */
67     for ( i = imin; i < imax; i++ ) {
68         ssgVertexArray   *vl = new ssgVertexArray();
69         ssgNormalArray   *nl = new ssgNormalArray();
70         ssgTexCoordArray *tl = new ssgTexCoordArray();
71
72         rho = i * drho;
73         s = 0.0;
74         for ( j = 0; j <= slices; j++ ) {
75             theta = (j == slices) ? 0.0 : j * dtheta;
76             x = -sin(theta) * sin(rho);
77             y = cos(theta) * sin(rho);
78             z = nsign * cos(rho);
79
80             // glNormal3f( x*nsign, y*nsign, z*nsign );
81             sgSetVec3( vec3, x*nsign, y*nsign, z*nsign );
82             sgNormalizeVec3( vec3 );
83             nl->add( vec3 );
84
85             // glTexCoord2f(s,t);
86             sgSetVec2( vec2, s, t );
87             tl->add( vec2 );
88
89             // glVertex3f( x*radius, y*radius, z*radius );
90             sgSetVec3( vec3, x*radius, y*radius, z*radius );
91             vl->add( vec3 );
92
93             x = -sin(theta) * sin(rho+drho);
94             y = cos(theta) * sin(rho+drho);
95             z = nsign * cos(rho+drho);
96
97             // glNormal3f( x*nsign, y*nsign, z*nsign );
98             sgSetVec3( vec3, x*nsign, y*nsign, z*nsign );
99             sgNormalizeVec3( vec3 );
100             nl->add( vec3 );
101
102             // glTexCoord2f(s,t-dt);
103             sgSetVec2( vec2, s, t-dt );
104             tl->add( vec2 );
105             s += ds;
106
107             // glVertex3f( x*radius, y*radius, z*radius );
108             sgSetVec3( vec3, x*radius, y*radius, z*radius );
109             vl->add( vec3 );
110         }
111
112         ssgLeaf *slice = 
113             new ssgVtxTable ( GL_TRIANGLE_STRIP, vl, nl, tl, cl );
114
115         if ( vl->getNum() != nl->getNum() ) {
116             cout << "bad sphere1\n";
117             exit(-1);
118         }
119         if ( vl->getNum() != tl->getNum() ) {
120             cout << "bad sphere2\n";
121             exit(-1);
122         }
123         slice->setState( state );
124         slice->setCallback( SSG_CALLBACK_PREDRAW, predraw );
125         slice->setCallback( SSG_CALLBACK_POSTDRAW, postdraw );
126
127         sphere->addKid( slice );
128
129         t -= dt;
130     }
131
132     return sphere;
133 }