예제 #1
0
파일: IAsset.cpp 프로젝트: katik/naali
bool IAsset::LoadFromFile(QString filename)
{
    PROFILE(IAsset_LoadFromFile);
    filename = filename.trimmed(); ///\todo Sanitate.
    if (filename.isEmpty())   
    {
        LogDebug("LoadFromFile failed for asset \"" + Name() + "\", given file path is empty!");
        return false;
    }

    std::vector<u8> fileData;
    bool success = LoadFileToVector(filename, fileData);
    if (!success)
    {
        LogDebug("LoadFromFile failed for file \"" + filename + "\", could not read file!");
        return false;
    }

    if (fileData.size() == 0)
    {
        LogDebug("LoadFromFile failed for file \"" + filename + "\", file size was 0!");
        return false;
    }

    // Invoke the actual virtual function to load the asset.
    // Do not allow asynchronous loading due the caller of this 
    // expects the asset to be usable when this function returns.
    return LoadFromFileInMemory(&fileData[0], fileData.size(), false);
}
예제 #2
0
bool OgreMeshAsset::SerializeTo(std::vector<u8> &data, const QString &serializationParameters) const
{
    UNREFERENCED_PARAM(serializationParameters);

    if (ogreMesh.isNull())
    {
        LogWarning("OgreMeshAsset::SerializeTo: Tried to export non-existing Ogre mesh " + Name() + ".");
        return false;
    }
    try
    {
        Ogre::MeshSerializer serializer;
        QString tempFilename = assetAPI->GenerateTemporaryNonexistingAssetFilename(Name());
        // Ogre has a limitation of not supporting serialization to a file in memory, so serialize directly to a temporary file,
        // load it up and delete the temporary file.
        serializer.exportMesh(ogreMesh.get(), tempFilename.toStdString());
        bool success = LoadFileToVector(tempFilename.toStdString().c_str(), data);
        QFile::remove(tempFilename); // Delete the temporary file we used for serialization.
        return success;
    }
    catch(const std::exception &e)
    {
        LogError("OgreMeshAsset::SerializeTo: Failed to export Ogre mesh " + Name() + ": " + (e.what() ? QString(e.what()) : ""));
    }
    return false;
}
예제 #3
0
Vector<u8> ZipAssetBundle::GetSubAssetData(const String &subAssetName)
{
    /* Makes no sense to keep the whole zip file contents in memory as only
       few files could be wanted from a 100mb bundle. Additionally all asset would take 2x the memory.
       We could make this function also open the zip file and uncompress the data for every sub asset request. 
       But that would be rather pointless, not to mention slower, as we already have the unpacked individual 
       assets on disk. If the unpacking to disk changes we might need to rethink this. */

    String filePath = GetSubAssetDiskSource(subAssetName);
    if (filePath.Empty())
        return Vector<u8>();

    Vector<u8> data;
    return LoadFileToVector(filePath, data) ? data : Vector<u8>();
}
예제 #4
0
파일: IAsset.cpp 프로젝트: jarmovh/naali
AssetLoadState IAsset::LoadFromFile(QString filename)
{
    filename = filename.trimmed(); ///\todo Sanitate.
    std::vector<u8> fileData;
    bool success = LoadFileToVector(filename.toStdString().c_str(), fileData);
    if (!success)
    {
        LogDebug("LoadFromFile failed for file \"" + filename.toStdString() + "\", could not read file!");
        return ASSET_LOAD_FAILED;
    }

    if (fileData.size() == 0)
    {
        LogDebug("LoadFromFile failed for file \"" + filename.toStdString() + "\", file size was 0!");
        return ASSET_LOAD_FAILED;
    }

    // Invoke the actual virtual function to load the asset.
    return LoadFromFileInMemory(&fileData[0], fileData.size());
}
예제 #5
0
void Sky::Update()
{
    if (!urhoNode_)
        return;

    Urho3D::ResourceCache* cache = GetSubsystem<Urho3D::ResourceCache>();

    if (material_ != nullptr && textureRefListListener_->Assets().Size() < 6)
    {
        // Use material as is

        ///\todo Remove diff color setting once DefaultOgreMaterialProcessor sets it properly.
        material_->UrhoMaterial()->SetShaderParameter("MatDiffColor", Urho3D::Vector4(1, 1, 1, 1));
        material_->UrhoMaterial()->SetCullMode(Urho3D::CULL_NONE);
        urhoNode_->GetComponent<Urho3D::Skybox>()->SetMaterial(material_->UrhoMaterial());
        return;
    }

    Vector<SharedPtr<Urho3D::Image> > images(6);
    Vector<AssetPtr> textureAssets = textureRefListListener_->Assets();
    int numLoadedImages = 0;
    for(uint mi=0; mi<textureAssets.Size(); ++mi)
    {
        AssetPtr &TextureAssetPtr = textureAssets[mi];
        BinaryAsset *binaryAsset = dynamic_cast<BinaryAsset*>(TextureAssetPtr.Get());
        TextureAsset *textureAsset = dynamic_cast<TextureAsset*>(TextureAssetPtr.Get());
        if ((textureAsset || binaryAsset) && TextureAssetPtr->IsLoaded())
        {
            SharedPtr<Urho3D::Image> image = SharedPtr<Urho3D::Image>(new Urho3D::Image(GetContext()));
            Vector<u8> data;
            if (binaryAsset)
                data = binaryAsset->data;
            ///\todo Loading raw image data from disksource leaves an extra GPU texture resource unused.
            else if (!LoadFileToVector(textureAsset->DiskSource(), data))
                continue;

            Urho3D::MemoryBuffer imageBuffer(&data[0], data.Size());
            if (!image->Load(imageBuffer))
                continue;

            images[mi] = image;
            numLoadedImages++;
        }
    }

    if (numLoadedImages == 6)
    {
        // Reuse the previous cube texture if possible
        SharedPtr<Urho3D::TextureCube> textureCube = (cubeTexture_.Get() == nullptr) ? SharedPtr<Urho3D::TextureCube>(new Urho3D::TextureCube(GetContext())) : cubeTexture_.Lock();

        const Urho3D::CubeMapFace faces[6] = { Urho3D::FACE_POSITIVE_X, Urho3D::FACE_NEGATIVE_X, Urho3D::FACE_POSITIVE_Y, Urho3D::FACE_NEGATIVE_Y, Urho3D::FACE_POSITIVE_Z, Urho3D::FACE_NEGATIVE_Z };
        const int faceOrder[6] = { 3, 2, 4, 5, 0, 1 };

        for (size_t i=0 ; i<images.Size() ; ++i)
            if (images[faceOrder[i]] != nullptr)
                textureCube->SetData(faces[i], images[faceOrder[i]]);

        // Remember the created texture for tracking device loss
        cubeTexture_ = textureCube;
        SubscribeToEvent(Urho3D::E_DEVICERESET, URHO3D_HANDLER(Sky, HandleDeviceReset));

        SharedPtr<Urho3D::Material> material;
        if (material_)
            material = material_->UrhoMaterial()->Clone();
        else
            material = SharedPtr<Urho3D::Material>(new Urho3D::Material(GetContext()));
        material->SetCullMode(Urho3D::CULL_NONE);
        material->SetTechnique(0, cache->GetResource<Urho3D::Technique>("Techniques/DiffSkybox.xml"));
        material->SetTexture(Urho3D::TU_DIFFUSE, textureCube);
        ///\todo Remove diff color setting once DefaultOgreMaterialProcessor sets it properly.
        material->SetShaderParameter("MatDiffColor", Urho3D::Vector4(1, 1, 1, 1));
    
        urhoNode_->GetComponent<Urho3D::Skybox>()->SetMaterial(material);
    } 
    else
    {
        if (GetFramework()->HasCommandLineParameter("--useErrorAsset"))
        {
            urhoNode_->GetComponent<Urho3D::Skybox>()->SetMaterial(cache->GetResource<Urho3D::Material>("Materials/AssetLoadError.xml"));
        }
    }
}