Commits

Frederic De Groef committed ca3d635

adding a .dae resource dir only one time to the resourcemanager
succesful loading of textured cube
injected pointer to logger in CMaterialConverter
log filename as static constant

Comments (0)

Files changed (6)

cologre-ng/include/ColladaDatabase.h

     void convertScene(Ogre::SceneManager* pOgreSceneManager);
 
 protected:
+    void _initLogger();  
+    void _logMessage(const std::string &_msg);         
+
+protected:
     /// the dae object
     DAE* m_pDae;
     ///the database belonging to the Dae object
     ///parses the asset tag of the database
     void parseAsset();
 
+    /// filename to use for the log file
+    static const std::string s_LogFilename;
+
     /// Ogre logger
-    Ogre::Log *m_pLogger;
+    Ogre::Log *m_pLog;
     /// Keeps track of the already added filesystem locations
     LocationsPtr m_spLocations;
 

cologre-ng/include/MaterialConverter.h

 {
 public:
     ///Constructor
-    CMaterialConverter(Ogre::Log *_logger, LocationsPtr _locations);
+    CMaterialConverter(Ogre::Log *_log, LocationsPtr _locations);
     ///Destructor
     virtual ~CMaterialConverter(void);
 
     */
     int convert(daeDatabase* pDatabase);
 
+
+protected:
+    PathBasename _getPathBasenameFromUri(xsAnyURI _uri);
+
+
+
 protected:
     ///adds COMMON techniques as techniques of the passed Ogre material
     void addTechnique_COMMON(const domProfile_COMMON::domTechniqueRef techRef, Ogre::MaterialPtr pMat);

cologre-ng/include/common.h

 #ifndef __180810_COMMON_H__
 #define __180810_COMMON_H__
 
+#include <utility>
+#include <set>
+#include <string>
 #include <boost/shared_ptr.hpp>
 
 typedef std::set<std::string>   Locations;
 typedef boost::shared_ptr<Locations>   LocationsPtr;
 
+typedef std::pair<std::string, std::string>   PathBasename;
+
 #endif 

cologre-ng/src/ColladaDatabase.cpp

 #include "ColladaDatabase.h"
 
 #include <sstream>
-
+#ifdef _DEBUG
+#include <cassert>
+#endif
 
 #include "ResourceConverter.h"
 #include "GeometryConverter.h"
 #include "ControllerConverter.h"
 #include "SceneConverter.h"
 
+//------------------------------------------------------------------------------
+const std::string CColladaDatabase::s_LogFilename("cologre_ng.log");
+//------------------------------------------------------------------------------
 CColladaDatabase::CColladaDatabase()
 {
     m_pDae = NULL;
     m_pDatabase = NULL;
 
-    m_pLogger = NULL;
+    m_pLog = NULL;
     m_spLocations = LocationsPtr(new Locations);
 }
 
 CColladaDatabase::~CColladaDatabase()
 {
-  if(m_pDae)
-  {
-    delete m_pDae;
-    m_pDae = NULL;
-  }
+    if(m_pDae)
+    {
+        delete m_pDae;
+        m_pDae = NULL;
+    }
 }
 
 int CColladaDatabase::load(std::string filename)
 {
-    m_pLogger = Ogre::LogManager::getSingletonPtr()->createLog("cologre_ng.log");
+    _initLogger();
+
+    std::stringstream s;
+    s << "Loading DAE file : " << filename;
+    _logMessage(s.str());
 
     m_pDae = new DAE();
     if(m_pDae->load(filename.c_str(), 0))
     {
         std::stringstream s;
         s << "DAE object not initialized! Probably .dae file " << filename << " not found.";
-        m_pLogger->logMessage(s.str());
+        _logMessage(s.str());
 
         return 1;
     }
+
     m_pDatabase = m_pDae->getDatabase();
     parseAsset();
     return 0;
     pResConv->convert(m_pDatabase);
     delete pResConv;
 
-    pResConv = new CMaterialConverter(m_pLogger, m_spLocations);
+    pResConv = new CMaterialConverter(m_pLog, m_spLocations);
     pResConv->convert(m_pDatabase);
     delete pResConv;
 
     if(upAxisRef->hasValue() && upAxisRef->getValue() == UPAXISTYPE_Z_UP)
       CConverter::m_zUp = true;
   }
+}  
+//-----------------------------------------------------------------------------
+void CColladaDatabase::_initLogger()
+{
+    m_pLog = Ogre::LogManager::getSingletonPtr()->createLog(s_LogFilename);
 }
-  
+//-----------------------------------------------------------------------------
+void CColladaDatabase::_logMessage(const std::string &_msg)
+{
+#ifdef _DEBUG
+    assert(m_pLog);
+#endif
+    m_pLog->logMessage(_msg);
+}

cologre-ng/src/MaterialConverter.cpp

 #include "cologre_ng_precompiled.h"
 #include "MaterialConverter.h"
 
+#ifdef _DEBUG
+#include <cassert>
+#endif
+
 #include <dae/daeSIDResolver.h>
 #include <dae/daeIDRef.h>
 #include <dom/domEffect.h>
 
 #include "Utility.h"
 
-CMaterialConverter::CMaterialConverter(Ogre::Log *_logger, LocationsPtr _locations)  : CResourceConverter()
+//-----------------------------------------------------------------------------
+CMaterialConverter::CMaterialConverter(Ogre::Log *_log, LocationsPtr _locations)  
+    :CResourceConverter()
+    ,m_spLocations(_locations)
+    ,m_pLog(_log)
 {
-
-  //Ogre::MaterialPtr pOgreMat = Ogre::MaterialManager::getSingleton().load("OgreCore.material", "Custom");
+#ifdef _DEBUG
+    assert(m_pLog);
+#endif  
+    //Ogre::MaterialPtr pOgreMat = Ogre::MaterialManager::getSingleton().load("OgreCore.material", "Custom");
 }
-
+//-----------------------------------------------------------------------------
 CMaterialConverter::~CMaterialConverter(void)
 {
 }
-
+//-----------------------------------------------------------------------------
 int CMaterialConverter::convert(daeDatabase* pDatabase)
 {
     unsigned int numElements = pDatabase->getElementCount(NULL, "material", NULL);
     }
     return 0;
 }
-
+//-----------------------------------------------------------------------------
 void CMaterialConverter::addTechnique_COMMON(const domProfile_COMMON::domTechniqueRef techRef, Ogre::MaterialPtr pMat)
 {
   Ogre::Technique* pOgreTech = pMat->createTechnique();
     pOgrePass->setSpecular(colorRef->getValue()[0], colorRef->getValue()[1], colorRef->getValue()[2],  colorRef->getValue()[3]);
   }
 }
-
+//-----------------------------------------------------------------------------
 void CMaterialConverter::convertTexture(const domCommon_color_or_texture_type_complexType::domTextureRef textureRef, Ogre::Pass* pOgrePass)
 {
-  daeElementRef elemRef;
-  domFx_sampler2D_commonRef sampler2dRef;
-  domFx_surface_commonRef surfaceRef;
-  domImage* pImg = NULL;
-  Ogre::TexturePtr pOgreTexture;
-  Ogre::TextureUnitState* pOgreTextureUnitState = NULL;
+    daeElementRef elemRef;
+    domFx_sampler2D_commonRef sampler2dRef;
+    domFx_surface_commonRef surfaceRef;
+    domImage* pImg = NULL;
+    Ogre::TexturePtr pOgreTexture;
+    Ogre::TextureUnitState* pOgreTextureUnitState = NULL;
 
-  xsNCName texName = textureRef->getTexture();
-  std::string strTarget = texName;
-  strTarget = "./" + strTarget;
-  daeSIDResolver res(textureRef->getDocument()->getDomRoot(), strTarget.c_str(), NULL);
-  elemRef = res.getElement();
+    xsNCName texName = textureRef->getTexture();
+    std::string strTarget = texName;
+    strTarget = "./" + strTarget;
+    daeSIDResolver res(textureRef->getDocument()->getDomRoot(), strTarget.c_str(), NULL);
+    elemRef = res.getElement();
 
-  domCommon_newparam_type* pNewparam = NULL;
-  if(elemRef)
-    pNewparam = daeSafeCast<domCommon_newparam_type>(&(*elemRef));
-  else
-  {
-    std::cerr << "Could not resolve to sampler2d for texture " << strTarget << std::endl;
-    return;
-  }
+    domCommon_newparam_type* pNewparam = NULL;
+    if(elemRef)
+        pNewparam = daeSafeCast<domCommon_newparam_type>(&(*elemRef));
+    else
+    {
+        std::stringstream s;
+        s << "Could not resolve to sampler2d for texture " << strTarget;
+        m_pLog->logMessage(s.str());
+        return;
+    }
 
-  sampler2dRef = pNewparam->getSampler2D();
-  strTarget = sampler2dRef->getSource()->getValue();
-  strTarget = "./" + strTarget;
-  res.setTarget(strTarget.c_str());
-  elemRef = res.getElement();
+    sampler2dRef = pNewparam->getSampler2D();
+    strTarget = sampler2dRef->getSource()->getValue();
+    strTarget = "./" + strTarget;
+    res.setTarget(strTarget.c_str());
+    elemRef = res.getElement();
 
-  if(elemRef)
-    pNewparam = daeSafeCast<domCommon_newparam_type>(&(*elemRef));
-  else
-  {
-    std::cerr << "Could not resolve to surface for sampler2d " << strTarget << std::endl;
-    return;
-  }
+    if(elemRef)
+        pNewparam = daeSafeCast<domCommon_newparam_type>(&(*elemRef));
+    else
+    {
+        std::stringstream s;
+        s << "Could not resolve to surface for sampler2d " << strTarget ;
+        m_pLog->logMessage(s.str());
+        return;
+    }
 
-  surfaceRef = pNewparam->getSurface();
-  switch(surfaceRef->getType())
-  {
-  case FX_SURFACE_TYPE_ENUM_2D:
+    surfaceRef = pNewparam->getSurface();
+    switch(surfaceRef->getType())
     {
+    case FX_SURFACE_TYPE_ENUM_2D:
+        {
 
-        // get image handle
-        daeElement *initFrom = surfaceRef->getChild("init_from");
+            // get image handle
+            daeElement *initFrom = surfaceRef->getChild("init_from");
                            
-        std::string imageFile;
-        initFrom->getCharData(imageFile);
+            std::string imageFile;
+            initFrom->getCharData(imageFile);
         
-        // find image in library
-        daeDocument *doc = initFrom->getDocument();
-        daeDatabase *db = initFrom->getDAE()->getDatabase();
+            // find image in library
+            daeDocument *doc = initFrom->getDocument();
+            daeDatabase *db = initFrom->getDAE()->getDatabase();
 
-        pImg = daeSafeCast<domImage>(db->idLookup(imageFile, doc));
+            pImg = daeSafeCast<domImage>(db->idLookup(imageFile, doc));
 
                                             
-      if(pImg)
-      {
-        domImage::domInit_fromRef initFromRef = pImg->getInit_from();
+            if(pImg)
+            {
+                domImage::domInit_fromRef initFromRef = pImg->getInit_from();
 
-        xsAnyURI imageURI = initFromRef->getValue();    
+                xsAnyURI imageURI = initFromRef->getValue();    
+                PathBasename pathBasename = _getPathBasenameFromUri(imageURI);
 
-        std::string uriPath = imageURI.getPath();
-        std::string uriScheme = imageURI.getScheme();
+                if(m_spLocations->find(pathBasename.first) == m_spLocations->end())
+                {
+                    std::stringstream s;
+                    s << "Adding location " << pathBasename.first << " resource manager";
+                    m_pLog->logMessage(s.str());
 
-        std::string qualifiedName = convertUriToPath(makeFullUri(uriScheme, uriPath));
+                    Ogre::ResourceGroupManager::getSingleton().addResourceLocation(pathBasename.first, "FileSystem", "DaeCustom");
+                }
+                else                
+                {
+                    std::stringstream s;
+                    s << "Location " << pathBasename.first << " already added to resource manager";
+                    m_pLog->logMessage(s.str());
+                }
 
+                std::stringstream s;
+                s << "Loading 2D image : " << pathBasename.second;
+                m_pLog->logMessage(s.str());
 
-        std::string basename, path;
-        Ogre::StringUtil::splitFilename(qualifiedName, basename, path);
 
+                pOgreTexture = Ogre::TextureManager::getSingleton().load(pathBasename.second, "DaeCustom", Ogre::TEX_TYPE_2D);
+                pOgreTextureUnitState = pOgrePass->createTextureUnitState(pathBasename.second, 0);
+            }
+            break;
+        }
+    default:
+        break;
+    }
 
-        //static std::string location = "";
-        //if(location != path)
-        //{
-        //    //path = convertUriToPath(path);
-        //    Ogre::ResourceGroupManager::getSingleton().addResourceLocation(path, "FileSystem", "DaeCustom");
-        //  
-        //    location = path;
-        //}
+    if(!(pOgreTexture.isNull()))
+    {
+        xsToken imgFormat;
+        domFx_surface_common_complexType::domFormatRef formatRef;
+        if(formatRef = surfaceRef->getFormat())
+        {
+            imgFormat = formatRef->getValue();
+            setImageFormat(imgFormat, pOgreTexture);
+        }
+        else if(imgFormat = pImg->getFormat())
+            setImageFormat(imgFormat, pOgreTexture);
 
-        //pOgreTexture = Ogre::TextureManager::getSingleton().load(basename, "DaeCustom", Ogre::TEX_TYPE_2D);
-        //pOgreTextureUnitState = pOgrePass->createTextureUnitState(basename, 0);
-      }
+        unsigned int imgHeight = pImg->getHeight();
+        if(imgHeight)
+            pOgreTexture->setHeight(imgHeight);
 
+        unsigned int imgWidth = pImg->getWidth();
+        if(imgWidth)
+            pOgreTexture->setHeight(imgWidth);
 
-      break;
+        setSamplerAttributes(sampler2dRef, pOgreTextureUnitState);
     }
-  default:
-    break;
-  }
-
-  if(!(pOgreTexture.isNull()))
-  {
-    xsToken imgFormat;
-    domFx_surface_common_complexType::domFormatRef formatRef;
-    if(formatRef = surfaceRef->getFormat())
-    {
-      imgFormat = formatRef->getValue();
-      setImageFormat(imgFormat, pOgreTexture);
-    }
-    else if(imgFormat = pImg->getFormat())
-      setImageFormat(imgFormat, pOgreTexture);
-
-    unsigned int imgHeight = pImg->getHeight();
-    if(imgHeight)
-      pOgreTexture->setHeight(imgHeight);
-
-    unsigned int imgWidth = pImg->getWidth();
-    if(imgWidth)
-      pOgreTexture->setHeight(imgWidth);
-
-    setSamplerAttributes(sampler2dRef, pOgreTextureUnitState);
-  }
-
 }
-
+//-----------------------------------------------------------------------------
 void CMaterialConverter::setImageFormat(const xsToken colladaFormat, Ogre::TexturePtr pOgreTexture)
 {
   if(colladaFormat == "A8R8G8B8")
     pOgreTexture->setFormat(Ogre::PF_A8R8G8B8);
 }
-
+//-----------------------------------------------------------------------------
 void CMaterialConverter::setSamplerAttributes(const domFx_sampler2D_commonRef sampler2dRef, Ogre::TextureUnitState *pTextureUnitState)
 {
   Ogre::FilterOptions foMinFilter = Ogre::FO_ANISOTROPIC;
   pTextureUnitState->setTextureFiltering(foMinFilter, foMagFilter, foMipFilter);
 
   //for later : maybe support texture wrapping modes, for now just leave it at the ogre default
-}
+}
+//-----------------------------------------------------------------------------
+PathBasename CMaterialConverter::_getPathBasenameFromUri(xsAnyURI _uri)
+{
+    std::string uriPath = _uri.getPath();
+    std::string uriScheme = _uri.getScheme();
+
+    std::string qualifiedName = convertUriToPath(makeFullUri(uriScheme, uriPath));
+
+
+    std::string basename, path;
+    Ogre::StringUtil::splitFilename(qualifiedName, basename, path);
+
+    return PathBasename(path, basename);
+}
+//-----------------------------------------------------------------------------

ogre-cologre-test/ColladaLoaderApp.cpp

     //
     //if(ret = db.load("../../../media/indochine.DAE"))
     //exit(-1);
-    ret = db.load("../../media/cube.dae");
+    ret = db.load("../../media/indochine.DAE");
+    //ret = db.load("../../media/cube.dae");
     //assert(ret != 0);              
 
     db.convertResources();