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 program is free software; you can redistribute it and/or
11 // modify it under the terms of the GNU General Public License as
12 // published by the Free Software Foundation; either version 2 of the
13 // License, or (at your option) any later version.
15 // This program is distributed in the hope that it will be useful, but
16 // WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 // General Public License for more details.
20 // You should have received a copy of the GNU General Public License
21 // along with this program; if not, write to the Free Software
22 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
36 #include <simgear/constants.h>
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->disable( GL_CULL_FACE );
208 orb_state->disable( GL_TEXTURE_2D );
209 orb_state->enable( GL_COLOR_MATERIAL );
210 orb_state->setColourMaterial( GL_AMBIENT_AND_DIFFUSE );
211 orb_state->disable( GL_BLEND );
212 orb_state->disable( GL_ALPHA_TEST );
214 cl = new ssgColourArray( 1 );
216 sgSetVec4( color, 1.0, 1.0, 1.0, 1.0 );
219 ssgBranch *orb = ssgMakeSphere( orb_state, cl, sun_size, 10, 10,
220 sgSunOrbPreDraw, sgSunOrbPostDraw );
222 // force a repaint of the sun colors with arbitrary defaults
226 // sun_texbuf = new GLubyte[64*64*3];
227 // sun_texid = makeHalo( sun_texbuf, 64 );
228 // my_glWritePPMFile("sunhalo.ppm", sun_texbuf, 64, 64, RGB);
230 // set up the halo state
231 path.append( "halo.rgba" );
232 halo_state = new ssgSimpleState();
233 halo_state->setTexture( (char *)path.c_str() );
234 halo_state->enable( GL_TEXTURE_2D );
235 halo_state->disable( GL_LIGHTING );
236 halo_state->setShadeModel( GL_SMOOTH );
237 halo_state->disable( GL_CULL_FACE );
238 halo_state->enable( GL_COLOR_MATERIAL );
239 halo_state->setColourMaterial( GL_AMBIENT_AND_DIFFUSE );
240 halo_state->enable( GL_ALPHA_TEST );
241 halo_state->setAlphaClamp(0.01);
242 halo_state->enable ( GL_BLEND ) ;
244 // Build ssg structure
245 double size = sun_size * 10.0;
247 halo_vl = new ssgVertexArray;
248 sgSetVec3( v3, -size, 0.0, -size );
250 sgSetVec3( v3, size, 0.0, -size );
252 sgSetVec3( v3, -size, 0.0, size );
254 sgSetVec3( v3, size, 0.0, size );
258 halo_tl = new ssgTexCoordArray;
259 sgSetVec2( v2, 0.0f, 0.0f );
261 sgSetVec2( v2, 1.0, 0.0 );
263 sgSetVec2( v2, 0.0, 1.0 );
265 sgSetVec2( v2, 1.0, 1.0 );
269 new ssgVtxTable ( GL_TRIANGLE_STRIP, halo_vl, NULL, halo_tl, cl );
270 halo->setState( halo_state );
272 // build the ssg scene graph sub tree for the sky and connected
273 // into the provide scene graph branch
274 sun_transform = new ssgTransform;
276 sun_transform->addKid( halo );
277 halo->setCallback( SSG_CALLBACK_PREDRAW, sgSunHaloPreDraw );
278 halo->setCallback( SSG_CALLBACK_POSTDRAW, sgSunHaloPostDraw );
279 sun_transform->addKid( orb );
281 return sun_transform;
285 // repaint the sun colors based on current value of sun_angle in
286 // degrees relative to verticle
287 // 0 degrees = high noon
288 // 90 degrees = sun rise/set
289 // 180 degrees = darkest midnight
290 bool SGSun::repaint( double sun_angle ) {
291 if ( sun_angle * RAD_TO_DEG < 100 ) {
292 // else sun is well below horizon (so no point in repainting it)
294 // x_10 = sun_angle^10
295 double x_10 = sun_angle * sun_angle * sun_angle * sun_angle * sun_angle
296 * sun_angle * sun_angle * sun_angle * sun_angle * sun_angle;
298 float ambient = (float)(0.4 * pow (1.1, - x_10 / 30.0));
299 if (ambient < 0.3) { ambient = 0.3; }
300 if (ambient > 1.0) { ambient = 1.0; }
304 (ambient * 6.0) - 1.0, // minimum value = 0.8
305 (ambient * 11.0) - 3.0, // minimum value = 0.3
306 (ambient * 12.0) - 3.6, // minimum value = 0.0
309 if (color[0] > 1.0) color[0] = 1.0;
310 if (color[1] > 1.0) color[1] = 1.0;
311 if (color[2] > 1.0) color[2] = 1.0;
313 // cout << "color = " << color[0] << " " << color[1] << " "
314 // << color[2] << endl;
318 sgCopyVec4( ptr, color );
325 // reposition the sun at the specified right ascension and
326 // declination, offset by our current position (p) so that it appears
327 // fixed at a great distance from the viewer. Also add in an optional
328 // rotation (i.e. for the current time of day.)
329 bool SGSun::reposition( sgVec3 p, double angle,
330 double rightAscension, double declination,
333 sgMat4 T1, T2, GST, RA, DEC;
337 sgMakeTransMat4( T1, p );
339 sgSetVec3( axis, 0.0, 0.0, -1.0 );
340 sgMakeRotMat4( GST, angle, axis );
342 // xglRotatef(((RAD_TO_DEG * rightAscension)- 90.0), 0.0, 0.0, 1.0);
343 sgSetVec3( axis, 0.0, 0.0, 1.0 );
344 sgMakeRotMat4( RA, (rightAscension * RAD_TO_DEG) - 90.0, axis );
346 // xglRotatef((RAD_TO_DEG * declination), 1.0, 0.0, 0.0);
347 sgSetVec3( axis, 1.0, 0.0, 0.0 );
348 sgMakeRotMat4( DEC, declination * RAD_TO_DEG, axis );
350 // xglTranslatef(0,60000,0);
351 sgSetVec3( v, 0.0, 60000.0, 0.0 );
352 sgMakeTransMat4( T2, v );
355 sgCopyMat4( TRANSFORM, T1 );
356 sgPreMultMat4( TRANSFORM, GST );
357 sgPreMultMat4( TRANSFORM, RA );
358 sgPreMultMat4( TRANSFORM, DEC );
359 sgPreMultMat4( TRANSFORM, T2 );
362 sgSetCoord( &skypos, TRANSFORM );
364 sun_transform->setTransform( &skypos );