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>
51 // Set up sun rendering call backs
52 static int sgSunOrbPreDraw( ssgEntity *e ) {
53 /* cout << endl << "Sun orb pre draw" << endl << "----------------"
56 ssgLeaf *f = (ssgLeaf *)e;
57 if ( f -> hasState () ) f->getState()->apply() ;
59 glPushAttrib( GL_DEPTH_BUFFER_BIT | GL_FOG_BIT );
60 // cout << "push error = " << glGetError() << endl;
62 glDisable( GL_DEPTH_TEST );
68 static int sgSunOrbPostDraw( ssgEntity *e ) {
69 /* cout << endl << "Sun orb post draw" << endl << "----------------"
73 // cout << "pop error = " << glGetError() << endl;
75 // glEnable( GL_DEPTH_TEST );
76 // glEnable( GL_FOG );
81 static int sgSunHaloPreDraw( ssgEntity *e ) {
82 /* cout << endl << "Sun halo pre draw" << endl << "----------------"
85 ssgLeaf *f = (ssgLeaf *)e;
86 if ( f -> hasState () ) f->getState()->apply() ;
88 glPushAttrib( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_FOG_BIT );
89 // cout << "push error = " << glGetError() << endl;
91 glDisable( GL_DEPTH_TEST );
93 glBlendFunc ( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA ) ;
98 static int sgSunHaloPostDraw( ssgEntity *e ) {
99 /* cout << endl << "Sun halo post draw" << endl << "----------------"
103 // cout << "pop error = " << glGetError() << endl;
105 // glEnable( GL_DEPTH_TEST );
106 // glEnable( GL_FOG );
113 SGSun::SGSun( void ) {
118 SGSun::~SGSun( void ) {
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 ) {
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);
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 ) ;
148 // create the actual texture contents
149 texSize = width * width;
152 cout << "ouch ..." << endl;
158 radius = (double)(width / 2);
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));
168 // t is 1.0 at center, 0.0 at edge
169 double t = 1.0 - (d / radius);
171 // inverse square looks nice
172 value = (int)((double) 0xff * (t*t));
185 /* glTexImage2D( GL_TEXTURE_2D,
190 GL_RGBA, GL_UNSIGNED_BYTE,
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)
202 unsigned char *ibuffer;
204 int pixelSize = mode==GL_RGBA?4:3;
206 ibuffer = (unsigned char *) malloc(win_width*win_height*RGB);
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);
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));
221 // *(buffer + (pixelSize*((win_height-1-i)*win_width+j)+k));
223 fwrite(ibuffer, sizeof(unsigned char), RGB*win_width*win_height, fp);
227 printf("wrote file (%d x %d pixels, %d bytes)\n",
228 win_width, win_height, RGB*win_width*win_height);
233 // initialize the sun object and connect it into our scene graph root
234 ssgBranch * SGSun::build( SGPath path, double sun_size ) {
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 );
250 cl = new ssgColourArray( 1 );
252 sgSetVec4( color, 1.0, 1.0, 1.0, 1.0 );
255 ssgBranch *orb = ssgMakeSphere( orb_state, cl, sun_size, 10, 10,
256 sgSunOrbPreDraw, sgSunOrbPostDraw );
258 // force a repaint of the sun colors with arbitrary defaults
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);
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 ) ;
283 // Build ssg structure
284 double size = sun_size * 10.0;
286 halo_vl = new ssgVertexArray;
287 sgSetVec3( v3, -size, 0.0, -size );
289 sgSetVec3( v3, size, 0.0, -size );
291 sgSetVec3( v3, -size, 0.0, size );
293 sgSetVec3( v3, size, 0.0, size );
297 halo_tl = new ssgTexCoordArray;
298 sgSetVec2( v2, 0.0f, 0.0f );
300 sgSetVec2( v2, 1.0, 0.0 );
302 sgSetVec2( v2, 0.0, 1.0 );
304 sgSetVec2( v2, 1.0, 1.0 );
308 new ssgVtxTable ( GL_TRIANGLE_STRIP, halo_vl, NULL, halo_tl, cl );
309 halo->setState( halo_state );
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;
315 halo->setCallback( SSG_CALLBACK_PREDRAW, sgSunHaloPreDraw );
316 halo->setCallback( SSG_CALLBACK_POSTDRAW, sgSunHaloPostDraw );
317 sun_transform->addKid( halo );
318 sun_transform->addKid( orb );
320 #ifdef FG_TEST_CHEESY_LENS_FLARE
322 sun_transform->addKid( new ssgaLensFlare );
325 return sun_transform;
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 ) {
335 if ( sun_angle * SGD_RADIANS_TO_DEGREES < 100 ) {
336 // else sun is well below horizon (so no point in repainting it)
338 // x_10 = sun_angle^10
339 double x_10 = sun_angle * sun_angle * sun_angle * sun_angle * sun_angle
340 * sun_angle * sun_angle * sun_angle * sun_angle * sun_angle;
342 float ambient = (float)(0.4 * pow (1.1, - x_10 / 30.0));
343 if (ambient < 0.3) { ambient = 0.3; }
344 if (ambient > 1.0) { ambient = 1.0; }
348 (ambient * 6.0) - 1.0, // minimum value = 0.8
349 (ambient * 11.0) - 3.0, // minimum value = 0.3
350 (ambient * 12.0) - 3.6, // minimum value = 0.0
353 // temp test, forces the color to always be white
354 // sgSetVec4( color, 1.0, 1.0, 1.0, 1.0 );
356 if (color[0] > 1.0) color[0] = 1.0;
357 if (color[1] > 1.0) color[1] = 1.0;
358 if (color[2] > 1.0) color[2] = 1.0;
360 // cout << "color = " << color[0] << " " << color[1] << " "
361 // << color[2] << endl;
365 sgCopyVec4( ptr, color );
372 // reposition the sun at the specified right ascension and
373 // declination, offset by our current position (p) so that it appears
374 // fixed at a great distance from the viewer. Also add in an optional
375 // rotation (i.e. for the current time of day.)
376 bool SGSun::reposition( sgVec3 p, double angle,
377 double rightAscension, double declination,
380 sgMat4 T1, T2, GST, RA, DEC;
384 sgMakeTransMat4( T1, p );
386 sgSetVec3( axis, 0.0, 0.0, -1.0 );
387 sgMakeRotMat4( GST, angle, axis );
389 // xglRotatef( ((SGD_RADIANS_TO_DEGREES * rightAscension)- 90.0),
391 sgSetVec3( axis, 0.0, 0.0, 1.0 );
392 sgMakeRotMat4( RA, (rightAscension * SGD_RADIANS_TO_DEGREES) - 90.0, axis );
394 // xglRotatef((SGD_RADIANS_TO_DEGREES * declination), 1.0, 0.0, 0.0);
395 sgSetVec3( axis, 1.0, 0.0, 0.0 );
396 sgMakeRotMat4( DEC, declination * SGD_RADIANS_TO_DEGREES, axis );
398 // xglTranslatef(0,sun_dist);
399 sgSetVec3( v, 0.0, sun_dist, 0.0 );
400 sgMakeTransMat4( T2, v );
403 sgCopyMat4( TRANSFORM, T1 );
404 sgPreMultMat4( TRANSFORM, GST );
405 sgPreMultMat4( TRANSFORM, RA );
406 sgPreMultMat4( TRANSFORM, DEC );
407 sgPreMultMat4( TRANSFORM, T2 );
410 sgSetCoord( &skypos, TRANSFORM );
412 sun_transform->setTransform( &skypos );