Skip to content

Commit

Permalink
Add support for png and ktx image files for custom skybox (fixes #877) (
Browse files Browse the repository at this point in the history
  • Loading branch information
bluemarvin authored and MortimerGoro committed Jan 10, 2019
1 parent 2aad0f1 commit a7a3472
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 3 deletions.
8 changes: 7 additions & 1 deletion app/src/main/cpp/BrowserWorld.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
46 changes: 44 additions & 2 deletions app/src/main/cpp/Skybox.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,26 @@
#include "vrb/VertexArray.h"

#include <array>
#include <list>
#include <sys/stat.h>

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<std::string> sBaseNameList = std::list<std::string>({
sPosx, sNegx, sPosy, sNegy, sPosz, sNegz
});
static const std::list<std::string> sFileExt = std::list<std::string>({
".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);
Expand All @@ -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;
}

Expand Down Expand Up @@ -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) {
Expand Down
1 change: 1 addition & 0 deletions app/src/main/cpp/Skybox.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ typedef std::shared_ptr<VRLayerCube> 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);
Expand Down

0 comments on commit a7a3472

Please sign in to comment.