]> git.mxchange.org Git - simgear.git/blobdiff - simgear/scene/sky/stars.cxx
Merge branch 'ehofman/sound'
[simgear.git] / simgear / scene / sky / stars.cxx
index 6e8636e31ddd22e109e9ef5b092144dd6bc0f0f8..32b8244f766403688b182f4a9c59cc880fb3e17e 100644 (file)
 //
 // $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"
 
-
-// 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 | GL_COLOR_BUFFER_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();
-
-    return true;
-}
-
-
 // Constructor
 SGStars::SGStars( void ) :
 old_phase(-1)
@@ -77,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 )
-        SG_LOG( SG_EVENT, SG_WARN, "null star data passed to SGStars::build()");
-
-
-    // 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;
-
-    stars_transform->addKid( stars_obj );
+    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);
 
-    SG_LOG( SG_EVENT, SG_INFO, "stars = " << stars_transform);
-
-    return stars_transform;
+    return geode;
 }
 
 
@@ -143,12 +121,11 @@ 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;
 
@@ -207,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 {
@@ -217,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;
     }
@@ -230,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;
-}