]> git.mxchange.org Git - simgear.git/blob - simgear/scene/sky/clouds3d/SkySceneLoader.cpp
Latest changes from John.
[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   if (SKYFAILED(archive.Load(filename.c_str()))) {
106         cout << "Archive file not found\n";
107     return false; }
108   char *pFilename;
109   
110   // Need to create the managers
111   GraphicsContext::Instantiate();
112   TextureManager::Instantiate();
113   DynamicTextureManager::Instantiate();
114   SceneManager::Instantiate();
115
116   unsigned int iNumFiles;
117   if (!SKYFAILED(archive.GetInfo("CloudFile", STRING_TYPE, &iNumFiles)))
118   {
119     for (unsigned int i = 0; i < iNumFiles; ++i)
120     {
121       FAIL_RETURN(archive.FindString("CloudFile", &pFilename, i));\r  
122       float rScale = 1.0;
123       FAIL_RETURN(archive.FindFloat32("CloudScale", &rScale, i));
124       rScale = 30.0;
125       SkyArchive cloudArchive;
126       FAIL_RETURN(cloudArchive.Load(pFilename));
127       FAIL_RETURN(SceneManager::InstancePtr()->LoadClouds(cloudArchive, rScale)); 
128     }
129   }
130   
131   Vec3f dir(0, 0, 1);
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));
138   
139    // No attenuation
140   pLight->SetAttenuation(1.0f, 0.0f, 0.0f);
141   SceneManager::InstancePtr()->AddLight(pLight);
142   
143   SceneManager::InstancePtr()->ShadeClouds();
144  
145   return true;
146 }
147
148 void SkySceneLoader::Set_Cloud_Orig( Point3D *posit )
149
150         // set origin for cloud coordinates to initial tile center
151         origin = *posit;
152         sgdSetVec3( delta, origin[0], origin[1], origin[2]);    
153         //printf("Cloud marker %f %f %f\n", origin[0], origin[1], origin[2] );
154         
155 }
156
157 void SkySceneLoader::Update( double *view_pos )
158 {
159         
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;
164         
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;
168         
169         SceneManager::InstancePtr()->Update(*pCam);
170         
171         // need some scheme to reshade selected clouds a few at a time to save frame rate cycles
172         ///SceneManager::InstancePtr()->ShadeClouds();
173         
174 }
175
176 void SkySceneLoader::Resize(  double w, double h )
177 {
178         
179   pCam->Perspective( (float) h, (float) (w / h), 0.5, 120000.0 );
180  
181 }
182
183 void SkySceneLoader::Draw( sgMat4 mat )
184 {/* need this if you want to look at FG matrix
185         if ( _ssgCurrentContext == NULL )
186   {
187     cout<< "ssg: No Current Context: Did you forgot to call ssgInit()?" ; char x; cin >> x;
188   }
189   
190   ssgForceBasicState () ;
191   */
192   sgMat4 test, m, *pm, viewmat,  cameraMatrix;
193   pm = &m;
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 ) ;
198
199   sgCopyMat4    ( cameraMatrix, my_copy_of_ssgOpenGLAxisSwapMatrix ) ;
200   sgPreMultMat4 ( cameraMatrix, viewmat ) ;
201   
202   //sgCopyMat4 ( test, cameraMatrix );
203   
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;
209         
210          // this is the cameraview matrix used by flightgear to render scene
211         //_ssgCurrentContext->getModelviewMatrix( m );
212         
213   glMatrixMode ( GL_MODELVIEW ) ;
214   glLoadIdentity () ;
215   glLoadMatrixf( (float *) cameraMatrix );
216   
217   //sgCopyMat4( test, m );
218
219         pCam->SetModelviewMatrix( (float *) cameraMatrix );
220   
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;
226
227         SceneManager::InstancePtr()->Display(*pCam);
228         
229         //pLight->Display(); // draw the light position to  debug with sun position
230
231   glMatrixMode ( GL_MODELVIEW ) ;
232   glLoadIdentity () ;
233
234 }