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(3000, 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 )
149 { // use this to adjust camera position for a new tile center
151 // set origin for cloud coordinates to initial tile center
153 sgdSetVec3( delta, origin[0], origin[1], origin[2]);
154 //printf("Cloud marker %f %f %f\n", origin[0], origin[1], origin[2] );
158 void SkySceneLoader::Update( double *view_pos )
160 sgdSubVec3( cam_pos, view_pos, delta );
161 //cout << "ORIGIN: " << delta[0] << " " << delta[1] << " " << delta[2] << endl;
162 //cout << "CAM : " << cam_pos[0] << " " << cam_pos[1] << " " << cam_pos[2] << endl;
164 SceneManager::InstancePtr()->Update(*pCam);
166 // need some scheme to reshade selected clouds a few at a time to save frame rate cycles
167 ///SceneManager::InstancePtr()->ShadeClouds();
171 void SkySceneLoader::Resize( double w, double h )
174 pCam->Perspective( (float) h, (float) (w / h), 0.5, 120000.0 );
178 void SkySceneLoader::Draw( sgMat4 mat )
179 {/* need this if you want to look at FG matrix
180 if ( _ssgCurrentContext == NULL )
182 cout<< "ssg: No Current Context: Did you forgot to call ssgInit()?" ; char x; cin >> x;
185 ssgForceBasicState () ;
187 sgMat4 test, m, *pm, viewmat, cameraMatrix;
189 sgSetVec4(mat[3], cam_pos[0], cam_pos[1], cam_pos[2], SG_ONE);
190 // at this point the view matrix has the cloud camera position relative to cloud origin
191 // now transform to screen coordinates
192 sgTransposeNegateMat4 ( viewmat, mat ) ;
194 sgCopyMat4 ( cameraMatrix, my_copy_of_ssgOpenGLAxisSwapMatrix ) ;
195 sgPreMultMat4 ( cameraMatrix, viewmat ) ;
197 //sgCopyMat4 ( test, cameraMatrix );
199 //printf( "\nSkyworks ViewModel matrix\n" );
200 //cout << test[0][0] << " " << test[1][0] << " " << test[2][0] << " " << test[3][0] << endl;
201 //cout << test[0][1] << " " << test[1][1] << " " << test[2][1] << " " << test[3][1] << endl;
202 //cout << test[0][2] << " " << test[1][2] << " " << test[2][2] << " " << test[3][2] << endl;
203 //cout << test[0][3] << " " << test[1][3] << " " << test[2][3] << " " << test[3][3] << endl;
205 // this is the cameraview matrix used by flightgear to render scene
206 //_ssgCurrentContext->getModelviewMatrix( m );
208 glMatrixMode ( GL_MODELVIEW ) ;
210 glLoadMatrixf( (float *) cameraMatrix );
212 //sgCopyMat4( test, m );
214 pCam->SetModelviewMatrix( (float *) cameraMatrix );
216 //printf( "\nFG modelview matrix\n" );
217 //cout << test[0][0] << " " << test[1][0] << " " << test[2][0] << " " << test[3][0] << endl;
218 //cout << test[0][1] << " " << test[1][1] << " " << test[2][1] << " " << test[3][1] << endl;
219 //cout << test[0][2] << " " << test[1][2] << " " << test[2][2] << " " << test[3][2] << endl;
220 //cout << test[0][3] << " " << test[1][3] << " " << test[2][3] << " " << test[3][3] << endl;
222 SceneManager::InstancePtr()->Display(*pCam);
224 //pLight->Display(); // draw the light position to debug with sun position
226 glMatrixMode ( GL_MODELVIEW ) ;