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