Fix opencsg example on Win32

This commit is contained in:
tamasmeszaros 2019-12-17 16:27:28 +01:00
parent 17de6ff51a
commit 558529146c
4 changed files with 51 additions and 33 deletions

View file

@ -2,7 +2,7 @@ cmake_minimum_required(VERSION 3.0)
project(OpenCSG-example) project(OpenCSG-example)
add_executable(opencsg_example main.cpp GLScene.hpp GLScene.cpp add_executable(opencsg_example WIN32 main.cpp GLScene.hpp GLScene.cpp
${CMAKE_CURRENT_SOURCE_DIR}/../../src/slic3r/GUI/ProgressStatusBar.cpp ${CMAKE_CURRENT_SOURCE_DIR}/../../src/slic3r/GUI/ProgressStatusBar.cpp
${CMAKE_CURRENT_SOURCE_DIR}/../../src/slic3r/GUI/I18N.hpp ${CMAKE_CURRENT_SOURCE_DIR}/../../src/slic3r/GUI/I18N.hpp
${CMAKE_CURRENT_SOURCE_DIR}/../../src/slic3r/GUI/I18N.cpp) ${CMAKE_CURRENT_SOURCE_DIR}/../../src/slic3r/GUI/I18N.cpp)
@ -11,11 +11,17 @@ find_package(wxWidgets 3.1 REQUIRED COMPONENTS core base gl html)
find_package(OpenGL REQUIRED) find_package(OpenGL REQUIRED)
find_package(GLEW REQUIRED) find_package(GLEW REQUIRED)
find_package(OpenCSG REQUIRED) find_package(OpenCSG REQUIRED)
find_package(GLUT REQUIRED) # find_package(GLUT REQUIRED)
include(${wxWidgets_USE_FILE}) include(${wxWidgets_USE_FILE})
target_link_libraries(opencsg_example libslic3r) target_link_libraries(opencsg_example libslic3r)
target_include_directories(opencsg_example PRIVATE ${wxWidgets_INCLUDE_DIRS}) target_include_directories(opencsg_example PRIVATE ${wxWidgets_INCLUDE_DIRS})
target_compile_definitions(opencsg_example PRIVATE ${wxWidgets_DEFINITIONS}) target_compile_definitions(opencsg_example PRIVATE ${wxWidgets_DEFINITIONS})
target_link_libraries(opencsg_example ${wxWidgets_LIBRARIES} GLEW::GLEW OpenCSG::opencsg GLUT::GLUT OpenGL::OpenGL -lXrandr -lXext -lX11)
target_link_libraries(opencsg_example ${wxWidgets_LIBRARIES}
OpenCSG::opencsg
GLEW::GLEW
OpenGL::GL
#-lXrandr -lXext -lX11
)

View file

@ -5,11 +5,11 @@
#include <GL/glew.h> #include <GL/glew.h>
#ifdef __APPLE__ //#ifdef __APPLE__
#include <GLUT/glut.h> //#include <GLUT/glut.h>
#else //#else
#include <GL/glut.h> //#include <GL/glut.h>
#endif //#endif
#include <boost/log/trivial.hpp> #include <boost/log/trivial.hpp>
@ -63,7 +63,7 @@ void renderfps () {
static int msec = 0; static int msec = 0;
last = msec; last = msec;
msec = glutGet(GLUT_ELAPSED_TIME); // msec = glutGet(GLUT_ELAPSED_TIME);
if (last / 1000 != msec / 1000) { if (last / 1000 != msec / 1000) {
float correctedFps = fps * 1000.0f / float(msec - ancient); float correctedFps = fps * 1000.0f / float(msec - ancient);
@ -82,9 +82,9 @@ void renderfps () {
glRasterPos2f(-1.0f, -1.0f); glRasterPos2f(-1.0f, -1.0f);
glDisable(GL_LIGHTING); glDisable(GL_LIGHTING);
std::string s = fpsStream.str(); std::string s = fpsStream.str();
for (unsigned int i=0; i<s.size(); ++i) { // for (unsigned int i=0; i<s.size(); ++i) {
glutBitmapCharacter(GLUT_BITMAP_8_BY_13, s[i]); // glutBitmapCharacter(GLUT_BITMAP_8_BY_13, s[i]);
} // }
glEnable(GL_LIGHTING); glEnable(GL_LIGHTING);
glPopMatrix(); glPopMatrix();
glMatrixMode(GL_MODELVIEW); glMatrixMode(GL_MODELVIEW);
@ -347,18 +347,21 @@ void Display::clear_screen()
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
} }
Display::~Display()
{
OpenCSG::freeResources();
}
void Display::set_active(long width, long height) void Display::set_active(long width, long height)
{ {
static int argc = 0; static int argc = 0;
if (!m_initialized) { if (!m_initialized) {
glewInit(); glewInit();
glutInit(&argc, nullptr); // glutInit(&argc, nullptr);
m_initialized = true; m_initialized = true;
} }
m_size = {width, height};
// gray background // gray background
glClearColor(0.9f, 0.9f, 0.9f, 1.0f); glClearColor(0.9f, 0.9f, 0.9f, 1.0f);
@ -380,7 +383,7 @@ void Display::set_active(long width, long height)
glEnable(GL_DEPTH_TEST); glEnable(GL_DEPTH_TEST);
glEnable(GL_STENCIL_TEST); glEnable(GL_STENCIL_TEST);
m_camera->set_screen(width, height); set_screen_size(width, height);
} }
void Display::set_screen_size(long width, long height) void Display::set_screen_size(long width, long height)
@ -389,8 +392,6 @@ void Display::set_screen_size(long width, long height)
m_camera->set_screen(width, height); m_camera->set_screen(width, height);
m_size = {width, height}; m_size = {width, height};
repaint();
} }
void Display::repaint() void Display::repaint()

View file

@ -303,6 +303,8 @@ public:
: m_camera(camera ? camera : std::make_shared<PerspectiveCamera>()) : m_camera(camera ? camera : std::make_shared<PerspectiveCamera>())
{} {}
~Display() override;
Camera * camera() { return m_camera.get(); } Camera * camera() { return m_camera.get(); }
virtual void swap_buffers() = 0; virtual void swap_buffers() = 0;
@ -370,5 +372,6 @@ public:
void move_clip_plane(double z) { call_cameras(&Camera::set_clip_z, z); } void move_clip_plane(double z) { call_cameras(&Camera::set_clip_z, z); }
}; };
}} // namespace Slic3r::GL }} // namespace Slic3r::GL
#endif // GLSCENE_HPP #endif // GLSCENE_HPP

View file

@ -31,7 +31,7 @@ using namespace Slic3r::GL;
class Canvas: public wxGLCanvas, public Slic3r::GL::Display class Canvas: public wxGLCanvas, public Slic3r::GL::Display
{ {
std::unique_ptr<wxGLContext> m_context; shptr<wxGLContext> m_context;
public: public:
void set_active(long w, long h) override void set_active(long w, long h) override
@ -54,6 +54,12 @@ public:
m_context.reset(ctx); m_context.reset(ctx);
} }
~Canvas() override
{
m_scene_cache.clear();
m_context.reset();
}
}; };
class MyFrame: public wxFrame class MyFrame: public wxFrame
@ -95,11 +101,11 @@ private:
void bind_canvas_events_to_controller(); void bind_canvas_events_to_controller();
void OnExit(wxCommandEvent& /*event*/) void OnClose(wxCloseEvent& /*event*/)
{ {
RemoveChild(m_canvas.get()); RemoveChild(m_canvas.get());
m_canvas->Destroy(); m_canvas.reset();
Close( true ); Destroy();
} }
void OnOpen(wxCommandEvent &/*evt*/) void OnOpen(wxCommandEvent &/*evt*/)
@ -119,9 +125,6 @@ private:
const wxSize ClientSize = GetClientSize(); const wxSize ClientSize = GetClientSize();
m_canvas->set_active(ClientSize.x, ClientSize.y); m_canvas->set_active(ClientSize.x, ClientSize.y);
m_canvas->set_screen_size(ClientSize.x, ClientSize.y);
m_canvas->repaint();
// Do the repaint continuously // Do the repaint continuously
Bind(wxEVT_IDLE, [this](wxIdleEvent &evt) { Bind(wxEVT_IDLE, [this](wxIdleEvent &evt) {
m_canvas->repaint(); m_canvas->repaint();
@ -181,6 +184,7 @@ MyFrame::MyFrame(const wxString &title, const wxPoint &pos, const wxSize &size):
m_ctl->add_display(m_canvas); m_ctl->add_display(m_canvas);
wxPanel *control_panel = new wxPanel(this); wxPanel *control_panel = new wxPanel(this);
auto controlsizer = new wxBoxSizer(wxHORIZONTAL); auto controlsizer = new wxBoxSizer(wxHORIZONTAL);
auto slider_sizer = new wxBoxSizer(wxVERTICAL); auto slider_sizer = new wxBoxSizer(wxVERTICAL);
auto console_sizer = new wxBoxSizer(wxVERTICAL); auto console_sizer = new wxBoxSizer(wxVERTICAL);
@ -247,7 +251,9 @@ MyFrame::MyFrame(const wxString &title, const wxPoint &pos, const wxSize &size):
SetSizer(sizer); SetSizer(sizer);
Bind(wxEVT_MENU, &MyFrame::OnOpen, this, wxID_OPEN); Bind(wxEVT_MENU, &MyFrame::OnOpen, this, wxID_OPEN);
Bind(wxEVT_MENU, &MyFrame::OnExit, this, wxID_EXIT); Bind(wxEVT_CLOSE_WINDOW, &MyFrame::OnClose, this);
Bind(wxEVT_MENU, [this](wxCommandEvent &) { Close(true); }, wxID_EXIT);
Bind(wxEVT_SHOW, &MyFrame::OnShown, this, GetId()); Bind(wxEVT_SHOW, &MyFrame::OnShown, this, GetId());
Bind(wxEVT_SLIDER, [this, slider](wxCommandEvent &) { Bind(wxEVT_SLIDER, [this, slider](wxCommandEvent &) {
@ -334,7 +340,7 @@ void MyFrame::bind_canvas_events_to_controller()
m_canvas->Bind(wxEVT_PAINT, [this](wxPaintEvent &) { m_canvas->Bind(wxEVT_PAINT, [this](wxPaintEvent &) {
// This is required even though dc is not used otherwise. // This is required even though dc is not used otherwise.
wxPaintDC dc(this); wxPaintDC dc(m_canvas.get());
// Set the OpenGL viewport according to the client size of this // Set the OpenGL viewport according to the client size of this
// canvas. This is done here rather than in a wxSizeEvent handler // canvas. This is done here rather than in a wxSizeEvent handler
@ -347,7 +353,7 @@ void MyFrame::bind_canvas_events_to_controller()
m_canvas->set_screen_size(ClientSize.x, ClientSize.y); m_canvas->set_screen_size(ClientSize.x, ClientSize.y);
m_canvas->repaint(); m_canvas->repaint();
}); }, m_canvas->GetId());
} }
void MyFrame::SLAJob::process() void MyFrame::SLAJob::process()
@ -370,3 +376,5 @@ void MyFrame::SLAJob::process()
m_print->process(); m_print->process();
} }
//int main() {}