1 // moon.hxx -- model earth's moon
3 // Written by Durk Talsma. Originally started October 1997, for distribution
4 // with the FlightGear project. Version 2 was written in August and
5 // September 1998. This code is based upon algorithms and data kindly
6 // provided by Mr. Paul Schlyter. (pausch@saaf.se).
8 // Separated out rendering pieces and converted to ssg by Curt Olson,
10 // This library is free software; you can redistribute it and/or
11 // modify it under the terms of the GNU Library General Public
12 // License as published by the Free Software Foundation; either
13 // version 2 of the License, or (at your option) any later version.
15 // This library is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 // Library General Public License for more details.
20 // You should have received a copy of the GNU General Public License
21 // along with this program; if not, write to the Free Software
22 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
27 # include <simgear_config.h>
30 #include <simgear/compiler.h>
36 #include <osg/AlphaFunc>
37 #include <osg/BlendFunc>
38 #include <osg/CullFace>
39 #include <osg/Geometry>
42 #include <osg/ShadeModel>
44 #include <osg/Texture2D>
46 #include <simgear/constants.h>
47 #include <simgear/screen/colors.hxx>
48 #include <simgear/scene/model/model.hxx>
49 #include <simgear/misc/PathOptions.hxx>
54 using namespace simgear;
57 SGMoon::SGMoon( void ) :
64 SGMoon::~SGMoon( void ) {
68 // build the moon object
70 SGMoon::build( SGPath path, double moon_size ) {
72 osg::Node* orb = SGMakeSphere(moon_size, 15, 15);
73 osg::StateSet* stateSet = orb->getOrCreateStateSet();
74 stateSet->setRenderBinDetails(-5, "RenderBin");
76 // set up the orb state
77 osg::ref_ptr<osgDB::ReaderWriter::Options> options
78 = makeOptionsFromPath(path);
80 osg::Texture2D* texture = SGLoadTexture2D("moon.png", options.get());
81 stateSet->setTextureAttributeAndModes(0, texture, osg::StateAttribute::ON);
82 osg::TexEnv* texEnv = new osg::TexEnv;
83 texEnv->setMode(osg::TexEnv::MODULATE);
84 stateSet->setTextureAttribute(0, texEnv, osg::StateAttribute::ON);
86 orb_material = new osg::Material;
87 orb_material->setColorMode(osg::Material::DIFFUSE);
88 orb_material->setDiffuse(osg::Material::FRONT_AND_BACK,
89 osg::Vec4(1, 1, 1, 1));
90 orb_material->setAmbient(osg::Material::FRONT_AND_BACK,
91 osg::Vec4(0, 0, 0, 1));
92 orb_material->setEmission(osg::Material::FRONT_AND_BACK,
93 osg::Vec4(0, 0, 0, 1));
94 orb_material->setSpecular(osg::Material::FRONT_AND_BACK,
95 osg::Vec4(0, 0, 0, 1));
96 orb_material->setShininess(osg::Material::FRONT_AND_BACK, 0);
97 stateSet->setAttribute(orb_material.get());
98 stateSet->setMode(GL_LIGHTING, osg::StateAttribute::ON);
99 stateSet->setMode(GL_DEPTH_TEST, osg::StateAttribute::OFF);
100 stateSet->setMode(GL_FOG, osg::StateAttribute::OFF);
101 osg::ShadeModel* shadeModel = new osg::ShadeModel;
102 shadeModel->setMode(osg::ShadeModel::SMOOTH);
103 stateSet->setAttributeAndModes(shadeModel);
104 osg::CullFace* cullFace = new osg::CullFace;
105 cullFace->setMode(osg::CullFace::BACK);
106 stateSet->setAttributeAndModes(cullFace);
108 osg::BlendFunc* blendFunc = new osg::BlendFunc;
109 blendFunc->setFunction(osg::BlendFunc::SRC_ALPHA, osg::BlendFunc::ONE);
110 stateSet->setAttributeAndModes(blendFunc);
112 osg::AlphaFunc* alphaFunc = new osg::AlphaFunc;
113 alphaFunc->setFunction(osg::AlphaFunc::GREATER);
114 alphaFunc->setReferenceValue(0.01);
115 stateSet->setAttribute(alphaFunc);
116 stateSet->setMode(GL_ALPHA_TEST, osg::StateAttribute::ON);
118 // force a repaint of the moon colors with arbitrary defaults
121 // build the ssg scene graph sub tree for the sky and connected
122 // into the provide scene graph branch
123 moon_transform = new osg::MatrixTransform;
125 moon_transform->addChild( orb );
127 return moon_transform.get();
131 // repaint the moon colors based on current value of moon_angle in
132 // degrees relative to verticle
133 // 0 degrees = high noon
134 // 90 degrees = moon rise/set
135 // 180 degrees = darkest midnight
136 bool SGMoon::repaint( double moon_angle ) {
138 if (prev_moon_angle == moon_angle)
141 prev_moon_angle = moon_angle;
143 float moon_factor = 4*cos(moon_angle);
145 if (moon_factor > 1) moon_factor = 1.0;
146 if (moon_factor < -1) moon_factor = -1.0;
147 moon_factor = moon_factor/2 + 0.5;
150 color[1] = sqrt(moon_factor);
151 color[0] = sqrt(color[1]);
152 color[2] = moon_factor * moon_factor;
153 color[2] *= color[2];
156 gamma_correct_rgb( color._v );
158 orb_material->setDiffuse(osg::Material::FRONT_AND_BACK, color);
164 // reposition the moon at the specified right ascension and
165 // declination, offset by our current position (p) so that it appears
166 // fixed at a great distance from the viewer. Also add in an optional
167 // rotation (i.e. for the current time of day.)
168 bool SGMoon::reposition( const SGVec3f& p, double angle,
169 double rightAscension, double declination,
172 osg::Matrix T1, T2, GST, RA, DEC;
174 T1.makeTranslate(p.osg());
176 GST.makeRotate(SGD_DEGREES_TO_RADIANS*angle, osg::Vec3(0, 0, -1));
178 // xglRotatef( ((SGD_RADIANS_TO_DEGREES * rightAscension)- 90.0),
180 RA.makeRotate(rightAscension - 90.0 * SGD_DEGREES_TO_RADIANS,
183 // xglRotatef((SGD_RADIANS_TO_DEGREES * declination), 1.0, 0.0, 0.0);
184 DEC.makeRotate(declination, osg::Vec3(1, 0, 0));
186 // xglTranslatef(0,moon_dist);
187 T2.makeTranslate(osg::Vec3(0, moon_dist, 0));
189 moon_transform->setMatrix(T2*DEC*RA*GST*T1);