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 if (SKYFAILED(archive.Load(filename.c_str()))) {
106 cout << "Archive file not found\n";
110 // Need to create the managers
111 GraphicsContext::Instantiate();
112 TextureManager::Instantiate();
113 DynamicTextureManager::Instantiate();
114 SceneManager::Instantiate();
116 unsigned int iNumFiles;
117 if (!SKYFAILED(archive.GetInfo("CloudFile", STRING_TYPE, &iNumFiles)))
119 for (unsigned int i = 0; i < iNumFiles; ++i)
121 FAIL_RETURN(archive.FindString("CloudFile", &pFilename, i));
\r
123 FAIL_RETURN(archive.FindFloat32("CloudScale", &rScale, i));
125 SkyArchive cloudArchive;
126 FAIL_RETURN(cloudArchive.Load(pFilename));
127 FAIL_RETURN(SceneManager::InstancePtr()->LoadClouds(cloudArchive, rScale));
132 pLight->SetPosition(Vec3f(0, 0, 7000));
133 pLight->SetDirection(dir);
134 pLight->SetAmbient(Vec4f( 0.0f, 0.0f, 0.0f, 0.0f));
135 pLight->SetDiffuse(Vec4f(1.0f, 1.0f, 1.0f, 0.0f));
136 //pLight->SetDiffuse(Vec4f(0.0f, 0.0f, 0.0f, 0.0f));
137 //pLight->SetSpecular(Vec4f(1.0f, 1.0f, 1.0f, 0.0f));
140 pLight->SetAttenuation(1.0f, 0.0f, 0.0f);
141 SceneManager::InstancePtr()->AddLight(pLight);
143 SceneManager::InstancePtr()->ShadeClouds();
148 void SkySceneLoader::Set_Cloud_Orig( Point3D *posit )
150 // set origin for cloud coordinates to initial tile center
152 sgdSetVec3( delta, origin[0], origin[1], origin[2]);
153 //printf("Cloud marker %f %f %f\n", origin[0], origin[1], origin[2] );
157 void SkySceneLoader::Update( double *view_pos )
160 double wind_x, wind_y, wind_z;
161 wind_x = -0.05; wind_z = 0.05;
162 // just a dumb test to see if we can move the clouds en masse via the camera
163 delta[0] += wind_x; delta[2] += wind_z;
165 sgdSubVec3( cam_pos, view_pos, delta );
166 //cout << "ORIGIN: " << delta[0] << " " << delta[1] << " " << delta[2] << endl;
167 //cout << "CAM : " << cam_pos[0] << " " << cam_pos[1] << " " << cam_pos[2] << endl;
169 SceneManager::InstancePtr()->Update(*pCam);
171 // need some scheme to reshade selected clouds a few at a time to save frame rate cycles
172 ///SceneManager::InstancePtr()->ShadeClouds();
176 void SkySceneLoader::Resize( double w, double h )
179 pCam->Perspective( (float) h, (float) (w / h), 0.5, 120000.0 );
183 void SkySceneLoader::Draw( sgMat4 mat )
184 {/* need this if you want to look at FG matrix
185 if ( _ssgCurrentContext == NULL )
187 cout<< "ssg: No Current Context: Did you forgot to call ssgInit()?" ; char x; cin >> x;
190 ssgForceBasicState () ;
192 sgMat4 test, m, *pm, viewmat, cameraMatrix;
194 sgSetVec4(mat[3], cam_pos[0], cam_pos[1], cam_pos[2], SG_ONE);
195 // at this point the view matrix has the cloud camera position relative to cloud origin
196 // now transform to screen coordinates
197 sgTransposeNegateMat4 ( viewmat, mat ) ;
199 sgCopyMat4 ( cameraMatrix, my_copy_of_ssgOpenGLAxisSwapMatrix ) ;
200 sgPreMultMat4 ( cameraMatrix, viewmat ) ;
202 //sgCopyMat4 ( test, cameraMatrix );
204 //printf( "\nSkyworks ViewModel matrix\n" );
205 //cout << test[0][0] << " " << test[1][0] << " " << test[2][0] << " " << test[3][0] << endl;
206 //cout << test[0][1] << " " << test[1][1] << " " << test[2][1] << " " << test[3][1] << endl;
207 //cout << test[0][2] << " " << test[1][2] << " " << test[2][2] << " " << test[3][2] << endl;
208 //cout << test[0][3] << " " << test[1][3] << " " << test[2][3] << " " << test[3][3] << endl;
210 // this is the cameraview matrix used by flightgear to render scene
211 //_ssgCurrentContext->getModelviewMatrix( m );
213 glMatrixMode ( GL_MODELVIEW ) ;
215 glLoadMatrixf( (float *) cameraMatrix );
217 //sgCopyMat4( test, m );
219 pCam->SetModelviewMatrix( (float *) cameraMatrix );
221 //printf( "\nFG modelview matrix\n" );
222 //cout << test[0][0] << " " << test[1][0] << " " << test[2][0] << " " << test[3][0] << endl;
223 //cout << test[0][1] << " " << test[1][1] << " " << test[2][1] << " " << test[3][1] << endl;
224 //cout << test[0][2] << " " << test[1][2] << " " << test[2][2] << " " << test[3][2] << endl;
225 //cout << test[0][3] << " " << test[1][3] << " " << test[2][3] << " " << test[3][3] << endl;
227 SceneManager::InstancePtr()->Display(*pCam);
229 //pLight->Display(); // draw the light position to debug with sun position
231 glMatrixMode ( GL_MODELVIEW ) ;