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