Skip to content
This repository was archived by the owner on Jul 22, 2024. It is now read-only.

Commit a7a3472

Browse files
bluemarvinMortimerGoro
authored andcommitted
Add support for png and ktx image files for custom skybox (fixes #877) (#920)
1 parent 2aad0f1 commit a7a3472

File tree

3 files changed

+52
-3
lines changed

3 files changed

+52
-3
lines changed

app/src/main/cpp/BrowserWorld.cpp

+7-1
Original file line numberDiff line numberDiff line change
@@ -510,7 +510,13 @@ BrowserWorld::InitializeJava(JNIEnv* aEnv, jobject& aActivity, jobject& aAssetMa
510510
std::string storagePath = VRBrowser::GetStorageAbsolutePath(INJECT_SKYBOX_PATH);
511511
if (std::ifstream(storagePath)) {
512512
skyboxPath = storagePath;
513-
extension = ".jpg";
513+
extension = Skybox::ValidateCustomSkyboxAndFindFileExtension(storagePath);
514+
if (!extension.empty()) {
515+
skyboxPath = storagePath;
516+
VRB_DEBUG("Found custom skybox file extension: %s", extension.c_str());
517+
} else {
518+
VRB_ERROR("Failed to find custom skybox files.");
519+
}
514520
}
515521
}
516522
#if !defined(SNAPDRAGONVR)

app/src/main/cpp/Skybox.cpp

+44-2
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,26 @@
2020
#include "vrb/VertexArray.h"
2121

2222
#include <array>
23+
#include <list>
24+
#include <sys/stat.h>
2325

2426
using namespace vrb;
2527

2628
namespace crow {
2729

30+
static const std::string sPosx = "posx";
31+
static const std::string sNegx = "negx";
32+
static const std::string sPosy = "posy";
33+
static const std::string sNegy = "negy";
34+
static const std::string sPosz = "posz";
35+
static const std::string sNegz = "negz";
36+
static const std::list<std::string> sBaseNameList = std::list<std::string>({
37+
sPosx, sNegx, sPosy, sNegy, sPosz, sNegz
38+
});
39+
static const std::list<std::string> sFileExt = std::list<std::string>({
40+
".ktx", ".jpg", ".png"
41+
});
42+
2843
static TextureCubeMapPtr LoadTextureCube(vrb::CreationContextPtr& aContext, const std::string& aBasePath,
2944
const std::string& aExtension, GLuint targetTexture = 0) {
3045
TextureCubeMapPtr cubemap = vrb::TextureCubeMap::Create(aContext, targetTexture);
@@ -35,8 +50,8 @@ static TextureCubeMapPtr LoadTextureCube(vrb::CreationContextPtr& aContext, cons
3550
cubemap->SetTextureParameter(GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
3651

3752
auto path = [&](const std::string &name) { return aBasePath + "/" + name + aExtension; };
38-
vrb::TextureCubeMap::Load(aContext, cubemap, path("posx"), path("negx"), path("posy"),
39-
path("negy"), path("posz"), path("negz"));
53+
vrb::TextureCubeMap::Load(aContext, cubemap, path(sPosx), path(sNegx), path(sPosy),
54+
path(sNegy), path(sPosz), path(sNegz));
4055
return cubemap;
4156
}
4257

@@ -185,6 +200,33 @@ Skybox::GetRoot() const {
185200
return m.root;
186201
}
187202

203+
static bool
204+
FileDoesNotExist (const std::string& aName) {
205+
struct stat buffer;
206+
return (stat(aName.c_str(), &buffer) != 0);
207+
}
208+
209+
std::string
210+
Skybox::ValidateCustomSkyboxAndFindFileExtension(const std::string& aBasePath) {
211+
for (const std::string& ext: sFileExt) {
212+
int32_t fileCount = 0;
213+
for (const std::string& baseName: sBaseNameList) {
214+
const std::string file = aBasePath + "/" + baseName + ext;
215+
if (FileDoesNotExist(file)) {
216+
if (fileCount > 0) {
217+
VRB_ERROR("Custom skybox file missing: %s", file.c_str());
218+
}
219+
break;
220+
}
221+
fileCount++;
222+
}
223+
if (fileCount == sBaseNameList.size()) {
224+
return ext;
225+
}
226+
}
227+
228+
return std::string();
229+
}
188230

189231
SkyboxPtr
190232
Skybox::Create(vrb::CreationContextPtr aContext, const VRLayerCubePtr& aLayer) {

app/src/main/cpp/Skybox.h

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ typedef std::shared_ptr<VRLayerCube> VRLayerCubePtr;
1919

2020
class Skybox {
2121
public:
22+
static std::string ValidateCustomSkyboxAndFindFileExtension(const std::string& aBasePath);
2223
static SkyboxPtr Create(vrb::CreationContextPtr aContext, const VRLayerCubePtr& aLayer = nullptr);
2324
void Load(const vrb::ModelLoaderAndroidPtr& aLoader, const std::string& aBasePath, const std::string& aExtension);
2425
void SetVisible(bool aVisible);

0 commit comments

Comments
 (0)