]> git.mxchange.org Git - simgear.git/blob - simgear/scene/sky/stars.cxx
Added some OSG headers for the correct evaluation of the OSG_VERSION_LESS_THAN macro.
[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 <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 #include <osg/Node>
46
47 #include "stars.hxx"
48
49 // Constructor
50 SGStars::SGStars( void ) :
51 old_phase(-1)
52 {
53 }
54
55
56 // Destructor
57 SGStars::~SGStars( void ) {
58 }
59
60
61 // initialize the stars object and connect it into our scene graph root
62 osg::Node*
63 SGStars::build( int num, const SGVec3d star_data[], double star_dist ) {
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     osg::BlendFunc* blendFunc = new osg::BlendFunc;
70     blendFunc->setFunction(osg::BlendFunc::SRC_ALPHA,
71                            osg::BlendFunc::ONE_MINUS_SRC_ALPHA);
72     stateSet->setAttributeAndModes(blendFunc);
73
74 //     osg::Point* point = new osg::Point;
75 //     point->setSize(5);
76 //     stateSet->setAttributeAndModes(point);
77
78     stateSet->setMode(GL_FOG, osg::StateAttribute::OFF);
79     stateSet->setMode(GL_LIGHTING, osg::StateAttribute::OFF);
80     stateSet->setMode(GL_CULL_FACE, osg::StateAttribute::OFF);
81     stateSet->setMode(GL_DEPTH_TEST, osg::StateAttribute::OFF);
82     stateSet->setMode(GL_ALPHA_TEST, osg::StateAttribute::OFF);
83
84     // Build scenegraph structure
85     
86     cl = new osg::Vec4Array;
87     osg::Vec3Array* vl = new osg::Vec3Array;
88
89     // Build scenegraph structure
90     for ( int i = 0; i < num; ++i ) {
91         // position seeded to arbitrary values
92         vl->push_back(osg::Vec3(star_dist * cos( star_data[i][0])
93                                 * cos( star_data[i][1] ),
94                                 star_dist * sin( star_data[i][0])
95                                 * cos( star_data[i][1] ),
96                                 star_dist * sin( star_data[i][1])));
97
98         // color (magnitude)
99         cl->push_back(osg::Vec4(1, 1, 1, 1));
100     }
101
102     osg::Geometry* geometry = new osg::Geometry;
103     geometry->setUseDisplayList(false);
104     geometry->setVertexArray(vl);
105     geometry->setColorArray(cl.get());
106     geometry->setColorBinding(osg::Geometry::BIND_PER_VERTEX);
107     geometry->setNormalBinding(osg::Geometry::BIND_OFF);
108     geometry->addPrimitiveSet(new osg::DrawArrays(GL_POINTS, 0, vl->size()));
109     geode->addDrawable(geometry);
110
111     return geode;
112 }
113
114
115 // repaint the sun colors based on current value of sun_angle in
116 // degrees relative to verticle
117 // 0 degrees = high noon
118 // 90 degrees = sun rise/set
119 // 180 degrees = darkest midnight
120 bool SGStars::repaint( double sun_angle, int num, const SGVec3d star_data[] ) {
121     // cout << "repainting stars" << endl;
122     // double min = 100;
123     // double max = -100;
124     double mag, nmag, alpha, factor, cutoff;
125
126     int phase;
127
128     // determine which star structure to draw
129     if ( sun_angle > (SGD_PI_2 + 10.0 * SGD_DEGREES_TO_RADIANS ) ) {
130         // deep night
131         factor = 1.0;
132         cutoff = 4.5;
133         phase = 0;
134     } else if ( sun_angle > (SGD_PI_2 + 8.8 * SGD_DEGREES_TO_RADIANS ) ) {
135         factor = 1.0;
136         cutoff = 3.8;
137         phase = 1;
138     } else if ( sun_angle > (SGD_PI_2 + 7.5 * SGD_DEGREES_TO_RADIANS ) ) {
139         factor = 0.95;
140         cutoff = 3.1;
141         phase = 2;
142     } else if ( sun_angle > (SGD_PI_2 + 7.0 * SGD_DEGREES_TO_RADIANS ) ) {
143         factor = 0.9;
144         cutoff = 2.4;
145         phase = 3;
146     } else if ( sun_angle > (SGD_PI_2 + 6.5 * SGD_DEGREES_TO_RADIANS ) ) {
147         factor = 0.85;
148         cutoff = 1.8;
149         phase = 4;
150     } else if ( sun_angle > (SGD_PI_2 + 6.0 * SGD_DEGREES_TO_RADIANS ) ) {
151         factor = 0.8;
152         cutoff = 1.2;
153         phase = 5;
154     } else if ( sun_angle > (SGD_PI_2 + 5.5 * SGD_DEGREES_TO_RADIANS ) ) {
155         factor = 0.75;
156         cutoff = 0.6;
157         phase = 6;
158     } else {
159         // early dusk or late dawn
160         factor = 0.7;
161         cutoff = 0.0;
162         phase = 7;
163     }
164
165     if( phase != old_phase ) {
166         // cout << "  phase change, repainting stars, num = " << num << endl;
167         old_phase = phase;
168         for ( int i = 0; i < num; ++i ) {
169             // if ( star_data[i][2] < min ) { min = star_data[i][2]; }
170             // if ( star_data[i][2] > max ) { max = star_data[i][2]; }
171
172             // magnitude ranges from -1 (bright) to 4 (dim).  The
173             // range of star and planet magnitudes can actually go
174             // outside of this, but for our purpose, if it is brighter
175             // that -1, we'll color it full white/alpha anyway and 4
176             // is a convenient cutoff point which keeps the number of
177             // stars drawn at about 500.
178
179             // color (magnitude)
180             mag = star_data[i][2];
181             if ( mag < cutoff ) {
182                 nmag = ( 4.5 - mag ) / 5.5; // translate to 0 ... 1.0 scale
183                 alpha = nmag * 0.85 + 0.15; // translate to a 0.15 ... 1.0 scale
184                 alpha *= factor;          // dim when the sun is brighter
185             } else {
186                 alpha = 0.0;
187             }
188
189             if (alpha > 1.0) { alpha = 1.0; }
190             if (alpha < 0.0) { alpha = 0.0; }
191
192             (*cl)[i] = osg::Vec4(1, 1, 1, alpha);
193             // cout << "alpha[" << i << "] = " << alpha << endl;
194         }
195         cl->dirty();
196     } else {
197         // cout << "  no phase change, skipping" << endl;
198     }
199
200     // cout << "min = " << min << " max = " << max << " count = " << num 
201     //      << endl;
202
203     return true;
204 }