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.
32 #include <simgear/constants.h>
38 // Set up sun rendering call backs
39 static int sgSunOrbPreDraw( ssgEntity *e ) {
40 /* cout << endl << "Sun orb pre draw" << endl << "----------------"
42 glDisable( GL_DEPTH_TEST );
48 static int sgSunOrbPostDraw( ssgEntity *e ) {
49 /* cout << endl << "Sun orb post draw" << endl << "----------------"
51 glEnable( GL_DEPTH_TEST );
57 static int sgSunHaloPreDraw( ssgEntity *e ) {
58 /* cout << endl << "Sun halo pre draw" << endl << "----------------"
60 glDisable( GL_DEPTH_TEST );
62 glBlendFunc ( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA ) ;
67 static int sgSunHaloPostDraw( ssgEntity *e ) {
68 /* cout << endl << "Sun halo post draw" << endl << "----------------"
70 glEnable( GL_DEPTH_TEST );
78 SGSun::SGSun( void ) {
83 SGSun::~SGSun( void ) {
88 static GLuint makeHalo( GLubyte *sun_texbuf, int width ) {
95 // create a texture id
97 glGenTextures(1, &texid);
98 glBindTexture(GL_TEXTURE_2D, texid);
99 #elif GL_EXT_texture_object
100 glGenTexturesEXT(1, &texid);
101 glBindTextureEXT(GL_TEXTURE_2D, texid);
106 glPixelStorei( GL_UNPACK_ALIGNMENT, 4 );
107 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
108 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
109 glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE ) ;
111 // create the actual texture contents
112 texSize = width * width;
115 cout << "ouch ..." << endl;
121 radius = (double)(width / 2);
125 for ( i = 0; i < width; i++ ) {
126 for ( j = 0; j < width; j++ ) {
127 x = fabs((double)(i - (width / 2)));
128 y = fabs((double)(j - (width / 2)));
129 d = sqrt((x * x) + (y * y));
131 // t is 1.0 at center, 0.0 at edge
132 double t = 1.0 - (d / radius);
134 // inverse square looks nice
135 value = (int)((double) 0xff * (t*t));
148 /* glTexImage2D( GL_TEXTURE_2D,
153 GL_RGBA, GL_UNSIGNED_BYTE,
160 #define RGB 3 // 3 bytes of color info per pixel
161 #define RGBA 4 // 4 bytes of color+alpha info
162 void my_glWritePPMFile(const char *filename, GLubyte *buffer, int win_width, int win_height, int mode)
165 unsigned char *ibuffer;
167 int pixelSize = mode==GL_RGBA?4:3;
169 ibuffer = (unsigned char *) malloc(win_width*win_height*RGB);
171 fp = fopen(filename, "wb");
172 fprintf(fp, "P6\n# CREATOR: glReadPixel()\n%d %d\n%d\n",
173 win_width, win_height, UCHAR_MAX);
175 for (i = 0; i < win_height; i++) {
176 for (j = 0; j < win_width; j++) {
177 for (k = 0; k < RGB; k++) {
178 ibuffer[q++] = (unsigned char)
179 *(buffer + (pixelSize*((win_height-1-i)*win_width+j)+k));
184 // *(buffer + (pixelSize*((win_height-1-i)*win_width+j)+k));
186 fwrite(ibuffer, sizeof(unsigned char), RGB*win_width*win_height, fp);
190 printf("wrote file (%d x %d pixels, %d bytes)\n",
191 win_width, win_height, RGB*win_width*win_height);
196 // initialize the sun object and connect it into our scene graph root
197 ssgBranch * SGSun::build( FGPath path, double sun_size ) {
199 // set up the orb state
200 orb_state = new ssgSimpleState();
201 orb_state->setShadeModel( GL_SMOOTH );
202 orb_state->disable( GL_LIGHTING );
203 orb_state->disable( GL_CULL_FACE );
204 orb_state->disable( GL_TEXTURE_2D );
205 orb_state->enable( GL_COLOR_MATERIAL );
206 orb_state->setColourMaterial( GL_AMBIENT_AND_DIFFUSE );
207 orb_state->disable( GL_BLEND );
208 orb_state->disable( GL_ALPHA_TEST );
210 cl = new ssgColourArray( 1 );
212 sgSetVec4( color, 1.0, 1.0, 1.0, 1.0 );
215 ssgBranch *orb = ssgMakeSphere( orb_state, cl, sun_size, 10, 10,
216 sgSunOrbPreDraw, sgSunOrbPostDraw );
218 // force a repaint of the sun colors with arbitrary defaults
222 // sun_texbuf = new GLubyte[64*64*3];
223 // sun_texid = makeHalo( sun_texbuf, 64 );
224 // my_glWritePPMFile("sunhalo.ppm", sun_texbuf, 64, 64, RGB);
226 // set up the halo state
227 path.append( "halo.rgba" );
228 halo_state = new ssgSimpleState();
229 halo_state->setTexture( (char *)path.c_str() );
230 // halo_state->setTexture( sun_texid );
231 halo_state->enable( GL_TEXTURE_2D );
232 halo_state->disable( GL_LIGHTING );
233 halo_state->setShadeModel( GL_SMOOTH );
234 halo_state->disable( GL_CULL_FACE );
235 halo_state->enable( GL_COLOR_MATERIAL );
236 halo_state->setColourMaterial( GL_AMBIENT_AND_DIFFUSE );
237 halo_state->enable( GL_ALPHA_TEST );
238 halo_state->setAlphaClamp(0.01);
239 halo_state->enable ( GL_BLEND ) ;
241 // Build ssg structure
242 double size = sun_size * 10.0;
244 halo_vl = new ssgVertexArray;
245 sgSetVec3( v3, -size, 0.0, -size );
247 sgSetVec3( v3, size, 0.0, -size );
249 sgSetVec3( v3, -size, 0.0, size );
251 sgSetVec3( v3, size, 0.0, size );
255 halo_tl = new ssgTexCoordArray;
256 sgSetVec2( v2, 0.0f, 0.0f );
258 sgSetVec2( v2, 1.0, 0.0 );
260 sgSetVec2( v2, 0.0, 1.0 );
262 sgSetVec2( v2, 1.0, 1.0 );
266 new ssgVtxTable ( GL_TRIANGLE_STRIP, halo_vl, NULL, halo_tl, cl );
267 halo->setState( halo_state );
269 // build the ssg scene graph sub tree for the sky and connected
270 // into the provide scene graph branch
271 sun_transform = new ssgTransform;
273 sun_transform->addKid( halo );
274 halo->setCallback( SSG_CALLBACK_PREDRAW, sgSunHaloPreDraw );
275 halo->setCallback( SSG_CALLBACK_POSTDRAW, sgSunHaloPostDraw );
276 sun_transform->addKid( orb );
278 return sun_transform;
282 // repaint the sun colors based on current value of sun_angle in
283 // degrees relative to verticle
284 // 0 degrees = high noon
285 // 90 degrees = sun rise/set
286 // 180 degrees = darkest midnight
287 bool SGSun::repaint( double sun_angle ) {
288 if ( sun_angle * RAD_TO_DEG < 100 ) {
289 // else sun is well below horizon (so no point in repainting it)
291 // x_10 = sun_angle^10
292 double x_10 = sun_angle * sun_angle * sun_angle * sun_angle * sun_angle
293 * sun_angle * sun_angle * sun_angle * sun_angle * sun_angle;
295 float ambient = (float)(0.4 * pow (1.1, - x_10 / 30.0));
296 if (ambient < 0.3) { ambient = 0.3; }
297 if (ambient > 1.0) { ambient = 1.0; }
301 (ambient * 6.0) - 1.0, // minimum value = 0.8
302 (ambient * 11.0) - 3.0, // minimum value = 0.3
303 (ambient * 12.0) - 3.6, // minimum value = 0.0
306 if (color[0] > 1.0) color[0] = 1.0;
307 if (color[1] > 1.0) color[1] = 1.0;
308 if (color[2] > 1.0) color[2] = 1.0;
310 // cout << "color = " << color[0] << " " << color[1] << " "
311 // << color[2] << endl;
315 sgCopyVec4( ptr, color );
322 // reposition the sun at the specified right ascension and
323 // declination, offset by our current position (p) so that it appears
324 // fixed at a great distance from the viewer. Also add in an optional
325 // rotation (i.e. for the current time of day.)
326 bool SGSun::reposition( sgVec3 p, double angle,
327 double rightAscension, double declination,
330 sgMat4 T1, T2, GST, RA, DEC;
334 sgMakeTransMat4( T1, p );
336 sgSetVec3( axis, 0.0, 0.0, -1.0 );
337 sgMakeRotMat4( GST, angle, axis );
339 // xglRotatef(((RAD_TO_DEG * rightAscension)- 90.0), 0.0, 0.0, 1.0);
340 sgSetVec3( axis, 0.0, 0.0, 1.0 );
341 sgMakeRotMat4( RA, (rightAscension * RAD_TO_DEG) - 90.0, axis );
343 // xglRotatef((RAD_TO_DEG * declination), 1.0, 0.0, 0.0);
344 sgSetVec3( axis, 1.0, 0.0, 0.0 );
345 sgMakeRotMat4( DEC, declination * RAD_TO_DEG, axis );
347 // xglTranslatef(0,60000,0);
348 sgSetVec3( v, 0.0, 60000.0, 0.0 );
349 sgMakeTransMat4( T2, v );
352 sgCopyMat4( TRANSFORM, T1 );
353 sgPreMultMat4( TRANSFORM, GST );
354 sgPreMultMat4( TRANSFORM, RA );
355 sgPreMultMat4( TRANSFORM, DEC );
356 sgPreMultMat4( TRANSFORM, T2 );
359 sgSetCoord( &skypos, TRANSFORM );
361 sun_transform->setTransform( &skypos );