]> git.mxchange.org Git - simgear.git/blob - simgear/scene/sky/clouds3d/SkySceneLoader.cpp
Improve endian awareness somehwat. Still not therer though
[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     _ulEndianSwap(&iNumFiles);
132     for (unsigned int i = 0; i < iNumFiles; ++i)
133     {
134       FAIL_RETURN(archive.FindString("CloudFile", &pFilename, i));
135       //  this is where we have to append the $fg_root string to the filename
136       base.append( pFilename );
137       const char *FilePath = base.c_str();
138      
139       //float rScale = 1.0;
140       //FAIL_RETURN(archive.FindFloat32("CloudScale", &rScale, i));
141       float rScale = 40.0;
142       SkyArchive cloudArchive;
143       FAIL_RETURN(cloudArchive.Load(FilePath));
144       FAIL_RETURN(SceneManager::InstancePtr()->LoadClouds(cloudArchive, rScale, latitude, longitude)); 
145     }
146   }
147   
148   Vec3f dir(0, 0, 1);
149   pLight->SetPosition(Vec3f(0, 0, 17000));
150   pLight->SetDirection(dir);
151   pLight->SetAmbient(Vec4f( 0.0f, 0.0f, 0.0f, 0.0f));
152   pLight->SetDiffuse(Vec4f(1.0f, 1.0f, 1.0f, 0.0f));
153   //pLight->SetDiffuse(Vec4f(0.0f, 0.0f, 0.0f, 0.0f));
154   //pLight->SetSpecular(Vec4f(1.0f, 1.0f, 1.0f, 0.0f));
155   
156    // No attenuation
157   pLight->SetAttenuation(1.0f, 0.0f, 0.0f);
158   SceneManager::InstancePtr()->AddLight(pLight);
159   
160   SceneManager::InstancePtr()->ShadeClouds();
161  
162   return true;
163 }
164
165 void SkySceneLoader::Set_Cloud_Orig( Point3D *posit )
166
167         // set origin for cloud coordinates to initial tile center
168         origin = *posit;
169         sgdSetVec3( delta, origin[0], origin[1], origin[2]);    
170         //printf("Cloud marker %f %f %f\n", origin[0], origin[1], origin[2] );
171         
172 }
173
174 void SkySceneLoader::Update( double *view_pos )
175 {
176         
177         double wind_x, wind_y, wind_z;
178         wind_x = 0.0; wind_z =  0.0;
179         // just a dumb test to see what happens if we can move the clouds en masse via the camera
180         delta[0] += wind_x; delta[2] += wind_z;
181         
182         sgdSubVec3( cam_pos, view_pos, delta );
183         //cout << "ORIGIN: " << delta[0] << " " << delta[1] << " " << delta[2] << endl;
184         //cout << "CAM   : " << cam_pos[0] << " " << cam_pos[1] << " "  << cam_pos[2] << endl;
185         
186         SceneManager::InstancePtr()->Update(*pCam);
187         
188         // need some scheme to reshade selected clouds a few at a time to save frame rate cycles
189         // SceneManager::InstancePtr()->ShadeClouds();
190         
191 }
192
193 void SkySceneLoader::Resize(  double w, double h )
194 {
195         
196   pCam->Perspective( (float) h, (float) (w / h), 0.5, 120000.0 );
197  
198 }
199
200 void SkySceneLoader::Draw( sgMat4 mat )
201 {
202     sgMat4 cameraMatrix;
203
204     // sgCopyMat4(cameraMatrix,mat);    
205     // or just 
206     ssgGetModelviewMatrix(cameraMatrix);
207
208     glMatrixMode ( GL_MODELVIEW ) ;
209     glLoadIdentity () ;
210     glLoadMatrixf( (float *) cameraMatrix );
211   
212     pCam->SetModelviewMatrix( (float *) cameraMatrix );
213   
214     SceneManager::InstancePtr()->Display(*pCam);
215         
216     //pLight->Display(); // draw the light position to  debug with sun position
217
218     glMatrixMode ( GL_MODELVIEW ) ;
219     glLoadIdentity () ;
220 }