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