1 // skysun.hxx -- draw a sun object
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>
33 #include <simgear/misc/fgpath.hxx>
40 FGSkySun::FGSkySun( void ) {
45 FGSkySun::~FGSkySun( void ) {
50 static GLuint makeHalo( GLubyte *sun_texbuf, int width ) {
57 // create a texture id
59 glGenTextures(1, &texid);
60 glBindTexture(GL_TEXTURE_2D, texid);
61 #elif GL_EXT_texture_object
62 glGenTexturesEXT(1, &texid);
63 glBindTextureEXT(GL_TEXTURE_2D, texid);
68 glPixelStorei( GL_UNPACK_ALIGNMENT, 4 );
69 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
70 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
71 glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE ) ;
73 // create the actual texture contents
74 texSize = width * width;
77 cout << "ouch ..." << endl;
83 radius = (double)(width / 2);
87 for ( i = 0; i < width; i++ ) {
88 for ( j = 0; j < width; j++ ) {
89 x = fabs((double)(i - (width / 2)));
90 y = fabs((double)(j - (width / 2)));
91 d = sqrt((x * x) + (y * y));
93 // t is 1.0 at center, 0.0 at edge
94 double t = 1.0 - (d / radius);
96 // inverse square looks nice
97 value = (int)((double) 0xff * (t*t));
110 /* glTexImage2D( GL_TEXTURE_2D,
115 GL_RGBA, GL_UNSIGNED_BYTE,
122 #define RGB 3 // 3 bytes of color info per pixel
123 #define RGBA 4 // 4 bytes of color+alpha info
124 void my_glWritePPMFile(const char *filename, GLubyte *buffer, int win_width, int win_height, int mode)
127 unsigned char *ibuffer;
129 int pixelSize = mode==GL_RGBA?4:3;
131 ibuffer = (unsigned char *) malloc(win_width*win_height*RGB);
133 fp = fopen(filename, "wb");
134 fprintf(fp, "P6\n# CREATOR: glReadPixel()\n%d %d\n%d\n",
135 win_width, win_height, UCHAR_MAX);
137 for (i = 0; i < win_height; i++) {
138 for (j = 0; j < win_width; j++) {
139 for (k = 0; k < RGB; k++) {
140 ibuffer[q++] = (unsigned char)
141 *(buffer + (pixelSize*((win_height-1-i)*win_width+j)+k));
146 // *(buffer + (pixelSize*((win_height-1-i)*win_width+j)+k));
148 fwrite(ibuffer, sizeof(unsigned char), RGB*win_width*win_height, fp);
152 printf("wrote file (%d x %d pixels, %d bytes)\n",
153 win_width, win_height, RGB*win_width*win_height);
158 // initialize the sun object and connect it into our scene graph root
159 bool FGSkySun::initialize( const FGPath& path ) {
160 // create the scene graph for the sun/halo
161 skysun = new ssgRoot;
162 skysun->setName( "Sky Sun" );
164 // set up the orb state
165 orb_state = new ssgSimpleState();
166 orb_state->setShadeModel( GL_SMOOTH );
167 orb_state->disable( GL_LIGHTING );
168 orb_state->disable( GL_CULL_FACE );
169 orb_state->disable( GL_TEXTURE_2D );
170 orb_state->enable( GL_COLOR_MATERIAL );
171 orb_state->setColourMaterial( GL_AMBIENT_AND_DIFFUSE );
172 orb_state->disable( GL_BLEND );
173 orb_state->disable( GL_ALPHA_TEST );
175 cl = new ssgColourArray( 1 );
177 sgSetVec4( color, 1.0, 1.0, 1.0, 1.0 );
180 ssgBranch *orb = ssgMakeSphere( orb_state, cl, 550.0, 10, 10 );
182 // force a repaint of the sun colors with arbitrary defaults
186 // sun_texbuf = new GLubyte[64*64*3];
187 // sun_texid = makeHalo( sun_texbuf, 64 );
188 // my_glWritePPMFile("sunhalo.ppm", sun_texbuf, 64, 64, RGB);
190 // set up the halo state
191 FGPath halo_path = path;
192 halo_path.append( "halo.rgba" );
194 halo_state = new ssgSimpleState();
195 halo_state->setTexture( (char *)halo_path.c_str() );
196 // halo_state->setTexture( sun_texid );
197 halo_state->enable( GL_TEXTURE_2D );
198 halo_state->disable( GL_LIGHTING );
199 halo_state->setShadeModel( GL_SMOOTH );
200 halo_state->disable( GL_CULL_FACE );
202 halo_state->disable( GL_COLOR_MATERIAL );
203 halo_state->setColourMaterial( GL_AMBIENT_AND_DIFFUSE );
204 halo_state->setMaterial ( GL_AMBIENT_AND_DIFFUSE, 1, 1, 1, 1 ) ;
205 halo_state -> setMaterial ( GL_EMISSION, 0, 0, 0, 1 ) ;
206 halo_state -> setMaterial ( GL_SPECULAR, 0, 0, 0, 1 ) ;
207 // halo_state -> setShininess ( 0 ) ;
209 halo_state->setTranslucent();
210 halo_state->enable( GL_ALPHA_TEST );
211 halo_state->setAlphaClamp(0.01);
212 halo_state->enable ( GL_BLEND ) ;
215 // Build ssg structure
217 halo_vl = new ssgVertexArray;
218 sgSetVec3( v3, -5000.0, 0.0, -5000.0 );
220 sgSetVec3( v3, 5000.0, 0.0, -5000.0 );
222 sgSetVec3( v3, -5000.0, 0.0, 5000.0 );
224 sgSetVec3( v3, 5000.0, 0.0, 5000.0 );
228 halo_tl = new ssgTexCoordArray;
229 sgSetVec2( v2, 0.0f, 0.0f );
231 sgSetVec2( v2, 1.0, 0.0 );
233 sgSetVec2( v2, 0.0, 1.0 );
235 sgSetVec2( v2, 1.0, 1.0 );
239 new ssgVtxTable ( GL_TRIANGLE_STRIP, halo_vl, NULL, halo_tl, cl );
240 halo->setState( halo_state );
242 // build the ssg scene graph sub tree for the sky and connected
243 // into the provide scene graph branch
244 sun_selector = new ssgSelector;
245 sun_transform = new ssgTransform;
247 sun_selector->addKid( sun_transform );
248 sun_selector->clrTraversalMaskBits( SSGTRAV_HOT );
250 skysun->addKid( sun_selector );
252 sun_transform->addKid( halo );
253 sun_transform->addKid( orb );
259 // repaint the sun colors based on current value of sun_angle in
260 // degrees relative to verticle
261 // 0 degrees = high noon
262 // 90 degrees = sun rise/set
263 // 180 degrees = darkest midnight
264 bool FGSkySun::repaint( double sun_angle ) {
265 if ( sun_angle * RAD_TO_DEG < 100 ) {
266 // else sun is well below horizon (so no point in repainting it)
268 // x_10 = sun_angle^10
269 double x_10 = sun_angle * sun_angle * sun_angle * sun_angle * sun_angle
270 * sun_angle * sun_angle * sun_angle * sun_angle * sun_angle;
272 float ambient = (float)(0.4 * pow (1.1, - x_10 / 30.0));
273 if (ambient < 0.3) { ambient = 0.3; }
274 if (ambient > 1.0) { ambient = 1.0; }
278 (ambient * 6.0) - 1.0, // minimum value = 0.8
279 (ambient * 11.0) - 3.0, // minimum value = 0.3
280 (ambient * 12.0) - 3.6, // minimum value = 0.0
283 if (color[0] > 1.0) color[0] = 1.0;
284 if (color[1] > 1.0) color[1] = 1.0;
285 if (color[2] > 1.0) color[2] = 1.0;
287 // cout << "color = " << color[0] << " " << color[1] << " "
288 // << color[2] << endl;
292 sgCopyVec4( ptr, color );
299 // reposition the sun at the specified right ascension and
300 // declination, offset by our current position (p) so that it appears
301 // fixed at a great distance from the viewer. Also add in an optional
302 // rotation (i.e. for the current time of day.)
303 bool FGSkySun::reposition( sgVec3 p, double angle,
304 double rightAscension, double declination )
306 sgMat4 T1, T2, GST, RA, DEC;
310 sgMakeTransMat4( T1, p );
312 sgSetVec3( axis, 0.0, 0.0, -1.0 );
313 sgMakeRotMat4( GST, angle, axis );
315 // xglRotatef(((RAD_TO_DEG * rightAscension)- 90.0), 0.0, 0.0, 1.0);
316 sgSetVec3( axis, 0.0, 0.0, 1.0 );
317 sgMakeRotMat4( RA, (rightAscension * RAD_TO_DEG) - 90.0, axis );
319 // xglRotatef((RAD_TO_DEG * declination), 1.0, 0.0, 0.0);
320 sgSetVec3( axis, 1.0, 0.0, 0.0 );
321 sgMakeRotMat4( DEC, declination * RAD_TO_DEG, axis );
323 // xglTranslatef(0,60000,0);
324 sgSetVec3( v, 0.0, 60000.0, 0.0 );
325 sgMakeTransMat4( T2, v );
328 sgCopyMat4( TRANSFORM, T1 );
329 sgPreMultMat4( TRANSFORM, GST );
330 sgPreMultMat4( TRANSFORM, RA );
331 sgPreMultMat4( TRANSFORM, DEC );
332 sgPreMultMat4( TRANSFORM, T2 );
335 sgSetCoord( &skypos, TRANSFORM );
337 sun_transform->setTransform( &skypos );
344 bool FGSkySun::draw() {
345 ssgCullAndDraw( skysun );