]> git.mxchange.org Git - simgear.git/blob - simgear/scene/sky/stars.cxx
Rename matobj -> matmodel.
[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 Library General Public
21 // License along with this library; if not, write to the
22 // Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 // Boston, MA  02111-1307, USA.
24 //
25 // $Id$
26
27
28 #include <simgear/compiler.h>
29 #include <simgear/debug/logstream.hxx>
30
31 #include <stdio.h>
32 #include STL_IOSTREAM
33
34 #include <plib/sg.h>
35 #include <plib/ssg.h>
36
37 #include "stars.hxx"
38
39
40 // Set up star rendering call backs
41 static int sgStarPreDraw( ssgEntity *e ) {
42     /* cout << endl << "Star pre draw" << endl << "----------------" 
43        << endl << endl; */
44
45     ssgLeaf *f = (ssgLeaf *)e;
46     if ( f -> hasState () ) f->getState()->apply() ;
47
48     glPushAttrib( GL_DEPTH_BUFFER_BIT | GL_FOG_BIT );
49
50     glDisable( GL_DEPTH_TEST );
51     glDisable( GL_FOG );
52     // glBlendFunc ( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA ) ;
53
54     return true;
55 }
56
57 static int sgStarPostDraw( ssgEntity *e ) {
58     /* cout << endl << "Star post draw" << endl << "----------------" 
59        << endl << endl; */
60
61     glPopAttrib();
62
63     // glEnable( GL_DEPTH_TEST );
64     // glEnable( GL_FOG );
65
66     return true;
67 }
68
69
70 // Constructor
71 SGStars::SGStars( void ) :
72 old_phase(-1)
73 {
74 }
75
76
77 // Destructor
78 SGStars::~SGStars( void ) {
79 }
80
81
82 // initialize the stars object and connect it into our scene graph root
83 ssgBranch * SGStars::build( int num, sgdVec3 *star_data, double star_dist ) {
84     sgVec4 color;
85
86     if ( star_data == NULL )
87         SG_LOG( SG_EVENT, SG_WARN, "null star data passed to SGStars::build()");
88
89
90     // set up the orb state
91     state = new ssgSimpleState();
92     state->disable( GL_LIGHTING );
93     state->disable( GL_CULL_FACE );
94     state->disable( GL_TEXTURE_2D );
95     state->enable( GL_COLOR_MATERIAL );
96     state->setColourMaterial( GL_AMBIENT_AND_DIFFUSE );
97     state->setMaterial( GL_EMISSION, 0, 0, 0, 1 );
98     state->setMaterial( GL_SPECULAR, 0, 0, 0, 1 );
99     state->enable( GL_BLEND );
100     state->disable( GL_ALPHA_TEST );
101
102     vl = new ssgVertexArray( num );
103     cl = new ssgColourArray( num );
104     // cl = new ssgColourArray( 1 );
105     // sgSetVec4( color, 1.0, 1.0, 1.0, 1.0 );
106     // cl->add( color );
107
108     // Build ssg structure
109     sgVec3 p;
110     for ( int i = 0; i < num; ++i ) {
111         // position seeded to arbitrary values
112         sgSetVec3( p, 
113                    star_dist * cos( star_data[i][0] )
114                    * cos( star_data[i][1] ),
115                    star_dist * sin( star_data[i][0] )
116                    * cos( star_data[i][1] ),
117                    star_dist * sin( star_data[i][1] )
118                    );
119         vl->add( p );
120
121         // color (magnitude)
122         sgSetVec4( color, 1.0, 1.0, 1.0, 1.0 );
123         cl->add( color );
124     }
125
126     ssgLeaf *stars_obj = 
127         new ssgVtxTable ( GL_POINTS, vl, NULL, NULL, cl );
128     stars_obj->setState( state );
129     stars_obj->setCallback( SSG_CALLBACK_PREDRAW, sgStarPreDraw );
130     stars_obj->setCallback( SSG_CALLBACK_POSTDRAW, sgStarPostDraw );
131
132     // build the ssg scene graph sub tree for the sky and connected
133     // into the provide scene graph branch
134     stars_transform = new ssgTransform;
135
136     stars_transform->addKid( stars_obj );
137
138     SG_LOG( SG_EVENT, SG_INFO, "stars = " << stars_transform);
139
140     return stars_transform;
141 }
142
143
144 // repaint the sun colors based on current value of sun_angle in
145 // degrees relative to verticle
146 // 0 degrees = high noon
147 // 90 degrees = sun rise/set
148 // 180 degrees = darkest midnight
149 bool SGStars::repaint( double sun_angle, int num, sgdVec3 *star_data ) {
150     // cout << "repainting stars" << endl;
151     // double min = 100;
152     // double max = -100;
153     double mag, nmag, alpha, factor, cutoff;
154     float *color;
155
156     int phase;
157
158     // determine which star structure to draw
159     if ( sun_angle > (0.5 * SGD_PI + 10.0 * SGD_DEGREES_TO_RADIANS ) ) {
160         // deep night
161         factor = 1.0;
162         cutoff = 4.5;
163         phase = 0;
164     } else if ( sun_angle > (0.5 * SGD_PI + 8.8 * SGD_DEGREES_TO_RADIANS ) ) {
165         factor = 1.0;
166         cutoff = 3.8;
167         phase = 1;
168     } else if ( sun_angle > (0.5 * SGD_PI + 7.5 * SGD_DEGREES_TO_RADIANS ) ) {
169         factor = 0.95;
170         cutoff = 3.1;
171         phase = 2;
172     } else if ( sun_angle > (0.5 * SGD_PI + 7.0 * SGD_DEGREES_TO_RADIANS ) ) {
173         factor = 0.9;
174         cutoff = 2.4;
175         phase = 3;
176     } else if ( sun_angle > (0.5 * SGD_PI + 6.5 * SGD_DEGREES_TO_RADIANS ) ) {
177         factor = 0.85;
178         cutoff = 1.8;
179         phase = 4;
180     } else if ( sun_angle > (0.5 * SGD_PI + 6.0 * SGD_DEGREES_TO_RADIANS ) ) {
181         factor = 0.8;
182         cutoff = 1.2;
183         phase = 5;
184     } else if ( sun_angle > (0.5 * SGD_PI + 5.5 * SGD_DEGREES_TO_RADIANS ) ) {
185         factor = 0.75;
186         cutoff = 0.6;
187         phase = 6;
188     } else {
189         // early dusk or late dawn
190         factor = 0.7;
191         cutoff = 0.0;
192         phase = 7;
193     }
194
195     if( phase != old_phase ) {
196         // cout << "  phase change, repainting stars, num = " << num << endl;
197         old_phase = phase;
198         for ( int i = 0; i < num; ++i ) {
199             // if ( star_data[i][2] < min ) { min = star_data[i][2]; }
200             // if ( star_data[i][2] > max ) { max = star_data[i][2]; }
201
202             // magnitude ranges from -1 (bright) to 4 (dim).  The
203             // range of star and planet magnitudes can actually go
204             // outside of this, but for our purpose, if it is brighter
205             // that -1, we'll color it full white/alpha anyway and 4
206             // is a convenient cutoff point which keeps the number of
207             // stars drawn at about 500.
208
209             // color (magnitude)
210             mag = star_data[i][2];
211             if ( mag < cutoff ) {
212                 nmag = ( 4.5 - mag ) / 5.5; // translate to 0 ... 1.0 scale
213                 // alpha = nmag * 0.7 + 0.3; // translate to a 0.3 ... 1.0 scale
214                 alpha = nmag * 0.85 + 0.15; // translate to a 0.15 ... 1.0 scale
215                 alpha *= factor;          // dim when the sun is brighter
216             } else {
217                 alpha = 0.0;
218             }
219
220             if (alpha > 1.0) { alpha = 1.0; }
221             if (alpha < 0.0) { alpha = 0.0; }
222
223             color = cl->get( i );
224             sgSetVec4( color, 1.0, 1.0, 1.0, alpha );
225             // cout << "alpha[" << i << "] = " << alpha << endl;
226         }
227     } else {
228         // cout << "  no phase change, skipping" << endl;
229     }
230
231     // cout << "min = " << min << " max = " << max << " count = " << num 
232     //      << endl;
233
234     return true;
235 }
236
237
238 // reposition the stars for the specified time (GST rotation),
239 // offset by our current position (p) so that it appears fixed at a
240 // great distance from the viewer.
241 bool SGStars::reposition( sgVec3 p, double angle )
242 {
243     sgMat4 T1, GST;
244     sgVec3 axis;
245
246     sgMakeTransMat4( T1, p );
247
248     sgSetVec3( axis, 0.0, 0.0, -1.0 );
249     sgMakeRotMat4( GST, angle, axis );
250
251     sgMat4 TRANSFORM;
252     sgCopyMat4( TRANSFORM, T1 );
253     sgPreMultMat4( TRANSFORM, GST );
254
255     sgCoord skypos;
256     sgSetCoord( &skypos, TRANSFORM );
257
258     stars_transform->setTransform( &skypos );
259
260     return true;
261 }