-
Notifications
You must be signed in to change notification settings - Fork 188
/
Copy pathGLFWDistribOSPRayWindow.cpp
435 lines (353 loc) · 11.4 KB
/
GLFWDistribOSPRayWindow.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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
// Copyright 2018 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
#include "GLFWDistribOSPRayWindow.h"
#include <mpi.h>
#include <ospray/ospray_util.h>
#include <iostream>
#include <stdexcept>
#include "imgui.h"
#include "imgui_impl_glfw.h"
#include "imgui_impl_opengl3.h"
using namespace ospray;
using namespace rkcommon::math;
GLFWDistribOSPRayWindow *GLFWDistribOSPRayWindow::activeWindow = nullptr;
static bool g_quitNextFrame = false;
WindowState::WindowState()
: quit(false),
cameraChanged(false),
fbSizeChanged(false),
spp(1),
windowSize(0),
eyePos(0.f),
lookDir(0.f),
upDir(0.f)
{}
GLFWDistribOSPRayWindow::GLFWDistribOSPRayWindow(const vec2i &windowSize,
const box3f &worldBounds,
cpp::World world,
cpp::Renderer renderer)
: windowSize(windowSize),
worldBounds(worldBounds),
world(world),
renderer(renderer)
{
MPI_Comm_rank(MPI_COMM_WORLD, &mpiRank);
MPI_Comm_size(MPI_COMM_WORLD, &mpiWorldSize);
if (mpiRank == 0) {
if (activeWindow != nullptr) {
throw std::runtime_error(
"Cannot create more than one GLFWDistribOSPRayWindow!");
}
activeWindow = this;
// initialize GLFW
if (!glfwInit()) {
throw std::runtime_error("Failed to initialize GLFW!");
}
glfwWindowHint(GLFW_SRGB_CAPABLE, GLFW_TRUE);
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
#ifdef __APPLE__
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
const char *glslVersion = "#version 150";
#else
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0);
const char *glslVersion = "#version 130";
#endif
glfwWindow = glfwCreateWindow(
windowSize.x, windowSize.y, "OSPRay Tutorial", NULL, NULL);
if (!glfwWindow) {
glfwTerminate();
throw std::runtime_error("Failed to create GLFW window!");
}
// make the window's context current
glfwMakeContextCurrent(glfwWindow);
// Setup Dear ImGui context
IMGUI_CHECKVERSION();
ImGui::CreateContext();
ImGuiIO &io = ImGui::GetIO();
io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard;
ImGui_ImplGlfw_InitForOpenGL(glfwWindow, false);
// set GLFW callbacks
glfwSetFramebufferSizeCallback(
glfwWindow, [](GLFWwindow *, int newWidth, int newHeight) {
activeWindow->reshape(vec2i{newWidth, newHeight});
});
glfwSetCursorPosCallback(glfwWindow, [](GLFWwindow *, double x, double y) {
ImGuiIO &io = ImGui::GetIO();
if (!io.WantCaptureMouse) {
activeWindow->motion(vec2f{float(x), float(y)});
}
});
glfwSetKeyCallback(
glfwWindow, [](GLFWwindow *, int key, int, int action, int) {
if (action == GLFW_PRESS) {
switch (key) {
case GLFW_KEY_G:
activeWindow->showUi = !(activeWindow->showUi);
break;
case GLFW_KEY_Q:
g_quitNextFrame = true;
break;
}
}
});
// will chain our callbacks above
ImGui_ImplGlfw_InstallCallbacks(glfwWindow);
ImGui_ImplOpenGL3_Init(glslVersion);
// set initial OpenGL state
glEnable(GL_TEXTURE_2D);
glDisable(GL_LIGHTING);
// create OpenGL frame buffer texture
glGenTextures(1, &framebufferTexture);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, framebufferTexture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
}
// OSPRay setup
// create the arcball camera model
arcballCamera = std::unique_ptr<ArcballCamera>(
new ArcballCamera(worldBounds, windowSize));
// create camera
camera = cpp::Camera("perspective");
camera.setParam("aspect", windowSize.x / float(windowSize.y));
camera.setParam("position", arcballCamera->eyePos());
camera.setParam("direction", arcballCamera->lookDir());
camera.setParam("up", arcballCamera->upDir());
camera.commit();
renderer.commit();
windowState.windowSize = windowSize;
windowState.eyePos = arcballCamera->eyePos();
windowState.lookDir = arcballCamera->lookDir();
windowState.upDir = arcballCamera->upDir();
if (mpiRank == 0) {
// trigger window reshape events with current window size
glfwGetFramebufferSize(
glfwWindow, &this->windowSize.x, &this->windowSize.y);
reshape(this->windowSize);
}
}
GLFWDistribOSPRayWindow::~GLFWDistribOSPRayWindow()
{
if (mpiRank == 0) {
ImGui_ImplOpenGL3_Shutdown();
ImGui_ImplGlfw_Shutdown();
ImGui::DestroyContext();
glfwDestroyWindow(glfwWindow);
glfwTerminate();
}
}
GLFWDistribOSPRayWindow *GLFWDistribOSPRayWindow::getActiveWindow()
{
return activeWindow;
}
cpp::World GLFWDistribOSPRayWindow::getWorld()
{
return world;
}
void GLFWDistribOSPRayWindow::setWorld(cpp::World newWorld)
{
world = newWorld;
addObjectToCommit(world.handle());
}
void GLFWDistribOSPRayWindow::resetAccumulation()
{
framebuffer.resetAccumulation();
}
void GLFWDistribOSPRayWindow::registerDisplayCallback(
std::function<void(GLFWDistribOSPRayWindow *)> callback)
{
displayCallback = callback;
}
void GLFWDistribOSPRayWindow::registerImGuiCallback(
std::function<void()> callback)
{
uiCallback = callback;
}
void GLFWDistribOSPRayWindow::mainLoop()
{
while (true) {
MPI_Bcast(&windowState, sizeof(WindowState), MPI_BYTE, 0, MPI_COMM_WORLD);
if (windowState.quit) {
break;
}
// TODO: Actually render asynchronously, if we have MPI thread multiple
// support
startNewOSPRayFrame();
waitOnOSPRayFrame();
if (mpiRank == 0) {
glfwPollEvents();
ImGui_ImplOpenGL3_NewFrame();
ImGui_ImplGlfw_NewFrame();
ImGui::NewFrame();
if (displayCallback)
displayCallback(this);
display();
windowState.quit = glfwWindowShouldClose(glfwWindow) || g_quitNextFrame;
}
}
}
void GLFWDistribOSPRayWindow::reshape(const vec2i &newWindowSize)
{
windowSize = newWindowSize;
windowState.windowSize = windowSize;
windowState.fbSizeChanged = true;
// update camera
arcballCamera->updateWindowSize(windowSize);
// reset OpenGL viewport and orthographic projection
glViewport(0, 0, windowSize.x, windowSize.y);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0, windowSize.x, 0.0, windowSize.y, -1.0, 1.0);
}
void GLFWDistribOSPRayWindow::motion(const vec2f &position)
{
static vec2f previousMouse(-1);
const vec2f mouse(position.x, position.y);
if (previousMouse != vec2f(-1)) {
const bool leftDown =
glfwGetMouseButton(glfwWindow, GLFW_MOUSE_BUTTON_LEFT) == GLFW_PRESS;
const bool rightDown =
glfwGetMouseButton(glfwWindow, GLFW_MOUSE_BUTTON_RIGHT) == GLFW_PRESS;
const bool middleDown =
glfwGetMouseButton(glfwWindow, GLFW_MOUSE_BUTTON_MIDDLE) == GLFW_PRESS;
const vec2f prev = previousMouse;
bool cameraChanged = leftDown || rightDown || middleDown;
if (leftDown) {
const vec2f mouseFrom(clamp(prev.x * 2.f / windowSize.x - 1.f, -1.f, 1.f),
clamp(prev.y * 2.f / windowSize.y - 1.f, -1.f, 1.f));
const vec2f mouseTo(clamp(mouse.x * 2.f / windowSize.x - 1.f, -1.f, 1.f),
clamp(mouse.y * 2.f / windowSize.y - 1.f, -1.f, 1.f));
arcballCamera->rotate(mouseFrom, mouseTo);
} else if (rightDown) {
arcballCamera->zoom(mouse.y - prev.y);
} else if (middleDown) {
arcballCamera->pan(vec2f(mouse.x - prev.x, prev.y - mouse.y));
}
if (cameraChanged) {
windowState.cameraChanged = true;
windowState.eyePos = arcballCamera->eyePos();
windowState.lookDir = arcballCamera->lookDir();
windowState.upDir = arcballCamera->upDir();
}
}
previousMouse = mouse;
}
void GLFWDistribOSPRayWindow::display()
{
// clock used to compute frame rate
static auto displayStart = std::chrono::high_resolution_clock::now();
if (showUi && uiCallback) {
ImGuiWindowFlags flags = ImGuiWindowFlags_AlwaysAutoResize;
ImGui::Begin(
"Tutorial Controls (press 'g' to hide / show)", nullptr, flags);
uiCallback();
ImGui::End();
}
updateTitleBar();
static bool firstFrame = true;
if (firstFrame || currentFrame.isReady()) {
// display frame rate in window title
auto displayEnd = std::chrono::high_resolution_clock::now();
auto durationMilliseconds =
std::chrono::duration_cast<std::chrono::milliseconds>(
displayEnd - displayStart);
latestFPS = 1000.f / float(durationMilliseconds.count());
// map OSPRay frame buffer, update OpenGL texture with its contents, then
// unmap
uint32_t *fb = (uint32_t *)framebuffer.map(OSP_FB_COLOR);
glBindTexture(GL_TEXTURE_2D, framebufferTexture);
glTexImage2D(GL_TEXTURE_2D,
0,
GL_RGBA,
windowSize.x,
windowSize.y,
0,
GL_RGBA,
GL_UNSIGNED_BYTE,
fb);
framebuffer.unmap(fb);
// Start new frame and reset frame timing interval start
displayStart = std::chrono::high_resolution_clock::now();
firstFrame = false;
}
// clear current OpenGL color buffer
glClear(GL_COLOR_BUFFER_BIT);
// render textured quad with OSPRay frame buffer contents
glBegin(GL_QUADS);
glTexCoord2f(0.f, 0.f);
glVertex2f(0.f, 0.f);
glTexCoord2f(0.f, 1.f);
glVertex2f(0.f, windowSize.y);
glTexCoord2f(1.f, 1.f);
glVertex2f(windowSize.x, windowSize.y);
glTexCoord2f(1.f, 0.f);
glVertex2f(windowSize.x, 0.f);
glEnd();
ImGui::Render();
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
glfwSwapBuffers(glfwWindow);
}
void GLFWDistribOSPRayWindow::startNewOSPRayFrame()
{
bool fbNeedsClear = false;
auto handles = objectsToCommit.consume();
if (!handles.empty()) {
for (auto &h : handles)
ospCommit(h);
fbNeedsClear = true;
}
if (windowState.fbSizeChanged) {
windowState.fbSizeChanged = false;
windowSize = windowState.windowSize;
framebuffer = cpp::FrameBuffer(
windowSize.x, windowSize.y, OSP_FB_SRGBA, OSP_FB_COLOR | OSP_FB_ACCUM);
camera.setParam("aspect", windowSize.x / float(windowSize.y));
camera.commit();
fbNeedsClear = true;
}
if (windowState.cameraChanged) {
windowState.cameraChanged = false;
camera.setParam("aspect", windowSize.x / float(windowSize.y));
camera.setParam("position", windowState.eyePos);
camera.setParam("direction", windowState.lookDir);
camera.setParam("up", windowState.upDir);
camera.commit();
fbNeedsClear = true;
}
if (fbNeedsClear) {
resetAccumulation();
}
currentFrame = framebuffer.renderFrame(renderer, camera, world);
}
void GLFWDistribOSPRayWindow::waitOnOSPRayFrame()
{
if (currentFrame.handle() != nullptr) {
currentFrame.wait(OSP_FRAME_FINISHED);
}
}
void GLFWDistribOSPRayWindow::addObjectToCommit(OSPObject obj)
{
objectsToCommit.push_back(obj);
}
void GLFWDistribOSPRayWindow::updateTitleBar()
{
std::stringstream windowTitle;
windowTitle << "OSPRay: " << std::setprecision(3) << latestFPS << " fps";
if (latestFPS < 2.f) {
float progress = currentFrame.progress();
windowTitle << " | ";
int barWidth = 20;
std::string progBar;
progBar.resize(barWidth + 2);
auto start = progBar.begin() + 1;
auto end = start + progress * barWidth;
std::fill(start, end, '=');
std::fill(end, progBar.end(), '_');
*end = '>';
progBar.front() = '[';
progBar.back() = ']';
windowTitle << progBar;
}
glfwSetWindowTitle(glfwWindow, windowTitle.str().c_str());
}