]> git.mxchange.org Git - simgear.git/blob - simgear/sky/sphere.cxx
Added gdbm to SimGear. Many systems will already have gdbm installed so
[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, ssgColourArray *cl,
30                           double radius, int slices, int stacks,
31                           ssgCallback predraw, ssgCallback postdraw )
32 {
33     float rho, drho, theta, dtheta;
34     float x, y, z;
35     float s, t, ds, dt;
36     int i, j, imin, imax;
37     float nsign = 1.0;
38     ssgBranch *sphere = new ssgBranch;
39     sgVec2 vec2;
40     sgVec3 vec3;
41
42     // handle cl whether it is preinitialized or not
43     if ( cl == NULL ) {
44         // create a new array if needed
45         cl = new ssgColourArray( 1 );
46     }
47
48     sgVec4 color;
49     sgSetVec4( color, 1.0, 1.0, 1.0, 1.0 );
50
51     if ( cl->getNum() > 1 ) {
52         cl->removeAll();
53         cl->add( color );
54     } else if ( cl->getNum() == 0 ) {
55         cl->add( color );
56     } else {
57         // accept value as given to us in
58     }
59
60     drho = M_PI / (float) stacks;
61     dtheta = 2.0 * M_PI / (float) slices;
62
63     /* texturing: s goes from 0.0/0.25/0.5/0.75/1.0 at +y/+x/-y/-x/+y
64        axis t goes from -1.0/+1.0 at z = -radius/+radius (linear along
65        longitudes) cannot use triangle fan on texturing (s coord. at
66        top/bottom tip varies) */
67
68     ds = 1.0 / slices;
69     dt = 1.0 / stacks;
70     t = 1.0;  /* because loop now runs from 0 */
71     imin = 0;
72     imax = stacks;
73
74     /* build slices as quad strips */
75     for ( i = imin; i < imax; i++ ) {
76         ssgVertexArray   *vl = new ssgVertexArray();
77         ssgNormalArray   *nl = new ssgNormalArray();
78         ssgTexCoordArray *tl = new ssgTexCoordArray();
79
80         rho = i * drho;
81         s = 0.0;
82         for ( j = 0; j <= slices; j++ ) {
83             theta = (j == slices) ? 0.0 : j * dtheta;
84             x = -sin(theta) * sin(rho);
85             y = cos(theta) * sin(rho);
86             z = nsign * cos(rho);
87
88             // glNormal3f( x*nsign, y*nsign, z*nsign );
89             sgSetVec3( vec3, x*nsign, y*nsign, z*nsign );
90             sgNormalizeVec3( vec3 );
91             nl->add( vec3 );
92
93             // glTexCoord2f(s,t);
94             sgSetVec2( vec2, s, t );
95             tl->add( vec2 );
96
97             // glVertex3f( x*radius, y*radius, z*radius );
98             sgSetVec3( vec3, x*radius, y*radius, z*radius );
99             vl->add( vec3 );
100
101             x = -sin(theta) * sin(rho+drho);
102             y = cos(theta) * sin(rho+drho);
103             z = nsign * cos(rho+drho);
104
105             // glNormal3f( x*nsign, y*nsign, z*nsign );
106             sgSetVec3( vec3, x*nsign, y*nsign, z*nsign );
107             sgNormalizeVec3( vec3 );
108             nl->add( vec3 );
109
110             // glTexCoord2f(s,t-dt);
111             sgSetVec2( vec2, s, t-dt );
112             tl->add( vec2 );
113             s += ds;
114
115             // glVertex3f( x*radius, y*radius, z*radius );
116             sgSetVec3( vec3, x*radius, y*radius, z*radius );
117             vl->add( vec3 );
118         }
119
120         ssgLeaf *slice = 
121             new ssgVtxTable ( GL_TRIANGLE_STRIP, vl, nl, tl, cl );
122         slice->setState( state );
123         slice->setCallback( SSG_CALLBACK_PREDRAW, predraw );
124         slice->setCallback( SSG_CALLBACK_POSTDRAW, postdraw );
125
126         sphere->addKid( slice );
127
128         t -= dt;
129     }
130
131     return sphere;
132 }