forked from KhronosGroup/ANARI-SDK
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
203 lines (168 loc) · 5.52 KB
/
main.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
// Copyright 2023-2024 The Khronos Group
// SPDX-License-Identifier: Apache-2.0
#include "anari_viewer/Application.h"
#include "anari_viewer/windows/LightsEditor.h"
#include "anari_viewer/windows/SceneSelector.h"
#include "anari_viewer/windows/Viewport.h"
// anari
#include <anari_test_scenes.h>
// std
#include <iostream>
static const bool g_true = true;
static bool g_verbose = false;
static bool g_useDefaultLayout = true;
static bool g_enableDebug = false;
static std::string g_libraryName = "environment";
static anari::Library g_debug = nullptr;
static anari::Device g_device = nullptr;
static const char *g_traceDir = nullptr;
extern const char *getDefaultUILayout();
struct AppState
{
anari_viewer::manipulators::Orbit manipulator;
anari::Device device{nullptr};
};
static void statusFunc(const void *userData,
ANARIDevice device,
ANARIObject source,
ANARIDataType sourceType,
ANARIStatusSeverity severity,
ANARIStatusCode code,
const char *message)
{
const bool verbose = userData ? *(const bool *)userData : false;
if (severity == ANARI_SEVERITY_FATAL_ERROR) {
fprintf(stderr, "[FATAL][%p] %s\n", source, message);
std::exit(1);
} else if (severity == ANARI_SEVERITY_ERROR) {
fprintf(stderr, "[ERROR][%p] %s\n", source, message);
} else if (severity == ANARI_SEVERITY_WARNING) {
fprintf(stderr, "[WARN ][%p] %s\n", source, message);
} else if (verbose && severity == ANARI_SEVERITY_PERFORMANCE_WARNING) {
fprintf(stderr, "[PERF ][%p] %s\n", source, message);
} else if (verbose && severity == ANARI_SEVERITY_INFO) {
fprintf(stderr, "[INFO ][%p] %s\n", source, message);
} else if (verbose && severity == ANARI_SEVERITY_DEBUG) {
fprintf(stderr, "[DEBUG][%p] %s\n", source, message);
}
}
static void initializeANARI()
{
auto library =
anariLoadLibrary(g_libraryName.c_str(), statusFunc, &g_verbose);
if (!library)
throw std::runtime_error("Failed to load ANARI library");
anari::Device dev = anariNewDevice(library, "default");
anari::unloadLibrary(library);
if (g_enableDebug)
anari::setParameter(dev, dev, "glDebug", true);
#ifdef USE_GLES2
anari::setParameter(dev, dev, "glAPI", "OpenGL_ES");
#else
anari::setParameter(dev, dev, "glAPI", "OpenGL");
#endif
if (g_traceDir) {
anari::setParameter(dev, dev, "traceDir", g_traceDir);
anari::setParameter(dev, dev, "traceMode", "code");
}
anari::commitParameters(dev, dev);
g_device = dev;
}
// Application definition /////////////////////////////////////////////////////
struct Application : public anari_viewer::Application
{
Application() = default;
~Application() override = default;
anari_viewer::WindowArray setupWindows() override
{
anari_viewer::ui::init();
// ANARI //
m_state.device = g_device;
// ImGui //
ImGuiIO &io = ImGui::GetIO();
io.FontGlobalScale = 1.5f;
io.IniFilename = nullptr;
if (g_useDefaultLayout)
ImGui::LoadIniSettingsFromMemory(getDefaultUILayout());
auto *viewport = new anari_viewer::windows::Viewport(g_device, "Viewport");
viewport->setManipulator(&m_state.manipulator);
auto *leditor = new anari_viewer::windows::LightsEditor(g_device);
auto *sselector = new anari_viewer::windows::SceneSelector();
sselector->setCallback([=](const char *category, const char *scene) {
try {
auto s = anari::scenes::createScene(g_device, category, scene);
anari::scenes::commit(s);
auto w = anari::scenes::getWorld(s);
viewport->setWorld(w, true);
leditor->setWorlds({w});
sselector->setScene(s);
} catch (const std::runtime_error &e) {
printf("%s\n", e.what());
}
});
anari_viewer::WindowArray windows;
windows.emplace_back(viewport);
windows.emplace_back(leditor);
windows.emplace_back(sselector);
return windows;
}
void uiFrameStart() override
{
if (ImGui::BeginMainMenuBar()) {
if (ImGui::BeginMenu("File")) {
if (ImGui::MenuItem("print ImGui ini")) {
const char *info = ImGui::SaveIniSettingsToMemory();
printf("%s\n", info);
}
ImGui::EndMenu();
}
ImGui::EndMainMenuBar();
}
}
void teardown() override
{
anari::release(m_state.device, m_state.device);
anari_viewer::ui::shutdown();
}
private:
AppState m_state;
};
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
static void printUsage()
{
std::cout << "./anariViewer [{--help|-h}]\n"
<< " [{--verbose|-v}] [{--debug|-g}]\n"
<< " [{--library|-l} <ANARI library>]\n"
<< " [{--trace|-t} <directory>]\n";
}
static void parseCommandLine(int argc, char *argv[])
{
for (int i = 1; i < argc; i++) {
std::string arg = argv[i];
if (arg == "-v" || arg == "--verbose")
g_verbose = true;
if (arg == "--help" || arg == "-h") {
printUsage();
std::exit(0);
} else if (arg == "--noDefaultLayout")
g_useDefaultLayout = false;
else if (arg == "-l" || arg == "--library")
g_libraryName = argv[++i];
else if (arg == "--debug" || arg == "-g")
g_enableDebug = true;
else if (arg == "--trace" || arg == "-t")
g_traceDir = argv[++i];
}
}
int main(int argc, char *argv[])
{
parseCommandLine(argc, argv);
initializeANARI();
if (!g_device)
return 1;
Application app;
app.run(1920, 1200, "ANARI Demo Viewer");
return 0;
}