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