diff --git a/app/src/main/cpp/BrowserWorld.cpp b/app/src/main/cpp/BrowserWorld.cpp index 88854808e..0c7829890 100644 --- a/app/src/main/cpp/BrowserWorld.cpp +++ b/app/src/main/cpp/BrowserWorld.cpp @@ -510,7 +510,13 @@ BrowserWorld::InitializeJava(JNIEnv* aEnv, jobject& aActivity, jobject& aAssetMa std::string storagePath = VRBrowser::GetStorageAbsolutePath(INJECT_SKYBOX_PATH); if (std::ifstream(storagePath)) { skyboxPath = storagePath; - extension = ".jpg"; + extension = Skybox::ValidateCustomSkyboxAndFindFileExtension(storagePath); + if (!extension.empty()) { + skyboxPath = storagePath; + VRB_DEBUG("Found custom skybox file extension: %s", extension.c_str()); + } else { + VRB_ERROR("Failed to find custom skybox files."); + } } } #if !defined(SNAPDRAGONVR) diff --git a/app/src/main/cpp/Skybox.cpp b/app/src/main/cpp/Skybox.cpp index 419f237d7..99fb8a8c7 100644 --- a/app/src/main/cpp/Skybox.cpp +++ b/app/src/main/cpp/Skybox.cpp @@ -20,11 +20,26 @@ #include "vrb/VertexArray.h" #include +#include +#include using namespace vrb; namespace crow { +static const std::string sPosx = "posx"; +static const std::string sNegx = "negx"; +static const std::string sPosy = "posy"; +static const std::string sNegy = "negy"; +static const std::string sPosz = "posz"; +static const std::string sNegz = "negz"; +static const std::list sBaseNameList = std::list({ + sPosx, sNegx, sPosy, sNegy, sPosz, sNegz +}); +static const std::list sFileExt = std::list({ + ".ktx", ".jpg", ".png" +}); + static TextureCubeMapPtr LoadTextureCube(vrb::CreationContextPtr& aContext, const std::string& aBasePath, const std::string& aExtension, GLuint targetTexture = 0) { TextureCubeMapPtr cubemap = vrb::TextureCubeMap::Create(aContext, targetTexture); @@ -35,8 +50,8 @@ static TextureCubeMapPtr LoadTextureCube(vrb::CreationContextPtr& aContext, cons cubemap->SetTextureParameter(GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE); auto path = [&](const std::string &name) { return aBasePath + "/" + name + aExtension; }; - vrb::TextureCubeMap::Load(aContext, cubemap, path("posx"), path("negx"), path("posy"), - path("negy"), path("posz"), path("negz")); + vrb::TextureCubeMap::Load(aContext, cubemap, path(sPosx), path(sNegx), path(sPosy), + path(sNegy), path(sPosz), path(sNegz)); return cubemap; } @@ -185,6 +200,33 @@ Skybox::GetRoot() const { return m.root; } +static bool +FileDoesNotExist (const std::string& aName) { + struct stat buffer; + return (stat(aName.c_str(), &buffer) != 0); +} + +std::string +Skybox::ValidateCustomSkyboxAndFindFileExtension(const std::string& aBasePath) { + for (const std::string& ext: sFileExt) { + int32_t fileCount = 0; + for (const std::string& baseName: sBaseNameList) { + const std::string file = aBasePath + "/" + baseName + ext; + if (FileDoesNotExist(file)) { + if (fileCount > 0) { + VRB_ERROR("Custom skybox file missing: %s", file.c_str()); + } + break; + } + fileCount++; + } + if (fileCount == sBaseNameList.size()) { + return ext; + } + } + + return std::string(); +} SkyboxPtr Skybox::Create(vrb::CreationContextPtr aContext, const VRLayerCubePtr& aLayer) { diff --git a/app/src/main/cpp/Skybox.h b/app/src/main/cpp/Skybox.h index 5234ffb88..d04c39243 100644 --- a/app/src/main/cpp/Skybox.h +++ b/app/src/main/cpp/Skybox.h @@ -19,6 +19,7 @@ typedef std::shared_ptr VRLayerCubePtr; class Skybox { public: + static std::string ValidateCustomSkyboxAndFindFileExtension(const std::string& aBasePath); static SkyboxPtr Create(vrb::CreationContextPtr aContext, const VRLayerCubePtr& aLayer = nullptr); void Load(const vrb::ModelLoaderAndroidPtr& aLoader, const std::string& aBasePath, const std::string& aExtension); void SetVisible(bool aVisible);