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