Skip to content

Commit

Permalink
separated rendering and imgui
Browse files Browse the repository at this point in the history
  • Loading branch information
Mario Botsch committed Oct 5, 2017
1 parent ca84897 commit fa8b3bb
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 50 deletions.
55 changes: 55 additions & 0 deletions src/apps/MeshProcessingViewer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
#include <pmp/algorithms/SurfaceSimplification.h>
#include <pmp/algorithms/SurfaceRemeshing.h>

#include <imgui.h>

using namespace pmp;

//=============================================================================
Expand Down Expand Up @@ -110,3 +112,56 @@ void MeshProcessingViewer::keyboard(GLFWwindow* window, int key, int scancode, i
}
}
}

//----------------------------------------------------------------------------

void MeshProcessingViewer::processImGUI()
{
MeshViewer::processImGUI();

#ifndef __EMSCRIPTEN__
ImGui::SetNextWindowSize(ImVec2(0,0));
#endif
ImGui::Begin("Mesh Processing");

if (ImGui::Button("Decimate"))
{
SurfaceSimplification ss(m_mesh);
ss.initialize(5); // aspect ratio
ss.simplify(m_mesh.nVertices() * 0.1);
updateMesh();
}

if (ImGui::Button("Subdivision"))
{
if (m_mesh.isTriangleMesh())
SurfaceSubdivision(m_mesh).loop();
else
SurfaceSubdivision(m_mesh).catmullClark();
updateMesh();
}

if (ImGui::Button("Adaptive Remeshing"))
{
auto bb = m_mesh.bounds().size();
SurfaceRemeshing(m_mesh).adaptiveRemeshing(
0.001 * bb, // min length
1.0 * bb, // max length
0.001 * bb); // approx. error
updateMesh();
}

if (ImGui::Button("Uniform Remeshing"))
{
Scalar l(0);
for (auto eit : m_mesh.edges())
l += distance(m_mesh.position(m_mesh.vertex(eit, 0)),
m_mesh.position(m_mesh.vertex(eit, 1)));
l /= (Scalar)m_mesh.nEdges();
SurfaceRemeshing(m_mesh).uniformRemeshing(l);
updateMesh();
}

ImGui::End();
}

5 changes: 4 additions & 1 deletion src/apps/MeshProcessingViewer.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,10 @@ class MeshProcessingViewer : public pmp::MeshViewer
protected:
//! this function handles keyboard events
void keyboard(GLFWwindow* window, int key, int scancode,
int action, int mod);
int action, int mod) override;

//! draw the scene in different draw modes
virtual void processImGUI() override;
};

//=============================================================================
24 changes: 15 additions & 9 deletions src/pmp/gl/MeshViewer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,29 +97,35 @@ void MeshViewer::updateMesh()

//-----------------------------------------------------------------------------

void MeshViewer::draw(const std::string& drawMode)
void MeshViewer::processImGUI()
{
// uncomment next line to showcase imgui!
//ImGui::ShowTestWindow();
//ImGui::ShowStyleEditor();

// draw GUI
ImGui::SetNextWindowPos(ImVec2(10,10));
#ifndef __EMSCRIPTEN__
#ifndef __EMSCRIPTEN__ // this does not work in emscripten
ImGui::SetNextWindowSize(ImVec2(0,0));
#endif
ImGui::Begin("Mesh Info");
ImGui::Text("#vertices: %d", (int)m_mesh.nVertices());
ImGui::Text("#faces: %d", (int)m_mesh.nFaces());

// output mesh statistics
ImGui::BulletText("%d vertices", (int)m_mesh.nVertices());
ImGui::BulletText("%d edges", (int)m_mesh.nEdges());
ImGui::BulletText("%d faces", (int)m_mesh.nFaces());

// control crease angle
ImGui::PushItemWidth(100);
ImGui::SliderFloat("Crease Angle", &m_creaseAngle, 0.0f, 180.0f, "%.0f");
ImGui::PopItemWidth();
if (m_creaseAngle != m_mesh.creaseAngle())
{
m_mesh.setCreaseAngle(m_creaseAngle);
}

ImGui::End();
}

//-----------------------------------------------------------------------------

void MeshViewer::draw(const std::string& drawMode)
{
// draw mesh
m_mesh.draw(m_projectionMatrix, m_modelviewMatrix, drawMode);
}
Expand Down
9 changes: 6 additions & 3 deletions src/pmp/gl/MeshViewer.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,14 @@ class MeshViewer : public TrackballViewer
virtual void updateMesh();

//! draw the scene in different draw modes
virtual void draw(const std::string& _draw_mode);
virtual void draw(const std::string& _draw_mode) override;

//! handle ImGUI interface
virtual void processImGUI() override;

//! this function handles keyboard events
void keyboard(GLFWwindow* window, int key, int scancode,
int action, int mod);
virtual void keyboard(GLFWwindow* window, int key, int scancode,
int action, int mod) override;

protected:
SurfaceMeshGL m_mesh; //!< the mesh
Expand Down
43 changes: 11 additions & 32 deletions src/pmp/gl/Window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,13 +116,12 @@ Window::Window(const char* title, int width, int height)
glfwSetFramebufferSizeCallback(m_window, glfwResize);

// setup imgui
init_gui();
initImGUI();
}

//-----------------------------------------------------------------------------


void Window::init_gui()
void Window::initImGUI()
{
ImGui_Init(m_window, false);

Expand Down Expand Up @@ -188,7 +187,6 @@ void Window::init_gui()
style.Colors[ImGuiCol_ModalWindowDarkening] = ImVec4(0.20f, 0.20f, 0.20f, 0.35f);
}


//-----------------------------------------------------------------------------

Window::~Window()
Expand All @@ -205,41 +203,22 @@ Window::~Window()
int Window::run()
{
#if __EMSCRIPTEN__
emscripten_set_main_loop(Window::emscripten_render_loop, 0, 1);
emscripten_set_main_loop(Window::render_frame, 0, 1);
#else
while (!glfwWindowShouldClose(m_window))
{
// prepare GUI
ImGui_NewFrame();

// draw scene
display();

// draw GUI
ImGui::Render();

// swap buffers
glfwSwapBuffers(m_window);

// handle events
//glfwPollEvents();
glfwWaitEvents();
Window::render_frame();
}
#endif

glfwDestroyWindow(m_window);

return EXIT_SUCCESS;
}


//-----------------------------------------------------------------------------


#if __EMSCRIPTEN__

void Window::emscripten_render_loop()
void Window::render_frame()
{
#if __EMSCRIPTEN__
// determine correct canvas/framebuffer size
int w, h, f;
double dw, dh;
Expand All @@ -251,9 +230,11 @@ void Window::emscripten_render_loop()
emscripten_set_canvas_size(w, h);
glfwResize(m_instance->m_window, w, h);;
}
#endif

// prepare GUI
// preapre and process ImGUI elements
ImGui_NewFrame();
m_instance->processImGUI();

// draw scene
m_instance->display();
Expand All @@ -265,12 +246,10 @@ void Window::emscripten_render_loop()
glfwSwapBuffers(m_instance->m_window);

// handle events
glfwPollEvents();
glfwWaitEvents();
//glfwPollEvents();
}

#endif


//-----------------------------------------------------------------------------

void Window::glfwError(int error, const char* description)
Expand Down
11 changes: 6 additions & 5 deletions src/pmp/gl/Window.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,7 @@ class Window
static void glfwScroll(GLFWwindow* window, double xoffset, double yoffset);
static void glfwResize(GLFWwindow* window, int width, int height);

#if __EMSCRIPTEN__
static void emscripten_render_loop();
#endif
static void render_frame();

static Window* m_instance;

Expand All @@ -89,9 +87,12 @@ class Window
//! this function is called if the window is resized
virtual void resize(GLFWwindow* window, int width, int height) = 0;

//! this function renders the ImGUI elements and handles their events
virtual void processImGUI() {}

protected:
//! setup imgui style
void init_gui();
//! setup ImGUI user interface
void initImGUI();

protected:
//! GLFW window pointer
Expand Down

0 comments on commit fa8b3bb

Please sign in to comment.