]> git.mxchange.org Git - simgear.git/blobdiff - simgear/scene/sky/clouds3d/SkySceneLoader.cpp
Clouds3D crashes because there is no Light
[simgear.git] / simgear / scene / sky / clouds3d / SkySceneLoader.cpp
index 6ccfe80d54c24054ed99eed3cb457d0c2ff3cf94..efd5f7aadcc7fa0d13a12b13a8a7c00be3879a14 100644 (file)
  */
 
 #include <plib/ssg.h>
+#include <plib/sg.h>
+
+#include <simgear/misc/sg_path.hxx>
 #include <simgear/math/point3d.hxx>
+#include <simgear/math/polar3d.hxx>
+#include <simgear/math/vector.hxx>
 
 #include "SkySceneLoader.hpp"
 #include "SkySceneManager.hpp"
 #include "SkySceneManager.hpp"
 #include "SkyDynamicTextureManager.hpp"
 #include "SkyContext.hpp"
-//#include "SkyViewManager.hpp"
-//#include "SkyRenderableInstanceGroup.hpp" 
 #include "SkyLight.hpp"
 #include "camera.hpp"
 
 ssgLight _sky_ssgLights [ 8 ] ;
-static Point3D origin;
-Point3D offset;
-//int      _ssgFrameCounter = 0 ;
+
+sgdVec3 cam_pos;
+static sgdVec3 delta;
+Point3D origin;
+
 Camera *pCam = new Camera();
 // Need to add a light here until we figure out how to use the sun position and color
 SkyLight::SkyLightType eType = SkyLight::SKY_LIGHT_DIRECTIONAL;
-SkyLight *pLight = new SkyLight(eType);
+SkyLight *pLight = 0;
 
+// hack
+sgMat4 my_copy_of_ssgOpenGLAxisSwapMatrix =
+{
+  {  1.0f,  0.0f,  0.0f,  0.0f },
+  {  0.0f,  0.0f, -1.0f,  0.0f },
+  {  0.0f,  1.0f,  0.0f,  0.0f },
+  {  0.0f,  0.0f,  0.0f,  1.0f }
+};
 //------------------------------------------------------------------------------
 // Function              : SkySceneLoader::SkySceneLoader
 // Description     : 
@@ -76,6 +89,55 @@ SkySceneLoader::~SkySceneLoader()
 }
 
 
+
+//------------------------------------------------------------------------------
+// Function               : SkySceneLoader::Load
+// Description      :
+//------------------------------------------------------------------------------
+/**
+ * @fn SkySceneLoader::Load(std::string filename)
+ * @brief Loads a SkyWorks scene.
+ *
+ * This is a temporary fix, as it loads only limited scenes
+ *  It can however, load any number of Cloud
+ +
+ */
+//bool SkySceneLoader::Load(std::string filepath)
+bool SkySceneLoader::Load( unsigned char *data, unsigned int size, double latitude, double longitude )
+{
+    if( !pLight)
+       pLight = new SkyLight(eType);
+
+  // Need to create the managers
+  cout << "GraphicsContext::Instantiate();" << endl;
+  GraphicsContext::Instantiate();
+  cout << "  TextureManager::Instantiate();" << endl;
+  TextureManager::Instantiate();
+  cout << "  DynamicTextureManager::Instantiate();" << endl;
+  DynamicTextureManager::Instantiate();
+  cout << "  SceneManager::Instantiate();" << endl;
+  SceneManager::Instantiate();
+
+  float rScale = 40.0;
+  FAIL_RETURN(SceneManager::InstancePtr()->LoadClouds(data, size, rScale, latitude, longitude));
+
+  Vec3f dir(0, 0, 1);
+  pLight->SetPosition(Vec3f(0, 0, 17000));
+  pLight->SetDirection(dir);
+  pLight->SetAmbient(Vec4f( 0.0f, 0.0f, 0.0f, 0.0f));
+  pLight->SetDiffuse(Vec4f(1.0f, 1.0f, 1.0f, 0.0f));
+  //pLight->SetDiffuse(Vec4f(0.0f, 0.0f, 0.0f, 0.0f));
+  //pLight->SetSpecular(Vec4f(1.0f, 1.0f, 1.0f, 0.0f));
+   // No attenuation
+  pLight->SetAttenuation(1.0f, 0.0f, 0.0f);
+  SceneManager::InstancePtr()->AddLight(pLight);
+  SceneManager::InstancePtr()->ShadeClouds();
+
+  return true;
+}
+
 //------------------------------------------------------------------------------
 // Function              : SkySceneLoader::Load
 // Description     : 
@@ -88,37 +150,58 @@ SkySceneLoader::~SkySceneLoader()
  *  It can however, load any number of Cloud
  +
  */ 
-bool SkySceneLoader::Load(std::string filename)
+//bool SkySceneLoader::Load(std::string filepath)
+bool SkySceneLoader::Load( SGPath filename, double latitude, double longitude )
 { 
   SkyArchive archive;
+  SGPath base = filename.dir();
   if (SKYFAILED(archive.Load(filename.c_str()))) {
-       cout << "Archive file not found\n";
-    return false; }
+      cout << "Archive file not found\n" <<  filename.c_str();
+      return false;
+  }
+    
   char *pFilename;
   
   // Need to create the managers
+  cout << "GraphicsContext::Instantiate();" << endl;
   GraphicsContext::Instantiate();
+  cout << "  TextureManager::Instantiate();" << endl;
   TextureManager::Instantiate();
+  cout << "  DynamicTextureManager::Instantiate();" << endl;
   DynamicTextureManager::Instantiate();
+  cout << "  SceneManager::Instantiate();" << endl;
   SceneManager::Instantiate();
 
   unsigned int iNumFiles;
   if (!SKYFAILED(archive.GetInfo("CloudFile", STRING_TYPE, &iNumFiles)))
   {
+    iNumFiles = ulEndianLittle32(iNumFiles);
     for (unsigned int i = 0; i < iNumFiles; ++i)
     {
-      FAIL_RETURN(archive.FindString("CloudFile", &pFilename, i));\r  
-      float rScale = 1.0;
-      FAIL_RETURN(archive.FindFloat32("CloudScale", &rScale, i));
-      rScale = 5.0;
+      FAIL_RETURN(archive.FindString("CloudFile", &pFilename, i));
+      //  this is where we have to append the $fg_root string to the filename
+      base.append( pFilename );
+      const char *FilePath = base.c_str();
+     
+      //float rScale = 1.0;
+      //FAIL_RETURN(archive.FindFloat32("CloudScale", &rScale, i));
+      float rScale = 40.0;
       SkyArchive cloudArchive;
-      FAIL_RETURN(cloudArchive.Load(pFilename));
-      FAIL_RETURN(SceneManager::InstancePtr()->LoadClouds(cloudArchive, rScale)); 
+      cout << "Calling cloudArchive.Load(FilePath)" << endl;
+      FAIL_RETURN(cloudArchive.Load(FilePath));
+      cout << "Calling SceneManager::InstancePtr()->LoadClouds" << endl;
+      FAIL_RETURN(SceneManager::InstancePtr()->LoadClouds(cloudArchive, rScale, latitude, longitude)); 
     }
   }
+  cout << "After Load Clouds" << endl;
   
   Vec3f dir(0, 0, 1);
-  pLight->SetPosition(Vec3f(0, 0, 7000));
+  if( !pLight)
+      pLight = new SkyLight(eType);
+
+  pLight->SetPosition(Vec3f(0, 0, 17000));
   pLight->SetDirection(dir);
   pLight->SetAmbient(Vec4f( 0.0f, 0.0f, 0.0f, 0.0f));
   pLight->SetDiffuse(Vec4f(1.0f, 1.0f, 1.0f, 0.0f));
@@ -127,31 +210,41 @@ bool SkySceneLoader::Load(std::string filename)
   
    // No attenuation
   pLight->SetAttenuation(1.0f, 0.0f, 0.0f);
+  cout << "Before SceneManager::InstancePtr()->AddLight(pLight)" << endl;
   SceneManager::InstancePtr()->AddLight(pLight);
   
+  cout << "Before SceneManager::InstancePtr()->ShadeClouds()" << endl;
   SceneManager::InstancePtr()->ShadeClouds();
+  cout << "After SceneManager::InstancePtr()->ShadeClouds()" << endl;
  
   return true;
 }
 
 void SkySceneLoader::Set_Cloud_Orig( Point3D *posit )
-{ // use this to adjust camera position for a new tile center
-
-       origin = *posit; // set origin to current tile center
-       printf("Cloud marker %f %f %f\n", origin.x(), origin.y(), origin.z() );
+{ 
+       // set origin for cloud coordinates to initial tile center
+       origin = *posit;
+       sgdSetVec3( delta, origin[0], origin[1], origin[2]);    
+       //printf("Cloud marker %f %f %f\n", origin[0], origin[1], origin[2] );
        
 }
 
-void SkySceneLoader::Update( sgMat4  viewmat, Point3D *posit )
-//void SkySceneLoader::Update()
+void SkySceneLoader::Update( double *view_pos )
 {
-       offset = *posit - origin;
-       cout << "X: " << offset.x() << "Y: " <<  offset.y() << "Z: " << offset.z() << endl;
+       
+       double wind_x, wind_y, wind_z;
+       wind_x = 0.0; wind_z =  0.0;
+       // just a dumb test to see what happens if we can move the clouds en masse via the camera
+       delta[0] += wind_x; delta[2] += wind_z;
+       
+       sgdSubVec3( cam_pos, view_pos, delta );
+       //cout << "ORIGIN: " << delta[0] << " " << delta[1] << " " << delta[2] << endl;
+       //cout << "CAM   : " << cam_pos[0] << " " << cam_pos[1] << " "  << cam_pos[2] << endl;
        
        SceneManager::InstancePtr()->Update(*pCam);
        
        // need some scheme to reshade selected clouds a few at a time to save frame rate cycles
-       ///SceneManager::InstancePtr()->ShadeClouds();
+       // SceneManager::InstancePtr()->ShadeClouds();
        
 }
 
@@ -162,71 +255,24 @@ void SkySceneLoader::Resize(  double w, double h )
  
 }
 
-void SkySceneLoader::Draw()
-{ // this is a clone of the plib ssgCullAndDraw except there is no scene graph
-       if ( _ssgCurrentContext == NULL )
-  {
-    cout<< "ssg: No Current Context: Did you forgot to call ssgInit()?" ; char x; cin >> x;
-  }
-  
-  //ssgForceBasicState () ;
-  
-  sgMat4 test;
-
-  //glMatrixMode ( GL_PROJECTION );
-  //glLoadIdentity();
-  //_ssgCurrentContext->loadProjectionMatrix ();
-  // test/debug section
-  //_ssgCurrentContext->getProjectionMatrix( test );
-  /*
-  printf( "\nFG Projection matrix\n" );
-  cout << test[0][0] << " " << test[1][0] << " " << test[2][0] << " " << test[3][0] << endl;
-  cout << test[0][1] << " " << test[1][1] << " " << test[2][1] << " " << test[3][1] << endl;
-  cout << test[0][2] << " " << test[1][2] << " " << test[2][2] << " " << test[3][2] << endl;
-  cout << test[0][3] << " " << test[1][3] << " " << test[2][3] << " " << test[3][3] << endl;
-  */
-       sgMat4 m, *pm;
-       sgVec3 temp;
-       pm = &m;
-       
-        // this is the cameraview matrix used by flightgear to render scene
-  // need to play with this to build a new matrix that accounts for tile crossings
-  // for now it resets the clouds when a boundary is crossed
-        _ssgCurrentContext->getModelviewMatrix( m );
-       
-       ///pCam->GetProjectionMatrix( (float *) pm );
-       //sgCopyMat4( test, m );
-       /*printf( "\nSkyworks Projection matrix\n" );
-       cout << test[0][0] << " " << test[1][0] << " " << test[2][0] << " " << test[3][0] << endl;
-  cout << test[0][1] << " " << test[1][1] << " " << test[2][1] << " " << test[3][1] << endl;
-  cout << test[0][2] << " " << test[1][2] << " " << test[2][2] << " " << test[3][2] << endl;
-  cout << test[0][3] << " " << test[1][3] << " " << test[2][3] << " " << test[3][3] << endl;
-   */  
-  glMatrixMode ( GL_MODELVIEW ) ;
-  glLoadIdentity () ;
-  glLoadMatrixf( (float *) pm );
-  
-  //sgCopyMat4( test, m );
+void SkySceneLoader::Draw( sgMat4 mat )
+{
+    sgMat4 cameraMatrix;
 
-       pCam->SetModelviewMatrix( (float *) pm );
- //printf( "\nFG modelview matrix\n" );
-  //cout << test[0][0] << " " << test[1][0] << " " << test[2][0] << " " << test[3][0] << endl;
-  //cout << test[0][1] << " " << test[1][1] << " " << test[2][1] << " " << test[3][1] << endl;
-  //cout << test[0][2] << " " << test[1][2] << " " << test[2][2] << " " << test[3][2] << endl;
-  //cout << test[0][3] << " " << test[1][3] << " " << test[2][3] << " " << test[3][3] << endl;
-       
-       //pCam->Print();
-       
-  //_ssgCurrentContext->cull(r) ;
-  //_ssgDrawDList () ;
+    // sgCopyMat4(cameraMatrix,mat);    
+    // or just 
+    ssgGetModelviewMatrix(cameraMatrix);
 
-       SceneManager::InstancePtr()->Display(*pCam);
+    glMatrixMode ( GL_MODELVIEW ) ;
+    glLoadIdentity () ;
+    glLoadMatrixf( (float *) cameraMatrix );
+  
+    pCam->SetModelviewMatrix( (float *) cameraMatrix );
+  
+    SceneManager::InstancePtr()->Display(*pCam);
        
-       //pLight->Display(); // draw the light position to  debug with sun position
+    //pLight->Display(); // draw the light position to  debug with sun position
 
-  glMatrixMode ( GL_MODELVIEW ) ;
-  glLoadIdentity () ;
-
-}
\ No newline at end of file
+    glMatrixMode ( GL_MODELVIEW ) ;
+    glLoadIdentity () ;
+}