-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathscene.cpp
335 lines (287 loc) · 8.51 KB
/
scene.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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
#include "scene.h"
#include <boost/filesystem/operations.hpp>
#include <boost/filesystem/path.hpp>
#include <boost/predef/os.h>
#include <pxr/usd/usd/primRange.h>
#include <pxr/usd/usd/references.h>
#include <pxr/usd/usdGeom/camera.h>
#include <pxr/usd/usdGeom/xformCommonAPI.h>
#include <string>
#if (BOOST_OS_WINDOWS)
# include <stdlib.h>
#elif (BOOST_OS_LINUX)
# include <unistd.h>
# include <limits.h>
#elif (BOOST_OS_MACOS)
# include <mach-o/dyld.h>
#endif
// Returns the full path to the currently running executable.
boost::filesystem::path currentPath()
{
#if (BOOST_OS_WINDOWS)
char *exePath;
if (_get_pgmptr(&exePath) != 0)
{
exePath = "";
}
#elif (BOOST_OS_LINUX)
char exePath[PATH_MAX];
ssize_t len = ::readlink("/proc/self/exe", exePath, sizeof(exePath));
if (len == -1 || len == sizeof(exePath))
{
len = 0;
}
exePath[len] = '\0';
#elif (BOOST_OS_MACOS)
char exePath[PATH_MAX];
uint32_t len = sizeof(exePath);
if (_NSGetExecutablePath(exePath, &len) != 0)
{
exePath[0] = '\0'; // buffer too small (!)
}
else
{
// resolve symlinks, ., .. if possible
char *canonicalPath = realpath(exePath, NULL);
if (canonicalPath != NULL)
{
strncpy(exePath, canonicalPath, len);
free(canonicalPath);
}
}
#endif
boost::system::error_code ec;
boost::filesystem::path path(
boost::filesystem::canonical(
exePath, boost::filesystem::current_path(), ec));
return path.parent_path();
}
Scene::Scene() :
mWidth(0),
mHeight(0),
mWon(false)
{
boost::filesystem::path current = currentPath();
boost::filesystem::path boardPath = current / "board.usda";
boost::filesystem::path switchPath = current / "switch.usda";
mStage = UsdStage::Open(boardPath.string());
UsdStageRefPtr switchStage = UsdStage::Open(switchPath.string());
mBoard = mStage->GetPrimAtPath(SdfPath("/board1"));
auto cameraPrim = mStage->GetPrimAtPath(SdfPath("/camera1"));
mCamera = UsdGeomCamera(cameraPrim).GetCamera(UsdTimeCode::Default());
// Static USD produces warning that the visibility attribute doesn't exist.
{
UsdGeomImageable imBoard(mBoard);
imBoard.CreateVisibilityAttr();
}
UsdPrimRange range(switchStage->GetPrimAtPath(SdfPath::AbsoluteRootPath()));
// Iterate everything except root.
for (auto it = ++range.begin(); it != range.end(); it++)
{
const auto& prim = *it;
UsdGeomImageable imageable(prim);
imageable.CreateVisibilityAttr();
}
srand(time(NULL));
char name[32];
for (int i=0; i<4; i++)
{
for (int j=0; j<4; j++)
{
sprintf(name, "/board1/xf%ix%i", i, j);
auto xfPrim = mStage->DefinePrim(SdfPath(name), TfToken("Xform"));
// Static USD produces warning that the visibility attribute doesn't
// exist.
UsdGeomImageable imageable(xfPrim);
imageable.CreateVisibilityAttr();
UsdGeomXformCommonAPI(xfPrim).SetTranslate(
GfVec3f(105.0f * i, 0.0f, -105.0f * j));
// Set attributes
// Number of turns
auto turnsAttr = xfPrim.CreateAttribute(
TfToken("turns"),
SdfValueTypeNames->Int,
true);
turnsAttr.Set(rand() % 2);
// Index
auto indexIAttr = xfPrim.CreateAttribute(
TfToken("indexI"),
SdfValueTypeNames->Int,
true);
indexIAttr.Set(i);
auto indexJAttr = xfPrim.CreateAttribute(
TfToken("indexJ"),
SdfValueTypeNames->Int,
true);
indexJAttr.Set(j);
// Add an object
sprintf(name, "%s/switch", name);
auto instPrim = mStage->DefinePrim(SdfPath(name));
instPrim.GetReferences().AddReference(
switchStage->GetRootLayer()->GetIdentifier(),
SdfPath("/switch1"));
}
}
GLint major = 0;
GLint minor = 0;
glGetIntegerv(GL_MAJOR_VERSION, &major);
glGetIntegerv(GL_MINOR_VERSION, &minor);
printf("OpenGL version is %i.%i\n", major, minor);
printf(
"Hydra is %s\n",
UsdImagingGL::IsEnabledHydra() ? "enabled" : "disabled");
mParams.frame = 1.0;
mParams.complexity = 1.1f;
}
void Scene::prepare(float seconds)
{
bool rotated = false;
for(const auto& prim : mBoard.GetChildren())
{
auto turnsAttr = prim.GetAttribute(TfToken("turns"));
if (!turnsAttr.IsValid())
{
continue;
}
UsdGeomXformCommonAPI xf(prim);
GfVec3d translation;
GfVec3f rotation;
GfVec3f scale;
GfVec3f pivot;
UsdGeomXformCommonAPI::RotationOrder rotOrder;
xf.GetXformVectors(
&translation,
&rotation,
&scale,
&pivot,
&rotOrder,
UsdTimeCode::Default());
int turns;
turnsAttr.Get(&turns);
float target = std::min(90.0f * turns, rotation[1] + 300.0f * seconds);
if (target > rotation[1])
{
rotation[1] = target;
xf.SetRotate(rotation);
rotated = true;
}
if (mCurrent.HasPrefix(prim.GetPath()))
{
float s = std::min(scale[0]+seconds, 1.1f);
if (scale[0] < s)
{
xf.SetScale(GfVec3f(s, s, s));
}
}
else
{
float s = std::max(scale[0]-seconds, 1.0f);
if (scale[0] > s)
{
xf.SetScale(GfVec3f(s, s, s));
}
}
if (mWon > 1)
{
double t = std::max(translation[1]-100.0*seconds, -200.0);
if (translation[1] > t)
{
translation[1] = t;
xf.SetTranslate(translation);
}
}
}
if (mWon && !rotated)
{
mWon = 2;
}
}
void Scene::draw(int width, int height)
{
mWidth = width;
mHeight = height;
auto frustum = mCamera.GetFrustum();
mRenderer.SetCameraState(
frustum.ComputeViewMatrix(),
frustum.ComputeProjectionMatrix(),
GfVec4d(0, 0, mWidth, mHeight));
// USD render.
mRenderer.Render(mBoard, mParams);
// Clear OpenGL errors. Because UsdImagingGL::TestIntersection prints them.
while (glGetError() != GL_NO_ERROR) {}
}
void Scene::click()
{
if (mCurrent.IsEmpty())
{
return;
}
auto clickedPrim = mStage->GetPrimAtPath(mCurrent);
while (clickedPrim.IsValid() && clickedPrim.GetTypeName() != "Xform")
{
clickedPrim = clickedPrim.GetParent();
}
if (!clickedPrim.IsValid())
{
return;
}
// Get index
int clickedI;
int clickedJ;
auto indexIAttr = clickedPrim.GetAttribute(TfToken("indexI"));
auto indexJAttr = clickedPrim.GetAttribute(TfToken("indexJ"));
indexIAttr.Get(&clickedI);
indexJAttr.Get(&clickedJ);
mWon = 1;
// Turn the switches of the same row and column
for(const auto& prim : mBoard.GetChildren())
{
auto turnsAttr = prim.GetAttribute(TfToken("turns"));
if (!turnsAttr.IsValid())
{
continue;
}
indexIAttr = prim.GetAttribute(TfToken("indexI"));
indexJAttr = prim.GetAttribute(TfToken("indexJ"));
int i;
int j;
indexIAttr.Get(&i);
indexJAttr.Get(&j);
int turns;
turnsAttr.Get(&turns);
if (clickedI == i || clickedJ == j)
{
turnsAttr.Set(++turns);
}
mWon = mWon & (turns%2);
}
}
void Scene::cursor(float x, float y)
{
GfVec2d size(1.0 / mWidth, 1.0 / mHeight);
// Compute pick frustum.
auto cameraFrustum = mCamera.GetFrustum();
auto frustum = cameraFrustum.ComputeNarrowedFrustum(
GfVec2d(2.0 * x - 1.0, 2.0 * (1.0-y) - 1.0),
size);
GfVec3d outHitPoint;
SdfPath outHitPrimPath;
if (mRenderer.TestIntersection(
frustum.ComputeViewMatrix(),
frustum.ComputeProjectionMatrix(),
GfMatrix4d(1.0),
mBoard,
mParams,
&outHitPoint,
&outHitPrimPath))
{
if (outHitPrimPath != mCurrent)
{
mCurrent = outHitPrimPath;
}
}
else
{
mCurrent = SdfPath();
}
}