]> git.mxchange.org Git - simgear.git/blob - simgear/scene/sky/sphere.cxx
Update the SoundSample api so we can request that a copy of the sample be
[simgear.git] / simgear / scene / 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 #include <simgear/compiler.h>
27 #include <simgear/debug/logstream.hxx>
28
29 #include STL_IOSTREAM
30
31 #include <plib/sg.h>
32 #include <plib/ssg.h>
33
34
35 // return a sphere object as an ssgBranch
36 ssgBranch *ssgMakeSphere( ssgSimpleState *state, ssgColourArray *cl,
37                           double radius, int slices, int stacks,
38                           ssgCallback predraw, ssgCallback postdraw )
39 {
40     float rho, drho, theta, dtheta;
41     float x, y, z;
42     float s, t, ds, dt;
43     int i, j, imin, imax;
44     float nsign = 1.0;
45     ssgBranch *sphere = new ssgBranch;
46     sgVec2 vec2;
47     sgVec3 vec3;
48
49     drho = SGD_PI / (float) stacks;
50     dtheta = 2.0 * SGD_PI / (float) slices;
51
52     /* texturing: s goes from 0.0/0.25/0.5/0.75/1.0 at +y/+x/-y/-x/+y
53        axis t goes from -1.0/+1.0 at z = -radius/+radius (linear along
54        longitudes) cannot use triangle fan on texturing (s coord. at
55        top/bottom tip varies) */
56
57     ds = 1.0 / slices;
58     dt = 1.0 / stacks;
59     t = 1.0;  /* because loop now runs from 0 */
60     imin = 0;
61     imax = stacks;
62
63     /* build slices as quad strips */
64     for ( i = imin; i < imax; i++ ) {
65         ssgVertexArray   *vl = new ssgVertexArray();
66         ssgNormalArray   *nl = new ssgNormalArray();
67         ssgTexCoordArray *tl = new ssgTexCoordArray();
68
69         rho = i * drho;
70         s = 0.0;
71         for ( j = 0; j <= slices; j++ ) {
72             theta = (j == slices) ? 0.0 : j * dtheta;
73             x = -sin(theta) * sin(rho);
74             y = cos(theta) * sin(rho);
75             z = nsign * cos(rho);
76
77             // glNormal3f( x*nsign, y*nsign, z*nsign );
78             sgSetVec3( vec3, x*nsign, y*nsign, z*nsign );
79             sgNormalizeVec3( vec3 );
80             nl->add( vec3 );
81
82             // glTexCoord2f(s,t);
83             sgSetVec2( vec2, s, t );
84             tl->add( vec2 );
85
86             // glVertex3f( x*radius, y*radius, z*radius );
87             sgSetVec3( vec3, x*radius, y*radius, z*radius );
88             vl->add( vec3 );
89
90             x = -sin(theta) * sin(rho+drho);
91             y = cos(theta) * sin(rho+drho);
92             z = nsign * cos(rho+drho);
93
94             // glNormal3f( x*nsign, y*nsign, z*nsign );
95             sgSetVec3( vec3, x*nsign, y*nsign, z*nsign );
96             sgNormalizeVec3( vec3 );
97             nl->add( vec3 );
98
99             // glTexCoord2f(s,t-dt);
100             sgSetVec2( vec2, s, t-dt );
101             tl->add( vec2 );
102             s += ds;
103
104             // glVertex3f( x*radius, y*radius, z*radius );
105             sgSetVec3( vec3, x*radius, y*radius, z*radius );
106             vl->add( vec3 );
107         }
108
109         ssgLeaf *slice = 
110             new ssgVtxTable ( GL_TRIANGLE_STRIP, vl, nl, tl, cl );
111
112         if ( vl->getNum() != nl->getNum() ) {
113             SG_LOG( SG_EVENT, SG_ALERT, "bad sphere1");
114             exit(-1);
115         }
116         if ( vl->getNum() != tl->getNum() ) {
117             SG_LOG( SG_EVENT, SG_ALERT, "bad sphere2");
118             exit(-1);
119         }
120         slice->setState( state );
121         slice->setCallback( SSG_CALLBACK_PREDRAW, predraw );
122         slice->setCallback( SSG_CALLBACK_POSTDRAW, postdraw );
123
124         sphere->addKid( slice );
125
126         t -= dt;
127     }
128
129     return sphere;
130 }