-
Notifications
You must be signed in to change notification settings - Fork 215
/
Skybox.cpp
255 lines (223 loc) · 7.52 KB
/
Skybox.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
/* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 2 -*-
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "Skybox.h"
#include "VRLayer.h"
#include "VRLayerNode.h"
#include "vrb/ConcreteClass.h"
#include "vrb/Color.h"
#include "vrb/CreationContext.h"
#include "vrb/Geometry.h"
#include "vrb/Matrix.h"
#include "vrb/ModelLoaderAndroid.h"
#include "vrb/RenderState.h"
#include "vrb/RenderContext.h"
#include "vrb/TextureCubeMap.h"
#include "vrb/Toggle.h"
#include "vrb/Transform.h"
#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);
cubemap->SetTextureParameter(GL_TEXTURE_MAG_FILTER, GL_LINEAR);
cubemap->SetTextureParameter(GL_TEXTURE_MIN_FILTER, GL_LINEAR);
cubemap->SetTextureParameter(GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
cubemap->SetTextureParameter(GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
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(sPosx), path(sNegx), path(sPosy),
path(sNegy), path(sPosz), path(sNegz));
return cubemap;
}
struct Skybox::State {
vrb::CreationContextWeak context;
vrb::TogglePtr root;
VRLayerCubePtr layer;
GLuint layerTextureHandle;
vrb::TransformPtr transform;
vrb::GeometryPtr geometry;
vrb::ModelLoaderAndroidPtr loader;
std::string basePath;
std::string extension;
TextureCubeMapPtr texture;
vrb::Color tintColor;
State():
layerTextureHandle(0),
tintColor(1.0f, 1.0f, 1.0f, 1.0f)
{}
void Initialize() {
vrb::CreationContextPtr create = context.lock();
root = vrb::Toggle::Create(create);
transform = vrb::Transform::Create(create);
if (layer) {
root->AddNode(VRLayerNode::Create(create, layer));
layer->SetSurfaceChangedDelegate([=](const VRLayer& aLayer, VRLayer::SurfaceChange aChange, const std::function<void()>& aCallback) {
this->layerTextureHandle = layer->GetTextureHandle();
LoadLayer();
if (aCallback) {
aCallback();
}
});
} else {
root->AddNode(transform);
}
}
void LoadGeometry() {
LoadTask task = [=](CreationContextPtr &aContext) -> GroupPtr {
std::array<GLfloat, 24> cubeVertices{
-1.0f, 1.0f, 1.0f, // 0
-1.0f, -1.0f, 1.0f, // 1
1.0f, -1.0f, 1.0f, // 2
1.0f, 1.0f, 1.0f, // 3
-1.0f, 1.0f, -1.0f, // 4
-1.0f, -1.0f, -1.0f, // 5
1.0f, -1.0f, -1.0f, // 6
1.0f, 1.0f, -1.0f, // 7
};
std::array<GLushort, 24> cubeIndices{
0, 1, 2, 3,
3, 2, 6, 7,
7, 6, 5, 4,
4, 5, 1, 0,
0, 3, 7, 4,
1, 5, 6, 2
};
VertexArrayPtr array = VertexArray::Create(aContext);
const float kLength = 140.0f;
for (int i = 0; i < cubeVertices.size(); i += 3) {
array->AppendVertex(Vector(-kLength * cubeVertices[i], -kLength * cubeVertices[i + 1],
-kLength * cubeVertices[i + 2]));
array->AppendUV(Vector(-kLength * cubeVertices[i], -kLength * cubeVertices[i + 1],
-kLength * cubeVertices[i + 2]));
}
geometry = vrb::Geometry::Create(aContext);
geometry->SetVertexArray(array);
for (int i = 0; i < cubeIndices.size(); i += 4) {
std::vector<int> indices = {cubeIndices[i] + 1, cubeIndices[i + 1] + 1,
cubeIndices[i + 2] + 1, cubeIndices[i + 3] + 1};
geometry->AddFace(indices, indices, {});
}
RenderStatePtr state = RenderState::Create(aContext);
geometry->SetRenderState(state);
texture = LoadTextureCube(aContext, basePath, extension);
state->SetTexture(texture);
state->SetMaterial(Color(1.0f, 1.0f, 1.0f), Color(1.0f, 1.0f, 1.0f), Color(0.0f, 0.0f, 0.0f),
0.0f);
geometry->SetRenderState(state);
vrb::GroupPtr group = vrb::Transform::Create(aContext);
group->AddNode(geometry);
return group;
};
vrb::GeometryPtr oldGeometry = geometry;
LoadFinishedCallback loadedCallback = [=](GroupPtr&) {
if (geometry) {
geometry->GetRenderState()->SetTintColor(tintColor);
}
if (oldGeometry) {
oldGeometry->RemoveFromParents();
}
};
loader->RunLoadTask(transform, task, loadedCallback);
}
void LoadLayer() {
if (basePath.empty() || layerTextureHandle == 0) {
return;
}
vrb::CreationContextPtr create = context.lock();
texture = LoadTextureCube(create, basePath, extension, layerTextureHandle);
texture->Bind();
layer->SetLoaded(true);
}
};
void
Skybox::Load(const vrb::ModelLoaderAndroidPtr& aLoader, const std::string& aBasePath, const std::string& aExtension) {
if (m.basePath == aBasePath) {
return;
}
m.loader = aLoader;
m.basePath = aBasePath;
m.extension = aExtension;
if (m.layer) {
m.LoadLayer();
} else {
m.LoadGeometry();
}
}
void
Skybox::SetVisible(bool aVisible) {
m.root->ToggleAll(aVisible);
}
void
Skybox::SetTransform(const vrb::Matrix& aTransform) {
if (m.transform) {
m.transform->SetTransform(aTransform);
}
}
void
Skybox::SetTintColor(const vrb::Color &aTintColor) {
m.tintColor = aTintColor;
if (m.layer) {
m.layer->SetTintColor(aTintColor);
} else if (m.geometry) {
m.geometry->GetRenderState()->SetTintColor(aTintColor);
}
}
vrb::NodePtr
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) {
SkyboxPtr result = std::make_shared<vrb::ConcreteClass<Skybox, Skybox::State> >(aContext);
result->m.layer = aLayer;
result->m.Initialize();
return result;
}
Skybox::Skybox(State& aState, vrb::CreationContextPtr& aContext) : m(aState) {
m.context = aContext;
}
Skybox::~Skybox() {}
} // namespace crow