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