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