]> git.mxchange.org Git - simgear.git/blob - simgear/scene/sky/stars.cxx
Move SGReadFileCallback from model.cxx to public class ModelRegistry
[simgear.git] / simgear / scene / sky / stars.cxx
1 // stars.cxx -- model the stars
2 //
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). 
7 //
8 // Separated out rendering pieces and converted to ssg by Curt Olson,
9 // March 2000
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.
14 //
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.
19 //
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.
23 //
24 // $Id$
25
26 #ifdef HAVE_CONFIG_H
27 #  include <simgear_config.h>
28 #endif
29
30 #ifdef __CYGWIN__
31 #include <ieeefp.h>
32 #endif
33
34 #include <simgear/compiler.h>
35 #include <simgear/constants.h>
36 #include <simgear/debug/logstream.hxx>
37
38 #include <stdio.h>
39 #include STL_IOSTREAM
40
41 #include <osg/AlphaFunc>
42 #include <osg/BlendFunc>
43 #include <osg/Geode>
44 #include <osg/Geometry>
45 #include <osg/Image>
46 #include <osg/Material>
47 #include <osg/Point>
48 #include <osg/ShadeModel>
49
50 #include "stars.hxx"
51
52 // Constructor
53 SGStars::SGStars( void ) :
54 old_phase(-1)
55 {
56 }
57
58
59 // Destructor
60 SGStars::~SGStars( void ) {
61 }
62
63
64 // initialize the stars object and connect it into our scene graph root
65 osg::Node*
66 SGStars::build( int num, const SGVec3d star_data[], double star_dist ) {
67     // build the ssg scene graph sub tree for the sky and connected
68     // into the provide scene graph branch
69     stars_transform = new osg::MatrixTransform;
70
71     osg::Geode* geode = new osg::Geode;
72     osg::StateSet* stateSet = geode->getOrCreateStateSet();
73     stateSet->setRenderBinDetails(-9, "RenderBin");
74
75     // set up the star state
76
77     // Ok, the old implementation did have a color material set.
78     // But with lighting off, I don't see how this should change the result
79     osg::Material* material = new osg::Material;
80 //     material->setColorMode(osg::Material::AMBIENT_AND_DIFFUSE);
81 //     material->setEmission(osg::Material::FRONT_AND_BACK,
82 //                           osg::Vec4(0, 0, 0, 1));
83 //     material->setSpecular(osg::Material::FRONT_AND_BACK,
84 //                               osg::Vec4(0, 0, 0, 1));
85     stateSet->setAttribute(material);
86
87     osg::BlendFunc* blendFunc = new osg::BlendFunc;
88     blendFunc->setFunction(osg::BlendFunc::SRC_ALPHA,
89                            osg::BlendFunc::ONE_MINUS_SRC_ALPHA);
90     stateSet->setAttributeAndModes(blendFunc);
91
92 //     osg::Point* point = new osg::Point;
93 //     point->setSize(5);
94 //     stateSet->setAttributeAndModes(point);
95
96     stateSet->setMode(GL_FOG, osg::StateAttribute::OFF);
97     stateSet->setMode(GL_LIGHTING, osg::StateAttribute::OFF);
98     stateSet->setMode(GL_CULL_FACE, osg::StateAttribute::OFF);
99     stateSet->setMode(GL_DEPTH_TEST, osg::StateAttribute::OFF);
100     stateSet->setMode(GL_ALPHA_TEST, osg::StateAttribute::OFF);
101
102     // Build scenegraph structure
103     
104     cl = new osg::Vec4Array;
105     osg::Vec3Array* vl = new osg::Vec3Array;
106
107     // Build scenegraph structure
108     for ( int i = 0; i < num; ++i ) {
109         // position seeded to arbitrary values
110         vl->push_back(osg::Vec3(star_dist * cos( star_data[i][0])
111                                 * cos( star_data[i][1] ),
112                                 star_dist * sin( star_data[i][0])
113                                 * cos( star_data[i][1] ),
114                                 star_dist * sin( star_data[i][1])));
115
116         // color (magnitude)
117         cl->push_back(osg::Vec4(1, 1, 1, 1));
118     }
119
120     osg::Geometry* geometry = new osg::Geometry;
121     geometry->setUseDisplayList(false);
122     geometry->setVertexArray(vl);
123     geometry->setColorArray(cl.get());
124     geometry->setColorBinding(osg::Geometry::BIND_PER_VERTEX);
125     geometry->setNormalBinding(osg::Geometry::BIND_OFF);
126     geometry->addPrimitiveSet(new osg::DrawArrays(GL_POINTS, 0, vl->size()));
127     geode->addDrawable(geometry);
128
129     stars_transform->addChild(geode);
130
131     SG_LOG( SG_EVENT, SG_INFO, "stars = " << stars_transform.get());
132
133     return stars_transform.get();
134 }
135
136
137 // repaint the sun colors based on current value of sun_angle in
138 // degrees relative to verticle
139 // 0 degrees = high noon
140 // 90 degrees = sun rise/set
141 // 180 degrees = darkest midnight
142 bool SGStars::repaint( double sun_angle, int num, const SGVec3d star_data[] ) {
143     // cout << "repainting stars" << endl;
144     // double min = 100;
145     // double max = -100;
146     double mag, nmag, alpha, factor, cutoff;
147
148     int phase;
149
150     // determine which star structure to draw
151     if ( sun_angle > (SGD_PI_2 + 10.0 * SGD_DEGREES_TO_RADIANS ) ) {
152         // deep night
153         factor = 1.0;
154         cutoff = 4.5;
155         phase = 0;
156     } else if ( sun_angle > (SGD_PI_2 + 8.8 * SGD_DEGREES_TO_RADIANS ) ) {
157         factor = 1.0;
158         cutoff = 3.8;
159         phase = 1;
160     } else if ( sun_angle > (SGD_PI_2 + 7.5 * SGD_DEGREES_TO_RADIANS ) ) {
161         factor = 0.95;
162         cutoff = 3.1;
163         phase = 2;
164     } else if ( sun_angle > (SGD_PI_2 + 7.0 * SGD_DEGREES_TO_RADIANS ) ) {
165         factor = 0.9;
166         cutoff = 2.4;
167         phase = 3;
168     } else if ( sun_angle > (SGD_PI_2 + 6.5 * SGD_DEGREES_TO_RADIANS ) ) {
169         factor = 0.85;
170         cutoff = 1.8;
171         phase = 4;
172     } else if ( sun_angle > (SGD_PI_2 + 6.0 * SGD_DEGREES_TO_RADIANS ) ) {
173         factor = 0.8;
174         cutoff = 1.2;
175         phase = 5;
176     } else if ( sun_angle > (SGD_PI_2 + 5.5 * SGD_DEGREES_TO_RADIANS ) ) {
177         factor = 0.75;
178         cutoff = 0.6;
179         phase = 6;
180     } else {
181         // early dusk or late dawn
182         factor = 0.7;
183         cutoff = 0.0;
184         phase = 7;
185     }
186
187     if( phase != old_phase ) {
188         // cout << "  phase change, repainting stars, num = " << num << endl;
189         old_phase = phase;
190         for ( int i = 0; i < num; ++i ) {
191             // if ( star_data[i][2] < min ) { min = star_data[i][2]; }
192             // if ( star_data[i][2] > max ) { max = star_data[i][2]; }
193
194             // magnitude ranges from -1 (bright) to 4 (dim).  The
195             // range of star and planet magnitudes can actually go
196             // outside of this, but for our purpose, if it is brighter
197             // that -1, we'll color it full white/alpha anyway and 4
198             // is a convenient cutoff point which keeps the number of
199             // stars drawn at about 500.
200
201             // color (magnitude)
202             mag = star_data[i][2];
203             if ( mag < cutoff ) {
204                 nmag = ( 4.5 - mag ) / 5.5; // translate to 0 ... 1.0 scale
205                 // alpha = nmag * 0.7 + 0.3; // translate to a 0.3 ... 1.0 scale
206                 alpha = nmag * 0.85 + 0.15; // translate to a 0.15 ... 1.0 scale
207                 alpha *= factor;          // dim when the sun is brighter
208             } else {
209                 alpha = 0.0;
210             }
211
212             if (alpha > 1.0) { alpha = 1.0; }
213             if (alpha < 0.0) { alpha = 0.0; }
214
215             (*cl)[i] = osg::Vec4(1, 1, 1, alpha);
216             // cout << "alpha[" << i << "] = " << alpha << endl;
217         }
218         cl->dirty();
219     } else {
220         // cout << "  no phase change, skipping" << endl;
221     }
222
223     // cout << "min = " << min << " max = " << max << " count = " << num 
224     //      << endl;
225
226     return true;
227 }
228
229
230 // reposition the stars for the specified time (GST rotation),
231 // offset by our current position (p) so that it appears fixed at a
232 // great distance from the viewer.
233 bool
234 SGStars::reposition( const SGVec3f& p, double angle )
235 {
236     osg::Matrix T1, GST;
237     T1.makeTranslate(p.osg());
238
239     GST.makeRotate(angle*SGD_DEGREES_TO_RADIANS, osg::Vec3(0, 0, -1));
240
241     stars_transform->setMatrix(GST*T1);
242
243     return true;
244 }