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