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 )
108 pLight = new SkyLight(eType);
109 // Need to create the managers
110 cout << "GraphicsContext::Instantiate();" << endl;
111 GraphicsContext::Instantiate();
112 cout << " TextureManager::Instantiate();" << endl;
113 TextureManager::Instantiate();
114 cout << " DynamicTextureManager::Instantiate();" << endl;
115 DynamicTextureManager::Instantiate();
116 cout << " SceneManager::Instantiate();" << endl;
117 SceneManager::Instantiate();
120 FAIL_RETURN(SceneManager::InstancePtr()->LoadClouds(data, size, rScale, latitude, longitude));
123 pLight->SetPosition(Vec3f(0, 0, 17000));
124 pLight->SetDirection(dir);
125 pLight->SetAmbient(Vec4f( 0.0f, 0.0f, 0.0f, 0.0f));
126 pLight->SetDiffuse(Vec4f(1.0f, 1.0f, 1.0f, 0.0f));
127 //pLight->SetDiffuse(Vec4f(0.0f, 0.0f, 0.0f, 0.0f));
128 //pLight->SetSpecular(Vec4f(1.0f, 1.0f, 1.0f, 0.0f));
131 pLight->SetAttenuation(1.0f, 0.0f, 0.0f);
132 SceneManager::InstancePtr()->AddLight(pLight);
134 SceneManager::InstancePtr()->ShadeClouds();
139 //------------------------------------------------------------------------------
140 // Function : SkySceneLoader::Load
142 //------------------------------------------------------------------------------
144 * @fn SkySceneLoader::Load(std::string filename)
145 * @brief Loads a SkyWorks scene.
147 * This is a temporary fix, as it loads only limited scenes
148 * It can however, load any number of Cloud
151 //bool SkySceneLoader::Load(std::string filepath)
152 bool SkySceneLoader::Load( SGPath filename, double latitude, double longitude )
156 SGPath base = filename.dir();
158 if (SKYFAILED(archive.Load(filename.c_str()))) {
159 cout << "Archive file not found\n" << filename.c_str();
165 // Need to create the managers
166 cout << "GraphicsContext::Instantiate();" << endl;
167 GraphicsContext::Instantiate();
168 cout << " TextureManager::Instantiate();" << endl;
169 TextureManager::Instantiate();
170 cout << " DynamicTextureManager::Instantiate();" << endl;
171 DynamicTextureManager::Instantiate();
172 cout << " SceneManager::Instantiate();" << endl;
173 SceneManager::Instantiate();
175 unsigned int iNumFiles;
176 if (!SKYFAILED(archive.GetInfo("CloudFile", STRING_TYPE, &iNumFiles)))
178 iNumFiles = ulEndianLittle32(iNumFiles);
179 for (unsigned int i = 0; i < iNumFiles; ++i)
181 FAIL_RETURN(archive.FindString("CloudFile", &pFilename, i));
182 // this is where we have to append the $fg_root string to the filename
183 base.append( pFilename );
184 const char *FilePath = base.c_str();
186 //float rScale = 1.0;
187 //FAIL_RETURN(archive.FindFloat32("CloudScale", &rScale, i));
189 SkyArchive cloudArchive;
190 FAIL_RETURN(cloudArchive.Load(FilePath));
191 FAIL_RETURN(SceneManager::InstancePtr()->LoadClouds(cloudArchive, rScale, latitude, longitude));
196 pLight->SetPosition(Vec3f(0, 0, 17000));
197 pLight->SetDirection(dir);
198 pLight->SetAmbient(Vec4f( 0.0f, 0.0f, 0.0f, 0.0f));
199 pLight->SetDiffuse(Vec4f(1.0f, 1.0f, 1.0f, 0.0f));
200 //pLight->SetDiffuse(Vec4f(0.0f, 0.0f, 0.0f, 0.0f));
201 //pLight->SetSpecular(Vec4f(1.0f, 1.0f, 1.0f, 0.0f));
204 pLight->SetAttenuation(1.0f, 0.0f, 0.0f);
205 SceneManager::InstancePtr()->AddLight(pLight);
207 SceneManager::InstancePtr()->ShadeClouds();
212 void SkySceneLoader::Set_Cloud_Orig( Point3D *posit )
214 // set origin for cloud coordinates to initial tile center
216 sgdSetVec3( delta, origin[0], origin[1], origin[2]);
217 //printf("Cloud marker %f %f %f\n", origin[0], origin[1], origin[2] );
221 void SkySceneLoader::Update( double *view_pos )
224 double wind_x, wind_y, wind_z;
225 wind_x = 0.0; wind_z = 0.0;
226 // just a dumb test to see what happens if we can move the clouds en masse via the camera
227 delta[0] += wind_x; delta[2] += wind_z;
229 sgdSubVec3( cam_pos, view_pos, delta );
230 //cout << "ORIGIN: " << delta[0] << " " << delta[1] << " " << delta[2] << endl;
231 //cout << "CAM : " << cam_pos[0] << " " << cam_pos[1] << " " << cam_pos[2] << endl;
233 SceneManager::InstancePtr()->Update(*pCam);
235 // need some scheme to reshade selected clouds a few at a time to save frame rate cycles
236 // SceneManager::InstancePtr()->ShadeClouds();
240 void SkySceneLoader::Resize( double w, double h )
243 pCam->Perspective( (float) h, (float) (w / h), 0.5, 120000.0 );
247 void SkySceneLoader::Draw( sgMat4 mat )
251 // sgCopyMat4(cameraMatrix,mat);
253 ssgGetModelviewMatrix(cameraMatrix);
255 glMatrixMode ( GL_MODELVIEW ) ;
257 glLoadMatrixf( (float *) cameraMatrix );
259 pCam->SetModelviewMatrix( (float *) cameraMatrix );
261 SceneManager::InstancePtr()->Display(*pCam);
263 //pLight->Display(); // draw the light position to debug with sun position
265 glMatrixMode ( GL_MODELVIEW ) ;