1 // stars.cxx -- model the stars
3 // Written by Durk Talsma. Originally started October 1997, for distribution
4 // with the FlightGear project. Version 2 was written in August and
5 // September 1998. This code is based upon algorithms and data kindly
6 // provided by Mr. Paul Schlyter. (pausch@saaf.se).
8 // Separated out rendering pieces and converted to ssg by Curt Olson,
10 // This library is free software; you can redistribute it and/or
11 // modify it under the terms of the GNU Library General Public
12 // License as published by the Free Software Foundation; either
13 // version 2 of the License, or (at your option) any later version.
15 // This library is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 // Library General Public License for more details.
20 // You should have received a copy of the GNU General Public License
21 // along with this program; if not, write to the Free Software
22 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
27 # include <simgear_config.h>
34 #include <simgear/compiler.h>
35 #include <simgear/constants.h>
36 #include <simgear/debug/logstream.hxx>
41 #include <osg/AlphaFunc>
42 #include <osg/BlendFunc>
44 #include <osg/Geometry>
46 #include <osg/Material>
48 #include <osg/ShadeModel>
54 SGStars::SGStars( void ) :
61 SGStars::~SGStars( void ) {
65 // initialize the stars object and connect it into our scene graph root
67 SGStars::build( int num, const SGVec3d star_data[], double star_dist ) {
68 osg::Geode* geode = new osg::Geode;
69 osg::StateSet* stateSet = geode->getOrCreateStateSet();
70 stateSet->setRenderBinDetails(-9, "RenderBin");
72 // set up the star state
73 osg::BlendFunc* blendFunc = new osg::BlendFunc;
74 blendFunc->setFunction(osg::BlendFunc::SRC_ALPHA,
75 osg::BlendFunc::ONE_MINUS_SRC_ALPHA);
76 stateSet->setAttributeAndModes(blendFunc);
78 // osg::Point* point = new osg::Point;
80 // stateSet->setAttributeAndModes(point);
82 stateSet->setMode(GL_FOG, osg::StateAttribute::OFF);
83 stateSet->setMode(GL_LIGHTING, osg::StateAttribute::OFF);
84 stateSet->setMode(GL_CULL_FACE, osg::StateAttribute::OFF);
85 stateSet->setMode(GL_DEPTH_TEST, osg::StateAttribute::OFF);
86 stateSet->setMode(GL_ALPHA_TEST, osg::StateAttribute::OFF);
88 // Build scenegraph structure
90 cl = new osg::Vec4Array;
91 osg::Vec3Array* vl = new osg::Vec3Array;
93 // Build scenegraph structure
94 for ( int i = 0; i < num; ++i ) {
95 // position seeded to arbitrary values
96 vl->push_back(osg::Vec3(star_dist * cos( star_data[i][0])
97 * cos( star_data[i][1] ),
98 star_dist * sin( star_data[i][0])
99 * cos( star_data[i][1] ),
100 star_dist * sin( star_data[i][1])));
103 cl->push_back(osg::Vec4(1, 1, 1, 1));
106 osg::Geometry* geometry = new osg::Geometry;
107 geometry->setUseDisplayList(false);
108 geometry->setVertexArray(vl);
109 geometry->setColorArray(cl.get());
110 geometry->setColorBinding(osg::Geometry::BIND_PER_VERTEX);
111 geometry->setNormalBinding(osg::Geometry::BIND_OFF);
112 geometry->addPrimitiveSet(new osg::DrawArrays(GL_POINTS, 0, vl->size()));
113 geode->addDrawable(geometry);
119 // repaint the sun colors based on current value of sun_angle in
120 // degrees relative to verticle
121 // 0 degrees = high noon
122 // 90 degrees = sun rise/set
123 // 180 degrees = darkest midnight
124 bool SGStars::repaint( double sun_angle, int num, const SGVec3d star_data[] ) {
125 // cout << "repainting stars" << endl;
127 // double max = -100;
128 double mag, nmag, alpha, factor, cutoff;
132 // determine which star structure to draw
133 if ( sun_angle > (SGD_PI_2 + 10.0 * SGD_DEGREES_TO_RADIANS ) ) {
138 } else if ( sun_angle > (SGD_PI_2 + 8.8 * SGD_DEGREES_TO_RADIANS ) ) {
142 } else if ( sun_angle > (SGD_PI_2 + 7.5 * SGD_DEGREES_TO_RADIANS ) ) {
146 } else if ( sun_angle > (SGD_PI_2 + 7.0 * SGD_DEGREES_TO_RADIANS ) ) {
150 } else if ( sun_angle > (SGD_PI_2 + 6.5 * SGD_DEGREES_TO_RADIANS ) ) {
154 } else if ( sun_angle > (SGD_PI_2 + 6.0 * SGD_DEGREES_TO_RADIANS ) ) {
158 } else if ( sun_angle > (SGD_PI_2 + 5.5 * SGD_DEGREES_TO_RADIANS ) ) {
163 // early dusk or late dawn
169 if( phase != old_phase ) {
170 // cout << " phase change, repainting stars, num = " << num << endl;
172 for ( int i = 0; i < num; ++i ) {
173 // if ( star_data[i][2] < min ) { min = star_data[i][2]; }
174 // if ( star_data[i][2] > max ) { max = star_data[i][2]; }
176 // magnitude ranges from -1 (bright) to 4 (dim). The
177 // range of star and planet magnitudes can actually go
178 // outside of this, but for our purpose, if it is brighter
179 // that -1, we'll color it full white/alpha anyway and 4
180 // is a convenient cutoff point which keeps the number of
181 // stars drawn at about 500.
184 mag = star_data[i][2];
185 if ( mag < cutoff ) {
186 nmag = ( 4.5 - mag ) / 5.5; // translate to 0 ... 1.0 scale
187 alpha = nmag * 0.85 + 0.15; // translate to a 0.15 ... 1.0 scale
188 alpha *= factor; // dim when the sun is brighter
193 if (alpha > 1.0) { alpha = 1.0; }
194 if (alpha < 0.0) { alpha = 0.0; }
196 (*cl)[i] = osg::Vec4(1, 1, 1, alpha);
197 // cout << "alpha[" << i << "] = " << alpha << endl;
201 // cout << " no phase change, skipping" << endl;
204 // cout << "min = " << min << " max = " << max << " count = " << num