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.
27 #include <simgear/math/point3d.hxx>
29 #include "SkySceneLoader.hpp"
30 #include "SkySceneManager.hpp"
31 #include "SkyTextureManager.hpp"
32 #include "SkySceneManager.hpp"
33 #include "SkyDynamicTextureManager.hpp"
34 #include "SkyContext.hpp"
35 //#include "SkyViewManager.hpp"
36 //#include "SkyRenderableInstanceGroup.hpp"
37 #include "SkyLight.hpp"
40 ssgLight _sky_ssgLights [ 8 ] ;
41 static Point3D origin;
43 //int _ssgFrameCounter = 0 ;
44 Camera *pCam = new Camera();
45 // Need to add a light here until we figure out how to use the sun position and color
46 SkyLight::SkyLightType eType = SkyLight::SKY_LIGHT_DIRECTIONAL;
47 SkyLight *pLight = new SkyLight(eType);
49 //------------------------------------------------------------------------------
50 // Function : SkySceneLoader::SkySceneLoader
52 //------------------------------------------------------------------------------
54 * @fn SkySceneLoader::SkySceneLoader()
57 SkySceneLoader::SkySceneLoader()
62 //------------------------------------------------------------------------------
63 // Function : SkySceneLoader::~SkySceneLoader
65 //------------------------------------------------------------------------------
67 * @fn SkySceneLoader::~SkySceneLoader()
70 SkySceneLoader::~SkySceneLoader()
72 SceneManager::Destroy();
73 DynamicTextureManager::Destroy();
74 TextureManager::Destroy();
75 GraphicsContext::Destroy();
79 //------------------------------------------------------------------------------
80 // Function : SkySceneLoader::Load
82 //------------------------------------------------------------------------------
84 * @fn SkySceneLoader::Load(std::string filename)
85 * @brief Loads a SkyWorks scene.
87 * This is a temporary fix, as it loads only limited scenes
88 * It can however, load any number of Cloud
91 bool SkySceneLoader::Load(std::string filename)
94 if (SKYFAILED(archive.Load(filename.c_str()))) {
95 cout << "Archive file not found\n";
99 // Need to create the managers
100 GraphicsContext::Instantiate();
101 TextureManager::Instantiate();
102 DynamicTextureManager::Instantiate();
103 SceneManager::Instantiate();
105 unsigned int iNumFiles;
106 if (!SKYFAILED(archive.GetInfo("CloudFile", STRING_TYPE, &iNumFiles)))
108 for (unsigned int i = 0; i < iNumFiles; ++i)
110 FAIL_RETURN(archive.FindString("CloudFile", &pFilename, i));
\r
112 FAIL_RETURN(archive.FindFloat32("CloudScale", &rScale, i));
114 SkyArchive cloudArchive;
115 FAIL_RETURN(cloudArchive.Load(pFilename));
116 FAIL_RETURN(SceneManager::InstancePtr()->LoadClouds(cloudArchive, rScale));
121 pLight->SetPosition(Vec3f(0, 0, 7000));
122 pLight->SetDirection(dir);
123 pLight->SetAmbient(Vec4f( 0.0f, 0.0f, 0.0f, 0.0f));
124 pLight->SetDiffuse(Vec4f(1.0f, 1.0f, 1.0f, 0.0f));
125 //pLight->SetDiffuse(Vec4f(0.0f, 0.0f, 0.0f, 0.0f));
126 //pLight->SetSpecular(Vec4f(1.0f, 1.0f, 1.0f, 0.0f));
129 pLight->SetAttenuation(1.0f, 0.0f, 0.0f);
130 SceneManager::InstancePtr()->AddLight(pLight);
132 SceneManager::InstancePtr()->ShadeClouds();
137 void SkySceneLoader::Set_Cloud_Orig( Point3D *posit )
138 { // use this to adjust camera position for a new tile center
140 origin = *posit; // set origin to current tile center
141 printf("Cloud marker %f %f %f\n", origin.x(), origin.y(), origin.z() );
145 void SkySceneLoader::Update( sgMat4 viewmat, Point3D *posit )
146 //void SkySceneLoader::Update()
148 offset = *posit - origin;
149 cout << "X: " << offset.x() << "Y: " << offset.y() << "Z: " << offset.z() << endl;
151 SceneManager::InstancePtr()->Update(*pCam);
153 // need some scheme to reshade selected clouds a few at a time to save frame rate cycles
154 ///SceneManager::InstancePtr()->ShadeClouds();
158 void SkySceneLoader::Resize( double w, double h )
161 pCam->Perspective( (float) h, (float) (w / h), 0.5, 120000.0 );
165 void SkySceneLoader::Draw()
166 { // this is a clone of the plib ssgCullAndDraw except there is no scene graph
167 if ( _ssgCurrentContext == NULL )
169 cout<< "ssg: No Current Context: Did you forgot to call ssgInit()?" ; char x; cin >> x;
172 //ssgForceBasicState () ;
176 //glMatrixMode ( GL_PROJECTION );
178 //_ssgCurrentContext->loadProjectionMatrix ();
179 // test/debug section
181 //_ssgCurrentContext->getProjectionMatrix( test );
183 printf( "\nFG Projection matrix\n" );
184 cout << test[0][0] << " " << test[1][0] << " " << test[2][0] << " " << test[3][0] << endl;
185 cout << test[0][1] << " " << test[1][1] << " " << test[2][1] << " " << test[3][1] << endl;
186 cout << test[0][2] << " " << test[1][2] << " " << test[2][2] << " " << test[3][2] << endl;
187 cout << test[0][3] << " " << test[1][3] << " " << test[2][3] << " " << test[3][3] << endl;
193 // this is the cameraview matrix used by flightgear to render scene
194 // need to play with this to build a new matrix that accounts for tile crossings
195 // for now it resets the clouds when a boundary is crossed
196 _ssgCurrentContext->getModelviewMatrix( m );
198 ///pCam->GetProjectionMatrix( (float *) pm );
199 //sgCopyMat4( test, m );
200 /*printf( "\nSkyworks Projection matrix\n" );
201 cout << test[0][0] << " " << test[1][0] << " " << test[2][0] << " " << test[3][0] << endl;
202 cout << test[0][1] << " " << test[1][1] << " " << test[2][1] << " " << test[3][1] << endl;
203 cout << test[0][2] << " " << test[1][2] << " " << test[2][2] << " " << test[3][2] << endl;
204 cout << test[0][3] << " " << test[1][3] << " " << test[2][3] << " " << test[3][3] << endl;
206 glMatrixMode ( GL_MODELVIEW ) ;
208 glLoadMatrixf( (float *) pm );
210 //sgCopyMat4( test, m );
212 pCam->SetModelviewMatrix( (float *) pm );
214 //printf( "\nFG modelview matrix\n" );
215 //cout << test[0][0] << " " << test[1][0] << " " << test[2][0] << " " << test[3][0] << endl;
216 //cout << test[0][1] << " " << test[1][1] << " " << test[2][1] << " " << test[3][1] << endl;
217 //cout << test[0][2] << " " << test[1][2] << " " << test[2][2] << " " << test[3][2] << endl;
218 //cout << test[0][3] << " " << test[1][3] << " " << test[2][3] << " " << test[3][3] << endl;
222 //_ssgCurrentContext->cull(r) ;
225 SceneManager::InstancePtr()->Update(*pCam);
226 SceneManager::InstancePtr()->Display(*pCam);
228 //pLight->Display(); // draw the light position to debug with sun position
230 glMatrixMode ( GL_MODELVIEW ) ;