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
00032 #include <stdio.h>
00033 #include <iostream>
00034
00035 #include <simgear/structure/OSGVersion.hxx>
00036 #include <osg/Array>
00037 #include <osg/AlphaFunc>
00038 #include <osg/BlendFunc>
00039 #include <osg/CullFace>
00040 #include <osg/Geometry>
00041 #include <osg/Geode>
00042 #include <osg/Node>
00043 #include <osg/ShadeModel>
00044 #include <osg/TexEnv>
00045 #include <osg/Texture2D>
00046 #if SG_OSG_MIN_VERSION_REQUIRED(2,9,5)
00047 #include <osgDB/Options>
00048 #endif
00049
00050 #include <simgear/constants.h>
00051 #include <simgear/screen/colors.hxx>
00052 #include <simgear/scene/model/model.hxx>
00053 #include <simgear/misc/PathOptions.hxx>
00054
00055 #include "sphere.hxx"
00056 #include "moon.hxx"
00057
00058 using namespace simgear;
00059
00060
00061 SGMoon::SGMoon( void ) :
00062 prev_moon_angle(-1)
00063 {
00064 }
00065
00066
00067
00068 SGMoon::~SGMoon( void ) {
00069 }
00070
00071
00072
00073 osg::Node*
00074 SGMoon::build( SGPath path, double moon_size ) {
00075
00076 osg::Node* orb = SGMakeSphere(moon_size, 15, 15);
00077 osg::StateSet* stateSet = orb->getOrCreateStateSet();
00078 stateSet->setRenderBinDetails(-5, "RenderBin");
00079
00080
00081 osg::ref_ptr<osgDB::ReaderWriter::Options> options
00082 = makeOptionsFromPath(path);
00083
00084 osg::Texture2D* texture = SGLoadTexture2D("moon.png", options.get());
00085 stateSet->setTextureAttributeAndModes(0, texture, osg::StateAttribute::ON);
00086 osg::TexEnv* texEnv = new osg::TexEnv;
00087 texEnv->setMode(osg::TexEnv::MODULATE);
00088 stateSet->setTextureAttribute(0, texEnv, osg::StateAttribute::ON);
00089
00090 orb_material = new osg::Material;
00091 orb_material->setColorMode(osg::Material::DIFFUSE);
00092 orb_material->setDiffuse(osg::Material::FRONT_AND_BACK,
00093 osg::Vec4(1, 1, 1, 1));
00094 orb_material->setAmbient(osg::Material::FRONT_AND_BACK,
00095 osg::Vec4(0, 0, 0, 1));
00096 orb_material->setEmission(osg::Material::FRONT_AND_BACK,
00097 osg::Vec4(0, 0, 0, 1));
00098 orb_material->setSpecular(osg::Material::FRONT_AND_BACK,
00099 osg::Vec4(0, 0, 0, 1));
00100 orb_material->setShininess(osg::Material::FRONT_AND_BACK, 0);
00101 stateSet->setAttribute(orb_material.get());
00102 stateSet->setMode(GL_LIGHTING, osg::StateAttribute::ON);
00103 stateSet->setMode(GL_DEPTH_TEST, osg::StateAttribute::OFF);
00104 stateSet->setMode(GL_FOG, osg::StateAttribute::OFF);
00105 osg::ShadeModel* shadeModel = new osg::ShadeModel;
00106 shadeModel->setMode(osg::ShadeModel::SMOOTH);
00107 stateSet->setAttributeAndModes(shadeModel);
00108 osg::CullFace* cullFace = new osg::CullFace;
00109 cullFace->setMode(osg::CullFace::BACK);
00110 stateSet->setAttributeAndModes(cullFace);
00111
00112 osg::BlendFunc* blendFunc = new osg::BlendFunc;
00113 blendFunc->setFunction(osg::BlendFunc::SRC_ALPHA, osg::BlendFunc::ONE);
00114 stateSet->setAttributeAndModes(blendFunc);
00115
00116 osg::AlphaFunc* alphaFunc = new osg::AlphaFunc;
00117 alphaFunc->setFunction(osg::AlphaFunc::GREATER);
00118 alphaFunc->setReferenceValue(0.01);
00119 stateSet->setAttribute(alphaFunc);
00120 stateSet->setMode(GL_ALPHA_TEST, osg::StateAttribute::ON);
00121
00122
00123 repaint( 0.0 );
00124
00125
00126
00127 moon_transform = new osg::MatrixTransform;
00128
00129 moon_transform->addChild( orb );
00130
00131 return moon_transform.get();
00132 }
00133
00134
00135
00136
00137
00138
00139
00140 bool SGMoon::repaint( double moon_angle ) {
00141
00142 if (prev_moon_angle == moon_angle)
00143 return true;
00144
00145 prev_moon_angle = moon_angle;
00146
00147 float moon_factor = 4*cos(moon_angle);
00148
00149 if (moon_factor > 1) moon_factor = 1.0;
00150 if (moon_factor < -1) moon_factor = -1.0;
00151 moon_factor = moon_factor/2 + 0.5;
00152
00153 osg::Vec4 color;
00154 color[1] = sqrt(moon_factor);
00155 color[0] = sqrt(color[1]);
00156 color[2] = moon_factor * moon_factor;
00157 color[2] *= color[2];
00158 color[3] = 1.0;
00159
00160 gamma_correct_rgb( color._v );
00161
00162 orb_material->setDiffuse(osg::Material::FRONT_AND_BACK, color);
00163
00164 return true;
00165 }
00166
00167
00168
00169
00170
00171
00172 bool SGMoon::reposition( double rightAscension, double declination,
00173 double moon_dist )
00174 {
00175 osg::Matrix T2, RA, DEC;
00176
00177 RA.makeRotate(rightAscension - 90.0 * SGD_DEGREES_TO_RADIANS,
00178 osg::Vec3(0, 0, 1));
00179
00180 DEC.makeRotate(declination, osg::Vec3(1, 0, 0));
00181
00182 T2.makeTranslate(osg::Vec3(0, moon_dist, 0));
00183
00184 moon_transform->setMatrix(T2*DEC*RA);
00185
00186 return true;
00187 }
00188