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