Ejemplo n.º 1
0
    bool OgreMeshResource::SetData(Foundation::AssetPtr source)
    {
        if (!source)
        {
            OgreRenderingModule::LogError("Null source asset data pointer");     
            return false;
        }
        if (!source->GetSize())
        {
            OgreRenderingModule::LogError("Zero sized mesh asset");     
            return false;
        }
                
        try
        {
            if (ogre_mesh_.isNull())
            {   
                ogre_mesh_ = Ogre::MeshManager::getSingleton().createManual(
                    id_, Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME);
                if (ogre_mesh_.isNull())
                {
                    OgreRenderingModule::LogError("Failed to create mesh " + id_);
                    return false; 
                }   
                ogre_mesh_->setAutoBuildEdgeLists(false);
            }

            Ogre::DataStreamPtr stream(new Ogre::MemoryDataStream((void*)source->GetData(), source->GetSize(), false));
            Ogre::MeshSerializer serializer;
            serializer.importMesh(stream, ogre_mesh_.getPointer());
            
            // Generate tangents to mesh
            try
            {
                unsigned short src, dest;
                if (!ogre_mesh_->suggestTangentVectorBuildParams(Ogre::VES_TANGENT, src, dest))
                    ogre_mesh_->buildTangentVectors(Ogre::VES_TANGENT, src, dest);
            }
            catch (...) {}
            
            // Generate extremity points to submeshes, 1 should be enough
            try
            {
                for(uint i = 0; i < ogre_mesh_->getNumSubMeshes(); ++i)
                {
                    Ogre::SubMesh *smesh = ogre_mesh_->getSubMesh(i);
                    if (smesh)
                        smesh->generateExtremes(1);
                }
            }
            catch (...) {}
            
            // Assign default materials that won't complain
            original_materials_.clear();
            for (uint i = 0; i < ogre_mesh_->getNumSubMeshes(); ++i)
            {
                Ogre::SubMesh* submesh = ogre_mesh_->getSubMesh(i);
                if (submesh)
                {
                    original_materials_.push_back(submesh->getMaterialName());
                    submesh->setMaterialName("UnlitTextured");
                }
            }
            
        }
        catch (Ogre::Exception &e)
        {
            OgreRenderingModule::LogError("Failed to create mesh " + id_ + ": " + std::string(e.what()));
            RemoveMesh();
            return false;
        }

        OgreRenderingModule::LogDebug("Ogre mesh " + id_ + " created");
        return true;
    }
Ejemplo n.º 2
0
bool OgreMeshAsset::GenerateMeshData()
{
    if (ogreMesh.isNull())
        return false;
    /* NOTE: only the last error handler here returns false - first are ignored.
       This is to keep the behaviour identical to the original version which had these checks inside 
       DeserializeFromData - see https://github.com/realXtend/naali/blob/1806ea04057d447263dbd7cf66d5731c36f4d4a3/src/Core/OgreRenderingModule/OgreMeshAsset.cpp#L89
    */
    
    // Generate tangents to mesh
    try
    {
        unsigned short src, dest;
        ///\bug Crashes if called for a mesh that has null or zero vertices in the vertex buffer, or null or zero indices in the index buffer.
        if (!ogreMesh->suggestTangentVectorBuildParams(Ogre::VES_TANGENT, src, dest))
            ogreMesh->buildTangentVectors(Ogre::VES_TANGENT, src, dest);
    }
    catch(const Ogre::Exception &e)
    {
        QString what(e.what());
        // "Cannot locate an appropriate 2D texture coordinate set" is benign, see OgreLogListener::messageLogged
        const bool hideBenignOgreMessages = ( assetAPI->GetFramework()->HasCommandLineParameter("--hideBenignOgreMessages") ||
            assetAPI->GetFramework()->HasCommandLineParameter("--hide_benign_ogre_messages")); /**< @todo Remove support for the deprecated underscore version at some point. */
        if (!hideBenignOgreMessages || (hideBenignOgreMessages && !what.contains("Cannot locate an appropriate 2D texture coordinate set")))
            LogError("OgreMeshAsset::GenerateMeshData: Failed to build tangents for mesh " + this->Name() + ": " + what);
    }

    // Generate extremity points to submeshes, 1 should be enough
    try
    {
        for(unsigned short i = 0; i < ogreMesh->getNumSubMeshes(); ++i)
        {
            Ogre::SubMesh *smesh = ogreMesh->getSubMesh(i);
            if (smesh)
                smesh->generateExtremes(1);
        }
    }
    catch(const Ogre::Exception &e)
    {
        LogError("OgreMeshAsset::GenerateMeshData: Failed to generate extremity points to submeshes for mesh " + this->Name() + ": " + QString(e.what()));
    }

    try
    {
        // Assign default materials that won't complain
        if (!IsAssimpFileType())
            SetDefaultMaterial();
        // Set asset references the mesh has
        //ResetReferences();
    }
    catch(const Ogre::Exception &e)
    {
        LogError("OgreMeshAsset::GenerateMeshData: Failed to set default materials to " + this->Name() + ": " + QString(e.what()));
        Unload();
        return false;
    }

    //internal_name_ = AssetAPI::SanitateAssetRef(id_);
    //LogDebug("Ogre mesh " + this->Name().toStdString() + " created");

    return true;
}
Ejemplo n.º 3
0
AssetLoadState OgreMeshAsset::DeserializeFromData(const u8 *data_, size_t numBytes)
{
    PROFILE(OgreMeshAsset_LoadFromFileInMemory);
    assert(data_);
    if (!data_)
        return ASSET_LOAD_FAILED;
    
    // Force an unload of this data first.
    Unload();

    if (OGRE_THREAD_SUPPORT != 0)
    {
        // We can only do threaded loading from disk, and not any disk location but only from asset cache.
        // local:// refs will return empty string here and those will fall back to the non-threaded loading.
        // Do not change this to do DiskCache() as that directory for local:// refs will not be a known resource location for ogre.
        QString cacheDiskSource = assetAPI->GetAssetCache()->GetDiskSource(QUrl(Name()));
        if (!cacheDiskSource.isEmpty())
        {
            QFileInfo fileInfo(cacheDiskSource);
            std::string sanitatedAssetRef = fileInfo.fileName().toStdString(); 
            loadTicket_ = Ogre::ResourceBackgroundQueue::getSingleton().load(Ogre::MeshManager::getSingleton().getResourceType(),
                                                                             sanitatedAssetRef, OgreRenderer::OgreRenderingModule::CACHE_RESOURCE_GROUP,
                                                                             false, 0, 0, this);
            return ASSET_LOAD_PROCESSING;
        }
    }

    if (ogreMesh.isNull())
    {   
        ogreMesh = Ogre::MeshManager::getSingleton().createManual(
            OgreRenderer::SanitateAssetIdForOgre(this->Name().toStdString()), Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME);
        if (ogreMesh.isNull())
        {
            LogError("Failed to create mesh " + Name().toStdString());
            return ASSET_LOAD_FAILED; 
        }
        ogreMesh->setAutoBuildEdgeLists(false);
    }

    std::vector<u8> tempData(data_, data_ + numBytes);
#include "DisableMemoryLeakCheck.h"
    Ogre::DataStreamPtr stream(new Ogre::MemoryDataStream((void*)&tempData[0], numBytes, false));
#include "EnableMemoryLeakCheck.h"
    Ogre::MeshSerializer serializer;
    serializer.importMesh(stream, ogreMesh.getPointer()); // Note: importMesh *adds* submeshes to an existing mesh. It doesn't replace old ones.
    
    // Generate tangents to mesh
    try
    {
        unsigned short src, dest;
        ///\bug Crashes if called for a mesh that has null or zero vertices in the vertex buffer, or null or zero indices in the index buffer.
        if (!ogreMesh->suggestTangentVectorBuildParams(Ogre::VES_TANGENT, src, dest))
            ogreMesh->buildTangentVectors(Ogre::VES_TANGENT, src, dest);
    }
    catch (...) {}
    
    // Generate extremity points to submeshes, 1 should be enough
    try
    {
        for(uint i = 0; i < ogreMesh->getNumSubMeshes(); ++i)
        {
            Ogre::SubMesh *smesh = ogreMesh->getSubMesh(i);
            if (smesh)
                smesh->generateExtremes(1);
        }
    }
    catch (...) {}
        
    try
    {
        // Assign default materials that won't complain
        SetDefaultMaterial();
        // Set asset references the mesh has
        //ResetReferences();
    }
    catch (Ogre::Exception &e)
    {
        LogError("Failed to create mesh " + this->Name().toStdString() + ": " + std::string(e.what()));
        Unload();
        return ASSET_LOAD_FAILED;
    }

    //internal_name_ = SanitateAssetIdForOgre(id_);
    
    LogDebug("Ogre mesh " + this->Name().toStdString() + " created");
    return ASSET_LOAD_SUCCESFULL;
}