]> git.mxchange.org Git - simgear.git/blob - simgear/sky/oursun.cxx
Minor fiddling with material properties *trying* to chase down a visual bug
[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->enable( GL_LIGHTING );
208     orb_state->disable( GL_CULL_FACE );
209     orb_state->disable( GL_TEXTURE_2D );
210     orb_state->enable( GL_COLOR_MATERIAL );
211     orb_state->setColourMaterial( GL_AMBIENT_AND_DIFFUSE );
212     orb_state->setMaterial( GL_EMISSION, 0, 0, 0, 1 );
213     orb_state->setMaterial( GL_SPECULAR, 0, 0, 0, 1 );
214     orb_state->disable( GL_BLEND );
215     orb_state->disable( GL_ALPHA_TEST );
216
217     cl = new ssgColourArray( 1 );
218     sgVec4 color;
219     sgSetVec4( color, 1.0, 1.0, 1.0, 1.0 );
220     cl->add( color );
221
222     ssgBranch *orb = ssgMakeSphere( orb_state, cl, sun_size, 10, 10, 
223                                     sgSunOrbPreDraw, sgSunOrbPostDraw );
224
225     // force a repaint of the sun colors with arbitrary defaults
226     repaint( 0.0 );
227
228     // build the halo
229     // sun_texbuf = new GLubyte[64*64*3];
230     // sun_texid = makeHalo( sun_texbuf, 64 );
231     // my_glWritePPMFile("sunhalo.ppm", sun_texbuf, 64, 64, RGB);
232
233     // set up the halo state
234     path.append( "halo.rgba" );
235     halo_state = new ssgSimpleState();
236     halo_state->setTexture( (char *)path.c_str() );
237     halo_state->enable( GL_TEXTURE_2D );
238     halo_state->disable( GL_LIGHTING );
239     // halo_state->enable( GL_LIGHTING );
240     halo_state->setShadeModel( GL_SMOOTH );
241     halo_state->disable( GL_CULL_FACE );
242     halo_state->enable( GL_COLOR_MATERIAL );
243     halo_state->setColourMaterial( GL_AMBIENT_AND_DIFFUSE );
244     halo_state->setMaterial( GL_EMISSION, 0, 0, 0, 1 );
245     halo_state->setMaterial( GL_SPECULAR, 0, 0, 0, 1 );
246     halo_state->enable( GL_ALPHA_TEST );
247     halo_state->setAlphaClamp(0.01);
248     halo_state->enable ( GL_BLEND ) ;
249
250     // Build ssg structure
251     double size = sun_size * 10.0;
252     sgVec3 v3;
253     halo_vl = new ssgVertexArray;
254     sgSetVec3( v3, -size, 0.0, -size );
255     halo_vl->add( v3 );
256     sgSetVec3( v3, size, 0.0, -size );
257     halo_vl->add( v3 );
258     sgSetVec3( v3, -size, 0.0,  size );
259     halo_vl->add( v3 );
260     sgSetVec3( v3, size, 0.0,  size );
261     halo_vl->add( v3 );
262
263     sgVec2 v2;
264     halo_tl = new ssgTexCoordArray;
265     sgSetVec2( v2, 0.0f, 0.0f );
266     halo_tl->add( v2 );
267     sgSetVec2( v2, 1.0, 0.0 );
268     halo_tl->add( v2 );
269     sgSetVec2( v2, 0.0, 1.0 );
270     halo_tl->add( v2 );
271     sgSetVec2( v2, 1.0, 1.0 );
272     halo_tl->add( v2 );
273
274     ssgLeaf *halo = 
275         new ssgVtxTable ( GL_TRIANGLE_STRIP, halo_vl, NULL, halo_tl, cl );
276     halo->setState( halo_state );
277
278     // build the ssg scene graph sub tree for the sky and connected
279     // into the provide scene graph branch
280     sun_transform = new ssgTransform;
281
282     halo->setCallback( SSG_CALLBACK_PREDRAW, sgSunHaloPreDraw );
283     halo->setCallback( SSG_CALLBACK_POSTDRAW, sgSunHaloPostDraw );
284     sun_transform->addKid( halo );
285     sun_transform->addKid( orb );
286
287     return sun_transform;
288 }
289
290
291 // repaint the sun colors based on current value of sun_angle in
292 // degrees relative to verticle
293 // 0 degrees = high noon
294 // 90 degrees = sun rise/set
295 // 180 degrees = darkest midnight
296 bool SGSun::repaint( double sun_angle ) {
297     if ( sun_angle * RAD_TO_DEG < 100 ) {
298         // else sun is well below horizon (so no point in repainting it)
299     
300         // x_10 = sun_angle^10
301         double x_10 = sun_angle * sun_angle * sun_angle * sun_angle * sun_angle
302             * sun_angle * sun_angle * sun_angle * sun_angle * sun_angle;
303
304         float ambient = (float)(0.4 * pow (1.1, - x_10 / 30.0));
305         if (ambient < 0.3) { ambient = 0.3; }
306         if (ambient > 1.0) { ambient = 1.0; }
307
308         sgVec4 color;
309         sgSetVec4( color,
310                    (ambient * 6.0)  - 1.0, // minimum value = 0.8
311                    (ambient * 11.0) - 3.0, // minimum value = 0.3
312                    (ambient * 12.0) - 3.6, // minimum value = 0.0
313                    1.0 );
314     
315         if (color[0] > 1.0) color[0] = 1.0;
316         if (color[1] > 1.0) color[1] = 1.0;
317         if (color[2] > 1.0) color[2] = 1.0;
318
319         // cout << "color = " << color[0] << " " << color[1] << " " 
320         //      << color[2] << endl;
321
322         float *ptr;
323         ptr = cl->get( 0 );
324         sgCopyVec4( ptr, color );
325     }
326
327     return true;
328 }
329
330
331 // reposition the sun at the specified right ascension and
332 // declination, offset by our current position (p) so that it appears
333 // fixed at a great distance from the viewer.  Also add in an optional
334 // rotation (i.e. for the current time of day.)
335 bool SGSun::reposition( sgVec3 p, double angle,
336                         double rightAscension, double declination, 
337                         double sun_dist )
338 {
339     sgMat4 T1, T2, GST, RA, DEC;
340     sgVec3 axis;
341     sgVec3 v;
342
343     sgMakeTransMat4( T1, p );
344
345     sgSetVec3( axis, 0.0, 0.0, -1.0 );
346     sgMakeRotMat4( GST, angle, axis );
347
348     // xglRotatef(((RAD_TO_DEG * rightAscension)- 90.0), 0.0, 0.0, 1.0);
349     sgSetVec3( axis, 0.0, 0.0, 1.0 );
350     sgMakeRotMat4( RA, (rightAscension * RAD_TO_DEG) - 90.0, axis );
351
352     // xglRotatef((RAD_TO_DEG * declination), 1.0, 0.0, 0.0);
353     sgSetVec3( axis, 1.0, 0.0, 0.0 );
354     sgMakeRotMat4( DEC, declination * RAD_TO_DEG, axis );
355
356     // xglTranslatef(0,sun_dist);
357     sgSetVec3( v, 0.0, sun_dist, 0.0 );
358     sgMakeTransMat4( T2, v );
359
360     sgMat4 TRANSFORM;
361     sgCopyMat4( TRANSFORM, T1 );
362     sgPreMultMat4( TRANSFORM, GST );
363     sgPreMultMat4( TRANSFORM, RA );
364     sgPreMultMat4( TRANSFORM, DEC );
365     sgPreMultMat4( TRANSFORM, T2 );
366
367     sgCoord skypos;
368     sgSetCoord( &skypos, TRANSFORM );
369
370     sun_transform->setTransform( &skypos );
371
372     return true;
373 }