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