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.
42 // Set up sun rendering call backs
43 static int sgSunOrbPreDraw( ssgEntity *e ) {
44 /* cout << endl << "Sun orb pre draw" << endl << "----------------"
47 ssgLeaf *f = (ssgLeaf *)e;
48 if ( f -> hasState () ) f->getState()->apply() ;
50 glPushAttrib( GL_DEPTH_BUFFER_BIT | GL_FOG_BIT );
51 // cout << "push error = " << glGetError() << endl;
53 glDisable( GL_DEPTH_TEST );
59 static int sgSunOrbPostDraw( ssgEntity *e ) {
60 /* cout << endl << "Sun orb post draw" << endl << "----------------"
64 // cout << "pop error = " << glGetError() << endl;
66 // glEnable( GL_DEPTH_TEST );
67 // glEnable( GL_FOG );
72 static int sgSunHaloPreDraw( ssgEntity *e ) {
73 /* cout << endl << "Sun halo pre draw" << endl << "----------------"
76 ssgLeaf *f = (ssgLeaf *)e;
77 if ( f -> hasState () ) f->getState()->apply() ;
79 glPushAttrib( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_FOG_BIT );
80 // cout << "push error = " << glGetError() << endl;
82 glDisable( GL_DEPTH_TEST );
84 glBlendFunc ( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA ) ;
89 static int sgSunHaloPostDraw( ssgEntity *e ) {
90 /* cout << endl << "Sun halo post draw" << endl << "----------------"
94 // cout << "pop error = " << glGetError() << endl;
96 // glEnable( GL_DEPTH_TEST );
97 // glEnable( GL_FOG );
104 SGSun::SGSun( void ) {
109 SGSun::~SGSun( void ) {
114 static GLuint makeHalo( GLubyte *sun_texbuf, int width ) {
121 // create a texture id
122 #ifdef GL_VERSION_1_1
123 glGenTextures(1, &texid);
124 glBindTexture(GL_TEXTURE_2D, texid);
125 #elif GL_EXT_texture_object
126 glGenTexturesEXT(1, &texid);
127 glBindTextureEXT(GL_TEXTURE_2D, texid);
132 glPixelStorei( GL_UNPACK_ALIGNMENT, 4 );
133 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
134 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
135 glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE ) ;
137 // create the actual texture contents
138 texSize = width * width;
141 cout << "ouch ..." << endl;
147 radius = (double)(width / 2);
151 for ( i = 0; i < width; i++ ) {
152 for ( j = 0; j < width; j++ ) {
153 x = fabs((double)(i - (width / 2)));
154 y = fabs((double)(j - (width / 2)));
155 d = sqrt((x * x) + (y * y));
157 // t is 1.0 at center, 0.0 at edge
158 double t = 1.0 - (d / radius);
160 // inverse square looks nice
161 value = (int)((double) 0xff * (t*t));
174 /* glTexImage2D( GL_TEXTURE_2D,
179 GL_RGBA, GL_UNSIGNED_BYTE,
186 #define RGB 3 // 3 bytes of color info per pixel
187 #define RGBA 4 // 4 bytes of color+alpha info
188 void my_glWritePPMFile(const char *filename, GLubyte *buffer, int win_width, int win_height, int mode)
191 unsigned char *ibuffer;
193 int pixelSize = mode==GL_RGBA?4:3;
195 ibuffer = (unsigned char *) malloc(win_width*win_height*RGB);
197 fp = fopen(filename, "wb");
198 fprintf(fp, "P6\n# CREATOR: glReadPixel()\n%d %d\n%d\n",
199 win_width, win_height, UCHAR_MAX);
201 for (i = 0; i < win_height; i++) {
202 for (j = 0; j < win_width; j++) {
203 for (k = 0; k < RGB; k++) {
204 ibuffer[q++] = (unsigned char)
205 *(buffer + (pixelSize*((win_height-1-i)*win_width+j)+k));
210 // *(buffer + (pixelSize*((win_height-1-i)*win_width+j)+k));
212 fwrite(ibuffer, sizeof(unsigned char), RGB*win_width*win_height, fp);
216 printf("wrote file (%d x %d pixels, %d bytes)\n",
217 win_width, win_height, RGB*win_width*win_height);
222 // initialize the sun object and connect it into our scene graph root
223 ssgBranch * SGSun::build( FGPath path, double sun_size ) {
225 // set up the orb state
226 orb_state = new ssgSimpleState();
227 orb_state->setShadeModel( GL_SMOOTH );
228 orb_state->disable( GL_LIGHTING );
229 // orb_state->enable( GL_LIGHTING );
230 orb_state->disable( GL_CULL_FACE );
231 orb_state->disable( GL_TEXTURE_2D );
232 orb_state->enable( GL_COLOR_MATERIAL );
233 orb_state->setColourMaterial( GL_AMBIENT_AND_DIFFUSE );
234 orb_state->setMaterial( GL_EMISSION, 0, 0, 0, 1 );
235 orb_state->setMaterial( GL_SPECULAR, 0, 0, 0, 1 );
236 orb_state->disable( GL_BLEND );
237 orb_state->disable( GL_ALPHA_TEST );
239 cl = new ssgColourArray( 1 );
241 sgSetVec4( color, 1.0, 1.0, 1.0, 1.0 );
244 ssgBranch *orb = ssgMakeSphere( orb_state, cl, sun_size, 10, 10,
245 sgSunOrbPreDraw, sgSunOrbPostDraw );
247 // force a repaint of the sun colors with arbitrary defaults
251 // sun_texbuf = new GLubyte[64*64*3];
252 // sun_texid = makeHalo( sun_texbuf, 64 );
253 // my_glWritePPMFile("sunhalo.ppm", sun_texbuf, 64, 64, RGB);
255 // set up the halo state
256 path.append( "halo.rgba" );
257 halo_state = new ssgSimpleState();
258 halo_state->setTexture( (char *)path.c_str() );
259 halo_state->enable( GL_TEXTURE_2D );
260 halo_state->disable( GL_LIGHTING );
261 // halo_state->enable( GL_LIGHTING );
262 halo_state->setShadeModel( GL_SMOOTH );
263 halo_state->disable( GL_CULL_FACE );
264 halo_state->enable( GL_COLOR_MATERIAL );
265 halo_state->setColourMaterial( GL_AMBIENT_AND_DIFFUSE );
266 halo_state->setMaterial( GL_EMISSION, 0, 0, 0, 1 );
267 halo_state->setMaterial( GL_SPECULAR, 0, 0, 0, 1 );
268 halo_state->enable( GL_ALPHA_TEST );
269 halo_state->setAlphaClamp(0.01);
270 halo_state->enable ( GL_BLEND ) ;
272 // Build ssg structure
273 double size = sun_size * 10.0;
275 halo_vl = new ssgVertexArray;
276 sgSetVec3( v3, -size, 0.0, -size );
278 sgSetVec3( v3, size, 0.0, -size );
280 sgSetVec3( v3, -size, 0.0, size );
282 sgSetVec3( v3, size, 0.0, size );
286 halo_tl = new ssgTexCoordArray;
287 sgSetVec2( v2, 0.0f, 0.0f );
289 sgSetVec2( v2, 1.0, 0.0 );
291 sgSetVec2( v2, 0.0, 1.0 );
293 sgSetVec2( v2, 1.0, 1.0 );
297 new ssgVtxTable ( GL_TRIANGLE_STRIP, halo_vl, NULL, halo_tl, cl );
298 halo->setState( halo_state );
300 // build the ssg scene graph sub tree for the sky and connected
301 // into the provide scene graph branch
302 sun_transform = new ssgTransform;
304 halo->setCallback( SSG_CALLBACK_PREDRAW, sgSunHaloPreDraw );
305 halo->setCallback( SSG_CALLBACK_POSTDRAW, sgSunHaloPostDraw );
306 sun_transform->addKid( halo );
307 sun_transform->addKid( orb );
309 return sun_transform;
313 // repaint the sun colors based on current value of sun_angle in
314 // degrees relative to verticle
315 // 0 degrees = high noon
316 // 90 degrees = sun rise/set
317 // 180 degrees = darkest midnight
318 bool SGSun::repaint( double sun_angle ) {
319 if ( sun_angle * SGD_RADIANS_TO_DEGREES < 100 ) {
320 // else sun is well below horizon (so no point in repainting it)
322 // x_10 = sun_angle^10
323 double x_10 = sun_angle * sun_angle * sun_angle * sun_angle * sun_angle
324 * sun_angle * sun_angle * sun_angle * sun_angle * sun_angle;
326 float ambient = (float)(0.4 * pow (1.1, - x_10 / 30.0));
327 if (ambient < 0.3) { ambient = 0.3; }
328 if (ambient > 1.0) { ambient = 1.0; }
332 (ambient * 6.0) - 1.0, // minimum value = 0.8
333 (ambient * 11.0) - 3.0, // minimum value = 0.3
334 (ambient * 12.0) - 3.6, // minimum value = 0.0
337 // temp test, forces the color to always be white
338 // sgSetVec4( color, 1.0, 1.0, 1.0, 1.0 );
340 if (color[0] > 1.0) color[0] = 1.0;
341 if (color[1] > 1.0) color[1] = 1.0;
342 if (color[2] > 1.0) color[2] = 1.0;
344 // cout << "color = " << color[0] << " " << color[1] << " "
345 // << color[2] << endl;
349 sgCopyVec4( ptr, color );
356 // reposition the sun at the specified right ascension and
357 // declination, offset by our current position (p) so that it appears
358 // fixed at a great distance from the viewer. Also add in an optional
359 // rotation (i.e. for the current time of day.)
360 bool SGSun::reposition( sgVec3 p, double angle,
361 double rightAscension, double declination,
364 sgMat4 T1, T2, GST, RA, DEC;
368 sgMakeTransMat4( T1, p );
370 sgSetVec3( axis, 0.0, 0.0, -1.0 );
371 sgMakeRotMat4( GST, angle, axis );
373 // xglRotatef( ((SGD_RADIANS_TO_DEGREES * rightAscension)- 90.0),
375 sgSetVec3( axis, 0.0, 0.0, 1.0 );
376 sgMakeRotMat4( RA, (rightAscension * SGD_RADIANS_TO_DEGREES) - 90.0, axis );
378 // xglRotatef((SGD_RADIANS_TO_DEGREES * declination), 1.0, 0.0, 0.0);
379 sgSetVec3( axis, 1.0, 0.0, 0.0 );
380 sgMakeRotMat4( DEC, declination * SGD_RADIANS_TO_DEGREES, axis );
382 // xglTranslatef(0,sun_dist);
383 sgSetVec3( v, 0.0, sun_dist, 0.0 );
384 sgMakeTransMat4( T2, v );
387 sgCopyMat4( TRANSFORM, T1 );
388 sgPreMultMat4( TRANSFORM, GST );
389 sgPreMultMat4( TRANSFORM, RA );
390 sgPreMultMat4( TRANSFORM, DEC );
391 sgPreMultMat4( TRANSFORM, T2 );
394 sgSetCoord( &skypos, TRANSFORM );
396 sun_transform->setTransform( &skypos );