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