]> git.mxchange.org Git - simgear.git/blobdiff - simgear/scene/sky/stars.cxx
Merge branch 'jester/aptsign'
[simgear.git] / simgear / scene / sky / stars.cxx
index 55602b2fb258cb2bce263c507d192a2b63018f6b..32b8244f766403688b182f4a9c59cc880fb3e17e 100644 (file)
 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 // Library General Public License for more details.
 //
-// You should have received a copy of the GNU Library General Public
-// License along with this library; if not, write to the
-// Free Software Foundation, Inc., 59 Temple Place - Suite 330,
-// Boston, MA  02111-1307, USA.
+// You should have received a copy of the GNU General Public License
+// along with this program; if not, write to the Free Software
+// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 //
 // $Id$
 
+#ifdef HAVE_CONFIG_H
+#  include <simgear_config.h>
+#endif
+
+#ifdef __CYGWIN__
+#include <ieeefp.h>
+#endif
 
 #include <simgear/compiler.h>
+#include <simgear/constants.h>
+#include <simgear/debug/logstream.hxx>
 
 #include <stdio.h>
-#include STL_IOSTREAM
-
-#include <plib/sg.h>
-#include <plib/ssg.h>
+#include <iostream>
+
+#include <osg/AlphaFunc>
+#include <osg/BlendFunc>
+#include <osg/Geode>
+#include <osg/Geometry>
+#include <osg/Image>
+#include <osg/Material>
+#include <osg/Point>
+#include <osg/ShadeModel>
+#include <osg/Node>
 
 #include "stars.hxx"
 
-SG_USING_STD(cout);
-SG_USING_STD(endl);
-
-
-// Set up star rendering call backs
-static int sgStarPreDraw( ssgEntity *e ) {
-    /* cout << endl << "Star pre draw" << endl << "----------------" 
-       << endl << endl; */
-
-    ssgLeaf *f = (ssgLeaf *)e;
-    if ( f -> hasState () ) f->getState()->apply() ;
-
-    glPushAttrib( GL_DEPTH_BUFFER_BIT | GL_FOG_BIT );
-
-    glDisable( GL_DEPTH_TEST );
-    glDisable( GL_FOG );
-    // glBlendFunc ( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA ) ;
-
-    return true;
-}
-
-static int sgStarPostDraw( ssgEntity *e ) {
-    /* cout << endl << "Star post draw" << endl << "----------------" 
-       << endl << endl; */
-
-    glPopAttrib();
-
-    // glEnable( GL_DEPTH_TEST );
-    // glEnable( GL_FOG );
-
-    return true;
-}
-
-
 // Constructor
 SGStars::SGStars( void ) :
 old_phase(-1)
@@ -82,64 +63,56 @@ SGStars::~SGStars( void ) {
 
 
 // initialize the stars object and connect it into our scene graph root
-ssgBranch * SGStars::build( int num, sgdVec3 *star_data, double star_dist ) {
-    sgVec4 color;
-
-    if ( star_data == NULL ) {
-        cout << "WARNING: null star data passed to SGStars::build()" << endl;
-    }
-
-    // set up the orb state
-    state = new ssgSimpleState();
-    state->disable( GL_LIGHTING );
-    state->disable( GL_CULL_FACE );
-    state->disable( GL_TEXTURE_2D );
-    state->enable( GL_COLOR_MATERIAL );
-    state->setColourMaterial( GL_AMBIENT_AND_DIFFUSE );
-    state->setMaterial( GL_EMISSION, 0, 0, 0, 1 );
-    state->setMaterial( GL_SPECULAR, 0, 0, 0, 1 );
-    state->enable( GL_BLEND );
-    state->disable( GL_ALPHA_TEST );
-
-    vl = new ssgVertexArray( num );
-    cl = new ssgColourArray( num );
-    // cl = new ssgColourArray( 1 );
-    // sgSetVec4( color, 1.0, 1.0, 1.0, 1.0 );
-    // cl->add( color );
-
-    // Build ssg structure
-    sgVec3 p;
+osg::Node*
+SGStars::build( int num, const SGVec3d star_data[], double star_dist ) {
+    osg::Geode* geode = new osg::Geode;
+    osg::StateSet* stateSet = geode->getOrCreateStateSet();
+    stateSet->setRenderBinDetails(-9, "RenderBin");
+
+    // set up the star state
+    osg::BlendFunc* blendFunc = new osg::BlendFunc;
+    blendFunc->setFunction(osg::BlendFunc::SRC_ALPHA,
+                           osg::BlendFunc::ONE_MINUS_SRC_ALPHA);
+    stateSet->setAttributeAndModes(blendFunc);
+
+//     osg::Point* point = new osg::Point;
+//     point->setSize(5);
+//     stateSet->setAttributeAndModes(point);
+
+    stateSet->setMode(GL_FOG, osg::StateAttribute::OFF);
+    stateSet->setMode(GL_LIGHTING, osg::StateAttribute::OFF);
+    stateSet->setMode(GL_CULL_FACE, osg::StateAttribute::OFF);
+    stateSet->setMode(GL_DEPTH_TEST, osg::StateAttribute::OFF);
+    stateSet->setMode(GL_ALPHA_TEST, osg::StateAttribute::OFF);
+
+    // Build scenegraph structure
+    
+    cl = new osg::Vec4Array;
+    osg::Vec3Array* vl = new osg::Vec3Array;
+
+    // Build scenegraph structure
     for ( int i = 0; i < num; ++i ) {
         // position seeded to arbitrary values
-        sgSetVec3( p, 
-                   star_dist * cos( star_data[i][0] )
-                   * cos( star_data[i][1] ),
-                   star_dist * sin( star_data[i][0] )
-                   * cos( star_data[i][1] ),
-                   star_dist * sin( star_data[i][1] )
-                   );
-        vl->add( p );
+        vl->push_back(osg::Vec3(star_dist * cos( star_data[i][0])
+                                * cos( star_data[i][1] ),
+                                star_dist * sin( star_data[i][0])
+                                * cos( star_data[i][1] ),
+                                star_dist * sin( star_data[i][1])));
 
         // color (magnitude)
-        sgSetVec4( color, 1.0, 1.0, 1.0, 1.0 );
-        cl->add( color );
+        cl->push_back(osg::Vec4(1, 1, 1, 1));
     }
 
-    ssgLeaf *stars_obj = 
-        new ssgVtxTable ( GL_POINTS, vl, NULL, NULL, cl );
-    stars_obj->setState( state );
-    stars_obj->setCallback( SSG_CALLBACK_PREDRAW, sgStarPreDraw );
-    stars_obj->setCallback( SSG_CALLBACK_POSTDRAW, sgStarPostDraw );
-
-    // build the ssg scene graph sub tree for the sky and connected
-    // into the provide scene graph branch
-    stars_transform = new ssgTransform;
+    osg::Geometry* geometry = new osg::Geometry;
+    geometry->setUseDisplayList(false);
+    geometry->setVertexArray(vl);
+    geometry->setColorArray(cl.get());
+    geometry->setColorBinding(osg::Geometry::BIND_PER_VERTEX);
+    geometry->setNormalBinding(osg::Geometry::BIND_OFF);
+    geometry->addPrimitiveSet(new osg::DrawArrays(GL_POINTS, 0, vl->size()));
+    geode->addDrawable(geometry);
 
-    stars_transform->addKid( stars_obj );
-
-    cout << "stars = " << stars_transform << endl;
-
-    return stars_transform;
+    return geode;
 }
 
 
@@ -148,42 +121,41 @@ ssgBranch * SGStars::build( int num, sgdVec3 *star_data, double star_dist ) {
 // 0 degrees = high noon
 // 90 degrees = sun rise/set
 // 180 degrees = darkest midnight
-bool SGStars::repaint( double sun_angle, int num, sgdVec3 *star_data ) {
+bool SGStars::repaint( double sun_angle, int num, const SGVec3d star_data[] ) {
     // cout << "repainting stars" << endl;
     // double min = 100;
     // double max = -100;
     double mag, nmag, alpha, factor, cutoff;
-    float *color;
 
     int phase;
 
     // determine which star structure to draw
-    if ( sun_angle > (0.5 * SGD_PI + 10.0 * SGD_DEGREES_TO_RADIANS ) ) {
+    if ( sun_angle > (SGD_PI_2 + 10.0 * SGD_DEGREES_TO_RADIANS ) ) {
         // deep night
         factor = 1.0;
         cutoff = 4.5;
         phase = 0;
-    } else if ( sun_angle > (0.5 * SGD_PI + 8.8 * SGD_DEGREES_TO_RADIANS ) ) {
+    } else if ( sun_angle > (SGD_PI_2 + 8.8 * SGD_DEGREES_TO_RADIANS ) ) {
         factor = 1.0;
         cutoff = 3.8;
         phase = 1;
-    } else if ( sun_angle > (0.5 * SGD_PI + 7.5 * SGD_DEGREES_TO_RADIANS ) ) {
+    } else if ( sun_angle > (SGD_PI_2 + 7.5 * SGD_DEGREES_TO_RADIANS ) ) {
         factor = 0.95;
         cutoff = 3.1;
         phase = 2;
-    } else if ( sun_angle > (0.5 * SGD_PI + 7.0 * SGD_DEGREES_TO_RADIANS ) ) {
+    } else if ( sun_angle > (SGD_PI_2 + 7.0 * SGD_DEGREES_TO_RADIANS ) ) {
         factor = 0.9;
         cutoff = 2.4;
         phase = 3;
-    } else if ( sun_angle > (0.5 * SGD_PI + 6.5 * SGD_DEGREES_TO_RADIANS ) ) {
+    } else if ( sun_angle > (SGD_PI_2 + 6.5 * SGD_DEGREES_TO_RADIANS ) ) {
         factor = 0.85;
         cutoff = 1.8;
         phase = 4;
-    } else if ( sun_angle > (0.5 * SGD_PI + 6.0 * SGD_DEGREES_TO_RADIANS ) ) {
+    } else if ( sun_angle > (SGD_PI_2 + 6.0 * SGD_DEGREES_TO_RADIANS ) ) {
         factor = 0.8;
         cutoff = 1.2;
         phase = 5;
-    } else if ( sun_angle > (0.5 * SGD_PI + 5.5 * SGD_DEGREES_TO_RADIANS ) ) {
+    } else if ( sun_angle > (SGD_PI_2 + 5.5 * SGD_DEGREES_TO_RADIANS ) ) {
         factor = 0.75;
         cutoff = 0.6;
         phase = 6;
@@ -212,7 +184,6 @@ bool SGStars::repaint( double sun_angle, int num, sgdVec3 *star_data ) {
             mag = star_data[i][2];
             if ( mag < cutoff ) {
                 nmag = ( 4.5 - mag ) / 5.5; // translate to 0 ... 1.0 scale
-                // alpha = nmag * 0.7 + 0.3; // translate to a 0.3 ... 1.0 scale
                 alpha = nmag * 0.85 + 0.15; // translate to a 0.15 ... 1.0 scale
                 alpha *= factor;          // dim when the sun is brighter
             } else {
@@ -222,10 +193,10 @@ bool SGStars::repaint( double sun_angle, int num, sgdVec3 *star_data ) {
             if (alpha > 1.0) { alpha = 1.0; }
             if (alpha < 0.0) { alpha = 0.0; }
 
-            color = cl->get( i );
-            sgSetVec4( color, 1.0, 1.0, 1.0, alpha );
+            (*cl)[i] = osg::Vec4(1, 1, 1, alpha);
             // cout << "alpha[" << i << "] = " << alpha << endl;
         }
+        cl->dirty();
     } else {
        // cout << "  no phase change, skipping" << endl;
     }
@@ -235,29 +206,3 @@ bool SGStars::repaint( double sun_angle, int num, sgdVec3 *star_data ) {
 
     return true;
 }
-
-
-// reposition the stars for the specified time (GST rotation),
-// offset by our current position (p) so that it appears fixed at a
-// great distance from the viewer.
-bool SGStars::reposition( sgVec3 p, double angle )
-{
-    sgMat4 T1, GST;
-    sgVec3 axis;
-
-    sgMakeTransMat4( T1, p );
-
-    sgSetVec3( axis, 0.0, 0.0, -1.0 );
-    sgMakeRotMat4( GST, angle, axis );
-
-    sgMat4 TRANSFORM;
-    sgCopyMat4( TRANSFORM, T1 );
-    sgPreMultMat4( TRANSFORM, GST );
-
-    sgCoord skypos;
-    sgSetCoord( &skypos, TRANSFORM );
-
-    stars_transform->setTransform( &skypos );
-
-    return true;
-}