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