]> git.mxchange.org Git - simgear.git/blob - simgear/sky/stars.cxx
Added gdbm to SimGear. Many systems will already have gdbm installed so
[simgear.git] / simgear / 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 program is free software; you can redistribute it and/or
11 // modify it under the terms of the GNU General Public License as
12 // published by the Free Software Foundation; either version 2 of the
13 // License, or (at your option) any later version.
14 //
15 // This program is distributed in the hope that it will be useful, but
16 // WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18 // 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., 675 Mass Ave, Cambridge, MA 02139, USA.
23 //
24 // $Id$
25
26
27 #include <stdio.h>
28 #include <iostream>
29
30 #include <plib/ssg.h>
31
32 #include <simgear/constants.h>
33
34 #include "stars.hxx"
35
36
37 // Set up star rendering call backs
38 static int sgStarPreDraw( ssgEntity *e ) {
39     /* cout << endl << "Star pre draw" << endl << "----------------" 
40          << endl << endl; */
41     glDisable( GL_DEPTH_TEST );
42     glDisable( GL_FOG );
43     glBlendFunc ( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA ) ;
44
45     return true;
46 }
47
48 static int sgStarPostDraw( ssgEntity *e ) {
49     /* cout << endl << "Star post draw" << endl << "----------------" 
50          << endl << endl; */
51     glEnable( GL_DEPTH_TEST );
52     glEnable( GL_FOG );
53
54     return true;
55 }
56
57
58 // Constructor
59 SGStars::SGStars( void ) {
60 }
61
62
63 // Destructor
64 SGStars::~SGStars( void ) {
65 }
66
67
68 // initialize the stars object and connect it into our scene graph root
69 ssgBranch * SGStars::build( int num, sgdVec3 *star_data, double star_dist ) {
70     sgVec4 color;
71
72     if ( star_data == NULL ) {
73         cout << "WARNING: null star data passed to SGStars::build()" << endl;
74     }
75
76     // set up the orb state
77     state = new ssgSimpleState();
78     state->disable( GL_LIGHTING );
79     state->disable( GL_CULL_FACE );
80     state->disable( GL_TEXTURE_2D );
81     state->enable( GL_COLOR_MATERIAL );
82     state->setColourMaterial( GL_AMBIENT_AND_DIFFUSE );
83     state->enable( GL_BLEND );
84     state->disable( GL_ALPHA_TEST );
85
86     vl = new ssgVertexArray( num );
87     cl = new ssgColourArray( num );
88     // cl = new ssgColourArray( 1 );
89     // sgSetVec4( color, 1.0, 1.0, 1.0, 1.0 );
90     // cl->add( color );
91    
92     // Build ssg structure
93     sgVec3 p;
94     for ( int i = 0; i < num; ++i ) {
95         // position seeded to arbitrary values
96         sgSetVec3( p, 
97                    star_dist * cos( star_data[i][0] )
98                        * cos( star_data[i][1] ),
99                    star_dist * sin( star_data[i][0] )
100                        * cos( star_data[i][1] ),
101                    star_dist * sin( star_data[i][1] )
102                    );
103         vl->add( p );
104
105         // color (magnitude)
106         sgSetVec4( color, 1.0, 1.0, 1.0, 1.0 );
107         cl->add( color );
108     }
109
110     ssgLeaf *stars_obj = 
111         new ssgVtxTable ( GL_POINTS, vl, NULL, NULL, cl );
112     stars_obj->setState( state );
113     stars_obj->setCallback( SSG_CALLBACK_PREDRAW, sgStarPreDraw );
114     stars_obj->setCallback( SSG_CALLBACK_POSTDRAW, sgStarPostDraw );
115
116     // build the ssg scene graph sub tree for the sky and connected
117     // into the provide scene graph branch
118     stars_transform = new ssgTransform;
119
120     stars_transform->addKid( stars_obj );
121
122     cout << "stars = " << stars_transform << endl;
123
124     return stars_transform;
125 }
126
127
128 // repaint the sun colors based on current value of sun_angle in
129 // degrees relative to verticle
130 // 0 degrees = high noon
131 // 90 degrees = sun rise/set
132 // 180 degrees = darkest midnight
133 bool SGStars::repaint( double sun_angle, int num, sgdVec3 *star_data ) {
134     // double min = 100;
135     // double max = -100;
136     double mag, nmag, alpha, factor, cutoff;
137     float *color;
138
139     // determine which star structure to draw
140     if ( sun_angle > (FG_PI_2 + 10.0 * DEG_TO_RAD ) ) {
141         // deep night
142         factor = 1.0;
143         cutoff = 4.5;
144     } else if ( sun_angle > (FG_PI_2 + 8.8 * DEG_TO_RAD ) ) {
145         factor = 1.0;
146         cutoff = 3.8;
147     } else if ( sun_angle > (FG_PI_2 + 7.5 * DEG_TO_RAD ) ) {
148         factor = 0.95;
149         cutoff = 3.1;
150     } else if ( sun_angle > (FG_PI_2 + 7.0 * DEG_TO_RAD ) ) {
151         factor = 0.9;
152         cutoff = 2.4;
153     } else if ( sun_angle > (FG_PI_2 + 6.5 * DEG_TO_RAD ) ) {
154         factor = 0.85;
155         cutoff = 1.8;
156     } else if ( sun_angle > (FG_PI_2 + 6.0 * DEG_TO_RAD ) ) {
157         factor = 0.8;
158         cutoff = 1.2;
159     } else if ( sun_angle > (FG_PI_2 + 5.5 * DEG_TO_RAD ) ) {
160         factor = 0.75;
161         cutoff = 0.6;
162     } else {
163         // early dusk or late dawn
164         factor = 0.7;
165         cutoff = 0.0;
166     }
167
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 range of
173         // star and planet magnitudes can actually go outside of this,
174         // but for our purpose, if it is brighter that -1, we'll color
175         // it full white/alpha anyway and 4 is a convenient cutoff
176         // point which keeps the number of stars drawn at about 500.
177
178         // color (magnitude)
179         mag = star_data[i][2];
180         if ( mag < cutoff ) {
181             nmag = ( 4.5 - mag ) / 5.5; // translate to 0 ... 1.0 scale
182             // alpha = nmag * 0.7 + 0.3; // translate to a 0.3 ... 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         color = cl->get( i );
193         sgSetVec4( color, 1.0, 1.0, 1.0, alpha );
194         // cout << "alpha[" << i << "] = " << alpha << endl;
195     }
196
197     // cout << "min = " << min << " max = " << max << " count = " << num 
198     //      << endl;
199
200     return true;
201 }
202
203
204 // reposition the stars for the specified time (GST rotation),
205 // offset by our current position (p) so that it appears fixed at a
206 // great distance from the viewer.
207 bool SGStars::reposition( sgVec3 p, double angle )
208 {
209     sgMat4 T1, GST;
210     sgVec3 axis;
211
212     sgMakeTransMat4( T1, p );
213
214     sgSetVec3( axis, 0.0, 0.0, -1.0 );
215     sgMakeRotMat4( GST, angle, axis );
216
217     sgMat4 TRANSFORM;
218     sgCopyMat4( TRANSFORM, T1 );
219     sgPreMultMat4( TRANSFORM, GST );
220
221     sgCoord skypos;
222     sgSetCoord( &skypos, TRANSFORM );
223
224     stars_transform->setTransform( &skypos );
225
226     return true;
227 }