]> git.mxchange.org Git - simgear.git/blob - simgear/scene/sky/sphere.cxx
Added some OSG headers for the correct evaluation of the OSG_VERSION_LESS_THAN macro.
[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 General Public License
19 // along with this program; if not, write to the Free Software
20 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
21 //
22 // $Id$
23
24 #include <simgear/compiler.h>
25 #include <simgear/constants.h>
26 #include <simgear/debug/logstream.hxx>
27
28 #include <iostream>
29 #include <cstdlib>
30
31 #include <osg/Node>
32 #include <osg/Geometry>
33 #include <osg/Geode>
34 #include <osg/Array>
35
36
37 // return a sphere object as an ssgBranch
38 osg::Node*
39 SGMakeSphere(double radius, int slices, int stacks)
40 {
41     float rho, drho, dtheta;
42     float s, t, ds, dt;
43     int i, j, imin, imax;
44     float nsign = 1.0;
45     osg::Geode* geode = new osg::Geode;
46
47     drho = SGD_PI / (float) stacks;
48     dtheta = SGD_2PI / (float) slices;
49
50     /* texturing: s goes from 0.0/0.25/0.5/0.75/1.0 at +y/+x/-y/-x/+y
51        axis t goes from -1.0/+1.0 at z = -radius/+radius (linear along
52        longitudes) cannot use triangle fan on texturing (s coord. at
53        top/bottom tip varies) */
54
55     ds = 1.0 / slices;
56     dt = 1.0 / stacks;
57     t = 1.0;  /* because loop now runs from 0 */
58     imin = 0;
59     imax = stacks;
60
61     /* build slices as quad strips */
62     for ( i = imin; i < imax; i++ ) {
63         osg::Geometry* geometry = new osg::Geometry;
64         osg::Vec3Array* vl = new osg::Vec3Array;
65         osg::Vec3Array* nl = new osg::Vec3Array;
66         osg::Vec2Array* tl = new osg::Vec2Array;
67
68         rho = i * drho;
69         s = 0.0;
70         for ( j = 0; j <= slices; j++ ) {
71             double theta = (j == slices) ? 0.0 : j * dtheta;
72             double x = -sin(theta) * sin(rho);
73             double y = cos(theta) * sin(rho);
74             double z = nsign * cos(rho);
75
76             // glNormal3f( x*nsign, y*nsign, z*nsign );
77             osg::Vec3 normal(x*nsign, y*nsign, z*nsign);
78             normal.normalize();
79             nl->push_back(normal);
80
81             // glTexCoord2f(s,t);
82             tl->push_back(osg::Vec2(s, t));
83
84             // glVertex3f( x*radius, y*radius, z*radius );
85             vl->push_back(osg::Vec3(x*radius, y*radius, z*radius));
86
87             x = -sin(theta) * sin(rho+drho);
88             y = cos(theta) * sin(rho+drho);
89             z = nsign * cos(rho+drho);
90
91             // glNormal3f( x*nsign, y*nsign, z*nsign );
92             normal = osg::Vec3(x*nsign, y*nsign, z*nsign);
93             normal.normalize();
94             nl->push_back(normal);
95
96             // glTexCoord2f(s,t-dt);
97             tl->push_back(osg::Vec2(s, t-dt));
98             s += ds;
99
100             // glVertex3f( x*radius, y*radius, z*radius );
101             vl->push_back(osg::Vec3(x*radius, y*radius, z*radius));
102         }
103
104         if ( vl->size() != nl->size() ) {
105             SG_LOG( SG_EVENT, SG_ALERT, "bad sphere1");
106             exit(-1);
107         }
108         if ( vl->size() != tl->size() ) {
109             SG_LOG( SG_EVENT, SG_ALERT, "bad sphere2");
110             exit(-1);
111         }
112
113         // colors
114         osg::Vec4Array* cl = new osg::Vec4Array;
115         cl->push_back(osg::Vec4(1, 1, 1, 1));
116
117         geometry->setUseDisplayList(false);
118         geometry->setVertexArray(vl);
119         geometry->setNormalArray(nl);
120         geometry->setNormalBinding(osg::Geometry::BIND_PER_VERTEX);
121         geometry->setTexCoordArray(0, tl);
122         geometry->setColorArray(cl);
123         geometry->setColorBinding(osg::Geometry::BIND_OVERALL);
124         geometry->addPrimitiveSet(new osg::DrawArrays(GL_TRIANGLE_STRIP, 0, vl->size()));
125         geode->addDrawable(geometry);
126
127         t -= dt;
128     }
129
130     return geode;
131 }