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