]> git.mxchange.org Git - simgear.git/blob - simgear/scene/sky/moon.cxx
Merge branch 'ehofman/sound'
[simgear.git] / simgear / scene / sky / moon.cxx
1 // moon.hxx -- model earth's moon
2 //
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). 
7 //
8 // Separated out rendering pieces and converted to ssg by Curt Olson,
9 // March 2000
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.
14 //
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.
19 //
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.
23 //
24 // $Id$
25
26 #ifdef HAVE_CONFIG_H
27 #  include <simgear_config.h>
28 #endif
29
30 #include <simgear/compiler.h>
31
32 #include <stdio.h>
33 #include <iostream>
34
35 #include <simgear/structure/OSGVersion.hxx>
36 #include <osg/Array>
37 #include <osg/AlphaFunc>
38 #include <osg/BlendFunc>
39 #include <osg/CullFace>
40 #include <osg/Geometry>
41 #include <osg/Geode>
42 #include <osg/Node>
43 #include <osg/ShadeModel>
44 #include <osg/TexEnv>
45 #include <osg/Texture2D>
46 #if SG_OSG_MIN_VERSION_REQUIRED(2,9,5)
47 #include <osgDB/Options>
48 #endif
49
50 #include <simgear/constants.h>
51 #include <simgear/screen/colors.hxx>
52 #include <simgear/scene/model/model.hxx>
53 #include <simgear/misc/PathOptions.hxx>
54
55 #include "sphere.hxx"
56 #include "moon.hxx"
57
58 using namespace simgear;
59
60 // Constructor
61 SGMoon::SGMoon( void ) :
62     prev_moon_angle(-1)
63 {
64 }
65
66
67 // Destructor
68 SGMoon::~SGMoon( void ) {
69 }
70
71
72 // build the moon object
73 osg::Node*
74 SGMoon::build( SGPath path, double moon_size ) {
75
76     osg::Node* orb = SGMakeSphere(moon_size, 15, 15);
77     osg::StateSet* stateSet = orb->getOrCreateStateSet();
78     stateSet->setRenderBinDetails(-5, "RenderBin");
79
80     // set up the orb state
81     osg::ref_ptr<osgDB::ReaderWriter::Options> options
82         = makeOptionsFromPath(path);
83
84     osg::Texture2D* texture = SGLoadTexture2D("moon.png", options.get());
85     stateSet->setTextureAttributeAndModes(0, texture, osg::StateAttribute::ON);
86     osg::TexEnv* texEnv = new osg::TexEnv;
87     texEnv->setMode(osg::TexEnv::MODULATE);
88     stateSet->setTextureAttribute(0, texEnv, osg::StateAttribute::ON);
89
90     orb_material = new osg::Material;
91     orb_material->setColorMode(osg::Material::DIFFUSE);
92     orb_material->setDiffuse(osg::Material::FRONT_AND_BACK,
93                              osg::Vec4(1, 1, 1, 1));
94     orb_material->setAmbient(osg::Material::FRONT_AND_BACK,
95                              osg::Vec4(0, 0, 0, 1));
96     orb_material->setEmission(osg::Material::FRONT_AND_BACK,
97                               osg::Vec4(0, 0, 0, 1));
98     orb_material->setSpecular(osg::Material::FRONT_AND_BACK,
99                               osg::Vec4(0, 0, 0, 1));
100     orb_material->setShininess(osg::Material::FRONT_AND_BACK, 0);
101     stateSet->setAttribute(orb_material.get());
102     stateSet->setMode(GL_LIGHTING, osg::StateAttribute::ON);
103     stateSet->setMode(GL_DEPTH_TEST, osg::StateAttribute::OFF);
104     stateSet->setMode(GL_FOG, osg::StateAttribute::OFF);
105     osg::ShadeModel* shadeModel = new osg::ShadeModel;
106     shadeModel->setMode(osg::ShadeModel::SMOOTH);
107     stateSet->setAttributeAndModes(shadeModel);
108     osg::CullFace* cullFace = new osg::CullFace;
109     cullFace->setMode(osg::CullFace::BACK);
110     stateSet->setAttributeAndModes(cullFace);
111
112     osg::BlendFunc* blendFunc = new osg::BlendFunc;
113     blendFunc->setFunction(osg::BlendFunc::SRC_ALPHA, osg::BlendFunc::ONE);
114     stateSet->setAttributeAndModes(blendFunc);
115
116     osg::AlphaFunc* alphaFunc = new osg::AlphaFunc;
117     alphaFunc->setFunction(osg::AlphaFunc::GREATER);
118     alphaFunc->setReferenceValue(0.01);
119     stateSet->setAttribute(alphaFunc);
120     stateSet->setMode(GL_ALPHA_TEST, osg::StateAttribute::ON);
121
122     // force a repaint of the moon colors with arbitrary defaults
123     repaint( 0.0 );
124
125     // build the ssg scene graph sub tree for the sky and connected
126     // into the provide scene graph branch
127     moon_transform = new osg::MatrixTransform;
128
129     moon_transform->addChild( orb );
130
131     return moon_transform.get();
132 }
133
134
135 // repaint the moon colors based on current value of moon_angle in
136 // degrees relative to verticle
137 // 0 degrees = high noon
138 // 90 degrees = moon rise/set
139 // 180 degrees = darkest midnight
140 bool SGMoon::repaint( double moon_angle ) {
141
142     if (prev_moon_angle == moon_angle)
143         return true;
144
145     prev_moon_angle = moon_angle;
146
147     float moon_factor = 4*cos(moon_angle);
148     
149     if (moon_factor > 1) moon_factor = 1.0;
150     if (moon_factor < -1) moon_factor = -1.0;
151     moon_factor = moon_factor/2 + 0.5;
152     
153     osg::Vec4 color;
154     color[1] = sqrt(moon_factor);
155     color[0] = sqrt(color[1]);
156     color[2] = moon_factor * moon_factor;
157     color[2] *= color[2];
158     color[3] = 1.0;
159     
160     gamma_correct_rgb( color._v );
161
162     orb_material->setDiffuse(osg::Material::FRONT_AND_BACK, color);
163
164     return true;
165 }
166
167
168 // reposition the moon at the specified right ascension and
169 // declination, offset by our current position (p) so that it appears
170 // fixed at a great distance from the viewer.  Also add in an optional
171 // rotation (i.e. for the current time of day.)
172 bool SGMoon::reposition( double rightAscension, double declination,
173                          double moon_dist )
174 {
175     osg::Matrix T2, RA, DEC;
176
177     RA.makeRotate(rightAscension - 90.0 * SGD_DEGREES_TO_RADIANS,
178                   osg::Vec3(0, 0, 1));
179
180     DEC.makeRotate(declination, osg::Vec3(1, 0, 0));
181
182     T2.makeTranslate(osg::Vec3(0, moon_dist, 0));
183
184     moon_transform->setMatrix(T2*DEC*RA);
185
186     return true;
187 }
188