]> git.mxchange.org Git - simgear.git/blob - simgear/scene/sky/clouds3d/SkySceneLoader.cpp
Norman Vine:
[simgear.git] / simgear / scene / sky / clouds3d / SkySceneLoader.cpp
1 //------------------------------------------------------------------------------
2 // File : SkySceneLoader.cpp
3 //------------------------------------------------------------------------------
4 // Adapted from SkyWorks for FlightGear by J. Wojnaroski -- castle@mminternet.com
5 // Copywrite July 2002
6 //
7 // This program is free software; you can redistribute it and/or modify
8 // it under the terms of the GNU General Public License as published by
9 // the Free Software Foundation; either version 2 of the License, or
10 // (at your option) any later version.
11 //
12 // This program is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 // GNU General Public License for more details.
16 //
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 /**
21  * @file SkySceneLoader.cpp
22  * 
23  * Implementation of class SkySceneLoader.
24  */
25
26 #include <plib/ssg.h>
27 #include <plib/sg.h>
28
29 #include <simgear/misc/sg_path.hxx>
30 #include <simgear/math/point3d.hxx>
31 #include <simgear/math/polar3d.hxx>
32 #include <simgear/math/vector.hxx>
33
34 #include "SkySceneLoader.hpp"
35 #include "SkySceneManager.hpp"
36 #include "SkyTextureManager.hpp"
37 #include "SkySceneManager.hpp"
38 #include "SkyDynamicTextureManager.hpp"
39 #include "SkyContext.hpp"
40 #include "SkyLight.hpp"
41 #include "camera.hpp"
42
43 ssgLight _sky_ssgLights [ 8 ] ;
44
45 sgdVec3 cam_pos;
46 static sgdVec3 delta;
47 Point3D origin;
48
49 Camera *pCam = new Camera();
50 // Need to add a light here until we figure out how to use the sun position and color
51 SkyLight::SkyLightType eType = SkyLight::SKY_LIGHT_DIRECTIONAL;
52 SkyLight *pLight = new SkyLight(eType);
53
54 // hack
55 sgMat4 my_copy_of_ssgOpenGLAxisSwapMatrix =
56 {
57   {  1.0f,  0.0f,  0.0f,  0.0f },
58   {  0.0f,  0.0f, -1.0f,  0.0f },
59   {  0.0f,  1.0f,  0.0f,  0.0f },
60   {  0.0f,  0.0f,  0.0f,  1.0f }
61 };
62 //------------------------------------------------------------------------------
63 // Function               : SkySceneLoader::SkySceneLoader
64 // Description      : 
65 //------------------------------------------------------------------------------
66 /**
67  * @fn SkySceneLoader::SkySceneLoader()
68  * @brief Constructor.
69  */ 
70 SkySceneLoader::SkySceneLoader()
71 {
72
73 }
74
75 //------------------------------------------------------------------------------
76 // Function               : SkySceneLoader::~SkySceneLoader
77 // Description      : 
78 //------------------------------------------------------------------------------
79 /**
80  * @fn SkySceneLoader::~SkySceneLoader()
81  * @brief Destructor.
82  */ 
83 SkySceneLoader::~SkySceneLoader()
84 {
85         SceneManager::Destroy();
86         DynamicTextureManager::Destroy();
87         TextureManager::Destroy();
88         GraphicsContext::Destroy();
89 }
90
91
92 //------------------------------------------------------------------------------
93 // Function               : SkySceneLoader::Load
94 // Description      : 
95 //------------------------------------------------------------------------------
96 /**
97  * @fn SkySceneLoader::Load(std::string filename)
98  * @brief Loads a SkyWorks scene.
99  * 
100  * This is a temporary fix, as it loads only limited scenes
101  *  It can however, load any number of Cloud
102  +
103  */ 
104 //bool SkySceneLoader::Load(std::string filepath)
105 bool SkySceneLoader::Load( SGPath filename, double latitude, double longitude )
106
107   SkyArchive archive;
108  
109   SGPath base = filename.dir();
110  
111   if (SKYFAILED(archive.Load(filename.c_str()))) {
112       cout << "Archive file not found\n" <<  filename.c_str();
113       return false;
114   }
115     
116   char *pFilename;
117   
118   // Need to create the managers
119   cout << "GraphicsContext::Instantiate();" << endl;
120   GraphicsContext::Instantiate();
121   cout << "  TextureManager::Instantiate();" << endl;
122   TextureManager::Instantiate();
123   cout << "  DynamicTextureManager::Instantiate();" << endl;
124   DynamicTextureManager::Instantiate();
125   cout << "  SceneManager::Instantiate();" << endl;
126   SceneManager::Instantiate();
127
128   unsigned int iNumFiles;
129   if (!SKYFAILED(archive.GetInfo("CloudFile", STRING_TYPE, &iNumFiles)))
130   {
131     for (unsigned int i = 0; i < iNumFiles; ++i)
132     {
133       FAIL_RETURN(archive.FindString("CloudFile", &pFilename, i));
134       //  this is where we have to append the $fg_root string to the filename
135       base.append( pFilename );
136       const char *FilePath = base.c_str();
137      
138       //float rScale = 1.0;
139       //FAIL_RETURN(archive.FindFloat32("CloudScale", &rScale, i));
140       float rScale = 40.0;
141       SkyArchive cloudArchive;
142       FAIL_RETURN(cloudArchive.Load(FilePath));
143       FAIL_RETURN(SceneManager::InstancePtr()->LoadClouds(cloudArchive, rScale, latitude, longitude)); 
144     }
145   }
146   
147   Vec3f dir(0, 0, 1);
148   pLight->SetPosition(Vec3f(0, 0, 17000));
149   pLight->SetDirection(dir);
150   pLight->SetAmbient(Vec4f( 0.0f, 0.0f, 0.0f, 0.0f));
151   pLight->SetDiffuse(Vec4f(1.0f, 1.0f, 1.0f, 0.0f));
152   //pLight->SetDiffuse(Vec4f(0.0f, 0.0f, 0.0f, 0.0f));
153   //pLight->SetSpecular(Vec4f(1.0f, 1.0f, 1.0f, 0.0f));
154   
155    // No attenuation
156   pLight->SetAttenuation(1.0f, 0.0f, 0.0f);
157   SceneManager::InstancePtr()->AddLight(pLight);
158   
159   SceneManager::InstancePtr()->ShadeClouds();
160  
161   return true;
162 }
163
164 void SkySceneLoader::Set_Cloud_Orig( Point3D *posit )
165
166         // set origin for cloud coordinates to initial tile center
167         origin = *posit;
168         sgdSetVec3( delta, origin[0], origin[1], origin[2]);    
169         //printf("Cloud marker %f %f %f\n", origin[0], origin[1], origin[2] );
170         
171 }
172
173 void SkySceneLoader::Update( double *view_pos )
174 {
175         
176         double wind_x, wind_y, wind_z;
177         wind_x = 0.0; wind_z =  0.0;
178         // just a dumb test to see what happens if we can move the clouds en masse via the camera
179         delta[0] += wind_x; delta[2] += wind_z;
180         
181         sgdSubVec3( cam_pos, view_pos, delta );
182         //cout << "ORIGIN: " << delta[0] << " " << delta[1] << " " << delta[2] << endl;
183         //cout << "CAM   : " << cam_pos[0] << " " << cam_pos[1] << " "  << cam_pos[2] << endl;
184         
185         SceneManager::InstancePtr()->Update(*pCam);
186         
187         // need some scheme to reshade selected clouds a few at a time to save frame rate cycles
188         // SceneManager::InstancePtr()->ShadeClouds();
189         
190 }
191
192 void SkySceneLoader::Resize(  double w, double h )
193 {
194         
195   pCam->Perspective( (float) h, (float) (w / h), 0.5, 120000.0 );
196  
197 }
198
199 void SkySceneLoader::Draw( sgMat4 mat )
200 {
201     sgMat4 cameraMatrix;
202
203     // sgCopyMat4(cameraMatrix,mat);    
204     // or just 
205     ssgGetModelviewMatrix(cameraMatrix);
206
207     glMatrixMode ( GL_MODELVIEW ) ;
208     glLoadIdentity () ;
209     glLoadMatrixf( (float *) cameraMatrix );
210   
211     pCam->SetModelviewMatrix( (float *) cameraMatrix );
212   
213     SceneManager::InstancePtr()->Display(*pCam);
214         
215     //pLight->Display(); // draw the light position to  debug with sun position
216
217     glMatrixMode ( GL_MODELVIEW ) ;
218     glLoadIdentity () ;
219 }