]> git.mxchange.org Git - simgear.git/blob - simgear/scene/sky/clouds3d/SkySceneLoader.cpp
Norman's most recent 3d clouds code tweaks.
[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 <plib/sg.h>
28 #include <simgear/math/point3d.hxx>
29 #include <simgear/math/polar3d.hxx>
30 #include <simgear/math/vector.hxx>
31
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"
39 #include "camera.hpp"
40
41 ssgLight _sky_ssgLights [ 8 ] ;
42
43 sgdVec3 cam_pos;
44 static sgdVec3 delta;
45 Point3D origin;
46
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);
51
52 // hack
53 sgMat4 my_copy_of_ssgOpenGLAxisSwapMatrix =
54 {
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 }
59 };
60 //------------------------------------------------------------------------------
61 // Function               : SkySceneLoader::SkySceneLoader
62 // Description      : 
63 //------------------------------------------------------------------------------
64 /**
65  * @fn SkySceneLoader::SkySceneLoader()
66  * @brief Constructor.
67  */ 
68 SkySceneLoader::SkySceneLoader()
69 {
70
71 }
72
73 //------------------------------------------------------------------------------
74 // Function               : SkySceneLoader::~SkySceneLoader
75 // Description      : 
76 //------------------------------------------------------------------------------
77 /**
78  * @fn SkySceneLoader::~SkySceneLoader()
79  * @brief Destructor.
80  */ 
81 SkySceneLoader::~SkySceneLoader()
82 {
83         SceneManager::Destroy();
84         DynamicTextureManager::Destroy();
85         TextureManager::Destroy();
86         GraphicsContext::Destroy();
87 }
88
89
90 //------------------------------------------------------------------------------
91 // Function               : SkySceneLoader::Load
92 // Description      : 
93 //------------------------------------------------------------------------------
94 /**
95  * @fn SkySceneLoader::Load(std::string filename)
96  * @brief Loads a SkyWorks scene.
97  * 
98  * This is a temporary fix, as it loads only limited scenes
99  *  It can however, load any number of Cloud
100  +
101  */ 
102 bool SkySceneLoader::Load(std::string filename)
103
104   SkyArchive archive;
105   cout << "SkySceneLoader::Load( " << filename << " )" << endl;
106   if (SKYFAILED(archive.Load(filename.c_str()))) {
107         cout << "Archive file not found\n";
108     return false;
109   }
110   char *pFilename;
111   
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();
121
122   unsigned int iNumFiles;
123   if (!SKYFAILED(archive.GetInfo("CloudFile", STRING_TYPE, &iNumFiles)))
124   {
125     for (unsigned int i = 0; i < iNumFiles; ++i)
126     {
127       FAIL_RETURN(archive.FindString("CloudFile", &pFilename, i));  
128       float rScale = 1.0;
129       FAIL_RETURN(archive.FindFloat32("CloudScale", &rScale, i));
130       rScale = 30.0;
131       SkyArchive cloudArchive;
132       FAIL_RETURN(cloudArchive.Load(pFilename));
133       FAIL_RETURN(SceneManager::InstancePtr()->LoadClouds(cloudArchive, rScale)); 
134     }
135   }
136   
137   Vec3f dir(0, 0, 1);
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));
144   
145    // No attenuation
146   pLight->SetAttenuation(1.0f, 0.0f, 0.0f);
147   SceneManager::InstancePtr()->AddLight(pLight);
148   
149   SceneManager::InstancePtr()->ShadeClouds();
150  
151   return true;
152 }
153
154 void SkySceneLoader::Set_Cloud_Orig( Point3D *posit )
155
156         // set origin for cloud coordinates to initial tile center
157         origin = *posit;
158         sgdSetVec3( delta, origin[0], origin[1], origin[2]);    
159         //printf("Cloud marker %f %f %f\n", origin[0], origin[1], origin[2] );
160         
161 }
162
163 void SkySceneLoader::Update( double *view_pos )
164 {
165         
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;
170         
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;
174         
175         SceneManager::InstancePtr()->Update(*pCam);
176         
177         // need some scheme to reshade selected clouds a few at a time to save frame rate cycles
178         ///SceneManager::InstancePtr()->ShadeClouds();
179         
180 }
181
182 void SkySceneLoader::Resize(  double w, double h )
183 {
184         
185   pCam->Perspective( (float) h, (float) (w / h), 0.5, 120000.0 );
186  
187 }
188
189 void SkySceneLoader::Draw( sgMat4 mat )
190 {/* need this if you want to look at FG matrix
191         if ( _ssgCurrentContext == NULL )
192   {
193     cout<< "ssg: No Current Context: Did you forgot to call ssgInit()?" ; char x; cin >> x;
194   }
195   
196   ssgForceBasicState () ;
197   */
198   sgMat4 test, m, *pm, viewmat,  cameraMatrix;
199   pm = &m;
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 ) ;
204
205   sgCopyMat4    ( cameraMatrix, my_copy_of_ssgOpenGLAxisSwapMatrix ) ;
206   sgPreMultMat4 ( cameraMatrix, viewmat ) ;
207   
208   //sgCopyMat4 ( test, cameraMatrix );
209   
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;
215         
216          // this is the cameraview matrix used by flightgear to render scene
217         //_ssgCurrentContext->getModelviewMatrix( m );
218         
219   glMatrixMode ( GL_MODELVIEW ) ;
220   glLoadIdentity () ;
221   glLoadMatrixf( (float *) cameraMatrix );
222   
223   //sgCopyMat4( test, m );
224
225         pCam->SetModelviewMatrix( (float *) cameraMatrix );
226   
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;
232
233         SceneManager::InstancePtr()->Display(*pCam);
234         
235         //pLight->Display(); // draw the light position to  debug with sun position
236
237   glMatrixMode ( GL_MODELVIEW ) ;
238   glLoadIdentity () ;
239
240 }