]> git.mxchange.org Git - simgear.git/blob - simgear/sky/oursun.cxx
Minor tidying up of interface.
[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/sg.h>
35 #include <plib/ssg.h>
36
37 #include "sphere.hxx"
38 #include "oursun.hxx"
39
40
41 // Set up sun rendering call backs
42 static int sgSunOrbPreDraw( ssgEntity *e ) {
43     /* cout << endl << "Sun orb pre draw" << endl << "----------------" 
44          << endl << endl; */
45     glDisable( GL_DEPTH_TEST );
46     glDisable( GL_FOG );
47
48     return true;
49 }
50
51 static int sgSunOrbPostDraw( ssgEntity *e ) {
52     /* cout << endl << "Sun orb post draw" << endl << "----------------" 
53          << endl << endl; */
54     glEnable( GL_DEPTH_TEST );
55     glEnable( GL_FOG );
56
57     return true;
58 }
59
60 static int sgSunHaloPreDraw( ssgEntity *e ) {
61     /* cout << endl << "Sun halo pre draw" << endl << "----------------" 
62          << endl << endl; */
63     glDisable( GL_DEPTH_TEST );
64     glDisable( GL_FOG );
65     glBlendFunc ( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA ) ;
66
67     return true;
68 }
69
70 static int sgSunHaloPostDraw( ssgEntity *e ) {
71     /* cout << endl << "Sun halo post draw" << endl << "----------------" 
72          << endl << endl; */
73     glEnable( GL_DEPTH_TEST );
74     glEnable( GL_FOG );
75
76     return true;
77 }
78
79
80 // Constructor
81 SGSun::SGSun( void ) {
82 }
83
84
85 // Destructor
86 SGSun::~SGSun( void ) {
87 }
88
89
90 #if 0
91 static GLuint makeHalo( GLubyte *sun_texbuf, int width ) {
92     int texSize;
93     GLuint texid;
94     GLubyte *p;
95     int i,j;
96     double radius;
97   
98     // create a texture id
99 #ifdef GL_VERSION_1_1
100     glGenTextures(1, &texid);
101     glBindTexture(GL_TEXTURE_2D, texid);
102 #elif GL_EXT_texture_object
103     glGenTexturesEXT(1, &texid);
104     glBindTextureEXT(GL_TEXTURE_2D, texid);
105 #else
106 #   error port me
107 #endif
108
109     glPixelStorei( GL_UNPACK_ALIGNMENT, 4 );
110     glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
111     glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
112     glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE ) ;
113  
114     // create the actual texture contents
115     texSize = width * width;
116   
117     if ( !sun_texbuf ) {
118         cout << "ouch ..." << endl;
119         exit(-1);  // Ugly!
120     }
121
122     p = sun_texbuf;
123   
124     radius = (double)(width / 2);
125   
126     GLubyte value;
127     double x, y, d;
128     for ( i = 0; i < width; i++ ) {
129         for ( j = 0; j < width; j++ ) {
130             x = fabs((double)(i - (width / 2)));
131             y = fabs((double)(j - (width / 2)));
132             d = sqrt((x * x) + (y * y));
133             if (d < radius) {
134                 // t is 1.0 at center, 0.0 at edge
135                 double t = 1.0 - (d / radius);
136
137                 // inverse square looks nice 
138                 value = (int)((double) 0xff * (t*t));
139             } else {
140                 value = 0x00;
141             }
142             *p = value;
143             *(p+1) = value;
144             *(p+2) = value;
145             // *(p+3) = value;
146
147             p += 3;
148         }
149     }
150
151     /* glTexImage2D( GL_TEXTURE_2D,
152                   0,
153                   GL_RGBA,
154                   width, width,
155                   0,
156                   GL_RGBA, GL_UNSIGNED_BYTE,
157                   sun_texbuf ); */
158
159     return texid;
160 }
161
162
163 #define RGB  3                  // 3 bytes of color info per pixel
164 #define RGBA 4                  // 4 bytes of color+alpha info
165 void my_glWritePPMFile(const char *filename, GLubyte *buffer, int win_width, int win_height, int mode)
166 {
167     int i, j, k, q;
168     unsigned char *ibuffer;
169     FILE *fp;
170     int pixelSize = mode==GL_RGBA?4:3;
171
172     ibuffer = (unsigned char *) malloc(win_width*win_height*RGB);
173
174     fp = fopen(filename, "wb");
175     fprintf(fp, "P6\n# CREATOR: glReadPixel()\n%d %d\n%d\n",
176             win_width, win_height, UCHAR_MAX);
177     q = 0;
178     for (i = 0; i < win_height; i++) {
179         for (j = 0; j < win_width; j++) {
180             for (k = 0; k < RGB; k++) {
181                 ibuffer[q++] = (unsigned char)
182                     *(buffer + (pixelSize*((win_height-1-i)*win_width+j)+k));
183             }
184         }
185     }
186
187     // *(buffer + (pixelSize*((win_height-1-i)*win_width+j)+k));
188
189     fwrite(ibuffer, sizeof(unsigned char), RGB*win_width*win_height, fp);
190     fclose(fp);
191     free(ibuffer);
192
193     printf("wrote file (%d x %d pixels, %d bytes)\n",
194            win_width, win_height, RGB*win_width*win_height);
195 }
196 #endif
197
198
199 // initialize the sun object and connect it into our scene graph root
200 ssgBranch * SGSun::build( FGPath path, double sun_size ) {
201
202     // set up the orb state
203     orb_state = new ssgSimpleState();
204     orb_state->setShadeModel( GL_SMOOTH );
205     orb_state->disable( GL_LIGHTING );
206     // orb_state->enable( 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->setMaterial( GL_EMISSION, 0, 0, 0, 1 );
212     orb_state->setMaterial( GL_SPECULAR, 0, 0, 0, 1 );
213     orb_state->disable( GL_BLEND );
214     orb_state->disable( GL_ALPHA_TEST );
215
216     cl = new ssgColourArray( 1 );
217     sgVec4 color;
218     sgSetVec4( color, 1.0, 1.0, 1.0, 1.0 );
219     cl->add( color );
220
221     ssgBranch *orb = ssgMakeSphere( orb_state, cl, sun_size, 10, 10, 
222                                     sgSunOrbPreDraw, sgSunOrbPostDraw );
223
224     // force a repaint of the sun colors with arbitrary defaults
225     repaint( 0.0 );
226
227     // build the halo
228     // sun_texbuf = new GLubyte[64*64*3];
229     // sun_texid = makeHalo( sun_texbuf, 64 );
230     // my_glWritePPMFile("sunhalo.ppm", sun_texbuf, 64, 64, RGB);
231
232     // set up the halo state
233     path.append( "halo.rgba" );
234     halo_state = new ssgSimpleState();
235     halo_state->setTexture( (char *)path.c_str() );
236     halo_state->enable( GL_TEXTURE_2D );
237     halo_state->disable( GL_LIGHTING );
238     // halo_state->enable( GL_LIGHTING );
239     halo_state->setShadeModel( GL_SMOOTH );
240     halo_state->disable( GL_CULL_FACE );
241     halo_state->enable( GL_COLOR_MATERIAL );
242     halo_state->setColourMaterial( GL_AMBIENT_AND_DIFFUSE );
243     halo_state->setMaterial( GL_EMISSION, 0, 0, 0, 1 );
244     halo_state->setMaterial( GL_SPECULAR, 0, 0, 0, 1 );
245     halo_state->enable( GL_ALPHA_TEST );
246     halo_state->setAlphaClamp(0.01);
247     halo_state->enable ( GL_BLEND ) ;
248
249     // Build ssg structure
250     double size = sun_size * 10.0;
251     sgVec3 v3;
252     halo_vl = new ssgVertexArray;
253     sgSetVec3( v3, -size, 0.0, -size );
254     halo_vl->add( v3 );
255     sgSetVec3( v3, size, 0.0, -size );
256     halo_vl->add( v3 );
257     sgSetVec3( v3, -size, 0.0,  size );
258     halo_vl->add( v3 );
259     sgSetVec3( v3, size, 0.0,  size );
260     halo_vl->add( v3 );
261
262     sgVec2 v2;
263     halo_tl = new ssgTexCoordArray;
264     sgSetVec2( v2, 0.0f, 0.0f );
265     halo_tl->add( v2 );
266     sgSetVec2( v2, 1.0, 0.0 );
267     halo_tl->add( v2 );
268     sgSetVec2( v2, 0.0, 1.0 );
269     halo_tl->add( v2 );
270     sgSetVec2( v2, 1.0, 1.0 );
271     halo_tl->add( v2 );
272
273     ssgLeaf *halo = 
274         new ssgVtxTable ( GL_TRIANGLE_STRIP, halo_vl, NULL, halo_tl, cl );
275     halo->setState( halo_state );
276
277     // build the ssg scene graph sub tree for the sky and connected
278     // into the provide scene graph branch
279     sun_transform = new ssgTransform;
280
281     halo->setCallback( SSG_CALLBACK_PREDRAW, sgSunHaloPreDraw );
282     halo->setCallback( SSG_CALLBACK_POSTDRAW, sgSunHaloPostDraw );
283     sun_transform->addKid( halo );
284     sun_transform->addKid( orb );
285
286     return sun_transform;
287 }
288
289
290 // repaint the sun colors based on current value of sun_angle in
291 // degrees relative to verticle
292 // 0 degrees = high noon
293 // 90 degrees = sun rise/set
294 // 180 degrees = darkest midnight
295 bool SGSun::repaint( double sun_angle ) {
296     if ( sun_angle * SGD_RADIANS_TO_DEGREES < 100 ) {
297         // else sun is well below horizon (so no point in repainting it)
298     
299         // x_10 = sun_angle^10
300         double x_10 = sun_angle * sun_angle * sun_angle * sun_angle * sun_angle
301             * sun_angle * sun_angle * sun_angle * sun_angle * sun_angle;
302
303         float ambient = (float)(0.4 * pow (1.1, - x_10 / 30.0));
304         if (ambient < 0.3) { ambient = 0.3; }
305         if (ambient > 1.0) { ambient = 1.0; }
306
307         sgVec4 color;
308         sgSetVec4( color,
309                    (ambient * 6.0)  - 1.0, // minimum value = 0.8
310                    (ambient * 11.0) - 3.0, // minimum value = 0.3
311                    (ambient * 12.0) - 3.6, // minimum value = 0.0
312                    1.0 );
313     
314         // temp test, forces the color to always be white
315         // sgSetVec4( color, 1.0, 1.0, 1.0, 1.0 );
316
317         if (color[0] > 1.0) color[0] = 1.0;
318         if (color[1] > 1.0) color[1] = 1.0;
319         if (color[2] > 1.0) color[2] = 1.0;
320
321         // cout << "color = " << color[0] << " " << color[1] << " " 
322         //      << color[2] << endl;
323
324         float *ptr;
325         ptr = cl->get( 0 );
326         sgCopyVec4( ptr, color );
327     }
328
329     return true;
330 }
331
332
333 // reposition the sun at the specified right ascension and
334 // declination, offset by our current position (p) so that it appears
335 // fixed at a great distance from the viewer.  Also add in an optional
336 // rotation (i.e. for the current time of day.)
337 bool SGSun::reposition( sgVec3 p, double angle,
338                         double rightAscension, double declination, 
339                         double sun_dist )
340 {
341     sgMat4 T1, T2, GST, RA, DEC;
342     sgVec3 axis;
343     sgVec3 v;
344
345     sgMakeTransMat4( T1, p );
346
347     sgSetVec3( axis, 0.0, 0.0, -1.0 );
348     sgMakeRotMat4( GST, angle, axis );
349
350     // xglRotatef( ((SGD_RADIANS_TO_DEGREES * rightAscension)- 90.0),
351     //             0.0, 0.0, 1.0);
352     sgSetVec3( axis, 0.0, 0.0, 1.0 );
353     sgMakeRotMat4( RA, (rightAscension * SGD_RADIANS_TO_DEGREES) - 90.0, axis );
354
355     // xglRotatef((SGD_RADIANS_TO_DEGREES * declination), 1.0, 0.0, 0.0);
356     sgSetVec3( axis, 1.0, 0.0, 0.0 );
357     sgMakeRotMat4( DEC, declination * SGD_RADIANS_TO_DEGREES, axis );
358
359     // xglTranslatef(0,sun_dist);
360     sgSetVec3( v, 0.0, sun_dist, 0.0 );
361     sgMakeTransMat4( T2, v );
362
363     sgMat4 TRANSFORM;
364     sgCopyMat4( TRANSFORM, T1 );
365     sgPreMultMat4( TRANSFORM, GST );
366     sgPreMultMat4( TRANSFORM, RA );
367     sgPreMultMat4( TRANSFORM, DEC );
368     sgPreMultMat4( TRANSFORM, T2 );
369
370     sgCoord skypos;
371     sgSetCoord( &skypos, TRANSFORM );
372
373     sun_transform->setTransform( &skypos );
374
375     return true;
376 }