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.
28 #include <simgear/math/point3d.hxx>
29 #include <simgear/math/polar3d.hxx>
30 #include <simgear/math/vector.hxx>
32 #include "SkySceneLoader.hpp"
33 #include "SkySceneManager.hpp"
34 #include "SkyTextureManager.hpp"
35 #include "SkySceneManager.hpp"
36 #include "SkyDynamicTextureManager.hpp"
37 #include "SkyContext.hpp"
38 #include "SkyLight.hpp"
41 ssgLight _sky_ssgLights [ 8 ] ;
47 Camera *pCam = new Camera();
48 // Need to add a light here until we figure out how to use the sun position and color
49 SkyLight::SkyLightType eType = SkyLight::SKY_LIGHT_DIRECTIONAL;
50 SkyLight *pLight = new SkyLight(eType);
53 sgMat4 my_copy_of_ssgOpenGLAxisSwapMatrix =
55 { 1.0f, 0.0f, 0.0f, 0.0f },
56 { 0.0f, 0.0f, -1.0f, 0.0f },
57 { 0.0f, 1.0f, 0.0f, 0.0f },
58 { 0.0f, 0.0f, 0.0f, 1.0f }
60 //------------------------------------------------------------------------------
61 // Function : SkySceneLoader::SkySceneLoader
63 //------------------------------------------------------------------------------
65 * @fn SkySceneLoader::SkySceneLoader()
68 SkySceneLoader::SkySceneLoader()
73 //------------------------------------------------------------------------------
74 // Function : SkySceneLoader::~SkySceneLoader
76 //------------------------------------------------------------------------------
78 * @fn SkySceneLoader::~SkySceneLoader()
81 SkySceneLoader::~SkySceneLoader()
83 SceneManager::Destroy();
84 DynamicTextureManager::Destroy();
85 TextureManager::Destroy();
86 GraphicsContext::Destroy();
90 //------------------------------------------------------------------------------
91 // Function : SkySceneLoader::Load
93 //------------------------------------------------------------------------------
95 * @fn SkySceneLoader::Load(std::string filename)
96 * @brief Loads a SkyWorks scene.
98 * This is a temporary fix, as it loads only limited scenes
99 * It can however, load any number of Cloud
102 bool SkySceneLoader::Load(std::string filename)
105 cout << "SkySceneLoader::Load( " << filename << " )" << endl;
106 if (SKYFAILED(archive.Load(filename.c_str()))) {
107 cout << "Archive file not found\n";
112 // Need to create the managers
113 cout << "GraphicsContext::Instantiate();" << endl;
114 GraphicsContext::Instantiate();
115 cout << " TextureManager::Instantiate();" << endl;
116 TextureManager::Instantiate();
117 cout << " DynamicTextureManager::Instantiate();" << endl;
118 DynamicTextureManager::Instantiate();
119 cout << " SceneManager::Instantiate();" << endl;
120 SceneManager::Instantiate();
122 unsigned int iNumFiles;
123 if (!SKYFAILED(archive.GetInfo("CloudFile", STRING_TYPE, &iNumFiles)))
125 for (unsigned int i = 0; i < iNumFiles; ++i)
127 FAIL_RETURN(archive.FindString("CloudFile", &pFilename, i));
129 FAIL_RETURN(archive.FindFloat32("CloudScale", &rScale, i));
131 SkyArchive cloudArchive;
132 FAIL_RETURN(cloudArchive.Load(pFilename));
133 FAIL_RETURN(SceneManager::InstancePtr()->LoadClouds(cloudArchive, rScale));
138 pLight->SetPosition(Vec3f(0, 0, 7000));
139 pLight->SetDirection(dir);
140 pLight->SetAmbient(Vec4f( 0.0f, 0.0f, 0.0f, 0.0f));
141 pLight->SetDiffuse(Vec4f(1.0f, 1.0f, 1.0f, 0.0f));
142 //pLight->SetDiffuse(Vec4f(0.0f, 0.0f, 0.0f, 0.0f));
143 //pLight->SetSpecular(Vec4f(1.0f, 1.0f, 1.0f, 0.0f));
146 pLight->SetAttenuation(1.0f, 0.0f, 0.0f);
147 SceneManager::InstancePtr()->AddLight(pLight);
149 SceneManager::InstancePtr()->ShadeClouds();
154 void SkySceneLoader::Set_Cloud_Orig( Point3D *posit )
156 // set origin for cloud coordinates to initial tile center
158 sgdSetVec3( delta, origin[0], origin[1], origin[2]);
159 //printf("Cloud marker %f %f %f\n", origin[0], origin[1], origin[2] );
163 void SkySceneLoader::Update( double *view_pos )
166 double wind_x, wind_y, wind_z;
167 wind_x = -0.05; wind_z = 0.05;
168 // just a dumb test to see if we can move the clouds en masse via the camera
169 delta[0] += wind_x; delta[2] += wind_z;
171 sgdSubVec3( cam_pos, view_pos, delta );
172 //cout << "ORIGIN: " << delta[0] << " " << delta[1] << " " << delta[2] << endl;
173 //cout << "CAM : " << cam_pos[0] << " " << cam_pos[1] << " " << cam_pos[2] << endl;
175 SceneManager::InstancePtr()->Update(*pCam);
177 // need some scheme to reshade selected clouds a few at a time to save frame rate cycles
178 ///SceneManager::InstancePtr()->ShadeClouds();
182 void SkySceneLoader::Resize( double w, double h )
185 pCam->Perspective( (float) h, (float) (w / h), 0.5, 120000.0 );
189 void SkySceneLoader::Draw( sgMat4 mat )
190 {/* need this if you want to look at FG matrix
191 if ( _ssgCurrentContext == NULL )
193 cout<< "ssg: No Current Context: Did you forgot to call ssgInit()?" ; char x; cin >> x;
196 ssgForceBasicState () ;
198 sgMat4 test, m, *pm, viewmat, cameraMatrix;
200 sgSetVec4(mat[3], cam_pos[0], cam_pos[1], cam_pos[2], SG_ONE);
201 // at this point the view matrix has the cloud camera position relative to cloud origin
202 // now transform to screen coordinates
203 sgTransposeNegateMat4 ( viewmat, mat ) ;
205 sgCopyMat4 ( cameraMatrix, my_copy_of_ssgOpenGLAxisSwapMatrix ) ;
206 sgPreMultMat4 ( cameraMatrix, viewmat ) ;
208 //sgCopyMat4 ( test, cameraMatrix );
210 //printf( "\nSkyworks ViewModel matrix\n" );
211 //cout << test[0][0] << " " << test[1][0] << " " << test[2][0] << " " << test[3][0] << endl;
212 //cout << test[0][1] << " " << test[1][1] << " " << test[2][1] << " " << test[3][1] << endl;
213 //cout << test[0][2] << " " << test[1][2] << " " << test[2][2] << " " << test[3][2] << endl;
214 //cout << test[0][3] << " " << test[1][3] << " " << test[2][3] << " " << test[3][3] << endl;
216 // this is the cameraview matrix used by flightgear to render scene
217 //_ssgCurrentContext->getModelviewMatrix( m );
219 glMatrixMode ( GL_MODELVIEW ) ;
221 glLoadMatrixf( (float *) cameraMatrix );
223 //sgCopyMat4( test, m );
225 pCam->SetModelviewMatrix( (float *) cameraMatrix );
227 //printf( "\nFG modelview matrix\n" );
228 //cout << test[0][0] << " " << test[1][0] << " " << test[2][0] << " " << test[3][0] << endl;
229 //cout << test[0][1] << " " << test[1][1] << " " << test[2][1] << " " << test[3][1] << endl;
230 //cout << test[0][2] << " " << test[1][2] << " " << test[2][2] << " " << test[3][2] << endl;
231 //cout << test[0][3] << " " << test[1][3] << " " << test[2][3] << " " << test[3][3] << endl;
233 SceneManager::InstancePtr()->Display(*pCam);
235 //pLight->Display(); // draw the light position to debug with sun position
237 glMatrixMode ( GL_MODELVIEW ) ;