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 << "----------------"
46 glDisable( GL_DEPTH_TEST );
52 static int sgSunOrbPostDraw( ssgEntity *e ) {
53 /* cout << endl << "Sun orb post draw" << endl << "----------------"
55 glEnable( GL_DEPTH_TEST );
61 static int sgSunHaloPreDraw( ssgEntity *e ) {
62 /* cout << endl << "Sun halo pre draw" << endl << "----------------"
64 glDisable( GL_DEPTH_TEST );
66 glBlendFunc ( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA ) ;
71 static int sgSunHaloPostDraw( ssgEntity *e ) {
72 /* cout << endl << "Sun halo post draw" << endl << "----------------"
74 glEnable( GL_DEPTH_TEST );
82 SGSun::SGSun( void ) {
87 SGSun::~SGSun( void ) {
92 static GLuint makeHalo( GLubyte *sun_texbuf, int width ) {
99 // create a texture id
100 #ifdef GL_VERSION_1_1
101 glGenTextures(1, &texid);
102 glBindTexture(GL_TEXTURE_2D, texid);
103 #elif GL_EXT_texture_object
104 glGenTexturesEXT(1, &texid);
105 glBindTextureEXT(GL_TEXTURE_2D, texid);
110 glPixelStorei( GL_UNPACK_ALIGNMENT, 4 );
111 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
112 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
113 glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE ) ;
115 // create the actual texture contents
116 texSize = width * width;
119 cout << "ouch ..." << endl;
125 radius = (double)(width / 2);
129 for ( i = 0; i < width; i++ ) {
130 for ( j = 0; j < width; j++ ) {
131 x = fabs((double)(i - (width / 2)));
132 y = fabs((double)(j - (width / 2)));
133 d = sqrt((x * x) + (y * y));
135 // t is 1.0 at center, 0.0 at edge
136 double t = 1.0 - (d / radius);
138 // inverse square looks nice
139 value = (int)((double) 0xff * (t*t));
152 /* glTexImage2D( GL_TEXTURE_2D,
157 GL_RGBA, GL_UNSIGNED_BYTE,
164 #define RGB 3 // 3 bytes of color info per pixel
165 #define RGBA 4 // 4 bytes of color+alpha info
166 void my_glWritePPMFile(const char *filename, GLubyte *buffer, int win_width, int win_height, int mode)
169 unsigned char *ibuffer;
171 int pixelSize = mode==GL_RGBA?4:3;
173 ibuffer = (unsigned char *) malloc(win_width*win_height*RGB);
175 fp = fopen(filename, "wb");
176 fprintf(fp, "P6\n# CREATOR: glReadPixel()\n%d %d\n%d\n",
177 win_width, win_height, UCHAR_MAX);
179 for (i = 0; i < win_height; i++) {
180 for (j = 0; j < win_width; j++) {
181 for (k = 0; k < RGB; k++) {
182 ibuffer[q++] = (unsigned char)
183 *(buffer + (pixelSize*((win_height-1-i)*win_width+j)+k));
188 // *(buffer + (pixelSize*((win_height-1-i)*win_width+j)+k));
190 fwrite(ibuffer, sizeof(unsigned char), RGB*win_width*win_height, fp);
194 printf("wrote file (%d x %d pixels, %d bytes)\n",
195 win_width, win_height, RGB*win_width*win_height);
200 // initialize the sun object and connect it into our scene graph root
201 ssgBranch * SGSun::build( FGPath path, double sun_size ) {
203 // set up the orb state
204 orb_state = new ssgSimpleState();
205 orb_state->setShadeModel( GL_SMOOTH );
206 orb_state->disable( GL_LIGHTING );
207 // orb_state->enable( GL_LIGHTING );
208 orb_state->disable( GL_CULL_FACE );
209 orb_state->disable( GL_TEXTURE_2D );
210 orb_state->enable( GL_COLOR_MATERIAL );
211 orb_state->setColourMaterial( GL_AMBIENT_AND_DIFFUSE );
212 orb_state->setMaterial( GL_EMISSION, 0, 0, 0, 1 );
213 orb_state->setMaterial( GL_SPECULAR, 0, 0, 0, 1 );
214 orb_state->disable( GL_BLEND );
215 orb_state->disable( GL_ALPHA_TEST );
217 cl = new ssgColourArray( 1 );
219 sgSetVec4( color, 1.0, 1.0, 1.0, 1.0 );
222 ssgBranch *orb = ssgMakeSphere( orb_state, cl, sun_size, 10, 10,
223 sgSunOrbPreDraw, sgSunOrbPostDraw );
225 // force a repaint of the sun colors with arbitrary defaults
229 // sun_texbuf = new GLubyte[64*64*3];
230 // sun_texid = makeHalo( sun_texbuf, 64 );
231 // my_glWritePPMFile("sunhalo.ppm", sun_texbuf, 64, 64, RGB);
233 // set up the halo state
234 path.append( "halo.rgba" );
235 halo_state = new ssgSimpleState();
236 halo_state->setTexture( (char *)path.c_str() );
237 halo_state->enable( GL_TEXTURE_2D );
238 halo_state->disable( GL_LIGHTING );
239 // halo_state->enable( GL_LIGHTING );
240 halo_state->setShadeModel( GL_SMOOTH );
241 halo_state->disable( GL_CULL_FACE );
242 halo_state->enable( GL_COLOR_MATERIAL );
243 halo_state->setColourMaterial( GL_AMBIENT_AND_DIFFUSE );
244 halo_state->setMaterial( GL_EMISSION, 0, 0, 0, 1 );
245 halo_state->setMaterial( GL_SPECULAR, 0, 0, 0, 1 );
246 halo_state->enable( GL_ALPHA_TEST );
247 halo_state->setAlphaClamp(0.01);
248 halo_state->enable ( GL_BLEND ) ;
250 // Build ssg structure
251 double size = sun_size * 10.0;
253 halo_vl = new ssgVertexArray;
254 sgSetVec3( v3, -size, 0.0, -size );
256 sgSetVec3( v3, size, 0.0, -size );
258 sgSetVec3( v3, -size, 0.0, size );
260 sgSetVec3( v3, size, 0.0, size );
264 halo_tl = new ssgTexCoordArray;
265 sgSetVec2( v2, 0.0f, 0.0f );
267 sgSetVec2( v2, 1.0, 0.0 );
269 sgSetVec2( v2, 0.0, 1.0 );
271 sgSetVec2( v2, 1.0, 1.0 );
275 new ssgVtxTable ( GL_TRIANGLE_STRIP, halo_vl, NULL, halo_tl, cl );
276 halo->setState( halo_state );
278 // build the ssg scene graph sub tree for the sky and connected
279 // into the provide scene graph branch
280 sun_transform = new ssgTransform;
282 halo->setCallback( SSG_CALLBACK_PREDRAW, sgSunHaloPreDraw );
283 halo->setCallback( SSG_CALLBACK_POSTDRAW, sgSunHaloPostDraw );
284 sun_transform->addKid( halo );
285 sun_transform->addKid( orb );
287 return sun_transform;
291 // repaint the sun colors based on current value of sun_angle in
292 // degrees relative to verticle
293 // 0 degrees = high noon
294 // 90 degrees = sun rise/set
295 // 180 degrees = darkest midnight
296 bool SGSun::repaint( double sun_angle ) {
297 if ( sun_angle * SGD_RADIANS_TO_DEGREES < 100 ) {
298 // else sun is well below horizon (so no point in repainting it)
300 // x_10 = sun_angle^10
301 double x_10 = sun_angle * sun_angle * sun_angle * sun_angle * sun_angle
302 * sun_angle * sun_angle * sun_angle * sun_angle * sun_angle;
304 float ambient = (float)(0.4 * pow (1.1, - x_10 / 30.0));
305 if (ambient < 0.3) { ambient = 0.3; }
306 if (ambient > 1.0) { ambient = 1.0; }
310 (ambient * 6.0) - 1.0, // minimum value = 0.8
311 (ambient * 11.0) - 3.0, // minimum value = 0.3
312 (ambient * 12.0) - 3.6, // minimum value = 0.0
315 // temp test, forces the color to always be white
316 // sgSetVec4( color, 1.0, 1.0, 1.0, 1.0 );
318 if (color[0] > 1.0) color[0] = 1.0;
319 if (color[1] > 1.0) color[1] = 1.0;
320 if (color[2] > 1.0) color[2] = 1.0;
322 // cout << "color = " << color[0] << " " << color[1] << " "
323 // << color[2] << endl;
327 sgCopyVec4( ptr, color );
334 // reposition the sun at the specified right ascension and
335 // declination, offset by our current position (p) so that it appears
336 // fixed at a great distance from the viewer. Also add in an optional
337 // rotation (i.e. for the current time of day.)
338 bool SGSun::reposition( sgVec3 p, double angle,
339 double rightAscension, double declination,
342 sgMat4 T1, T2, GST, RA, DEC;
346 sgMakeTransMat4( T1, p );
348 sgSetVec3( axis, 0.0, 0.0, -1.0 );
349 sgMakeRotMat4( GST, angle, axis );
351 // xglRotatef( ((SGD_RADIANS_TO_DEGREES * rightAscension)- 90.0),
353 sgSetVec3( axis, 0.0, 0.0, 1.0 );
354 sgMakeRotMat4( RA, (rightAscension * SGD_RADIANS_TO_DEGREES) - 90.0, axis );
356 // xglRotatef((SGD_RADIANS_TO_DEGREES * declination), 1.0, 0.0, 0.0);
357 sgSetVec3( axis, 1.0, 0.0, 0.0 );
358 sgMakeRotMat4( DEC, declination * SGD_RADIANS_TO_DEGREES, axis );
360 // xglTranslatef(0,sun_dist);
361 sgSetVec3( v, 0.0, sun_dist, 0.0 );
362 sgMakeTransMat4( T2, v );
365 sgCopyMat4( TRANSFORM, T1 );
366 sgPreMultMat4( TRANSFORM, GST );
367 sgPreMultMat4( TRANSFORM, RA );
368 sgPreMultMat4( TRANSFORM, DEC );
369 sgPreMultMat4( TRANSFORM, T2 );
372 sgSetCoord( &skypos, TRANSFORM );
374 sun_transform->setTransform( &skypos );