]> git.mxchange.org Git - simgear.git/blob - simgear/scene/sky/clouds3d/SkySceneLoader.cpp
Frederic Bouvier:
[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 = 0;
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 //------------------------------------------------------------------------------
94 // Function               : SkySceneLoader::Load
95 // Description      :
96 //------------------------------------------------------------------------------
97 /**
98  * @fn SkySceneLoader::Load(std::string filename)
99  * @brief Loads a SkyWorks scene.
100  *
101  * This is a temporary fix, as it loads only limited scenes
102  *  It can however, load any number of Cloud
103  +
104  */
105 //bool SkySceneLoader::Load(std::string filepath)
106 bool SkySceneLoader::Load( unsigned char *data, unsigned int size, double latitude, double longitude )
107 {
108   pLight = new SkyLight(eType);
109   // Need to create the managers
110   cout << "GraphicsContext::Instantiate();" << endl;
111   GraphicsContext::Instantiate();
112   cout << "  TextureManager::Instantiate();" << endl;
113   TextureManager::Instantiate();
114   cout << "  DynamicTextureManager::Instantiate();" << endl;
115   DynamicTextureManager::Instantiate();
116   cout << "  SceneManager::Instantiate();" << endl;
117   SceneManager::Instantiate();
118
119   float rScale = 40.0;
120   FAIL_RETURN(SceneManager::InstancePtr()->LoadClouds(data, size, rScale, latitude, longitude));
121
122   Vec3f dir(0, 0, 1);
123   pLight->SetPosition(Vec3f(0, 0, 17000));
124   pLight->SetDirection(dir);
125   pLight->SetAmbient(Vec4f( 0.0f, 0.0f, 0.0f, 0.0f));
126   pLight->SetDiffuse(Vec4f(1.0f, 1.0f, 1.0f, 0.0f));
127   //pLight->SetDiffuse(Vec4f(0.0f, 0.0f, 0.0f, 0.0f));
128   //pLight->SetSpecular(Vec4f(1.0f, 1.0f, 1.0f, 0.0f));
129  
130    // No attenuation
131   pLight->SetAttenuation(1.0f, 0.0f, 0.0f);
132   SceneManager::InstancePtr()->AddLight(pLight);
133  
134   SceneManager::InstancePtr()->ShadeClouds();
135
136   return true;
137 }
138
139 //------------------------------------------------------------------------------
140 // Function               : SkySceneLoader::Load
141 // Description      : 
142 //------------------------------------------------------------------------------
143 /**
144  * @fn SkySceneLoader::Load(std::string filename)
145  * @brief Loads a SkyWorks scene.
146  * 
147  * This is a temporary fix, as it loads only limited scenes
148  *  It can however, load any number of Cloud
149  +
150  */ 
151 //bool SkySceneLoader::Load(std::string filepath)
152 bool SkySceneLoader::Load( SGPath filename, double latitude, double longitude )
153
154   SkyArchive archive;
155  
156   SGPath base = filename.dir();
157  
158   if (SKYFAILED(archive.Load(filename.c_str()))) {
159       cout << "Archive file not found\n" <<  filename.c_str();
160       return false;
161   }
162     
163   char *pFilename;
164   
165   // Need to create the managers
166   cout << "GraphicsContext::Instantiate();" << endl;
167   GraphicsContext::Instantiate();
168   cout << "  TextureManager::Instantiate();" << endl;
169   TextureManager::Instantiate();
170   cout << "  DynamicTextureManager::Instantiate();" << endl;
171   DynamicTextureManager::Instantiate();
172   cout << "  SceneManager::Instantiate();" << endl;
173   SceneManager::Instantiate();
174
175   unsigned int iNumFiles;
176   if (!SKYFAILED(archive.GetInfo("CloudFile", STRING_TYPE, &iNumFiles)))
177   {
178     iNumFiles = ulEndianLittle32(iNumFiles);
179     for (unsigned int i = 0; i < iNumFiles; ++i)
180     {
181       FAIL_RETURN(archive.FindString("CloudFile", &pFilename, i));
182       //  this is where we have to append the $fg_root string to the filename
183       base.append( pFilename );
184       const char *FilePath = base.c_str();
185      
186       //float rScale = 1.0;
187       //FAIL_RETURN(archive.FindFloat32("CloudScale", &rScale, i));
188       float rScale = 40.0;
189       SkyArchive cloudArchive;
190       FAIL_RETURN(cloudArchive.Load(FilePath));
191       FAIL_RETURN(SceneManager::InstancePtr()->LoadClouds(cloudArchive, rScale, latitude, longitude)); 
192     }
193   }
194   
195   Vec3f dir(0, 0, 1);
196   pLight->SetPosition(Vec3f(0, 0, 17000));
197   pLight->SetDirection(dir);
198   pLight->SetAmbient(Vec4f( 0.0f, 0.0f, 0.0f, 0.0f));
199   pLight->SetDiffuse(Vec4f(1.0f, 1.0f, 1.0f, 0.0f));
200   //pLight->SetDiffuse(Vec4f(0.0f, 0.0f, 0.0f, 0.0f));
201   //pLight->SetSpecular(Vec4f(1.0f, 1.0f, 1.0f, 0.0f));
202   
203    // No attenuation
204   pLight->SetAttenuation(1.0f, 0.0f, 0.0f);
205   SceneManager::InstancePtr()->AddLight(pLight);
206   
207   SceneManager::InstancePtr()->ShadeClouds();
208  
209   return true;
210 }
211
212 void SkySceneLoader::Set_Cloud_Orig( Point3D *posit )
213
214         // set origin for cloud coordinates to initial tile center
215         origin = *posit;
216         sgdSetVec3( delta, origin[0], origin[1], origin[2]);    
217         //printf("Cloud marker %f %f %f\n", origin[0], origin[1], origin[2] );
218         
219 }
220
221 void SkySceneLoader::Update( double *view_pos )
222 {
223         
224         double wind_x, wind_y, wind_z;
225         wind_x = 0.0; wind_z =  0.0;
226         // just a dumb test to see what happens if we can move the clouds en masse via the camera
227         delta[0] += wind_x; delta[2] += wind_z;
228         
229         sgdSubVec3( cam_pos, view_pos, delta );
230         //cout << "ORIGIN: " << delta[0] << " " << delta[1] << " " << delta[2] << endl;
231         //cout << "CAM   : " << cam_pos[0] << " " << cam_pos[1] << " "  << cam_pos[2] << endl;
232         
233         SceneManager::InstancePtr()->Update(*pCam);
234         
235         // need some scheme to reshade selected clouds a few at a time to save frame rate cycles
236         // SceneManager::InstancePtr()->ShadeClouds();
237         
238 }
239
240 void SkySceneLoader::Resize(  double w, double h )
241 {
242         
243   pCam->Perspective( (float) h, (float) (w / h), 0.5, 120000.0 );
244  
245 }
246
247 void SkySceneLoader::Draw( sgMat4 mat )
248 {
249     sgMat4 cameraMatrix;
250
251     // sgCopyMat4(cameraMatrix,mat);    
252     // or just 
253     ssgGetModelviewMatrix(cameraMatrix);
254
255     glMatrixMode ( GL_MODELVIEW ) ;
256     glLoadIdentity () ;
257     glLoadMatrixf( (float *) cameraMatrix );
258   
259     pCam->SetModelviewMatrix( (float *) cameraMatrix );
260   
261     SceneManager::InstancePtr()->Display(*pCam);
262         
263     //pLight->Display(); // draw the light position to  debug with sun position
264
265     glMatrixMode ( GL_MODELVIEW ) ;
266     glLoadIdentity () ;
267 }