]> git.mxchange.org Git - simgear.git/blob - simgear/sky/oursun.cxx
Complete overhaul of the sky/sun/moon/stars/planets. It is now an ssg
[simgear.git] / simgear / sky / oursun.cxx
1 // oursun.hxx -- model earth's sun
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 "sphere.hxx"
35 #include "oursun.hxx"
36
37
38 // Set up sun rendering call backs
39 static int sgSunOrbPreDraw( ssgEntity *e ) {
40     /* cout << endl << "Sun orb pre draw" << endl << "----------------" 
41          << endl << endl; */
42     glDisable( GL_DEPTH_TEST );
43     glDisable( GL_FOG );
44
45     return true;
46 }
47
48 static int sgSunOrbPostDraw( ssgEntity *e ) {
49     /* cout << endl << "Sun orb post draw" << endl << "----------------" 
50          << endl << endl; */
51     glEnable( GL_DEPTH_TEST );
52     glEnable( GL_FOG );
53
54     return true;
55 }
56
57 static int sgSunHaloPreDraw( ssgEntity *e ) {
58     /* cout << endl << "Sun halo pre draw" << endl << "----------------" 
59          << endl << endl; */
60     glDisable( GL_DEPTH_TEST );
61     glDisable( GL_FOG );
62     glBlendFunc ( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA ) ;
63
64     return true;
65 }
66
67 static int sgSunHaloPostDraw( ssgEntity *e ) {
68     /* cout << endl << "Sun halo post draw" << endl << "----------------" 
69          << endl << endl; */
70     glEnable( GL_DEPTH_TEST );
71     glEnable( GL_FOG );
72
73     return true;
74 }
75
76
77 // Constructor
78 SGSun::SGSun( void ) {
79 }
80
81
82 // Destructor
83 SGSun::~SGSun( void ) {
84 }
85
86
87 #if 0
88 static GLuint makeHalo( GLubyte *sun_texbuf, int width ) {
89     int texSize;
90     GLuint texid;
91     GLubyte *p;
92     int i,j;
93     double radius;
94   
95     // create a texture id
96 #ifdef GL_VERSION_1_1
97     glGenTextures(1, &texid);
98     glBindTexture(GL_TEXTURE_2D, texid);
99 #elif GL_EXT_texture_object
100     glGenTexturesEXT(1, &texid);
101     glBindTextureEXT(GL_TEXTURE_2D, texid);
102 #else
103 #   error port me
104 #endif
105
106     glPixelStorei( GL_UNPACK_ALIGNMENT, 4 );
107     glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
108     glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
109     glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE ) ;
110  
111     // create the actual texture contents
112     texSize = width * width;
113   
114     if ( !sun_texbuf ) {
115         cout << "ouch ..." << endl;
116         exit(-1);  // Ugly!
117     }
118
119     p = sun_texbuf;
120   
121     radius = (double)(width / 2);
122   
123     GLubyte value;
124     double x, y, d;
125     for ( i = 0; i < width; i++ ) {
126         for ( j = 0; j < width; j++ ) {
127             x = fabs((double)(i - (width / 2)));
128             y = fabs((double)(j - (width / 2)));
129             d = sqrt((x * x) + (y * y));
130             if (d < radius) {
131                 // t is 1.0 at center, 0.0 at edge
132                 double t = 1.0 - (d / radius);
133
134                 // inverse square looks nice 
135                 value = (int)((double) 0xff * (t*t));
136             } else {
137                 value = 0x00;
138             }
139             *p = value;
140             *(p+1) = value;
141             *(p+2) = value;
142             // *(p+3) = value;
143
144             p += 3;
145         }
146     }
147
148     /* glTexImage2D( GL_TEXTURE_2D,
149                   0,
150                   GL_RGBA,
151                   width, width,
152                   0,
153                   GL_RGBA, GL_UNSIGNED_BYTE,
154                   sun_texbuf ); */
155
156     return texid;
157 }
158
159
160 #define RGB  3                  // 3 bytes of color info per pixel
161 #define RGBA 4                  // 4 bytes of color+alpha info
162 void my_glWritePPMFile(const char *filename, GLubyte *buffer, int win_width, int win_height, int mode)
163 {
164     int i, j, k, q;
165     unsigned char *ibuffer;
166     FILE *fp;
167     int pixelSize = mode==GL_RGBA?4:3;
168
169     ibuffer = (unsigned char *) malloc(win_width*win_height*RGB);
170
171     fp = fopen(filename, "wb");
172     fprintf(fp, "P6\n# CREATOR: glReadPixel()\n%d %d\n%d\n",
173             win_width, win_height, UCHAR_MAX);
174     q = 0;
175     for (i = 0; i < win_height; i++) {
176         for (j = 0; j < win_width; j++) {
177             for (k = 0; k < RGB; k++) {
178                 ibuffer[q++] = (unsigned char)
179                     *(buffer + (pixelSize*((win_height-1-i)*win_width+j)+k));
180             }
181         }
182     }
183
184     // *(buffer + (pixelSize*((win_height-1-i)*win_width+j)+k));
185
186     fwrite(ibuffer, sizeof(unsigned char), RGB*win_width*win_height, fp);
187     fclose(fp);
188     free(ibuffer);
189
190     printf("wrote file (%d x %d pixels, %d bytes)\n",
191            win_width, win_height, RGB*win_width*win_height);
192 }
193 #endif
194
195
196 // initialize the sun object and connect it into our scene graph root
197 ssgBranch * SGSun::build( FGPath path, double sun_size ) {
198
199     // set up the orb state
200     orb_state = new ssgSimpleState();
201     orb_state->setShadeModel( GL_SMOOTH );
202     orb_state->disable( GL_LIGHTING );
203     orb_state->disable( GL_CULL_FACE );
204     orb_state->disable( GL_TEXTURE_2D );
205     orb_state->enable( GL_COLOR_MATERIAL );
206     orb_state->setColourMaterial( GL_AMBIENT_AND_DIFFUSE );
207     orb_state->disable( GL_BLEND );
208     orb_state->disable( GL_ALPHA_TEST );
209
210     cl = new ssgColourArray( 1 );
211     sgVec4 color;
212     sgSetVec4( color, 1.0, 1.0, 1.0, 1.0 );
213     cl->add( color );
214
215     ssgBranch *orb = ssgMakeSphere( orb_state, cl, sun_size, 10, 10, 
216                                     sgSunOrbPreDraw, sgSunOrbPostDraw );
217
218     // force a repaint of the sun colors with arbitrary defaults
219     repaint( 0.0 );
220
221     // build the halo
222     // sun_texbuf = new GLubyte[64*64*3];
223     // sun_texid = makeHalo( sun_texbuf, 64 );
224     // my_glWritePPMFile("sunhalo.ppm", sun_texbuf, 64, 64, RGB);
225
226     // set up the halo state
227     path.append( "halo.rgba" );
228     halo_state = new ssgSimpleState();
229     halo_state->setTexture( (char *)path.c_str() );
230     // halo_state->setTexture( sun_texid );
231     halo_state->enable( GL_TEXTURE_2D );
232     halo_state->disable( GL_LIGHTING );
233     halo_state->setShadeModel( GL_SMOOTH );
234     halo_state->disable( GL_CULL_FACE );
235     halo_state->enable( GL_COLOR_MATERIAL );
236     halo_state->setColourMaterial( GL_AMBIENT_AND_DIFFUSE );
237     halo_state->enable( GL_ALPHA_TEST );
238     halo_state->setAlphaClamp(0.01);
239     halo_state->enable ( GL_BLEND ) ;
240
241     // Build ssg structure
242     double size = sun_size * 10.0;
243     sgVec3 v3;
244     halo_vl = new ssgVertexArray;
245     sgSetVec3( v3, -size, 0.0, -size );
246     halo_vl->add( v3 );
247     sgSetVec3( v3, size, 0.0, -size );
248     halo_vl->add( v3 );
249     sgSetVec3( v3, -size, 0.0,  size );
250     halo_vl->add( v3 );
251     sgSetVec3( v3, size, 0.0,  size );
252     halo_vl->add( v3 );
253
254     sgVec2 v2;
255     halo_tl = new ssgTexCoordArray;
256     sgSetVec2( v2, 0.0f, 0.0f );
257     halo_tl->add( v2 );
258     sgSetVec2( v2, 1.0, 0.0 );
259     halo_tl->add( v2 );
260     sgSetVec2( v2, 0.0, 1.0 );
261     halo_tl->add( v2 );
262     sgSetVec2( v2, 1.0, 1.0 );
263     halo_tl->add( v2 );
264
265     ssgLeaf *halo = 
266         new ssgVtxTable ( GL_TRIANGLE_STRIP, halo_vl, NULL, halo_tl, cl );
267     halo->setState( halo_state );
268
269     // build the ssg scene graph sub tree for the sky and connected
270     // into the provide scene graph branch
271     sun_transform = new ssgTransform;
272
273     sun_transform->addKid( halo );
274     halo->setCallback( SSG_CALLBACK_PREDRAW, sgSunHaloPreDraw );
275     halo->setCallback( SSG_CALLBACK_POSTDRAW, sgSunHaloPostDraw );
276     sun_transform->addKid( orb );
277
278     return sun_transform;
279 }
280
281
282 // repaint the sun colors based on current value of sun_angle in
283 // degrees relative to verticle
284 // 0 degrees = high noon
285 // 90 degrees = sun rise/set
286 // 180 degrees = darkest midnight
287 bool SGSun::repaint( double sun_angle ) {
288     if ( sun_angle * RAD_TO_DEG < 100 ) {
289         // else sun is well below horizon (so no point in repainting it)
290     
291         // x_10 = sun_angle^10
292         double x_10 = sun_angle * sun_angle * sun_angle * sun_angle * sun_angle
293             * sun_angle * sun_angle * sun_angle * sun_angle * sun_angle;
294
295         float ambient = (float)(0.4 * pow (1.1, - x_10 / 30.0));
296         if (ambient < 0.3) { ambient = 0.3; }
297         if (ambient > 1.0) { ambient = 1.0; }
298
299         sgVec4 color;
300         sgSetVec4( color,
301                    (ambient * 6.0)  - 1.0, // minimum value = 0.8
302                    (ambient * 11.0) - 3.0, // minimum value = 0.3
303                    (ambient * 12.0) - 3.6, // minimum value = 0.0
304                    1.0 );
305     
306         if (color[0] > 1.0) color[0] = 1.0;
307         if (color[1] > 1.0) color[1] = 1.0;
308         if (color[2] > 1.0) color[2] = 1.0;
309
310         // cout << "color = " << color[0] << " " << color[1] << " " 
311         //      << color[2] << endl;
312
313         float *ptr;
314         ptr = cl->get( 0 );
315         sgCopyVec4( ptr, color );
316     }
317
318     return true;
319 }
320
321
322 // reposition the sun at the specified right ascension and
323 // declination, offset by our current position (p) so that it appears
324 // fixed at a great distance from the viewer.  Also add in an optional
325 // rotation (i.e. for the current time of day.)
326 bool SGSun::reposition( sgVec3 p, double angle,
327                         double rightAscension, double declination, 
328                         double sun_dist )
329 {
330     sgMat4 T1, T2, GST, RA, DEC;
331     sgVec3 axis;
332     sgVec3 v;
333
334     sgMakeTransMat4( T1, p );
335
336     sgSetVec3( axis, 0.0, 0.0, -1.0 );
337     sgMakeRotMat4( GST, angle, axis );
338
339     // xglRotatef(((RAD_TO_DEG * rightAscension)- 90.0), 0.0, 0.0, 1.0);
340     sgSetVec3( axis, 0.0, 0.0, 1.0 );
341     sgMakeRotMat4( RA, (rightAscension * RAD_TO_DEG) - 90.0, axis );
342
343     // xglRotatef((RAD_TO_DEG * declination), 1.0, 0.0, 0.0);
344     sgSetVec3( axis, 1.0, 0.0, 0.0 );
345     sgMakeRotMat4( DEC, declination * RAD_TO_DEG, axis );
346
347     // xglTranslatef(0,60000,0);
348     sgSetVec3( v, 0.0, 60000.0, 0.0 );
349     sgMakeTransMat4( T2, v );
350
351     sgMat4 TRANSFORM;
352     sgCopyMat4( TRANSFORM, T1 );
353     sgPreMultMat4( TRANSFORM, GST );
354     sgPreMultMat4( TRANSFORM, RA );
355     sgPreMultMat4( TRANSFORM, DEC );
356     sgPreMultMat4( TRANSFORM, T2 );
357
358     sgCoord skypos;
359     sgSetCoord( &skypos, TRANSFORM );
360
361     sun_transform->setTransform( &skypos );
362
363     return true;
364 }