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