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;
52 SkyLight *pLight = new SkyLight(eType);
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 )
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();
119 FAIL_RETURN(SceneManager::InstancePtr()->LoadClouds(data, size, rScale, latitude, longitude));
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));
130 pLight->SetAttenuation(1.0f, 0.0f, 0.0f);
131 SceneManager::InstancePtr()->AddLight(pLight);
133 SceneManager::InstancePtr()->ShadeClouds();
138 //------------------------------------------------------------------------------
139 // Function : SkySceneLoader::Load
141 //------------------------------------------------------------------------------
143 * @fn SkySceneLoader::Load(std::string filename)
144 * @brief Loads a SkyWorks scene.
146 * This is a temporary fix, as it loads only limited scenes
147 * It can however, load any number of Cloud
150 //bool SkySceneLoader::Load(std::string filepath)
151 bool SkySceneLoader::Load( SGPath filename, double latitude, double longitude )
155 SGPath base = filename.dir();
157 if (SKYFAILED(archive.Load(filename.c_str()))) {
158 cout << "Archive file not found\n" << filename.c_str();
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();
174 unsigned int iNumFiles;
175 if (!SKYFAILED(archive.GetInfo("CloudFile", STRING_TYPE, &iNumFiles)))
177 iNumFiles = ulEndianLittle32(iNumFiles);
178 for (unsigned int i = 0; i < iNumFiles; ++i)
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();
185 //float rScale = 1.0;
186 //FAIL_RETURN(archive.FindFloat32("CloudScale", &rScale, i));
188 SkyArchive cloudArchive;
189 FAIL_RETURN(cloudArchive.Load(FilePath));
190 FAIL_RETURN(SceneManager::InstancePtr()->LoadClouds(cloudArchive, rScale, latitude, longitude));
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));
203 pLight->SetAttenuation(1.0f, 0.0f, 0.0f);
204 SceneManager::InstancePtr()->AddLight(pLight);
206 SceneManager::InstancePtr()->ShadeClouds();
211 void SkySceneLoader::Set_Cloud_Orig( Point3D *posit )
213 // set origin for cloud coordinates to initial tile center
215 sgdSetVec3( delta, origin[0], origin[1], origin[2]);
216 //printf("Cloud marker %f %f %f\n", origin[0], origin[1], origin[2] );
220 void SkySceneLoader::Update( double *view_pos )
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;
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;
232 SceneManager::InstancePtr()->Update(*pCam);
234 // need some scheme to reshade selected clouds a few at a time to save frame rate cycles
235 // SceneManager::InstancePtr()->ShadeClouds();
239 void SkySceneLoader::Resize( double w, double h )
242 pCam->Perspective( (float) h, (float) (w / h), 0.5, 120000.0 );
246 void SkySceneLoader::Draw( sgMat4 mat )
250 // sgCopyMat4(cameraMatrix,mat);
252 ssgGetModelviewMatrix(cameraMatrix);
254 glMatrixMode ( GL_MODELVIEW ) ;
256 glLoadMatrixf( (float *) cameraMatrix );
258 pCam->SetModelviewMatrix( (float *) cameraMatrix );
260 SceneManager::InstancePtr()->Display(*pCam);
262 //pLight->Display(); // draw the light position to debug with sun position
264 glMatrixMode ( GL_MODELVIEW ) ;