00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 #ifdef HAVE_CONFIG_H
00027 # include <simgear_config.h>
00028 #endif
00029
00030 #include <simgear/compiler.h>
00031 #include <simgear/constants.h>
00032 #include <simgear/debug/logstream.hxx>
00033
00034 #include <stdio.h>
00035 #include <iostream>
00036
00037 #include <osg/AlphaFunc>
00038 #include <osg/BlendFunc>
00039 #include <osg/Geode>
00040 #include <osg/Geometry>
00041 #include <osg/Image>
00042 #include <osg/Material>
00043 #include <osg/Point>
00044 #include <osg/ShadeModel>
00045 #include <osg/Node>
00046
00047 #include "stars.hxx"
00048
00049
00050 SGStars::SGStars( void ) :
00051 old_phase(-1)
00052 {
00053 }
00054
00055
00056
00057 SGStars::~SGStars( void ) {
00058 }
00059
00060
00061
00062 osg::Node*
00063 SGStars::build( int num, const SGVec3d star_data[], double star_dist ) {
00064 osg::Geode* geode = new osg::Geode;
00065 osg::StateSet* stateSet = geode->getOrCreateStateSet();
00066 stateSet->setRenderBinDetails(-9, "RenderBin");
00067
00068
00069 osg::BlendFunc* blendFunc = new osg::BlendFunc;
00070 blendFunc->setFunction(osg::BlendFunc::SRC_ALPHA,
00071 osg::BlendFunc::ONE_MINUS_SRC_ALPHA);
00072 stateSet->setAttributeAndModes(blendFunc);
00073
00074
00075
00076
00077
00078 stateSet->setMode(GL_FOG, osg::StateAttribute::OFF);
00079 stateSet->setMode(GL_LIGHTING, osg::StateAttribute::OFF);
00080 stateSet->setMode(GL_CULL_FACE, osg::StateAttribute::OFF);
00081 stateSet->setMode(GL_DEPTH_TEST, osg::StateAttribute::OFF);
00082 stateSet->setMode(GL_ALPHA_TEST, osg::StateAttribute::OFF);
00083
00084
00085
00086 cl = new osg::Vec4Array;
00087 osg::Vec3Array* vl = new osg::Vec3Array;
00088
00089
00090 for ( int i = 0; i < num; ++i ) {
00091
00092 vl->push_back(osg::Vec3(star_dist * cos( star_data[i][0])
00093 * cos( star_data[i][1] ),
00094 star_dist * sin( star_data[i][0])
00095 * cos( star_data[i][1] ),
00096 star_dist * sin( star_data[i][1])));
00097
00098
00099 cl->push_back(osg::Vec4(1, 1, 1, 1));
00100 }
00101
00102 osg::Geometry* geometry = new osg::Geometry;
00103 geometry->setUseDisplayList(false);
00104 geometry->setVertexArray(vl);
00105 geometry->setColorArray(cl.get());
00106 geometry->setColorBinding(osg::Geometry::BIND_PER_VERTEX);
00107 geometry->setNormalBinding(osg::Geometry::BIND_OFF);
00108 geometry->addPrimitiveSet(new osg::DrawArrays(GL_POINTS, 0, vl->size()));
00109 geode->addDrawable(geometry);
00110
00111 return geode;
00112 }
00113
00114
00115
00116
00117
00118
00119
00120 bool SGStars::repaint( double sun_angle, int num, const SGVec3d star_data[] ) {
00121
00122
00123
00124 double mag, nmag, alpha, factor, cutoff;
00125
00126 int phase;
00127
00128
00129 if ( sun_angle > (SGD_PI_2 + 10.0 * SGD_DEGREES_TO_RADIANS ) ) {
00130
00131 factor = 1.0;
00132 cutoff = 4.5;
00133 phase = 0;
00134 } else if ( sun_angle > (SGD_PI_2 + 8.8 * SGD_DEGREES_TO_RADIANS ) ) {
00135 factor = 1.0;
00136 cutoff = 3.8;
00137 phase = 1;
00138 } else if ( sun_angle > (SGD_PI_2 + 7.5 * SGD_DEGREES_TO_RADIANS ) ) {
00139 factor = 0.95;
00140 cutoff = 3.1;
00141 phase = 2;
00142 } else if ( sun_angle > (SGD_PI_2 + 7.0 * SGD_DEGREES_TO_RADIANS ) ) {
00143 factor = 0.9;
00144 cutoff = 2.4;
00145 phase = 3;
00146 } else if ( sun_angle > (SGD_PI_2 + 6.5 * SGD_DEGREES_TO_RADIANS ) ) {
00147 factor = 0.85;
00148 cutoff = 1.8;
00149 phase = 4;
00150 } else if ( sun_angle > (SGD_PI_2 + 6.0 * SGD_DEGREES_TO_RADIANS ) ) {
00151 factor = 0.8;
00152 cutoff = 1.2;
00153 phase = 5;
00154 } else if ( sun_angle > (SGD_PI_2 + 5.5 * SGD_DEGREES_TO_RADIANS ) ) {
00155 factor = 0.75;
00156 cutoff = 0.6;
00157 phase = 6;
00158 } else {
00159
00160 factor = 0.7;
00161 cutoff = 0.0;
00162 phase = 7;
00163 }
00164
00165 if( phase != old_phase ) {
00166
00167 old_phase = phase;
00168 for ( int i = 0; i < num; ++i ) {
00169
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179
00180 mag = star_data[i][2];
00181 if ( mag < cutoff ) {
00182 nmag = ( 4.5 - mag ) / 5.5;
00183 alpha = nmag * 0.85 + 0.15;
00184 alpha *= factor;
00185 } else {
00186 alpha = 0.0;
00187 }
00188
00189 if (alpha > 1.0) { alpha = 1.0; }
00190 if (alpha < 0.0) { alpha = 0.0; }
00191
00192 (*cl)[i] = osg::Vec4(1, 1, 1, alpha);
00193
00194 }
00195 cl->dirty();
00196 } else {
00197
00198 }
00199
00200
00201
00202
00203 return true;
00204 }