// $Id$
+#include <stdio.h>
+#include <iostream>
+
#include <plib/ssg.h>
#include <simgear/constants.h>
+#include <simgear/misc/fgpath.hxx>
#include "sphere.hxx"
#include "skysun.hxx"
}
-// initialize the sun object and connect it into our scene graph root
-bool FGSkySun::initialize() {
+#if 0
+static GLuint makeHalo( GLubyte *sun_texbuf, int width ) {
+ int texSize;
+ GLuint texid;
+ GLubyte *p;
+ int i,j;
+ double radius;
+
+ // create a texture id
+#ifdef GL_VERSION_1_1
+ glGenTextures(1, &texid);
+ glBindTexture(GL_TEXTURE_2D, texid);
+#elif GL_EXT_texture_object
+ glGenTexturesEXT(1, &texid);
+ glBindTextureEXT(GL_TEXTURE_2D, texid);
+#else
+# error port me
+#endif
+
+ glPixelStorei( GL_UNPACK_ALIGNMENT, 4 );
+ glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
+ glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
+ glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE ) ;
+
+ // create the actual texture contents
+ texSize = width * width;
+
+ if ( !sun_texbuf ) {
+ cout << "ouch ..." << endl;
+ exit(-1); // Ugly!
+ }
+
+ p = sun_texbuf;
+
+ radius = (double)(width / 2);
+
+ GLubyte value;
+ double x, y, d;
+ for ( i = 0; i < width; i++ ) {
+ for ( j = 0; j < width; j++ ) {
+ x = fabs((double)(i - (width / 2)));
+ y = fabs((double)(j - (width / 2)));
+ d = sqrt((x * x) + (y * y));
+ if (d < radius) {
+ // t is 1.0 at center, 0.0 at edge
+ double t = 1.0 - (d / radius);
+
+ // inverse square looks nice
+ value = (int)((double) 0xff * (t*t));
+ } else {
+ value = 0x00;
+ }
+ *p = value;
+ *(p+1) = value;
+ *(p+2) = value;
+ // *(p+3) = value;
+
+ p += 3;
+ }
+ }
+
+ /* glTexImage2D( GL_TEXTURE_2D,
+ 0,
+ GL_RGBA,
+ width, width,
+ 0,
+ GL_RGBA, GL_UNSIGNED_BYTE,
+ sun_texbuf ); */
+
+ return texid;
+}
+
+
+#define RGB 3 // 3 bytes of color info per pixel
+#define RGBA 4 // 4 bytes of color+alpha info
+void my_glWritePPMFile(const char *filename, GLubyte *buffer, int win_width, int win_height, int mode)
+{
+ int i, j, k, q;
+ unsigned char *ibuffer;
+ FILE *fp;
+ int pixelSize = mode==GL_RGBA?4:3;
+
+ ibuffer = (unsigned char *) malloc(win_width*win_height*RGB);
+
+ fp = fopen(filename, "wb");
+ fprintf(fp, "P6\n# CREATOR: glReadPixel()\n%d %d\n%d\n",
+ win_width, win_height, UCHAR_MAX);
+ q = 0;
+ for (i = 0; i < win_height; i++) {
+ for (j = 0; j < win_width; j++) {
+ for (k = 0; k < RGB; k++) {
+ ibuffer[q++] = (unsigned char)
+ *(buffer + (pixelSize*((win_height-1-i)*win_width+j)+k));
+ }
+ }
+ }
- // create the scene graph for the dome
+ // *(buffer + (pixelSize*((win_height-1-i)*win_width+j)+k));
+
+ fwrite(ibuffer, sizeof(unsigned char), RGB*win_width*win_height, fp);
+ fclose(fp);
+ free(ibuffer);
+
+ printf("wrote file (%d x %d pixels, %d bytes)\n",
+ win_width, win_height, RGB*win_width*win_height);
+}
+#endif
+
+
+// initialize the sun object and connect it into our scene graph root
+bool FGSkySun::initialize( const FGPath& path ) {
+ // create the scene graph for the sun/halo
skysun = new ssgRoot;
skysun->setName( "Sky Sun" );
- // set up the state
+ // set up the orb state
orb_state = new ssgSimpleState();
orb_state->setShadeModel( GL_SMOOTH );
orb_state->disable( GL_LIGHTING );
- orb_state->disable( GL_DEPTH_TEST );
orb_state->disable( GL_CULL_FACE );
orb_state->disable( GL_TEXTURE_2D );
- orb_state->disable( GL_COLOR_MATERIAL );
- orb_state->setMaterial( GL_AMBIENT_AND_DIFFUSE, 1.0, 1.0, 1.0, 1.0 );
+ orb_state->enable( GL_COLOR_MATERIAL );
+ orb_state->setColourMaterial( GL_AMBIENT_AND_DIFFUSE );
+ orb_state->disable( GL_BLEND );
+ orb_state->disable( GL_ALPHA_TEST );
+
+ cl = new ssgColourArray( 1 );
+ sgVec4 color;
+ sgSetVec4( color, 1.0, 1.0, 1.0, 1.0 );
+ cl->add( color );
- ssgBranch *orb = ssgMakeSphere( orb_state, 550.0, 10, 10 );
+ ssgBranch *orb = ssgMakeSphere( orb_state, cl, 550.0, 10, 10 );
// force a repaint of the sun colors with arbitrary defaults
repaint( 0.0 );
+ // build the halo
+ // sun_texbuf = new GLubyte[64*64*3];
+ // sun_texid = makeHalo( sun_texbuf, 64 );
+ // my_glWritePPMFile("sunhalo.ppm", sun_texbuf, 64, 64, RGB);
+
+ // set up the halo state
+ FGPath halo_path = path;
+ halo_path.append( "halo.rgba" );
+
+ halo_state = new ssgSimpleState();
+ halo_state->setTexture( (char *)halo_path.c_str() );
+ // halo_state->setTexture( sun_texid );
+ halo_state->enable( GL_TEXTURE_2D );
+ halo_state->disable( GL_LIGHTING );
+ halo_state->setShadeModel( GL_SMOOTH );
+ halo_state->disable( GL_CULL_FACE );
+
+ halo_state->disable( GL_COLOR_MATERIAL );
+ halo_state->setColourMaterial( GL_AMBIENT_AND_DIFFUSE );
+ halo_state->setMaterial ( GL_AMBIENT_AND_DIFFUSE, 1, 1, 1, 1 ) ;
+ halo_state -> setMaterial ( GL_EMISSION, 0, 0, 0, 1 ) ;
+ halo_state -> setMaterial ( GL_SPECULAR, 0, 0, 0, 1 ) ;
+ // halo_state -> setShininess ( 0 ) ;
+
+ halo_state->setTranslucent();
+ halo_state->enable( GL_ALPHA_TEST );
+ halo_state->setAlphaClamp(0.01);
+ halo_state->enable ( GL_BLEND ) ;
+
+
+ // Build ssg structure
+ sgVec3 v3;
+ halo_vl = new ssgVertexArray;
+ sgSetVec3( v3, -5000.0, 0.0, -5000.0 );
+ halo_vl->add( v3 );
+ sgSetVec3( v3, 5000.0, 0.0, -5000.0 );
+ halo_vl->add( v3 );
+ sgSetVec3( v3, -5000.0, 0.0, 5000.0 );
+ halo_vl->add( v3 );
+ sgSetVec3( v3, 5000.0, 0.0, 5000.0 );
+ halo_vl->add( v3 );
+
+ sgVec2 v2;
+ halo_tl = new ssgTexCoordArray;
+ sgSetVec2( v2, 0.0f, 0.0f );
+ halo_tl->add( v2 );
+ sgSetVec2( v2, 1.0, 0.0 );
+ halo_tl->add( v2 );
+ sgSetVec2( v2, 0.0, 1.0 );
+ halo_tl->add( v2 );
+ sgSetVec2( v2, 1.0, 1.0 );
+ halo_tl->add( v2 );
+
+ ssgLeaf *halo =
+ new ssgVtxTable ( GL_TRIANGLE_STRIP, halo_vl, NULL, halo_tl, cl );
+ halo->setState( halo_state );
+
// build the ssg scene graph sub tree for the sky and connected
// into the provide scene graph branch
sun_selector = new ssgSelector;
sun_transform = new ssgTransform;
- // orb->setState( orb_state );
-
- sun_transform->addKid( orb );
-
sun_selector->addKid( sun_transform );
sun_selector->clrTraversalMaskBits( SSGTRAV_HOT );
skysun->addKid( sun_selector );
+ sun_transform->addKid( halo );
+ sun_transform->addKid( orb );
+
return true;
}
if (ambient < 0.3) { ambient = 0.3; }
if (ambient > 1.0) { ambient = 1.0; }
- sgVec3 color;
- sgSetVec3( color,
+ sgVec4 color;
+ sgSetVec4( color,
(ambient * 6.0) - 1.0, // minimum value = 0.8
(ambient * 11.0) - 3.0, // minimum value = 0.3
- (ambient * 12.0) - 3.6 // minimum value = 0.0
- );
+ (ambient * 12.0) - 3.6, // minimum value = 0.0
+ 1.0 );
if (color[0] > 1.0) color[0] = 1.0;
if (color[1] > 1.0) color[1] = 1.0;
if (color[2] > 1.0) color[2] = 1.0;
- orb_state->setMaterial( GL_AMBIENT_AND_DIFFUSE,
- color[0], color[1], color[2], 1.0 );
+ // cout << "color = " << color[0] << " " << color[1] << " "
+ // << color[2] << endl;
+
+ float *ptr;
+ ptr = cl->get( 0 );
+ sgCopyVec4( ptr, color );
}
return true;
}
-// reposition the sun at the specified right ascension and declination
-bool FGSkySun::reposition( double rightAscension, double declination ) {
- sgMat4 T, RA, DEC;
+// reposition the sun at the specified right ascension and
+// declination, offset by our current position (p) so that it appears
+// fixed at a great distance from the viewer. Also add in an optional
+// rotation (i.e. for the current time of day.)
+bool FGSkySun::reposition( sgVec3 p, double angle,
+ double rightAscension, double declination )
+{
+ sgMat4 T1, T2, GST, RA, DEC;
sgVec3 axis;
sgVec3 v;
+ sgMakeTransMat4( T1, p );
+
+ sgSetVec3( axis, 0.0, 0.0, -1.0 );
+ sgMakeRotMat4( GST, angle, axis );
+
// xglRotatef(((RAD_TO_DEG * rightAscension)- 90.0), 0.0, 0.0, 1.0);
sgSetVec3( axis, 0.0, 0.0, 1.0 );
sgMakeRotMat4( RA, (rightAscension * RAD_TO_DEG) - 90.0, axis );
// xglTranslatef(0,60000,0);
sgSetVec3( v, 0.0, 60000.0, 0.0 );
- sgMakeTransMat4( T, v );
+ sgMakeTransMat4( T2, v );
sgMat4 TRANSFORM;
- sgCopyMat4( TRANSFORM, RA );
+ sgCopyMat4( TRANSFORM, T1 );
+ sgPreMultMat4( TRANSFORM, GST );
+ sgPreMultMat4( TRANSFORM, RA );
sgPreMultMat4( TRANSFORM, DEC );
- sgPreMultMat4( TRANSFORM, T );
+ sgPreMultMat4( TRANSFORM, T2 );
sgCoord skypos;
sgSetCoord( &skypos, TRANSFORM );
return true;
}
-
-
-#if 0
-/*************************************************************************
- * Star::Star(FGTime *t)
- * Public constructor for class Star
- * Argument: The current time.
- * the hard coded orbital elements our sun are passed to
- * CelestialBody::CelestialBody();
- * note that the word sun is avoided, in order to prevent some compilation
- * problems on sun systems
- ************************************************************************/
-Star::Star(FGTime *t) :
- CelestialBody (0.000000, 0.0000000000,
- 0.0000, 0.00000,
- 282.9404, 4.7093500E-5,
- 1.0000000, 0.000000,
- 0.016709, -1.151E-9,
- 356.0470, 0.98560025850, t)
-{
-
- FG_LOG( FG_GENERAL, FG_INFO, "Initializing Sun Texture");
-#ifdef GL_VERSION_1_1
- xglGenTextures(1, &sun_texid);
- xglBindTexture(GL_TEXTURE_2D, sun_texid);
-#elif GL_EXT_texture_object
- xglGenTexturesEXT(1, &sun_texid);
- xglBindTextureEXT(GL_TEXTURE_2D, sun_texid);
-#else
-# error port me
-#endif
-
- glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
- setTexture();
- glTexImage2D( GL_TEXTURE_2D,
- 0,
- GL_RGBA,
- 256, 256,
- 0,
- GL_RGBA, GL_UNSIGNED_BYTE,
- sun_texbuf);
-
- SunObject = gluNewQuadric();
- if(SunObject == NULL)
- {
- printf("gluNewQuadric(SunObject) failed !\n");
- exit(0);
- }
-
- //SunList = 0;
- distance = 0.0;
-}
-
-Star::~Star()
-{
- //delete SunObject;
- delete [] sun_texbuf;
-}
-
-
-
-static int texWidth = 256; /* 64x64 is plenty */
-
-void Star::setTexture()
-{
- int texSize;
- //void *textureBuf;
- GLubyte *p;
- int i,j;
- double radius;
-
- texSize = texWidth*texWidth;
-
- sun_texbuf = new GLubyte[texSize*4];
- if (!sun_texbuf)
- return; // Ugly!
-
- p = sun_texbuf;
-
- radius = (double)(texWidth / 2);
-
- for (i=0; i < texWidth; i++) {
- for (j=0; j < texWidth; j++) {
- double x, y, d;
-
- *p = 0xff;
- *(p+1) = 0xff;
- *(p+2) = 0xff;
-
- x = fabs((double)(i - (texWidth / 2)));
- y = fabs((double)(j - (texWidth / 2)));
-
- d = sqrt((x * x) + (y * y));
- if (d < radius) {
- double t = 1.0 - (d / radius); // t is 1.0 at center, 0.0 at edge */
- // inverse square looks nice
- *(p+3) = (int)((double) 0xff * (t*t));
- } else {
- *(p+3) = 0x00;
- }
- p += 4;
- }
- }
- //gluBuild2DMipmaps(GL_TEXTURE_2D, 1, texWidth, texWidth,
- // GL_LUMINANCE,
- // GL_UNSIGNED_BYTE, textureBuf);
- //free(textureBuf);
-}
-/*************************************************************************
- * void Jupiter::updatePosition(FGTime *t, Star *ourSun)
- *
- * calculates the current position of our sun.
- *************************************************************************/
-void Star::updatePosition(FGTime *t)
-{
- double
- actTime, eccAnom,
- xv, yv, v, r,
- xe, ye, ze, ecl;
-
- updateOrbElements(t);
-
- actTime = fgCalcActTime(t);
- ecl = DEG_TO_RAD * (23.4393 - 3.563E-7 * actTime); // Angle in Radians
- eccAnom = fgCalcEccAnom(M, e); // Calculate the eccentric Anomaly (also known as solving Kepler's equation)
-
- xv = cos(eccAnom) - e;
- yv = sqrt (1.0 - e*e) * sin(eccAnom);
- v = atan2 (yv, xv); // the sun's true anomaly
- distance = r = sqrt (xv*xv + yv*yv); // and its distance
-
- lonEcl = v + w; // the sun's true longitude
- latEcl = 0;
-
- // convert the sun's true longitude to ecliptic rectangular
- // geocentric coordinates (xs, ys)
- xs = r * cos (lonEcl);
- ys = r * sin (lonEcl);
-
- // convert ecliptic coordinates to equatorial rectangular
- // geocentric coordinates
-
- xe = xs;
- ye = ys * cos (ecl);
- ze = ys * sin (ecl);
-
- // And finally, calculate right ascension and declination
- rightAscension = atan2 (ye, xe);
- declination = atan2 (ze, sqrt (xe*xe + ye*ye));
-}
-
-void Star::newImage(void)
-{
- /*static float stars[3];
- stars[0] = 0.0;
- stars[1] = 0.0;
- stars[2] = 1.0;*/
-
- fgLIGHT *l = &cur_light_params;
- float sun_angle = l->sun_angle;
-
- if( sun_angle*RAD_TO_DEG < 100 ) { // else no need to draw sun
-
-
- double x_2, x_4, x_8, x_10;
- GLfloat ambient;
- GLfloat amb[4];
- int sun_size = 550;
-
- // daily variation sun gets larger near horizon
- /*if(sun_angle*RAD_TO_DEG > 84.0 && sun_angle*RAD_TO_DEG < 95)
- {
- double sun_grow = 9*fabs(94-sun_angle*RAD_TO_DEG);
- sun_size = (int)(sun_size + sun_size * cos(sun_grow*DEG_TO_RAD));
- }*/
- x_2 = sun_angle * sun_angle;
- x_4 = x_2 * x_2;
- x_8 = x_4 * x_4;
- x_10 = x_8 * x_2;
- ambient = (float)(0.4 * pow (1.1, - x_10 / 30.0));
- if (ambient < 0.3) ambient = 0.3;
- if (ambient > 1.0) ambient = 1.0;
-
- amb[0] = ((ambient * 6.0) - 1.0); // minimum value = 0.8
- amb[1] = ((ambient * 11.0) - 3.0); // minimum value = 0.3
- amb[2] = ((ambient * 12.0) - 3.6); // minimum value = 0.0
- amb[3] = 1.00;
-
- if (amb[0] > 1.0) amb[0] = 1.0;
- if (amb[1] > 1.0) amb[1] = 1.0;
- if (amb[2] > 1.0) amb[2] = 1.0;
- xglColor3fv(amb);
- glPushMatrix();
- {
- xglRotatef(((RAD_TO_DEG * rightAscension)- 90.0), 0.0, 0.0, 1.0);
- xglRotatef((RAD_TO_DEG * declination), 1.0, 0.0, 0.0);
- xglTranslatef(0,60000,0);
- if (current_options.get_textures())
- {
- glEnable(GL_TEXTURE_2D); // TEXTURE ENABLED
- glEnable(GL_BLEND); // BLEND ENABLED
-
- // glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
- glBlendFunc ( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA ) ;
- glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
- glBindTexture(GL_TEXTURE_2D, sun_texid);
-
- glBegin(GL_QUADS);
- glTexCoord2f(0.0f, 0.0f); glVertex3f(-5000, 0.0, -5000);
- glTexCoord2f(1.0f, 0.0f); glVertex3f( 5000, 0.0, -5000);
- glTexCoord2f(1.0f, 1.0f); glVertex3f( 5000, 0.0, 5000);
- glTexCoord2f(0.0f, 1.0f); glVertex3f(-5000, 0.0, 5000);
- glEnd();
- }
- xglDisable(GL_TEXTURE_2D); // TEXTURE DISABLED
- xglDisable(GL_BLEND); // BLEND DISABLED
- }
-
- glPopMatrix();
- glDisable(GL_LIGHTING); // LIGHTING DISABLED
- glDisable(GL_BLEND); // BLEND DISABLED
- glPushMatrix();
- {
- xglRotatef(((RAD_TO_DEG * rightAscension)- 90.0), 0.0, 0.0, 1.0);
- xglRotatef((RAD_TO_DEG * declination), 1.0, 0.0, 0.0);
- xglColor4fv(amb);
- xglTranslatef(0,60000,0);
- gluSphere( SunObject, sun_size, 10, 10 );
- }
- glPopMatrix();
- glDisable(GL_TEXTURE_2D); // TEXTURE DISABLED
- glDisable(GL_BLEND); // BLEND DISABLED
- }
-}
-#endif