1 //------------------------------------------------------------------------------
2 // File : SkySceneLoader.cpp
3 //------------------------------------------------------------------------------
4 // Adapted from SkyWorks for FlightGear by J. Wojnaroski -- castle@mminternet.com
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.
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.
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.
21 * @file SkySceneLoader.cpp
23 * Implementation of class SkySceneLoader.
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>
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"
43 ssgLight _sky_ssgLights [ 8 ] ;
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;
55 sgMat4 my_copy_of_ssgOpenGLAxisSwapMatrix =
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 }
62 //------------------------------------------------------------------------------
63 // Function : SkySceneLoader::SkySceneLoader
65 //------------------------------------------------------------------------------
67 * @fn SkySceneLoader::SkySceneLoader()
70 SkySceneLoader::SkySceneLoader()
75 //------------------------------------------------------------------------------
76 // Function : SkySceneLoader::~SkySceneLoader
78 //------------------------------------------------------------------------------
80 * @fn SkySceneLoader::~SkySceneLoader()
83 SkySceneLoader::~SkySceneLoader()
85 SceneManager::Destroy();
86 DynamicTextureManager::Destroy();
87 TextureManager::Destroy();
88 GraphicsContext::Destroy();
93 //------------------------------------------------------------------------------
94 // Function : SkySceneLoader::Load
96 //------------------------------------------------------------------------------
98 * @fn SkySceneLoader::Load(std::string filename)
99 * @brief Loads a SkyWorks scene.
101 * This is a temporary fix, as it loads only limited scenes
102 * It can however, load any number of Cloud
105 //bool SkySceneLoader::Load(std::string filepath)
106 bool SkySceneLoader::Load( unsigned char *data, unsigned int size, double latitude, double longitude )
109 pLight = new SkyLight(eType);
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();
122 FAIL_RETURN(SceneManager::InstancePtr()->LoadClouds(data, size, rScale, latitude, longitude));
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));
133 pLight->SetAttenuation(1.0f, 0.0f, 0.0f);
134 SceneManager::InstancePtr()->AddLight(pLight);
136 SceneManager::InstancePtr()->ShadeClouds();
141 //------------------------------------------------------------------------------
142 // Function : SkySceneLoader::Load
144 //------------------------------------------------------------------------------
146 * @fn SkySceneLoader::Load(std::string filename)
147 * @brief Loads a SkyWorks scene.
149 * This is a temporary fix, as it loads only limited scenes
150 * It can however, load any number of Cloud
153 //bool SkySceneLoader::Load(std::string filepath)
154 bool SkySceneLoader::Load( SGPath filename, double latitude, double longitude )
158 SGPath base = filename.dir();
160 if (SKYFAILED(archive.Load(filename.c_str()))) {
161 cout << "Archive file not found\n" << filename.c_str();
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();
177 unsigned int iNumFiles;
178 if (!SKYFAILED(archive.GetInfo("CloudFile", STRING_TYPE, &iNumFiles)))
180 iNumFiles = ulEndianLittle32(iNumFiles);
181 for (unsigned int i = 0; i < iNumFiles; ++i)
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();
188 //float rScale = 1.0;
189 //FAIL_RETURN(archive.FindFloat32("CloudScale", &rScale, i));
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));
198 cout << "After Load Clouds" << endl;
202 pLight = new SkyLight(eType);
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));
212 pLight->SetAttenuation(1.0f, 0.0f, 0.0f);
213 cout << "Before SceneManager::InstancePtr()->AddLight(pLight)" << endl;
214 SceneManager::InstancePtr()->AddLight(pLight);
216 cout << "Before SceneManager::InstancePtr()->ShadeClouds()" << endl;
217 SceneManager::InstancePtr()->ShadeClouds();
218 cout << "After SceneManager::InstancePtr()->ShadeClouds()" << endl;
223 void SkySceneLoader::Set_Cloud_Orig( Point3D *posit )
225 // set origin for cloud coordinates to initial tile center
227 sgdSetVec3( delta, origin[0], origin[1], origin[2]);
228 //printf("Cloud marker %f %f %f\n", origin[0], origin[1], origin[2] );
232 void SkySceneLoader::Update( double *view_pos )
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;
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;
244 SceneManager::InstancePtr()->Update(*pCam);
246 // need some scheme to reshade selected clouds a few at a time to save frame rate cycles
247 // SceneManager::InstancePtr()->ShadeClouds();
251 void SkySceneLoader::Resize( double w, double h )
254 pCam->Perspective( (float) h, (float) (w / h), 0.5, 120000.0 );
258 void SkySceneLoader::Draw( sgMat4 mat )
262 // sgCopyMat4(cameraMatrix,mat);
264 ssgGetModelviewMatrix(cameraMatrix);
266 glMatrixMode ( GL_MODELVIEW ) ;
268 glLoadMatrixf( (float *) cameraMatrix );
270 pCam->SetModelviewMatrix( (float *) cameraMatrix );
272 SceneManager::InstancePtr()->Display(*pCam);
274 //pLight->Display(); // draw the light position to debug with sun position
276 glMatrixMode ( GL_MODELVIEW ) ;