]> git.mxchange.org Git - simgear.git/blob - simgear/sky/oursun.cxx
6b03214bc3a1e2c122f4ffb0a4fada288a64ffb7
[simgear.git] / simgear / sky / oursun.cxx
1 // oursun.hxx -- model earth's sun
2 //
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). 
7 //
8 // Separated out rendering pieces and converted to ssg by Curt Olson,
9 // March 2000
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.
14 //
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.
19 //
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.
23 //
24 // $Id$
25
26
27 #ifdef HAVE_CONFIG_H
28 #  include <config.h>
29 #endif
30
31 #include <stdio.h>
32 #include <iostream>
33
34 #include <plib/ssg.h>
35
36 #include <simgear/constants.h>
37
38 #include "sphere.hxx"
39 #include "oursun.hxx"
40
41
42 // Set up sun rendering call backs
43 static int sgSunOrbPreDraw( ssgEntity *e ) {
44     /* cout << endl << "Sun orb pre draw" << endl << "----------------" 
45          << endl << endl; */
46     glDisable( GL_DEPTH_TEST );
47     glDisable( GL_FOG );
48
49     return true;
50 }
51
52 static int sgSunOrbPostDraw( ssgEntity *e ) {
53     /* cout << endl << "Sun orb post draw" << endl << "----------------" 
54          << endl << endl; */
55     glEnable( GL_DEPTH_TEST );
56     glEnable( GL_FOG );
57
58     return true;
59 }
60
61 static int sgSunHaloPreDraw( ssgEntity *e ) {
62     /* cout << endl << "Sun halo pre draw" << endl << "----------------" 
63          << endl << endl; */
64     glDisable( GL_DEPTH_TEST );
65     glDisable( GL_FOG );
66     glBlendFunc ( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA ) ;
67
68     return true;
69 }
70
71 static int sgSunHaloPostDraw( ssgEntity *e ) {
72     /* cout << endl << "Sun halo post draw" << endl << "----------------" 
73          << endl << endl; */
74     glEnable( GL_DEPTH_TEST );
75     glEnable( GL_FOG );
76
77     return true;
78 }
79
80
81 // Constructor
82 SGSun::SGSun( void ) {
83 }
84
85
86 // Destructor
87 SGSun::~SGSun( void ) {
88 }
89
90
91 #if 0
92 static GLuint makeHalo( GLubyte *sun_texbuf, int width ) {
93     int texSize;
94     GLuint texid;
95     GLubyte *p;
96     int i,j;
97     double radius;
98   
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);
106 #else
107 #   error port me
108 #endif
109
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 ) ;
114  
115     // create the actual texture contents
116     texSize = width * width;
117   
118     if ( !sun_texbuf ) {
119         cout << "ouch ..." << endl;
120         exit(-1);  // Ugly!
121     }
122
123     p = sun_texbuf;
124   
125     radius = (double)(width / 2);
126   
127     GLubyte value;
128     double x, y, d;
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));
134             if (d < radius) {
135                 // t is 1.0 at center, 0.0 at edge
136                 double t = 1.0 - (d / radius);
137
138                 // inverse square looks nice 
139                 value = (int)((double) 0xff * (t*t));
140             } else {
141                 value = 0x00;
142             }
143             *p = value;
144             *(p+1) = value;
145             *(p+2) = value;
146             // *(p+3) = value;
147
148             p += 3;
149         }
150     }
151
152     /* glTexImage2D( GL_TEXTURE_2D,
153                   0,
154                   GL_RGBA,
155                   width, width,
156                   0,
157                   GL_RGBA, GL_UNSIGNED_BYTE,
158                   sun_texbuf ); */
159
160     return texid;
161 }
162
163
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)
167 {
168     int i, j, k, q;
169     unsigned char *ibuffer;
170     FILE *fp;
171     int pixelSize = mode==GL_RGBA?4:3;
172
173     ibuffer = (unsigned char *) malloc(win_width*win_height*RGB);
174
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);
178     q = 0;
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));
184             }
185         }
186     }
187
188     // *(buffer + (pixelSize*((win_height-1-i)*win_width+j)+k));
189
190     fwrite(ibuffer, sizeof(unsigned char), RGB*win_width*win_height, fp);
191     fclose(fp);
192     free(ibuffer);
193
194     printf("wrote file (%d x %d pixels, %d bytes)\n",
195            win_width, win_height, RGB*win_width*win_height);
196 }
197 #endif
198
199
200 // initialize the sun object and connect it into our scene graph root
201 ssgBranch * SGSun::build( FGPath path, double sun_size ) {
202
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 );
213
214     cl = new ssgColourArray( 1 );
215     sgVec4 color;
216     sgSetVec4( color, 1.0, 1.0, 1.0, 1.0 );
217     cl->add( color );
218
219     ssgBranch *orb = ssgMakeSphere( orb_state, cl, sun_size, 10, 10, 
220                                     sgSunOrbPreDraw, sgSunOrbPostDraw );
221
222     // force a repaint of the sun colors with arbitrary defaults
223     repaint( 0.0 );
224
225     // build the halo
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);
229
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 ) ;
243
244     // Build ssg structure
245     double size = sun_size * 10.0;
246     sgVec3 v3;
247     halo_vl = new ssgVertexArray;
248     sgSetVec3( v3, -size, 0.0, -size );
249     halo_vl->add( v3 );
250     sgSetVec3( v3, size, 0.0, -size );
251     halo_vl->add( v3 );
252     sgSetVec3( v3, -size, 0.0,  size );
253     halo_vl->add( v3 );
254     sgSetVec3( v3, size, 0.0,  size );
255     halo_vl->add( v3 );
256
257     sgVec2 v2;
258     halo_tl = new ssgTexCoordArray;
259     sgSetVec2( v2, 0.0f, 0.0f );
260     halo_tl->add( v2 );
261     sgSetVec2( v2, 1.0, 0.0 );
262     halo_tl->add( v2 );
263     sgSetVec2( v2, 0.0, 1.0 );
264     halo_tl->add( v2 );
265     sgSetVec2( v2, 1.0, 1.0 );
266     halo_tl->add( v2 );
267
268     ssgLeaf *halo = 
269         new ssgVtxTable ( GL_TRIANGLE_STRIP, halo_vl, NULL, halo_tl, cl );
270     halo->setState( halo_state );
271
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;
275
276     sun_transform->addKid( halo );
277     halo->setCallback( SSG_CALLBACK_PREDRAW, sgSunHaloPreDraw );
278     halo->setCallback( SSG_CALLBACK_POSTDRAW, sgSunHaloPostDraw );
279     sun_transform->addKid( orb );
280
281     return sun_transform;
282 }
283
284
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)
293     
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;
297
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; }
301
302         sgVec4 color;
303         sgSetVec4( color,
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
307                    1.0 );
308     
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;
312
313         // cout << "color = " << color[0] << " " << color[1] << " " 
314         //      << color[2] << endl;
315
316         float *ptr;
317         ptr = cl->get( 0 );
318         sgCopyVec4( ptr, color );
319     }
320
321     return true;
322 }
323
324
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, 
331                         double sun_dist )
332 {
333     sgMat4 T1, T2, GST, RA, DEC;
334     sgVec3 axis;
335     sgVec3 v;
336
337     sgMakeTransMat4( T1, p );
338
339     sgSetVec3( axis, 0.0, 0.0, -1.0 );
340     sgMakeRotMat4( GST, angle, axis );
341
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 );
345
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 );
349
350     // xglTranslatef(0,60000,0);
351     sgSetVec3( v, 0.0, 60000.0, 0.0 );
352     sgMakeTransMat4( T2, v );
353
354     sgMat4 TRANSFORM;
355     sgCopyMat4( TRANSFORM, T1 );
356     sgPreMultMat4( TRANSFORM, GST );
357     sgPreMultMat4( TRANSFORM, RA );
358     sgPreMultMat4( TRANSFORM, DEC );
359     sgPreMultMat4( TRANSFORM, T2 );
360
361     sgCoord skypos;
362     sgSetCoord( &skypos, TRANSFORM );
363
364     sun_transform->setTransform( &skypos );
365
366     return true;
367 }