Skip to content

Commit

Permalink
Address macos vis bugs with resizing and paths (PR #1823)
Browse files Browse the repository at this point in the history
* FLEETSW-3173: resizing viz causes incorrect display on macos
* FLEETSW-3174: relative paths fail for pcap replay on macos

The resizing bug is due to how polling for events works on macos,
explained here: glfw/glfw#408. This adds extra
rendering updates inside the window resize callback, as suggested in the
thread.

The relative path bug was, surprisingly also due to some weird glfw3
behavior: only on macos, initializing the library changes the working
directory. This adds an init hint to prevent that behavior.

Approved-by: Kairen Wong
Approved-by: Chris Bayruns
  • Loading branch information
Dima Garbuzov committed Jun 8, 2021
1 parent 62accda commit 58b871d
Showing 1 changed file with 59 additions and 46 deletions.
105 changes: 59 additions & 46 deletions ouster_viz/src/point_viz.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -133,18 +133,66 @@ struct PointViz::impl {
initialized(false) {}
};


static void draw(PointViz::impl& pimpl) {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// draw images
if (pimpl.image.enabled) {
pimpl.image.draw(pimpl.camera);
} else {
pimpl.camera.setOffset(0, 0);
}

pimpl.camera.update();

// draw point clouds
glUseProgram(pimpl.point_program_id);
for (auto& cloud : pimpl.clouds) {
if (!cloud.enabled) continue;
cloud.draw(pimpl.camera, pimpl.cloud_ids, pimpl.palette_texture_id);
}

// draw rings
if (pimpl.rings.enabled) {
pimpl.rings.draw(pimpl.camera);
}

glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_DST_ALPHA);
glBlendEquation(GL_FUNC_ADD);

// enable culling
glEnable(GL_CULL_FACE);
glCullFace(GL_FRONT);

// draw cuboids
if (pimpl.cuboids.enabled) {
pimpl.cuboids.draw(pimpl.camera);
}
glDisable(GL_BLEND);
// enable culling
glDisable(GL_CULL_FACE);

// Swap buffers
glfwSwapBuffers(pimpl.window);
}

/**
* callback for resizing the window. Should be called automatically by GLFW.
*
* @param window pointer to GLFW window
* @param width window width in pixels
* @param height window height in pixels
*/
static void updateWindowSize(GLFWwindow* window, int width, int height) {
impl::window_width = width;
impl::window_height = height;
static void updateFBSize(GLFWwindow* window, int fb_width, int fb_height) {
impl::window_width = fb_width;
impl::window_height = fb_height;
impl::window_to_viz[window]->pimpl->camera.update();
glViewport(0, 0, width, height);
glViewport(0, 0, fb_width, fb_height);
#ifdef __APPLE__
// glfwPollEvents blocks during resize. Keep rending to avoid artifacts
draw(*impl::window_to_viz[window]->pimpl);
#endif
}

/**
Expand Down Expand Up @@ -482,6 +530,11 @@ PointViz::~PointViz() {

bool PointViz::initialize() {
glfwSetErrorCallback(error_callback);

// avoid chdir to resources dir on macos
#ifdef __APPLE__
glfwInitHint(GLFW_COCOA_CHDIR_RESOURCES, GLFW_FALSE);
#endif
if (!glfwInit()) {
std::cerr << "Failed to initialize GLFW" << std::endl;
return false;
Expand All @@ -504,7 +557,7 @@ bool PointViz::initialize() {
glfwTerminate();
return false;
}
glfwSetWindowSizeCallback(pimpl->window, updateWindowSize);
glfwSetFramebufferSizeCallback(pimpl->window, updateFBSize);
glfwMakeContextCurrent(pimpl->window);

if (glewInit() != GLEW_OK) {
Expand Down Expand Up @@ -561,47 +614,7 @@ void PointViz::drawLoop() {
// we should set the opengl context to be current
glfwMakeContextCurrent(pimpl->window);
do {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// draw images
if (pimpl->image.enabled) {
pimpl->image.draw(pimpl->camera);
} else {
pimpl->camera.setOffset(0, 0);
}

pimpl->camera.update();

// draw point clouds
glUseProgram(pimpl->point_program_id);
for (auto& cloud : pimpl->clouds) {
if (!cloud.enabled) continue;
cloud.draw(pimpl->camera, pimpl->cloud_ids,
pimpl->palette_texture_id);
}

// draw rings
if (pimpl->rings.enabled) {
pimpl->rings.draw(pimpl->camera);
}

glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_DST_ALPHA);
glBlendEquation(GL_FUNC_ADD);

// enable culling
glEnable(GL_CULL_FACE);
glCullFace(GL_FRONT);

// draw cuboids
if (pimpl->cuboids.enabled) {
pimpl->cuboids.draw(pimpl->camera);
}
glDisable(GL_BLEND);
// enable culling
glDisable(GL_CULL_FACE);

// Swap buffers
glfwSwapBuffers(pimpl->window);
draw(*pimpl);
glfwPollEvents();
} while (!quit && glfwWindowShouldClose(pimpl->window) == 0);
glDeleteProgram(pimpl->point_program_id);
Expand Down

0 comments on commit 58b871d

Please sign in to comment.