1 // oursun.hxx -- model earth's sun
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).
8 // Separated out rendering pieces and converted to ssg by Curt Olson,
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.
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.
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.
29 # include <simgear_config.h>
32 #include <simgear/compiler.h>
40 // define the following to enable a cheesy lens flare effect for the sun
41 // #define FG_TEST_CHEESY_LENS_FLARE
43 #ifdef FG_TEST_CHEESY_LENS_FLARE
44 # include <plib/ssgaLensFlare.h>
47 #include <simgear/screen/colors.hxx>
52 // Set up sun rendering call backs
53 static int sgSunOrbPreDraw( ssgEntity *e ) {
54 /* cout << endl << "Sun orb pre draw" << endl << "----------------"
57 ssgLeaf *f = (ssgLeaf *)e;
58 if ( f -> hasState () ) f->getState()->apply() ;
60 glPushAttrib( GL_DEPTH_BUFFER_BIT | GL_FOG_BIT );
61 // cout << "push error = " << glGetError() << endl;
63 glDisable( GL_DEPTH_TEST );
69 static int sgSunOrbPostDraw( ssgEntity *e ) {
70 /* cout << endl << "Sun orb post draw" << endl << "----------------"
74 // cout << "pop error = " << glGetError() << endl;
76 // glEnable( GL_DEPTH_TEST );
77 // glEnable( GL_FOG );
82 static int sgSunHaloPreDraw( ssgEntity *e ) {
83 /* cout << endl << "Sun halo pre draw" << endl << "----------------"
86 ssgLeaf *f = (ssgLeaf *)e;
87 if ( f -> hasState () ) f->getState()->apply() ;
89 glPushAttrib( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_FOG_BIT );
90 // cout << "push error = " << glGetError() << endl;
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 ) ;
100 static int sgSunHaloPostDraw( ssgEntity *e ) {
101 /* cout << endl << "Sun halo post draw" << endl << "----------------"
105 // cout << "pop error = " << glGetError() << endl;
107 // glEnable( GL_DEPTH_TEST );
108 // glEnable( GL_FOG );
115 SGSun::SGSun( void ) {
116 prev_sun_angle = -9999.0;
117 visibility = -9999.0;
122 SGSun::~SGSun( void ) {
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 ) {
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);
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 ) ;
152 // create the actual texture contents
153 texSize = width * width;
156 SG_LOG( SG_EVENT, SG_ALERT,
157 "Could not allocate memroy for the sun texture");
163 radius = (double)(width / 2);
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));
173 // t is 1.0 at center, 0.0 at edge
174 double t = 1.0 - (d / radius);
176 // inverse square looks nice
177 value = (int)((double) 0xff * (t*t));
190 /* glTexImage2D( GL_TEXTURE_2D,
195 GL_RGBA, GL_UNSIGNED_BYTE,
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)
207 unsigned char *ibuffer;
209 int pixelSize = mode==GL_RGBA?4:3;
211 ibuffer = (unsigned char *) malloc(win_width*win_height*RGB);
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);
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));
226 // *(buffer + (pixelSize*((win_height-1-i)*win_width+j)+k));
228 fwrite(ibuffer, sizeof(unsigned char), RGB*win_width*win_height, fp);
232 printf("wrote file (%d x %d pixels, %d bytes)\n",
233 win_width, win_height, RGB*win_width*win_height);
238 // initialize the sun object and connect it into our scene graph root
239 ssgBranch * SGSun::build( SGPath path, double sun_size ) {
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 );
255 cl = new ssgColourArray( 1 );
257 sgSetVec4( color, 1.0, 1.0, 1.0, 1.0 );
260 ssgBranch *orb = ssgMakeSphere( orb_state, cl, sun_size, 10, 10,
261 sgSunOrbPreDraw, sgSunOrbPostDraw );
263 // force a repaint of the sun colors with arbitrary defaults
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);
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 ) ;
288 // Build ssg structure
289 double size = sun_size * 10.0;
291 halo_vl = new ssgVertexArray;
292 sgSetVec3( v3, -size, 0.0, -size );
294 sgSetVec3( v3, size, 0.0, -size );
296 sgSetVec3( v3, -size, 0.0, size );
298 sgSetVec3( v3, size, 0.0, size );
302 halo_tl = new ssgTexCoordArray;
303 sgSetVec2( v2, 0.0f, 0.0f );
305 sgSetVec2( v2, 1.0, 0.0 );
307 sgSetVec2( v2, 0.0, 1.0 );
309 sgSetVec2( v2, 1.0, 1.0 );
313 new ssgVtxTable ( GL_TRIANGLE_STRIP, halo_vl, NULL, halo_tl, cl );
314 halo->setState( halo_state );
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;
320 halo->setCallback( SSG_CALLBACK_PREDRAW, sgSunHaloPreDraw );
321 halo->setCallback( SSG_CALLBACK_POSTDRAW, sgSunHaloPostDraw );
322 sun_transform->addKid( halo );
323 sun_transform->addKid( orb );
325 #ifdef FG_TEST_CHEESY_LENS_FLARE
327 sun_transform->addKid( new ssgaLensFlare );
330 return sun_transform;
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;
343 static double sqrt_m_log01 = sqrt( -log( 0.01 ) );
344 sun_exp2_punch_through = sqrt_m_log01 / (visibility * 15);
347 if (prev_sun_angle != sun_angle) {
348 prev_sun_angle = sun_angle;
350 float sun_factor = 4*cos(sun_angle);
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;
357 color[1] = sqrt(sun_factor);
358 color[0] = sqrt(color[1]);
359 color[2] = sun_factor * sun_factor;
360 color[2] *= color[2];
363 gamma_correct_rgb( color );
365 // cout << "color = " << color[0] << " " << color[1] << " "
366 // << color[2] << endl;
370 sgCopyVec4( ptr, color );
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,
385 sgMat4 T1, T2, GST, RA, DEC;
389 sgMakeTransMat4( T1, p );
391 sgSetVec3( axis, 0.0, 0.0, -1.0 );
392 sgMakeRotMat4( GST, angle, axis );
394 // xglRotatef( ((SGD_RADIANS_TO_DEGREES * rightAscension)- 90.0),
396 sgSetVec3( axis, 0.0, 0.0, 1.0 );
397 sgMakeRotMat4( RA, (rightAscension * SGD_RADIANS_TO_DEGREES) - 90.0, axis );
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 );
403 // xglTranslatef(0,sun_dist);
404 sgSetVec3( v, 0.0, sun_dist, 0.0 );
405 sgMakeTransMat4( T2, v );
408 sgCopyMat4( TRANSFORM, T1 );
409 sgPreMultMat4( TRANSFORM, GST );
410 sgPreMultMat4( TRANSFORM, RA );
411 sgPreMultMat4( TRANSFORM, DEC );
412 sgPreMultMat4( TRANSFORM, T2 );
415 sgSetCoord( &skypos, TRANSFORM );
417 sun_transform->setTransform( &skypos );