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();
92 //------------------------------------------------------------------------------
93 // Function : SkySceneLoader::Load
95 //------------------------------------------------------------------------------
97 * @fn SkySceneLoader::Load(std::string filename)
98 * @brief Loads a SkyWorks scene.
100 * This is a temporary fix, as it loads only limited scenes
101 * It can however, load any number of Cloud
104 //bool SkySceneLoader::Load(std::string filepath)
105 bool SkySceneLoader::Load( SGPath filename, double latitude, double longitude )
109 SGPath base = filename.dir();
111 if (SKYFAILED(archive.Load(filename.c_str()))) {
112 cout << "Archive file not found\n" << filename.c_str();
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();
128 unsigned int iNumFiles;
129 if (!SKYFAILED(archive.GetInfo("CloudFile", STRING_TYPE, &iNumFiles)))
131 for (unsigned int i = 0; i < iNumFiles; ++i)
133 FAIL_RETURN(archive.FindString("CloudFile", &pFilename, i));
134 // this is where we have to append the $fg_root string to the filename
135 base.append( pFilename );
136 const char *FilePath = base.c_str();
138 //float rScale = 1.0;
139 //FAIL_RETURN(archive.FindFloat32("CloudScale", &rScale, i));
141 SkyArchive cloudArchive;
142 FAIL_RETURN(cloudArchive.Load(FilePath));
143 FAIL_RETURN(SceneManager::InstancePtr()->LoadClouds(cloudArchive, rScale, latitude, longitude));
148 pLight->SetPosition(Vec3f(0, 0, 17000));
149 pLight->SetDirection(dir);
150 pLight->SetAmbient(Vec4f( 0.0f, 0.0f, 0.0f, 0.0f));
151 pLight->SetDiffuse(Vec4f(1.0f, 1.0f, 1.0f, 0.0f));
152 //pLight->SetDiffuse(Vec4f(0.0f, 0.0f, 0.0f, 0.0f));
153 //pLight->SetSpecular(Vec4f(1.0f, 1.0f, 1.0f, 0.0f));
156 pLight->SetAttenuation(1.0f, 0.0f, 0.0f);
157 SceneManager::InstancePtr()->AddLight(pLight);
159 SceneManager::InstancePtr()->ShadeClouds();
164 void SkySceneLoader::Set_Cloud_Orig( Point3D *posit )
166 // set origin for cloud coordinates to initial tile center
168 sgdSetVec3( delta, origin[0], origin[1], origin[2]);
169 //printf("Cloud marker %f %f %f\n", origin[0], origin[1], origin[2] );
173 void SkySceneLoader::Update( double *view_pos )
176 double wind_x, wind_y, wind_z;
177 wind_x = 0.0; wind_z = 0.0;
178 // just a dumb test to see what happens if we can move the clouds en masse via the camera
179 delta[0] += wind_x; delta[2] += wind_z;
181 sgdSubVec3( cam_pos, view_pos, delta );
182 //cout << "ORIGIN: " << delta[0] << " " << delta[1] << " " << delta[2] << endl;
183 //cout << "CAM : " << cam_pos[0] << " " << cam_pos[1] << " " << cam_pos[2] << endl;
185 SceneManager::InstancePtr()->Update(*pCam);
187 // need some scheme to reshade selected clouds a few at a time to save frame rate cycles
188 // SceneManager::InstancePtr()->ShadeClouds();
192 void SkySceneLoader::Resize( double w, double h )
195 pCam->Perspective( (float) h, (float) (w / h), 0.5, 120000.0 );
199 void SkySceneLoader::Draw( sgMat4 mat )
203 // sgCopyMat4(cameraMatrix,mat);
205 ssgGetModelviewMatrix(cameraMatrix);
207 glMatrixMode ( GL_MODELVIEW ) ;
209 glLoadMatrixf( (float *) cameraMatrix );
211 pCam->SetModelviewMatrix( (float *) cameraMatrix );
213 SceneManager::InstancePtr()->Display(*pCam);
215 //pLight->Display(); // draw the light position to debug with sun position
217 glMatrixMode ( GL_MODELVIEW ) ;