]> git.mxchange.org Git - simgear.git/blob - simgear/scene/sky/clouds3d/SkySceneLoader.cpp
Rolled Norm's change back out; update needs to be called earlier.
[simgear.git] / simgear / scene / sky / clouds3d / SkySceneLoader.cpp
1 //------------------------------------------------------------------------------
2 // File : SkySceneLoader.cpp
3 //------------------------------------------------------------------------------
4 // Adapted from SkyWorks for FlightGear by J. Wojnaroski -- castle@mminternet.com
5 // Copywrite July 2002
6 //
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.
11 //
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.
16 //
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.
20 /**
21  * @file SkySceneLoader.cpp
22  * 
23  * Implementation of class SkySceneLoader.
24  */
25
26 #include <plib/ssg.h>
27 #include <simgear/math/point3d.hxx>
28
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"
38 #include "camera.hpp"
39
40 ssgLight _sky_ssgLights [ 8 ] ;
41 static Point3D origin;
42 Point3D offset;
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);
48
49 //------------------------------------------------------------------------------
50 // Function               : SkySceneLoader::SkySceneLoader
51 // Description      : 
52 //------------------------------------------------------------------------------
53 /**
54  * @fn SkySceneLoader::SkySceneLoader()
55  * @brief Constructor.
56  */ 
57 SkySceneLoader::SkySceneLoader()
58 {
59
60 }
61
62 //------------------------------------------------------------------------------
63 // Function               : SkySceneLoader::~SkySceneLoader
64 // Description      : 
65 //------------------------------------------------------------------------------
66 /**
67  * @fn SkySceneLoader::~SkySceneLoader()
68  * @brief Destructor.
69  */ 
70 SkySceneLoader::~SkySceneLoader()
71 {
72         SceneManager::Destroy();
73         DynamicTextureManager::Destroy();
74         TextureManager::Destroy();
75         GraphicsContext::Destroy();
76 }
77
78
79 //------------------------------------------------------------------------------
80 // Function               : SkySceneLoader::Load
81 // Description      : 
82 //------------------------------------------------------------------------------
83 /**
84  * @fn SkySceneLoader::Load(std::string filename)
85  * @brief Loads a SkyWorks scene.
86  * 
87  * This is a temporary fix, as it loads only limited scenes
88  *  It can however, load any number of Cloud
89  +
90  */ 
91 bool SkySceneLoader::Load(std::string filename)
92
93   SkyArchive archive;
94   if (SKYFAILED(archive.Load(filename.c_str()))) {
95         cout << "Archive file not found\n";
96     return false; }
97   char *pFilename;
98   
99   // Need to create the managers
100   GraphicsContext::Instantiate();
101   TextureManager::Instantiate();
102   DynamicTextureManager::Instantiate();
103   SceneManager::Instantiate();
104
105   unsigned int iNumFiles;
106   if (!SKYFAILED(archive.GetInfo("CloudFile", STRING_TYPE, &iNumFiles)))
107   {
108     for (unsigned int i = 0; i < iNumFiles; ++i)
109     {
110       FAIL_RETURN(archive.FindString("CloudFile", &pFilename, i));\r  
111       float rScale = 1.0;
112       FAIL_RETURN(archive.FindFloat32("CloudScale", &rScale, i));
113       rScale = 5.0;
114       SkyArchive cloudArchive;
115       FAIL_RETURN(cloudArchive.Load(pFilename));
116       FAIL_RETURN(SceneManager::InstancePtr()->LoadClouds(cloudArchive, rScale)); 
117     }
118   }
119   
120   Vec3f dir(0, 0, 1);
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));
127   
128    // No attenuation
129   pLight->SetAttenuation(1.0f, 0.0f, 0.0f);
130   SceneManager::InstancePtr()->AddLight(pLight);
131   
132   SceneManager::InstancePtr()->ShadeClouds();
133  
134   return true;
135 }
136
137 void SkySceneLoader::Set_Cloud_Orig( Point3D *posit )
138 { // use this to adjust camera position for a new tile center
139
140         origin = *posit; // set origin to current tile center
141         printf("Cloud marker %f %f %f\n", origin.x(), origin.y(), origin.z() );
142         
143 }
144
145 void SkySceneLoader::Update( sgMat4  viewmat, Point3D *posit )
146 //void SkySceneLoader::Update()
147 {
148         offset = *posit - origin;
149         cout << "X: " << offset.x() << "Y: " <<  offset.y() << "Z: " << offset.z() << endl;
150         
151         SceneManager::InstancePtr()->Update(*pCam);
152         
153         // need some scheme to reshade selected clouds a few at a time to save frame rate cycles
154         ///SceneManager::InstancePtr()->ShadeClouds();
155         
156 }
157
158 void SkySceneLoader::Resize(  double w, double h )
159 {
160         
161   pCam->Perspective( (float) h, (float) (w / h), 0.5, 120000.0 );
162  
163 }
164
165 void SkySceneLoader::Draw()
166 { // this is a clone of the plib ssgCullAndDraw except there is no scene graph
167         if ( _ssgCurrentContext == NULL )
168   {
169     cout<< "ssg: No Current Context: Did you forgot to call ssgInit()?" ; char x; cin >> x;
170   }
171   
172   //ssgForceBasicState () ;
173   
174   sgMat4 test;
175
176   //glMatrixMode ( GL_PROJECTION );
177   //glLoadIdentity();
178   //_ssgCurrentContext->loadProjectionMatrix ();
179   // test/debug section
180  
181   //_ssgCurrentContext->getProjectionMatrix( test );
182   /*
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;
188   */
189         sgMat4 m, *pm;
190         sgVec3 temp;
191         pm = &m;
192         
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 );
197         
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;
205    */  
206   glMatrixMode ( GL_MODELVIEW ) ;
207   glLoadIdentity () ;
208   glLoadMatrixf( (float *) pm );
209   
210   //sgCopyMat4( test, m );
211
212         pCam->SetModelviewMatrix( (float *) pm );
213  
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;
219         
220         //pCam->Print();
221         
222   //_ssgCurrentContext->cull(r) ;
223   //_ssgDrawDList () ;
224
225   SceneManager::InstancePtr()->Display(*pCam);
226         
227   //pLight->Display(); // draw the light position to  debug with sun position
228
229   glMatrixMode ( GL_MODELVIEW ) ;
230   glLoadIdentity () ;
231
232 }